How can I move my hero on slopes smoothly?

General discussion about Box2D tutorials
Post Reply
zerofield
Posts: 9
Joined: Tue Feb 19, 2013 10:54 am

How can I move my hero on slopes smoothly?

Post by zerofield »

I used to think that box2d is not for platform games because of collision with slope. I found that iforce2d had 6 videos on youtube showing platform games made with box2d. Could someone give me idea of how to move hero on slopes smoothly? In the video, the hero had two legs which could adapt to the slope, how to implement that? Any help would be appreciated.
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: How can I move my hero on slopes smoothly?

Post by iforce2d »

The method you are talking about was quite a lot of work and fiddling, tweaking things, and huge if/else spaghetti code. On the other hand, it gave a very nice feel when playing it. I don't know if I would try this again if I were making a game, because it means you need to implement many things that Box2D has already covered:

- spring/suspension-like behavior when landing
- friction on surfaces
- reaction for ground when landing on or jumping off dynamic bodies
- altering jump height when jumping from soft object (eg. swing bridge)
- sideways reaction when standing on sloped ground

The last one seems to be the point of your post. The legs are just ray casts, and the angle and friction of the ground at the intersection point are found. Depending on the friction, a vector parallel to the ground is calculated and this is applied to the main body as a force to make it slide sideways a bit. At least, that's if I remember correctly.

I have the source code for the demo in those videos, but it is a huge mess and works with a very old (2007) version of Box2D so would need some updating. Maybe sometime I can clean it up and make it available. It's Windows only though...
zerofield
Posts: 9
Joined: Tue Feb 19, 2013 10:54 am

Re: How can I move my hero on slopes smoothly?

Post by zerofield »

Thank you !
Post Reply