Page 1 of 1

how to construct loops in box2d

Posted: Sun Feb 08, 2015 1:32 am
by kyle.bong2@gmail.com
I am trying figure out how to construct a loop similar in screen below for a car game I am making. I having a hard time figuring out how to do part 1 where the loop intersect and car passes by. Maybe some kind of collisions filtering? Please any suggestions? :D
loop.png
loop.png (22.48 KiB) Viewed 22516 times

Re: how to construct loops in box2d

Posted: Sun Feb 08, 2015 3:10 pm
by iforce2d
Yes, you can do this with collision filtering. You will need to split the loop into three sections: the entry, the middle part, and the exit. Then you can turn off collisions with the exit section when the car is approaching the loop, and turn off collisions with the entry section as the car leaves the loop. You would also need some sensor fixtures to detect when to switch between these two states.

As a start, perhaps you could look at the pinball example in the RUBE sample source code (this is in the SFML, cocos2d, and cocos2d-x downloads). There are two cases where this same type of collision filter switching occurs to let the ball enter and exit ramps to make it look like it goes 'above' other walls. You can look at the RUBE scene file to see how the wall sections have been split up, and where the sensor fixtures are placed. The source code should have comments explaining how these are used. Let me know if you have any trouble with that.

Re: how to construct loops in box2d

Posted: Sun Feb 08, 2015 9:24 pm
by kyle.bong2@gmail.com
Very quick response and help! Thank you!