- Home /
Calculate Angular Velocity to Match Remote Position
I have a rigid body that I need to sync with a remote game object's rotational position & angular velocity. Unfortunately, this object has a massive amount of children causing direct modifications to transform.rotation to have severe lag. instead, I am directly setting angular velocity to get fluid movements. This is my code:
private Vector3 GetAngularVelocity(Quaternion remoteRotation, Vector3 remoteAngularVelocity, GameObject gameObject)
{
Vector3 difference = new Vector3(Mathf.DeltaAngle(gameObject.transform.rotation.eulerAngles.x, remoteRotation.eulerAngles.x),
Mathf.DeltaAngle(gameObject.transform.rotation.eulerAngles.y, remoteRotation.eulerAngles.y),
Mathf.DeltaAngle(gameObject.transform.rotation.eulerAngles.z, remoteRotation.eulerAngles.z));
if (difference.magnitude < 0.1 && remoteAngularVelocity == Vector3.zero) //overcorrections can cause jitter when standing still.
{
return Vector3.zero;
}
Vector3 eulersPerSecondToMakeUpDifference = difference / PlayerMovement.BROADCAST_INTERVAL; //BROADCAST_INTERVAL = 0.05 seconds
Vector3 radiansPerSecondToMakeUpDifference = new Vector3(eulersPerSecondToMakeUpDifference.x * Mathf.Deg2Rad,
eulersPerSecondToMakeUpDifference.y * Mathf.Deg2Rad,
eulersPerSecondToMakeUpDifference.z * Mathf.Deg2Rad);
float maxAdjustment = 0.5f; // to prevent jerkiness and correct error over time
Vector3 limitedVelocityChange = MathUtil.ClampMagnitude(radiansPerSecondToMakeUpDifference, maxAdjustment, maxAdjustment * -1);
Console.WriteLine("Remote Rotation: " + precise(remoteRotation.eulerAngles) + " " +
"Current Rotation: " + precise(gameObject.transform.rotation.eulerAngles) + " " +
"Difference: " + precise(difference) + " " +
"eulersPerSecondToMakeUpDifference: " + precise(eulersPerSecondToMakeUpDifference) + " " +
"radiansPerSecondToMakeUpDifference: " + precise(radiansPerSecondToMakeUpDifference) + " " +
"remoteAngularVelocity: " + precise(remoteAngularVelocity) + " " +
"limitedVelocityChange: " + precise(limitedVelocityChange) + " ");
return remoteAngularVelocity + limitedVelocityChange;
}
rigidbody.angularVelocity = GetVehicleAngularVelocity(remoteRotation, remoteAngularVelocity, gameObject);
This causes behavior that is close but not quite right. After moving the remote object from one position to another, the local object tends to spin without ever landing on the desired target. Could this be Gimbal lock?
Example output:
Remote Rotation: (2.629201, 208.2598, 358.6983) Current Rotation: (15.38525, 167.0688, 260.5146) Difference: (-12.75604, 41.19099, 98.18378) eulersPerSecondToMakeUpDifference: (-255.1208, 823.8199, 1963.676) radiansPerSecondToMakeUpDifference: (-4.452699, 14.37837, 34.2726) remoteAngularVelocity: (0, 0, 0) limitedVelocityChange: (-0.05947673, 0.1920584, 0.4577948)
Remote Rotation: (2.629201, 208.2598, 358.6983) Current Rotation: (15.33852, 167.1382, 260.2452) Difference: (-12.70932, 41.12169, 98.45313) eulersPerSecondToMakeUpDifference: (-254.1864, 822.4338, 1969.063) radiansPerSecondToMakeUpDifference: (-4.436389, 14.35418, 34.36662) remoteAngularVelocity: (0, 0, 0) limitedVelocityChange: (-0.05914053, 0.1913524, 0.4581339)
Remote Rotation: (2.629201, 208.2598, 358.6983) Current Rotation: (15.20987, 167.3288, 260.1854) Difference: (-12.58066, 40.93106, 98.51297) eulersPerSecondToMakeUpDifference: (-251.6132, 818.6212, 1970.259) radiansPerSecondToMakeUpDifference: (-4.391478, 14.28764, 34.38751) remoteAngularVelocity: (0, 0, 0) limitedVelocityChange: (-0.05855986, 0.190524, 0.4585535)
Remote Rotation: (2.629201, 208.2598, 358.6983) Current Rotation: (15.20987, 167.3288, 260.1854) Difference: (-12.58066, 40.93106, 98.51297) eulersPerSecondToMakeUpDifference: (-251.6132, 818.6212, 1970.259) radiansPerSecondToMakeUpDifference: (-4.391478, 14.28764, 34.38751) remoteAngularVelocity: (0, 0, 0) limitedVelocityChange: (-0.05855986, 0.190524, 0.4585535)
Comment
Your answer
Follow this Question
Related Questions
Stop velocity on rotation change 2 Answers
Rotate Rigidbody Towards Velocity? [3d] 0 Answers
Adjusting Roll and Pitch with Velocity 0 Answers