Implementing player movement

I intend to use Box2D for a platforming game, and have been playing with it quite a lot, really easily to get up and running... but I really can't wrap my head around what's the best way to solve accurate and tweakable player movement, relying on the physics simulation doesn't really seem reliable. I've been able to get it working, but it's not tweakable at all, and there are dozens of little issues that I doubt can really be fixed using the physics approach. Just playing with the linear velocity isn't really working out it seems.

So, I'm more than perfectly fine (and probably have to be) with simulating the player myself inside Box2D... but I'm not really sure what the best approach is here. I'm talking about complex navigating complex terrain shapes, but it will be curved.

Any recommendations, tutorials, hints, etc?
I'm not interested in a solution that just barely works, there has to be room for tweaking.
syranide wrote:
I intend to use Box2D for a platforming game, and have been playing with it quite a lot, really easily to get up and running... but I really can't wrap my head around what's the best way to solve accurate and tweakable player movement, relying on the physics simulation doesn't really seem reliable. I've been able to get it working, but it's not tweakable at all, and there are dozens of little issues that I doubt can really be fixed using the physics approach. Just playing with the linear velocity isn't really working out it seems.

So, I'm more than perfectly fine (and probably have to be) with simulating the player myself inside Box2D... but I'm not really sure what the best approach is here. I'm talking about complex navigating complex terrain shapes, but it will be curved.

Any recommendations, tutorials, hints, etc?
I'm not interested in a solution that just barely works, there has to be room for tweaking.


Google "game ai programming by example pdf" and download one of the first links that looks good. This has a lot of good information on player movement using linear velocity. You won't be able to accomplish anything close to navigating "bezier curves?" without using linear velocity. I'd suggest you try and implement just the seek and flee behaviors, if you can do those then you can probably do anything else. Probably should make a b2Object class to store and update all of your velocity, rotation, position, b2body, etc.

Good luck :]
By 'navigating', do you mean the user controls the character, or the computer controls the character?
Maybe you could give an example of the type of terrain you mean, what the issues are that you mention, and what kind of tweaking you mean. If you are writing the program yourself, I don't understand why it's not tweakable 🙂
ekdev wrote:
Google "game ai programming by example pdf" and download one of the first links that looks good. This has a lot of good information on player movement using linear velocity. You won't be able to accomplish anything close to navigating "bezier curves?" without using linear velocity. I'd suggest you try and implement just the seek and flee behaviors, if you can do those then you can probably do anything else. Probably should make a b2Object class to store and update all of your velocity, rotation, position, b2body, etc.

Good luck :]


From the sounds of it, it's not really what I'm looking for, but I'll look into it thanks!
(As per below, I may have been slightly unclear it seems).


irresistible force wrote:
By 'navigating', do you mean the user controls the character, or the computer controls the character?
Maybe you could give an example of the type of terrain you mean, what the issues are that you mention, and what kind of tweaking you mean. If you are writing the program yourself, I don't understand why it's not tweakable 🙂


Perhaps I wasn't as clear as I thought!
I want to implement a player character who is controlled by a human.

I currently have it up and running very crudely by setting the linear force of the player body perpendicular to the contact normal and disabling gravity while on the ground. However, there are obviously a bunch of issues with this, the body will leave the ground slightly when crossing edges... if the player jumps and smacks into the ground, depending on the angle, the player will slide small distance upon landing, etc.

I have little to no intention of having any real physics interaction for the player character, and any such interaction would be minimal and probably easiest just "hacked". The intention is a platformer and not a physics game, meaning, I want to have precise control over how the player moves and behaves (on various slopes and areas), and any physics will be visual fluff and not really interact with the player... meaning, my current intention is to discard the Box2D physics simulation for the player and manually solve it, unless there is a really good solution.

In practice, we may in the future want the player to be able to walk on walls, slide on slopes, walk on moving platforms, jump up and down "through" platforms, use a grappling hook, push/grab blocks, etc... and be able to precisely control how these behave and not just rely on whatever the physics simulation says. I.e, we may want the player to walk at a fixed speed at all angles between 0-30, and anything above that, the player will simply slide, etc.

Here is a very crude image I did a little while ago to demonstrate internally, #2 and #3 is pretty what we have in mind for terrain (again, very crude image, and it's "zoomed out"). But the core intent is basically, your average tiled platformer, but a little less flat.

http://home.syranide.com/terrain.jpg

So, as above, my current idea is to not simulate the player body via Box2D but simply implement that myself, giving me full control. Long story short, what (I think) I'm really looking for is just a way to move the player a certain distance along the ground... I've only had a little time to experiment with this approach yet... but I'm curious what's recommended or if I've missed something trivial.



