Better CalculateAngularVelocity

This commit is contained in:
MrGadget 2024-04-10 12:54:57 -04:00
parent 80f2d1e641
commit e93e2eae4d

View File

@ -148,13 +148,26 @@ protected virtual void Update()
// Update velocity and angular velocity
velocity = (pos - lastPosition) / Time.deltaTime;
angularVelocity = (rot.eulerAngles - lastRotation.eulerAngles) / Time.deltaTime;
CalculateAngularVelocity(rot);
// Update last position and rotation
lastPosition = pos;
lastRotation = rot;
}
void CalculateAngularVelocity(Quaternion currentRot)
{
// calculate angle between two rotations
Quaternion deltaRotation = currentRot * Quaternion.Inverse(lastRotation);
// convert to angle axis
deltaRotation.ToAngleAxis(out float angle, out Vector3 axis);
// we assume the angle is always the shortest path
// we don't need to check for 360 degree rotations
angularVelocity = axis * angle * Mathf.Deg2Rad / Time.deltaTime;
}
// snapshot functions //////////////////////////////////////////////////
// get local/world position
protected virtual Vector3 GetPosition() =>