# implementing a 2d looping using a physics engine

I am implementing a 2d mobile racing game. I have already implemented a 2d terrain using a physics engine (using edges). Now i would like to implement a looping very similar to the one in the old super sonic (http://higherorderfun.com/blog/wp-content/uploads/2012/05/Sonic_2_Loop.jpg). The problem is if i use a normal edge the car hits in the edge when going up the ramp (which is completely logical). Does any one have any experiences on how to make a looping using a 2d physics engine (without faking it)? Is it even possible (i know that is possible in 3d).

Thanks for the help!

Implementing layers would work, does that count as "faking it"? For loops you can transition between layers at the top of the loop and at the 'output' end of the loop. The left part of the ramp is in a layer below the right part of the loop:

Red is the back layer, blue is the forward layer. I imagine you can set a zone at the transition point to send your cars back a layer when they pass through that zone. Likewise, at the exit you switch back to the forward layer. When an object is in the forward layer, it ignores collision objects in the back layer and vice versa. For this reason, you'll want to have overlap at the transition points, to ensure you don't have objects falling through the terrain.

Alternatively, you can create a collision object that cares about the angle of incoming objects.

Compare the object's heading with the normal of the collision edge. If they are perpendicular, proceed with normal collision response. If the angle between the normal of the collision edge and heading are off perpendicular by some amount (20 degrees? It'll take some testing), allow the object to pass through the collision edge.

These special edges would be at the left side of the loop and the bottom.

I don't do game development yet, but I read up on it out of curiosity and for the sake of learning. I recently read an article by Patrick Wyatt about overcoming path finding and collision difficulties in the development of Star Craft because they used the old Warcraft engine, which didn't support the isometric view for collision boxes. They did some pretty neat hacks that I think will help you out. Have a look here.

Hope it helps!

• it doesn't help since my game is purely 2d and i am looking for a clean solution without hard coding the path when you enter the loop (i will resort to this solution only if i can't find any better one). Thanks for the effort though! – jernej Apr 5 '13 at 15:40
• Starcraft is a purely 2d game as well. I think the point of his suggestion is there may not be a way to easily implement this kind of behavior, and that you may need to implement a hack of some sort. – UnderscoreZero Apr 5 '13 at 16:32
• As UnderscoreZero said, the engine is 2d, but the artwork gives it the illusion of being "3d". The principle is the same, but the execution is different in the artwork – Deathnerd Apr 5 '13 at 19:52