Page 1 of 1

How can I move my hero on slopes smoothly?

Posted: Tue Feb 26, 2013 8:48 am
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.

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

Posted: Tue Feb 26, 2013 3:37 pm
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...

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

Posted: Wed Feb 27, 2013 9:05 am
by zerofield
Thank you !