# Collision

• 12-09-2011, 12:29 AM
PhQ
Collision
Let's say I have a tile looks like this:
Attachment 2171
The pink colour is transparent.

How can I check whether or not the user is touching the tile? It's okay for the user to touch the transparent area but not the actual tile (the boxes).
I have tried doing this by checking the colour at the player's pixel but my game started to lag. or maybe because I've had like 3 loops to check the tile next to the player and looping through each of the collidable tile IDs and checking whether or not it is colliding.
• 12-09-2011, 01:15 AM
pbrockway2
Re: Collision
What is a tile? What does it mean for a user to touch a tile?

Perhaps part of the tile's state could be position and orientation (think "Tetris") in which case part of its behaviour could be to say whether a given point is "within" the tile. (java.awt.Shape instances implement, in a well defined way, a notion of "insideness".) If there are only a few (hundreds, say) tiles I would go for iterating through the whole lot to decide which, if any, contained some given position. If there were rather more tiles (millions, say) I would think about putting them in some sort of data structure that would allow quick access to just those tiles that were possible candidates for including a given point.
• 12-09-2011, 03:26 AM
PhQ
Re: Collision
Quote:

Originally Posted by pbrockway2
What is a tile? What does it mean for a user to touch a tile?

Perhaps part of the tile's state could be position and orientation (think "Tetris") in which case part of its behaviour could be to say whether a given point is "within" the tile. (java.awt.Shape instances implement, in a well defined way, a notion of "insideness".) If there are only a few (hundreds, say) tiles I would go for iterating through the whole lot to decide which, if any, contained some given position. If there were rather more tiles (millions, say) I would think about putting them in some sort of data structure that would allow quick access to just those tiles that were possible candidates for including a given point.

A tile is just a Image with x and y coordinates.
You can move the player around the screen and when you do that player's x and y coordinates change.
When the user enters the image I want it to stop. Kind of like in tetris, you can go into the shape.
If I would use shapes will I need to convert the Image into a Shape?
Or is there an easier way of doing it?
• 12-09-2011, 06:10 AM
pbrockway2
Re: Collision
So if I understand you both player and tile are small graphics and you want to detect when they overlap (including touch).

I'm no expert in any of this, but the first thing I would observe is that what makes this tricky is that not all points in the images "count": ie it is only the non transparanet parts that matter. The first thing I would do is determine the "bounding" boxes for the player and all the tiles, and only check for a collision when the bounding boxes intersect. This won't involve much calculation.

Next it may be that the specific shapes have a geometry that make it relatively easy to detect if a point is inside the shape. Your example shape is like this: the "L" shape has a simple geometry that allows you to tell whether a given test point is inside just based on the coordinates of the 6 corners.

Only as a very last resort would I compare the location of non transparent pixels in the two images pixel by pixel.

-----

It might be worth reading up about the Shape interface. There is no simple image->shape conversion - basically the distinction is between raster and vector graphics. But it might be possible for you to represent the elements as both an image and a shape: the latter to facilitate collision detection.
• 12-09-2011, 10:48 AM
Tolls
Re: Collision
Have a look at gamedev.net.
There must be a number of articles there on collision detection with sprites, which is what you're talking about.
• 12-12-2011, 03:20 AM
PhQ
Re: Collision
Quote:

Originally Posted by pbrockway2
So if I understand you both player and tile are small graphics and you want to detect when they overlap (including touch).

I'm no expert in any of this, but the first thing I would observe is that what makes this tricky is that not all points in the images "count": ie it is only the non transparanet parts that matter. The first thing I would do is determine the "bounding" boxes for the player and all the tiles, and only check for a collision when the bounding boxes intersect. This won't involve much calculation.

Next it may be that the specific shapes have a geometry that make it relatively easy to detect if a point is inside the shape. Your example shape is like this: the "L" shape has a simple geometry that allows you to tell whether a given test point is inside just based on the coordinates of the 6 corners.

Only as a very last resort would I compare the location of non transparent pixels in the two images pixel by pixel.

-----

It might be worth reading up about the Shape interface. There is no simple image->shape conversion - basically the distinction is between raster and vector graphics. But it might be possible for you to represent the elements as both an image and a shape: the latter to facilitate collision detection.

Ugh. This is confusing. I'm sure my programming knowledge is too bad for this.
I have looked on gamedev.net, but I couldn't find anything.