Difference between impulse and force and velocity

What is the difference between these three methods of changing velocity. (Assume everything is applied to center of mass)

1. Set Linear Velocity directly to old velocity + x
2. Apply impulse of x*mass
3. Apply force of x*mass/timestep
Storyyeller wrote:
1. Set Linear Velocity directly to old velocity + x
2. Apply impulse of x

There's a factor of mass difference between these two: impulse is change in momentum, which is mass times velocity. Otherwise they do exactly the same thing, immediately changing the velocity of the object.

Quote:
3. Apply force of x/timestep

This is a little different: after a timestep is taken, the result will be about the same as 2), but 2) is applied immediately, and 3) needs a timestep to happen before the body feels the velocity change.
Sorry, I forgot to mention the mass factor. I meant that if the amount of each method is equal, what are the differences.
According to the setting linear velocity thread, the first method is apparently bad for some reason. Could anyone explain this please?
I take that back, setting the velocity is essentially the same as applying an impulse to the center of mass. So set the velocity all you want.
Hi,

I worked this out after much experimenting, I needed to remove lateral velocity - so I calculated orthogonal velocity to the wheel direction, and created a negative correction of this velocity - and then applied this as an impulse. i.e. applying an impulse was basically the same as setting the velocity (except I had to multiply by the time step 1/60)).

So if setting Linear Velocity and applying an impulse are so similar, when do you use which method? Which is best for the solver?

Code:


-(void) applyOrthoganalImpulseForTarget:(b2Body *)targetBody dt:(ccTime) dt {
   
   
   
   b2Vec2 localPoint;
   
   localPoint.Set(0,0);
   
   if (dt < b2_epsilon || dt == 0.0f) return;
   
   
   // Get the linear velocity of the the centre of the body
   b2Vec2 bodyVelocity = targetBody->GetLinearVelocityFromLocalPoint(localPoint);
   
   
   
   // Get the roation vector
   
   b2Vec2 bodyRotation = targetBody->GetTransform().R.col2;
   
   // get a corrected velocity vector by projecting the linear velocity onto the
   // rotation normal vector
   bodyRotation *= b2Dot(bodyVelocity,bodyRotation);
   b2Vec2 correctedVelocity = bodyRotation;
   
   
   // work out the difference vector from the original lateral velocity  to the new corrected velocity in
   // the direction of the wheel rotation
   b2Vec2 velocityDifference;
   
   velocityDifference.x = correctedVelocity.x - bodyVelocity.x;
   velocityDifference.y = correctedVelocity.y - bodyVelocity.y;
   
   
   
   // velocityDifference is effectively the velocity we need to kill by applying a force
   
   // To work out the force needed, we can assume that we want to take the
   // velocityDifference vector back to zero.
   
   
   b2Vec2 correctiveImpulse;
   

        // The impulse needed is actually a direct factor of the velocity (ie linked to the timestep)
        // WHATS BETTER? TO USE AN IMPULSE LIKE THIS, OR JUST TO SET LINEAR VELOCITY DIRECTLY
     
   correctiveImpulse.x = (velocityDifference.x) * dt;;
   
   correctiveImpulse.y = (velocityDifference.y) * dt;
   
   // apply the necessary opposing force to remove the velocityDifference
   if (correctiveImpulse.x != NAN  && correctiveImpulse.y != NAN) {
      
      targetBody->ApplyLinearImpulse(correctiveImpulse,targetBody->GetPosition());
      
   }
   
   
   
}



Regards

i
When you apply an impulse away from the center of mass you get rotation. Setting the linear velocity does not induce rotation. So setting the linear velocity can only represent applying an impulse at the center of mass. Other than those difference it does not matter which method you choose. Not one bit.
Thanks,

are they both bad for the solver...or is one better than the other?

Regards

i
To clarify what Erin means: neither setting the velocity nor applying an impulse will actually "screw up" the solver, and sometimes it's exactly what you need to do, so don't stress over it, do it if it works.

In more detail, though, which you mostly won't need to worry about (just ignore this if it doesn't make any sense): when you're manually altering the velocity, or applying an impulse, you're often trying to apply a new velocity constraint of your own. In your case, this constraint is "remove the lateral velocity". But you're doing it from outside the velocity constraint framework built in to Box2d, so here's what happens:

- You apply your own velocity constraint, everything looks good (e.g. lateral velocity is 0)
- Time step:
--- Box2d does a bunch of stuff
--- Box2d applies the built in velocity constraints (joints/contacts) a bunch of times (10, 5, or whatever number of velocity iterations is set)
--- Box2d does a bunch of other stuff
- Now your velocity constraint has potentially been violated, because Box2d checked all the joint/contact constraints many times, working very hard to simultaneously satisfy all of those, but it never checked your constraint, and doesn't even know that it's there
- Repeat

Roughly speaking, your problem is that the constraints that Box2d knows about are iterated over several times, whereas yours are only applied once and then forgotten about, so if the two are ever in conflict, your constraint is going to lose. And don't think you can make yours "win" by applying a stronger impulse, or anything like that - warm starting can deal with that situation, too, and it'll still win!

So if you are in a situation with joints, collisions, or a stack of objects, you'll find that your own constraint becomes a lot more spongy than the internal ones, and the engine will perfectly happily overrule your velocity changes completely.

And this is fine, as long as you realize what's going on, and know that if something in the engine is trying to violate your constraint, it will happen. The only other option is to write your own custom joint, which is much more difficult to do.
Hi,

Thanks, thsi explains why there is still residual lateral velocity which actually doesnt look too bad (gives an arcadey type feel). My concern with my method of removing lateral velocity (or impulses) is linked to collisions. If a Car hits my car from the side, then wont my method of removing lateral velocity - actually attempt to remove the collision effect at the next time step?

Good to know I can use lateral velocity and impulses if I need to. I think I read somewhere the issue with this method was more linked to collisions.

...and I guess I can reduce velocity iterations 😉

Regards

i

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