Results 1 to 6 of 6
Thread: Collision
- 12-08-2011, 11:29 PM #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.
- 12-09-2011, 12:15 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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, 02:26 AM #3
Re: Collision
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, 05:10 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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, 09:48 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
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, 02:20 AM #6
Similar Threads
-
Collision in j2me
By Ashish_jain in forum Java 2DReplies: 8Last Post: 06-24-2011, 03:58 PM -
2D - Collision messed up
By überfuzz in forum New To JavaReplies: 0Last Post: 06-22-2011, 09:20 PM -
Collision in GUI 2d Game help
By heysayjump in forum New To JavaReplies: 7Last Post: 03-14-2011, 02:01 AM -
Really Need help with some collision detection
By Harwad in forum New To JavaReplies: 1Last Post: 01-23-2011, 12:38 AM -
Collision
By shadycharacter in forum New To JavaReplies: 0Last Post: 04-13-2010, 09:58 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks