Question by
noman.waseem · Jan 14, 2016 at 06:11 PM ·
rigidbody2dtorqueaddtorquerigidbody 2d
Determining the torque required for Rigidbody2D.AddTorque given initial and final angle, revolutions and time
Hello,
I did come across this post:
http://answers.unity3d.com/questions/48836/determining-the-torque-needed-to-rotate-an-object.html
but it is for a Rigidbody which has the inertiaTensorRotation property whereas Rigidbody2D does not have this property.
Anyway, here's the code I have so far:
float revolutions = 0;// The number of times the object should rotate before arriving at the final angle.
time = 2;// The number of seconds to take to arrive at the final angle.
public GameObject target;// An object that holds the final position and angle values.
float thetaFinal = target.transform.rotation.eulerAngles.z + 360 * revolutions;
// Calculate the change in angular velocity.
float deltaAngularVelocity = (thetaFinal - transform.rotation.eulerAngles.z) / time - rb.angularVelocity;
// Calculate the torque required to complete the correct number of revolutions and end with the correct target angle of rotation.
float torque = rb.mass * (deltaAngularVelocity * Mathf.Deg2Rad);
rb.AddTorque(torque, ForceMode2D.Impulse);
The target game object in the code refers to a simple sprite object that I used as a reference for where the object should be thrown to and at what final angle it should reach its destination at. This final angle comes from:
target.transform.rotation.eulerAngles.z
But for some reason the actual angle when the object reaches the target's position keeps over-shooting the expected value regardless of whether revolutions is set to zero or some other number.
Am I messing up my math somewhere?
Thanks!
Comment