(My last quick and dirty approach with shape collision tests and "recursively" finding the point of collision was really really inefficient, and not precise enough it seemed)
Just to give you some idea of what we're intending, here's a random image of a platformer, with slightly curved terrain:
Image
I see. This is actually a pretty tricky thing to get just right. I think you are probably best to keep using the force as you are now, but I don't see why you need to disable gravity when on the ground and I don't see why the contact normal is relevant. Can't you just apply a force as necessary to reach a target speed?

For walking on walls though, you would have to do something about gravity.
irresistible force wrote:
I see. This is actually a pretty tricky thing to get just right. I think you are probably best to keep using the force as you are now, but I don't see why you need to disable gravity when on the ground and I don't see why the contact normal is relevant. Can't you just apply a force as necessary to reach a target speed?

For walking on walls though, you would have to do something about gravity.


Well, the contact normal is for knowing which direction to push the player... if the ground was just flat, then indeed it would just be to push the player left or right... but as we want to be able to have curved terrain, just applying a force doesn't really work as it will push the player into or away from the ground, i.e, either the force is diminished or the player keeps leaving the ground (hence, why I use the contact normal).

The reason I disable gravity is because it has a tendency to slip on certain slopes otherwise. So the point is, I have something working with physics now that is "reasonable", but it has a lot of quirks that we simply don't want, we want to be able to precisely tweak how the player behaves. I've been playing a bit more with my previous test and found a pretty stupid error, so now it actually works reasonable well it seems. So I guess'll continue on that path.


Speaking of which, is there an efficient way to adjust two overlapping shapes so that they no longer overlap? The manifold has a normal that would certainly do it I believe, but how would I know the distance, "re-collide" the shapes at different distances along the manifold normal?
True, the force will push the body into the ground somewhat, but as long as the force is strong enough, and the ground is reasonably smooth and not too steep the body should still be able to slide along it. The player leaving the ground will indeed be a problem though, especially if the player should move quickly sideways from standstill. I have tried making the player from two bodies with a little 'suspension' kind of connection (wheel joint) and it helps with this, but changing such a fundamental component could drastically alter your whole game I think. Example (this uses a rotor to move but I'm just pointing out the 'suspension' that I mean): http://www.ludumdare.com/compo/ludum-da ... w&uid=7091

If you are continuously applying a force to the body to keep it at the target speed, even when the target speed is zero, you shouldn't see it sliding down slopes. The trouble then would be if you did want it to slide 😀 Like I said it's a tricky

Adjusting two shapes so that they no longer overlap is basically the whole point of Box2D's collision, so not surprisingly the manifold is the perfect thing to do exactly that. If you want to handle it yourself you could disable the contact in the PreSolve function, and use the manifold to apply your own impulses or move bodies.
irresistible force wrote:
True, the force will push the body into the ground somewhat, but as long as the force is strong enough, and the ground is reasonably smooth and not too steep the body should still be able to slide along it. The player leaving the ground will indeed be a problem though, especially if the player should move quickly sideways from standstill. I have tried making the player from two bodies with a little 'suspension' kind of connection (wheel joint) and it helps with this, but changing such a fundamental component could drastically alter your whole game I think. Example (this uses a rotor to move but I'm just pointing out the 'suspension' that I mean): http://www.ludumdare.com/compo/ludum-da ... w&uid=7091

If you are continuously applying a force to the body to keep it at the target speed, even when the target speed is zero, you shouldn't see it sliding down slopes. The trouble then would be if you did want it to slide 😀 Like I said it's a tricky

Adjusting two shapes so that they no longer overlap is basically the whole point of Box2D's collision, so not surprisingly the manifold is the perfect thing to do exactly that. If you want to handle it yourself you could disable the contact in the PreSolve function, and use the manifold to apply your own impulses or move bodies.


Yeah, so what I figured was that rather than deal with all the little details and quirks of the physics, I'll just do it myself instead and it'll probably end up a lot more manageable in the long run. As I really don't care about the physics part of it for the player, I just want to use the shape and collision features of Box2D.

Regarding the manifold indeed that's what it's for, but it only gives the normal, not the "length", is there a good way to find out how much is required to actually push them apart for them, so they no longer overlap? From my very limited experience with Box2D, it seems like it simply approximates the required force and then does it several times instead... but I'm even sure how one would approximate it based on what the manifold says.

Thanks for your replies!
The length of the normal will be 1 unit, 'cos it's a normal.
hehe just kidding, I know what you mean 😀 I'm not aware of any easy way to get the 'length' of the overlap.

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