Reputation: 2127
I'm writing a script which rotates a Rigidbody
using a Configurable Joint
. I've got the targetRotation
figured out, but now I'm struggling with targetAngularVelocity
, which should help me avoid wobbliness if set correctly.
targetAngularVelocity
is defined like this in the documantation: "This is a Vector3
. It defines the desired angular velocity that the joint should rotate into". The problem is that I don't know how to get this Vector3
based on two Quaternions
- current rotation of the object and the target rotation.
Am I not understanding it correctly? Is there a function that returns a rotation vector based on two Quaternions
?
Upvotes: 1
Views: 2009
Reputation: 29264
So mathematically a Quaternion represents the orientation of a rigid body. Consider the forward problem first, and see how the orientation q_1 transforms to another orientation q_2 after a rotational velocity ω is applied for t time.
Mathematically the rotation vector has a magnitude ω and a direction k such that ω = ω* k
This is done with quaternion multiplication as
q_2 = q_ω * q_1
Where q_ω represents a rotation about the axis of k of an angle θ=ω*t.
In reverse, you need to find q_ω with
var q_ω = q_2 * Quaternion.Inverse(q_1);
and extract the rotation axis and angle
q_ω.ToAngleAxis(out float angle, out Vector3 axis);
and compose the rotational velocity vector, that corresponds to this transformation in time
seconds.
var ω = (angle/time)*axis;
Upvotes: 7