1. ## Collision

Let's say I have a tile looks like this:

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.

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## 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.

3. ## Re: Collision

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?

4. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## 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.

5. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
27

## 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.

6. ## Re: Collision

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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•