Advertisement

Best way to check collisions with multiple objects?

Started by January 28, 2016 10:08 AM
3 comments, last by frob 8 years, 11 months ago

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.

I love everyone

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.

Previously "Krohm"

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!

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.

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.

I love everyone


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.

This topic is closed to new replies.

Advertisement