non realistic platformer movement

I am trying to rewrite my platformer game to use Box2d, but I am stumped as to how to implement the movement correctly. Could anyone who is more knowledgeable about Box2d explain the best way to do this please?

I want to have the player move at a set speed horizontally when the arrow keys are pressed with instant acceleration and deceleration. However, I want this to be in addition to however fast the platform beneath them is moving.

So far, the best approach I've been able to come up with is to somehow always check whether there are any platforms beneath the player at any given time, and if so, attach a prismatic joint with a motor.

But what if the player wants to jump? I guess I need to somehow maintain a pointer to the joint so that I can disable it. But then what happens if the platform is deleted? It sounds like the joint will be freed, leading to the player object owning a dangling pointer to a nonexistent joint.

What can I do? I'm really confused.
Couldn't you just retrieve the speed of the platform and then add it to the speed of the player? Simple vector math (well, as simple as vector math gets 😉 ).

Or maybe I'm just as confused as you?
But what if the player is getting pushed off the platform by another platform?
If he gets hit by something, the 'collision resolver' (or whatever else name they gave it), should apply all the appropriate forces for you and adjust the body position and velocity accordingly.
But wouldn't resetting the velocities manually each frame nullify that?
Well, I guess he would get pushed 'out of the way' as far as the platform will go, and then yes, he probably would just stop in place until the player presses the key again.

Edit: I'm not entirely sure I understand the problem. 😕
So will it get pushed the same amount regardless of what I set the velocity to in between?
Not regardless no. When you set the velocity at a given step/frame, that velocity will act upon the player for that step (obviously).

Unless, of course, you zero the vector before the call to step. If you do that, then all that will act upon the player is the pushing platform.

Like my edit says in the previous post, I'm not sure I understand the problem fully. Can you give some more details?
Here's my pitiful paint drawing.

Image

The problem is that if the player is holding the right arrow key, and I blindly set it's x velocity to +3 or whatever each frame, it won't be pushed off like it should be, right?

What I want it to do is move to the right until it hits the platform, and then get pushed to the left by the platform after that. (Unless the player chooses to go to the left instead of right in which case it can outrun the top platform)
Quote:
The problem is that if the player is holding the right arrow key, and I blindly set it's x velocity to +3 or whatever each frame, it won't be pushed off like it should be, right?


No.
The engine will resolve all the forces applied to the body (the arrow press, any platforms, gravity, body restitution, etc). In fact, it does so many times per second (= one of those params you pass to step()).

So all you really have to do, is apply the force from the key press and let the engine resolve the new position and velocity based on whatever collisions or touching platforms it might have detected.

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