The Practice Of Collision Detection

This topic is 4817 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Ok, im making a 2d top down view networked multiplayer shooter, Scrolling map. Ok i have collision detection for a bullet and the player, it simple tests weather the bullet rectangle interestects the player rectangle. Java
			playerRect = new Rectangle(playerX + imgCropx1, playerY + imgCropy1, 20,20);
bulletRect = new Rectangle((int)temp.bx, (int)temp.by, 10, 10);
if(bulletRect.intersects(playerRect)){
System.out.println("Player Hit");
}

}


Works great, now my map is very simple atm, but wats the best way to detect collision when a player runs into a wall or a tree etc. Do i have to create rectangles for all of these objects, add them all to a Set (java.util.Set) and then every frame Iterate over the set testing foer intersection between the player rectangle and the current rectangle? Or is there a better wat to do this. Ill give u an example of wat im talking about.
|||||||||||||||||||||||||||||||||||||||||||
||                                       ||
||   P                        T          ||
||                T                      ||
||                                       ||
||               T           T           ||
||                  T                    ||
||                                       ||
||               T                       ||
|||||||||||||||||||||||||||||||||||||||||||


What im saying is that i would hve to create a rectangle object for each tree and the walls, and add them to the Set. Is this the correct way? Or is the the stupidest thing youve ever heard lol Cheers, Nick

Share on other sites
For collision with map I use 4 points - the sides of the objects, and check each them with map like this. Then I determen at what side did object colided - xy.

if( (map[(int)(posx - OBJSIZE)][(int)(posy - OBJSIZE)] == 0) ) //top left
{
//top left point collided
}

Objects I check using rectangles it looks similar.

Share on other sites
Quote:
 Or is the the stupidest thing youve ever heard
No :-)

A rectangle for each object in your world would be very reasonable. And if the number of objects is relatively few, you can probably get away with just testing your player against each one every frame, i.e. 'brute force'. How many is 'relatively few' depends on the context, but it could easily be into the hundreds.

If ever collision detection starts to become a bottleneck, then it'll be time to add spatial partitioning and broad-phase culling. But I'm guessing you can get by without that for a while.

Share on other sites
Thank you for your replies :) Ill keep you upto date with out things are going :)

Cheers, Nick

• 39
• 15
• 9
• 23
• 10