Trying to create a platformer

Hi everyone,

Total newcomer in this forum. I first want to congratulate and thank Erin for creating such an amazing library and allowing to use it free of charge. I've followed the project for quite a while but never got the time to actually put myself into it, but now I'm decided to actually do something with Box2D. As for my level of C++, it is all self-taught and I've been practicing for a few years now, so my code may not be perfectly clean but it generally works. All advice concerning the way I code things are welcome.

Now, I'll explain my situation. I am currently trying to build a platformer-like engine with Box2D. I'm using a modified version of the Testbed for displaying the shapes (just modified it so there is no bar on the right). So far I've created a static body for the ground with multiple shapes as a test level, and a dynamic body for my test character. I've used and updated some code from the old wiki to create an "Object" class for my bodies and buffer all b2Contacts for my character.

At first, I was making my character move horizontally, but it was very bouncy when I tried to move up or down a slope. So as to solve this problem, I now use the contact information to look for all contacts with the ground (localNormal.y>0, with slope<45°) and get an average normal. I use this normal to determine the direction the character must follow to move left or right. It works well with a flat ground or for going down a slope, but the character still tends to bounce a lot when going upwards. I've set the friction and restitution both to 0, and the iterations to 10, but it doesn't change anything.

Here's the source code, hoping that someone could help me for this matter :

Code:
edit : Cleared the code as the problem is solved, the code has now changed a lot, and so as not to flood the topic.


I hope I was clear, and thanks in advance.
Sorry about the double post, but I finally found the cause of the bouncing problem. It was the way I was clearing the horizontal velocity of the character in my Process function. I'll can solve it now, but if I find more problems I'll repost in this thread.
I should have posted here long ago but I really wanted to solve this problem by myself (since that's the best way to learn), still I now have to resolve to ask here for help.

First, I'll sum up what I have right now : I'm working on a 2d side scroller platformer with a character represented by a non-rotating edge. The character can recognize if it's standing on a platform it can move on, or a platform it should slide off. It can move both on horizontal grounds and slopes, jump, crouch (which is still a bit buggy but I'm working on it) as well as catch and climb ladders (represented by sensors). It can also recognize one-sided platforms and filter collisions properly.

Now, my problem is about how the character moves on the ground. I normally use the world normal of the character's contact point with the ground to determine the velocity of the character when it moves. In most cases, this worked perfectly, but there was a problem when the character finished going up a slope that led to a horizontal ground : following the slope tangent, it automatically jumped off the ground. I tried to apply a downwards force when the contact was lost with the ground but the effect was visually noticeable and quite unpractical for a few reasons.

That's how I came for another solution : manually calculating if the character is about to meet the end of a slope during the next frame, and if that's the case, manually calculate the character's new position and apply a velocity equal to ((new position)-(current position))*60. With that, the character now moves swiftly on all angles, but there is still one last problem : the contact point is lost during one frame, when the character meets the angle, and is then recovered the following frame. Since the contact point determines that the character is standing on the ground, that means that during this one frame, the character is considered airborne and therefore cannot jump or crouch, amongst other bugs. I'm sending an attachment that explains, I hope, more visually the problem. If that wasn't clear enough, though, I can upload an executable of my program.

So, does anyone know why this happens, or have another way to deal with angles ?
For this type of character controller, I would use a ray cast that extends below ground. In fact you can build a whole character controller with just ray casts and no physics simulation at all.

Original thread: https://box2d.org/forum/viewtopic.php?t=5143