Move a character mario style on multi world step call?

Here's the place to get help and discuss features. The focus is on the C++ version, but generic questions are welcome.
turman
Posts: 4
Joined: Sat Mar 03, 2012 7:29 am

Move a character mario style on multi world step call?

Postby turman » Sat Mar 03, 2012 7:43 am

Hello,

I think this question has been answered 100 times but I couldn't find a real answer,
especially because I call world.Step more than 1 time in Update()
because this http://gafferongames.com/game-physics/f ... -timestep/ says so.

I want to move my character that it only moves during I press the movement keys.
So, I thought about a simple procedure like

Code: Select all

case onleftkeydown:
body->applyimpulse
case onleftkeyup:
body->clearvelocity


but this would also clear the force which was added through other sources.
And because I call step() more than one time in an update and the force gets cleared after each call, its only useful for the first call to step().

If I only apply/set the force/impulse/velocity but dont clear it, the player doesn't stop to move.

turman
Posts: 4
Joined: Sat Mar 03, 2012 7:29 am

Re: Move a character mario style on multi world step call?

Postby turman » Tue Mar 06, 2012 4:17 pm

no one? :(

jayther
Posts: 266
Joined: Sun Oct 25, 2009 3:28 am
Contact:

Re: Move a character mario style on multi world step call?

Postby jayther » Tue Mar 06, 2012 11:09 pm

You could do a counter impulse, or increase linear damping in the character.

turman
Posts: 4
Joined: Sat Mar 03, 2012 7:29 am

Re: Move a character mario style on multi world step call?

Postby turman » Wed Mar 07, 2012 3:38 am

What do you mean with counter impulse?

And with damping you mean friction?
That doesn't help, because it only needs more force to move then.

jayther
Posts: 266
Joined: Sun Oct 25, 2009 3:28 am
Contact:

Re: Move a character mario style on multi world step call?

Postby jayther » Thu Mar 08, 2012 1:07 am

I meant apply an impulse in the opposite direction the character is moving.

And damping I mean damping. There is a linear damping value you can specify in the body definition.

turman
Posts: 4
Joined: Sat Mar 03, 2012 7:29 am

Re: Move a character mario style on multi world step call?

Postby turman » Thu Mar 08, 2012 1:24 am

well, increasing the linear damping to 10.f helped, but it also changes the gravitational force and other forces :S

jayther
Posts: 266
Joined: Sun Oct 25, 2009 3:28 am
Contact:

Re: Move a character mario style on multi world step call?

Postby jayther » Thu Mar 08, 2012 3:12 am

Hm, I guess you'll have to apply a counter impulse (probably just a horizontal counter impulse (leftwards or rightwards?)). You'll have to do something like:

Code: Select all

//get the negative x-component and multiply by the mass to
//get the amount of impulse needed to stop the character from
//moving it horizontally
float amountToStop = -body->GetLinearVelocity().x*body->GetMass();

//apply a gradual stop so it's not a sudden stop.
amountToStop *= 0.8;

//apply impulse
body->ApplyImpulse( b2Vec2(amountToStop,0), b2Vec2(0,0) );

That code may not work at all, but the idea is there.

tormentes
Posts: 1
Joined: Mon Dec 15, 2014 5:31 am
Contact:

Re: Move a character mario style on multi world step call?

Postby tormentes » Mon Dec 15, 2014 5:40 am

I know I'm resurrecting this topic, but I was looking for something like that and I just realized how to do it. I'm posting it here as a possible solution for the next one looking for it.

Code: Select all

vel = body->GetLinearVelocity();
vel.x = 0;
body->SetLinearVelocity(vel);


Return to “General Discussion”



Who is online

Users browsing this forum: No registered users and 3 guests