Jump to content
  • Advertisement
Sign in to follow this  
NickyP101

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!