Jump to content
  • Advertisement
Sign in to follow this  
Soka

Best way to check collisions with multiple objects?

This topic is 993 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

So yea, the title pretty much explains everything, I have a number of tiles in screen, this number changes, so I was thinking about the best way to check collisions only with the needed tiles, and I thought about having a vector of tiles that had only the tiles being render at that moment so the character had to only check with them, and not with all the tiles of the map. But one question I had is, how would I go about deleting the tiles that aren't on screen anymore?

Maybe for the tiles that aren't being rendered search them in the collision vector and if they are there, delete it?

Just clearing the whole vector each frame and only filling it with the tiles on screen?

Anything else?

 

Thank you very much in advance.

Share this post


Link to post
Share on other sites
Advertisement

Multiple questions here, albeit related. I would suggest to keep your collision checking independent from rendering - who says entities outside screen don't need collisions? They want some collisions as well!

 

Try something more generic. Tiles on a grid lead themselves well to some hierarchical system (see: QuadTree). You might also want to check out some other library doing the work for you. There are a few and they'll mangle thousands of static objects with relative ease.

Share this post


Link to post
Share on other sites

Multiple questions here, albeit related. I would suggest to keep your collision checking independent from rendering - who says entities outside screen don't need collisions? They want some collisions as well!

 

This is true. Things outside the screen may collide too (ie: a bullet against a wall).

 

 

There are several ways/algorithms to not check your character against ALL things in the world. The phrase you need to google is broad phase collision detection, and its purpose is to discard (very fast) the objects that are so far apart that it's impossible that they are colliding.

 

One simple way to do that is with a grid. You divide your world in squares and every object belongs to a square, you find the square by: (position.x / widht_of_square, position.y / height_of_square). That way when you have to check for collision you only check against the objects on the same square (and probably neighbors too).

 

Hope that helps to get you started.

Share this post


Link to post
Share on other sites

Thank you guys, I'll see what can I find, also I guess I didn't say it and I should've but it was just for character/tiles collisions. I should google search "how to explain myself" too lol.

Share this post


Link to post
Share on other sites


But one question I had is, how would I go about deleting the tiles that aren't on screen anymore?
Maybe for the tiles that aren't being rendered search them in the collision vector and if they are there, delete it?

What you see on your screen can be different from what you simulate.  

 

Game objects usually have several representations. The displayed graphics meshes and models and tiles are one representation, and the physics objects or collision objects are a second representation.  Often there will be another representation for AI and simulation steps, and yet another representation for user interface elements when buttons and labels are used.

 

 

For collision tests usually games use a spatial grid. If you can interpret the world as a 2D representation, a quadtree is common. If you must interpret it as 3D an octree can be used, but they take up far more space; many 3D games will still use a quadtree for their collision spatial grid. There is a variant that is useful for moving objects, called a "loose quadtree", that takes a little more math.  Every object in the world is given a 2D footprint, basically a small rectangle.

 

Using a spatial grid you can query for objects near your footprint or that pass through a region. It takes very little math and will reveal all the nearby objects as a convenient list. 

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!