- Home /
I've rewritten my problem with wording that better reflects my actual goal
Calculate Difference Between Two 3d Angles
I have a rigidbody and a point in space, and I'm trying to use Rigidbody.AddTorque
to make the rigidbody attract to the position and rotation of the point. Here's the code I have so far; public Rigidbody ObjectToAttract; [Range(0, 50)] public float Agressiveness = 10; [Range(0, 10)] public float RotationalAgressiveness = 10;
void FixedUpdate()
{
Vector3 Difference = transform.position - ObjectToAttract.transform.position;
ObjectToAttract.AddForce(Difference * Agressiveness - ObjectToAttract.velocity, ForceMode.VelocityChange);
Vector3 AngDifference = AngleDifference(ObjectToAttract.transform.rotation.eulerAngles, transform.rotation.eulerAngles);
ObjectToAttract.AddTorque(AngDifference * RotationalAgressiveness - ObjectToAttract.angularVelocity, ForceMode.VelocityChange); // This line causes issues
}
And here's the AngleDifference
function;
private Vector3 AngleDifference(Vector3 Angle1, Vector3 Angle2)
{
Vector3 Difference = new Vector3();
Difference.x = Angle2.x - Angle1.x;
if (Difference.x > 180) Difference.x -= 360;
else if (Difference.x < -180) Difference.x += 360;
Difference.y = Angle2.y - Angle1.y;
if (Difference.y > 180) Difference.y -= 360;
else if (Difference.y < -180) Difference.y += 360;
Difference.z = Angle2.z - Angle1.z;
if (Difference.z > 180) Difference.z -= 360;
else if (Difference.z < -180) Difference.z += 360;
return Difference;
}
I have the code for attracting the rigidbody to a specific position working fine, but the code for applying torque to it causes it to spin in seemingly random directions, regardless of the angle I rotate the point to. What am I doing wrong here?
Answer by Glurth · May 27, 2016 at 02:58 PM
While MSpiteri is correct about the way to find the angle between two vectors, from you code it looks like you are trying to find the angle between two QUATERNIONS. The Vector3.Angle function is NOT going to work right on "euler angles". Instead, use the Quaternion.Angle function. http://docs.unity3d.com/ScriptReference/Quaternion.Angle.html e.g.
float AngleDiff = Quaternion.Angle(ObjectToAttract.transform.rotation, transform.rotation);
However, I don't think a single float angle is going to be what you want. I think you will want a Quaternion result, that represents the DIFFERENCE in rotations, AS a rotation.
The quaternion MULTIPLY function is used to combine two rotations A and B into a final rotation C. You can also multiply by negative rotations, to get the difference in rotations. http://docs.unity3d.com/ScriptReference/Quaternion-operator_multiply.html
Answer by MSpiteri · May 27, 2016 at 12:00 PM
Just a note. If you have 2 vectors and you want to find the angle between them, you can use Vector3.Angle
http://docs.unity3d.com/ScriptReference/Vector3.Angle.html
Once you know the angle, you can apply torque forces to make the object look in the same direction as the target point.
Hope this helps.
Follow this Question
Related Questions
Figuring out the correct amount of torque to apply? 2 Answers
AddRelativeTorque not rotating 1 Answer
Torque applied only after force 0 Answers
add torque at position 2 Answers
Nose Diving Plane 1 Answer