constant player movement

Official forum for the Chipmunk2D Physics Library.
Predator106
Posts: 24
Joined: Sat Apr 27, 2013 10:02 am
Contact:

constant player movement

Post by Predator106 »

I used this trick, as i was told by some tutorials, in Box2D to give me constant player horizontal movement, but it ends up "tunneling" into my static tile bodies. It worked fine in b2, so I'm wondering how I can achieve the same thing in cp.

Code: Select all

        glm::vec2 desiredVelocity = m_velocity * glm::vec2(300, 300);

        const cpVect& currentVelocity = cpBodyGetVel(m_body);

        cpFloat velocityChange = desiredVelocity.x - currentVelocity.x;

        cpFloat mass = cpBodyGetMass(m_body);
        cpVect impulse = cpv(mass * velocityChange, 0.0);

        cpBodyApplyImpulse(m_body, impulse, cpvzero);
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: constant player movement

Post by slembcke »

The integration order is different in Chipmunk. Setting the velocity like that outside of the timestep doesn't really work so well. You have two options to fix it:

Put your velocity changes into a custom integration function. (Such as the following function)
https://github.com/slembcke/Chipmunk-Ph ... anet.c#L43

... or use constraints configured in a special way to calculate and apply forces:
https://github.com/slembcke/Chipmunk-Ph ... emo/Tank.c

The first solution should work exactly like it did in Box2D, but keep in mind that you are potentially applying gigantic impulses to your object to keep its velocity constant. Depending on the object's mass and your timestep, that can add up to a lot of force that you didn't intend. Secondly, because it's not done inside the solver, it may not apply enough force to keep something moving at the speed you wanted if other collisions/forces are involved.

The second solution works better because the constraint can apply the correct force even when other collisions become involved. You can also trivially limit the force to a sane amount.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Predator106
Posts: 24
Joined: Sat Apr 27, 2013 10:02 am
Contact:

Re: constant player movement

Post by Predator106 »

What might the second solution look like? I only see uses for rotational forces/objects, but not some example for horizontal movement.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: constant player movement

Post by slembcke »

Is this a top down viewed game or a sidescroller?

If it's a sidescroller, I'd recommend using the surface velocity feature of Chipmunk instead. If it's a top down game, then restricting the forces to just one axis is a bit trickier. I guess you could do that with a very specifically set up groove joint or even pin joint. Definitely not intuitive, but it would work.

I really need to finish my idea for an easy to set up, custom constraint...
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Predator106
Posts: 24
Joined: Sat Apr 27, 2013 10:02 am
Contact:

Re: constant player movement

Post by Predator106 »

yeah it's a side scroller.

hm, but cpShapeSetSurfaceVelocity has a couple issues. it doesn't move immediately and it doesn't stop immediately.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: constant player movement

Post by slembcke »

You don't have enough friction then. For platformers, I set the player's friction to correspond to the acceleration I want them to have. Like Mario doesn't start and stop instantly for example. Games that do that are generally pretty jarring.

edit:
So here is a really basic platformer example that ships with Chipmunk:
https://github.com/slembcke/Chipmunk-Ph ... o/Player.c
Here is a better example that I did for a contest game. I was pretty happy with how this turned out.
https://github.com/maximile/Your-Story/ ... cter.m#L41
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Predator106
Posts: 24
Joined: Sat Apr 27, 2013 10:02 am
Contact:

Re: constant player movement

Post by Predator106 »

yes but if you increase friction sure that means they stop faster, but it means it takes longer to start moving and more forces need to be applied which then makes him go faster :/

and yes, he should stop at least somewhat close to after the movement key is let go. and have an acceleration of instantaneous. that's how most platformers respond anyways, I guess.
Predator106
Posts: 24
Joined: Sat Apr 27, 2013 10:02 am
Contact:

Re: constant player movement

Post by Predator106 »

maybe i'm misunderstanding how movement like Terraria is achieved.
see:
http://www.youtube.com/watch?feature=pl ... l3M#t=573s

I can't tell if it's got a slight delay in movement or not. It's definitely got to be pretty close to instant though.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: constant player movement

Post by slembcke »

Why would increased friction make them take longer to start moving? The friction force is the same whether you are accelerating or decelerating. You also don't need to worry about the character going too fast as it will never go faster than the surface velocity you set. Well I mean it might if an external force acts on it, but then friction will slow it down to that speed again.

I've never played a satisfying platformer that didn't have at least a little inertia. Ones that don't feel extremely "sticky" and make it really hard to vary jumps and such since there is no intuitive feel from the acceleration.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: constant player movement

Post by slembcke »

Terraria definitely has a lot of inertia/acceleration in it. It takes a good fraction of a second to go from walking in one direction to fully accelerated in the other.

I mean you don't want it to take seconds to accelerate to walking speed, but you do want it to take a decent fraction of one.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 9 guests