Box2D Forums

It is currently Fri Oct 24, 2014 3:17 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Sat Mar 03, 2012 7:43 am 
Offline

Joined: Sat Mar 03, 2012 7:29 am
Posts: 4
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:
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.


Top
 Profile  
 
PostPosted: Tue Mar 06, 2012 4:17 pm 
Offline

Joined: Sat Mar 03, 2012 7:29 am
Posts: 4
no one? :(


Top
 Profile  
 
PostPosted: Tue Mar 06, 2012 11:09 pm 
Offline

Joined: Sun Oct 25, 2009 3:28 am
Posts: 258
You could do a counter impulse, or increase linear damping in the character.


Top
 Profile  
 
PostPosted: Wed Mar 07, 2012 3:38 am 
Offline

Joined: Sat Mar 03, 2012 7:29 am
Posts: 4
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.


Top
 Profile  
 
PostPosted: Thu Mar 08, 2012 1:07 am 
Offline

Joined: Sun Oct 25, 2009 3:28 am
Posts: 258
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.


Top
 Profile  
 
PostPosted: Thu Mar 08, 2012 1:24 am 
Offline

Joined: Sat Mar 03, 2012 7:29 am
Posts: 4
well, increasing the linear damping to 10.f helped, but it also changes the gravitational force and other forces :S


Top
 Profile  
 
PostPosted: Thu Mar 08, 2012 3:12 am 
Offline

Joined: Sun Oct 25, 2009 3:28 am
Posts: 258
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:
//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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: Google [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group