I use the following code to determine the new rotation for an object and to instantly rotate it to that orientation.
I want to change this code to use AddTorque to start the object rotating in the direction of the new rotation. I plan to use the distance between startingPoint and targetPoint to control the magnitude of the torque that is applied. Is there a way to use the beginning and ending rotations to determine the correct vector to pass to AddTorque() so that the object will rotate in the proper direction?
BTW, I don't want to use Quaternion.Slerp() since I want to object to behave physically correctly.
asked Feb 22 '11 at 03:56 AM
Fun question. Just like F = m a for linear forces, T = I alpha for angular forces. T is the torque, I is the inertia 3x3 tensor, and alpha is the angular acceleration. So basically your question amounts to finding an angular acceleration from a given change in rotation, and then multiplying that by I to get T.
Angular acceleration is a Vector3 whose direction is the axis of rotation and magnitude is rotational acc. in radians/sec^2. Since you already have two direction vectors (which need to be normalized), you can simply compute x = Vector3.Cross(oldPoint, newPoint) to get the required axis of rotation. This is the direction of alpha, but you still need the correct magnitude. We want radians/sec^2, so we need the angle between the two vectors. The magnitude of cross product is sin(theta) |v| |u|, since length of v and u are both 1, we just need Asin(x.magnitude).
Since you want to fully reach your newPoint in one frame, you can instead apply an impulse which is sort of like an instantaneous acceleration or change in velocity. So to summarize.
This gives us the desired change in angular velocity (w). Now we just multiply by the inertia tensor. Unfortunately this is in some weird diagonal space. It is easiest to transform w into this space, compute T, then transform T back to global coords.
Then just apply T to the rigidbody:
NOTE: PhysX seems to limit the speed of rotation, so this actually only works if the amount you are rotating by isn't too large.
Hi hellcats, thanks for the detailed answer.
I am trying to implement your solution in my project and I am running into difficulty.
Here's my situation: http://imageshack.us/photo/my-images/706/explanationp.png
My object is travelling at a fixed speed on a heading in a 2-d coordinate space (z is always 0). On a mouse click I would like to apply a relative torque to turn it to a new heading. The rotational axis is always (0,0,1) - the z-axis.
The way I see it, there are two ways to rotate an object with a rigidbody: 1) switch on kinematics and do it manually or, 2) apply a rotational force. I'd like to use the second option.
My FixedUpdate function looks like this:
The GetTorque function is:
Where totarget is an empty gameobject controlled by mouse move. Can anybody tell me if I am on the right track? Am I over complicating something that should be simple. Any and all help is greatly appreciated.