Can you run the collision detection between 2 objects only?

Topics: Developer Forum
Jul 29, 2010 at 9:18 PM

I'm making a platformer, which will use tiles for the levels.

My plan is to use my own broad phase collision detection, where physics objects only check for collisions with the tiles who's grid squares they overlap. But, to do that, I need to be able to run the collision detection and response just between the two objects involved.

This way, I can just use one Square tile object and move it around to where it needs to be, rather than have a level made of thousands of redundant square tile objects.

Jul 30, 2010 at 2:42 AM
Edited Jul 30, 2010 at 2:43 AM

What you want to do is possible, but it defeats the purpose of the physics engine and it will be a lot of work. Rather than that I would suggest disabling all the bodies outside a certain range, which can work pretty well with tile-based worlds.

The physics engine already only runs collision code if 2 bodies' AABB's overlap, and it is very fast at it. Also, the engine wont try to collide the tiles with each other because they are all fixed.

Jul 30, 2010 at 6:32 AM

I appreciate what you're saying, but with my implimentation, I think that letting the physics engine sort it out for itself will be very memory inefficient, and probably quite slow.

Let's say that the world is 500 by 500 tiles, and is 50% solid terrain. That would be 125,000 physical objects sitting in the memory. Even though only a few will be colliding at any one time, the memory requirement is fairly high.

Alternatively, I could represent the same world with a 500 by 500 array of bools, with true being a solid tile. It will take a little bit of code to move around a 32*32 square to check against active physical objects that have entered a tile's (eg, "true") square, but it will definately be better in terms of memory usage, and possibly better in terms of processing speed (I don't know how much processing power the physics engine will allocate to 124,800 innactive boxes though, so I can't say for sure)

So, for this particular implimentation, does anyone know of a way to check and resolve collisions against two specific objects?

Jul 30, 2010 at 6:45 AM

Actually, never mind. I just figured it out: I just add the tiles that could possibly be collided with to the simulation just before collision detection, based on which objects are in which squares of the array, and then remove them straight after!