how to construct loops in box2d

General discussion about Box2D tutorials
Post Reply
kyle.bong2@gmail.com
Posts: 41
Joined: Thu May 29, 2014 5:01 am

how to construct loops in box2d

Post by kyle.bong2@gmail.com » Sun Feb 08, 2015 1:32 am

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 15111 times

iforce2d
Site Admin
Posts: 860
Joined: Sat Dec 22, 2012 7:20 pm

Re: how to construct loops in box2d

Post by iforce2d » Sun Feb 08, 2015 3:10 pm

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.

kyle.bong2@gmail.com
Posts: 41
Joined: Thu May 29, 2014 5:01 am

Re: how to construct loops in box2d

Post by kyle.bong2@gmail.com » Sun Feb 08, 2015 9:24 pm

Very quick response and help! Thank you!

Post Reply