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
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.
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