Hello. I am a novice to BOX2D and looking at the lite version.
While looking at the code, I found the old impulse is applied again to update the velocity (if I choose to accumulate impulses):
In this way, my understanding is the velocity goes corrupted, but simulation is looking good. dunno why.
To explain further,
In Arbiter::PreStep(), the old impulse is applied to update velocity:
if (World::accumulateImpulses)
{
// Apply normal + friction impulse
Vec2 P = c->Pn * c->normal + c->Pt * tangent;
body1->velocity -= body1->invMass * P;
body1->angularVelocity -= body1->invI * Cross(r1, P);
...
}
And then, in Arbiter::ApplyImpulse(), the change of impulse, dPn is applied to again update velocity:
Therefore, after one loop (shown below), the velocity and impulse are up-to-date.
However, at the beginning of the next loop starting with Arbiter::PreStep(), the old impulse (calculated at the end of the previous loop) is applied again to update the velocity, although the velocity is up-to-date.
if (World::accumulateImpulses)
{
// Apply normal + friction impulse
Vec2 P = c->Pn * c->normal + c->Pt * tangent;
body1->velocity -= body1->invMass * P;
body1->angularVelocity -= body1->invI * Cross(r1, P);
...
}
I exactly do not understand why the old impulse is applied again to update the velocity.
Velocity was already up-to-date (although b->velocity += dt * gravity is applied in the new loop), and only the change of impulse, dPn, looks necessary to consider, but dunno why.
In the slide "GDC2006_Catto_Erin_PhysicsTutorial.ppt", it simply says "Apply old accumulated impulses at the beginning of the step." for coherence, and this looks related to my question, but could you shed some more lights on me?
Thanks in advance.
While looking at the code, I found the old impulse is applied again to update the velocity (if I choose to accumulate impulses):
In this way, my understanding is the velocity goes corrupted, but simulation is looking good. dunno why.
To explain further,
In Arbiter::PreStep(), the old impulse is applied to update velocity:
Code:
if (World::accumulateImpulses)
{
// Apply normal + friction impulse
Vec2 P = c->Pn * c->normal + c->Pt * tangent;
body1->velocity -= body1->invMass * P;
body1->angularVelocity -= body1->invI * Cross(r1, P);
...
}
And then, in Arbiter::ApplyImpulse(), the change of impulse, dPn is applied to again update velocity:
Code:
// Apply contact impulse
Vec2 Pn = dPn * c->normal;
b1->velocity -= b1->invMass * Pn;
b1->angularVelocity -= b1->invI * Cross(c->r1, Pn);
...
Vec2 Pn = dPn * c->normal;
b1->velocity -= b1->invMass * Pn;
b1->angularVelocity -= b1->invI * Cross(c->r1, Pn);
...
Therefore, after one loop (shown below), the velocity and impulse are up-to-date.
Code:
// Perform pre-steps.
for (ArbIter arb = arbiters.begin(); arb != arbiters.end(); ++arb)
{
arb->second.PreStep(inv_dt);
}
...
...
// Perform iterations
for (int i = 0; i < iterations; ++i)
{
for (ArbIter arb = arbiters.begin(); arb != arbiters.end(); ++arb)
{
arb->second.ApplyImpulse();
}
...
}
for (ArbIter arb = arbiters.begin(); arb != arbiters.end(); ++arb)
{
arb->second.PreStep(inv_dt);
}
...
...
// Perform iterations
for (int i = 0; i < iterations; ++i)
{
for (ArbIter arb = arbiters.begin(); arb != arbiters.end(); ++arb)
{
arb->second.ApplyImpulse();
}
...
}
However, at the beginning of the next loop starting with Arbiter::PreStep(), the old impulse (calculated at the end of the previous loop) is applied again to update the velocity, although the velocity is up-to-date.
Code:
if (World::accumulateImpulses)
{
// Apply normal + friction impulse
Vec2 P = c->Pn * c->normal + c->Pt * tangent;
body1->velocity -= body1->invMass * P;
body1->angularVelocity -= body1->invI * Cross(r1, P);
...
}
I exactly do not understand why the old impulse is applied again to update the velocity.
Velocity was already up-to-date (although b->velocity += dt * gravity is applied in the new loop), and only the change of impulse, dPn, looks necessary to consider, but dunno why.
In the slide "GDC2006_Catto_Erin_PhysicsTutorial.ppt", it simply says "Apply old accumulated impulses at the beginning of the step." for coherence, and this looks related to my question, but could you shed some more lights on me?
Thanks in advance.