AddImpulse instead of AddForce?

When we apply the sideways force, our goal is to reduce the velocity of the tank transverse to the forward direction to zero. Since we only do this once per frame (either on Tick or OnHit), we use the DeltaTime of that frame to match the strength of the sideways force, applied for the duration of the frame, to that velocity.

There’s a slightly better (IMO) way to do this, conceptually, which also allows us to dispense with DeltaTime. If F is the force magnitude, dt is the time over which it acts, and delta mv is the change of transverse momentum of the tank, we have F*dt = delta m v. Rather than divide through by dt to compute F = delta m v / dt, we can recognize that -F*dt/2 is the Impulse required (on each of the two tracks) to set the tank’s transverse momentum to zero, and instead of AddForce, we can do:

FVector RightDirection = TankRoot->GetRightVector();
FVector TankRootVelocity = TankRoot->GetPhysicsLinearVelocity();
FVector RightVelocity = FVector::DotProduct(RightDirection, TankRootVelocity)*RightDirection.GetSafeNormal();
FVector TransverseMomentum = TankRoot->GetMass() * RightVelocity;
FVector CorrectiveImpulse = -0.5f*TransverseMomentum;
TankRoot->AddImpulse(CorrectiveImpulse);

This, I believe, achieves the same effect, but with some cleaner physics, and without needing to both retrieve from GetWorld and divide by some small number (DeltaTime).

Privacy & Terms