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.
(comments are locked)

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. UPDATE: turns out you can change the max. angular velocity in Edit>Project Settings>Physics
Feb 25, 2011 at 05:13 AM
hellcats
o.O nice answer!
Apr 12, 2011 at 05:57 PM
Joshua
Wow.... What is the meaning of life!?! All jokes aside, very interesting answer. I have been trying to implement it for a while but I am not getting anything out of T. I might be a little confused as to what "old point" and "new point" represent. I replaced them with the position vectors of the object that I wish to rotate and the object I wish to rotate towards. Em I doing this wrong? While I love your answer and this all sounds like exactly what I am looking for but this is beyond me so if you could explain what oldpoint and new point is. That would be great. I am trying to rotate an object towards another object by torque. Picture a arrow at a bottom of the screen and a ball at the top, the arrow is pointing down and I wish to have it point at the ball but using torque to do so. I think that this is what your explanation goes over but I have not been able to get it to work so far.
Oct 08, 2012 at 03:35 AM
Anxo
This simple and clear solution worked right away in my scenario. How can it be expanded to include rotation around the Z axis as well? This is not currently taken into account. Would simply multiplying the 'q' quaternion by Quaternion.FromToRotation using the local current and destination rotations do the trick?
Nov 12, 2012 at 02:26 PM
daterre
daterre  I've been trying to figure that out myself (expanding to full rotation), but am missing something. I find if I do not factor in the inertialTensor, and simply assign rigidbody.angularVelocity = w; then I get the exact desired rotation in a single frame. Factoring in the inertialTensor makes it lag. ForceMode.Impulse assumes you have included mass in your value, however, switching to ForceMode.VelocityChange didn't have any effect in my case since my object's mass is 1. Combining angular velocities is the sticking point for me. Here's a straightforward expansion upon hellcats' presentation above, which provides full alignment:
I'd like to understand where that lag is coming from. I'm concerned that simply adding the two angular velocities (wZ and wY) only works here because they are getting scaled down small enough by the intertiaTensor. I would expect rigidbody.angularVelocity to be in object space, but that appears to not be the case?
Dec 11, 2012 at 11:43 PM
aleiby
(comments are locked)

Hi Sol. To be clear you will actually need two torques, one to start the object rotating, and one to stop it at the desired rotation. Then you need to decide how you want it to accelerate / decelerate. It's a bit like asking what force you need to get to the moon: Not much if you don't mind waiting a long time then smashing into the surface. However if you want to get there in an hour, and to accelerate smoothly you could accelerate at around 13g for 30 minutes, turnaround, then decelerate at the same rate for another half an hour. Same with your torques and rotation. Does that make sense? So the magnitude of the torque(s) you need depends not only the angle change, but on the time you want it to take (as well as the acceleration / deceleration strategy). The rotation won't be very "physically correct" if it instantaneously moves from one rotation to another, unless the object has no rotational inertia. So we must involve time in this, and you need to decide how long you want your rotation to appear to take. On that basis you're not really working out a torque, but a torque at a given time into the "animation". What's the method signature you're after, would this pattern do?...
If your object is 3D, and rotating around more than one axis you'll need an inertia tensor rather than the simplified "moment of inertia" about one axis (which is just one element of a 3x3 matrix). In which case readon. If you do go down the inertia tensor route, bear in mind...
==== On a related note in case it helps, I got a cool simulation of a phone flipping according to the parallel axis theorem, so have managed to add significant value to the existing physics engine. It seems that we need to do a transformation to the local coordinates of the flipping object, then calculate the rotation for things to work properly. I've attached a zipped Unity package with the working miniproject. I'll be going through this stepbystep when I add a section to my new game physics course in March 2015. I'll be removing the cumbersome single and double underscore notation don't worry! BenTristem, your object is set to Kinematic and this is somewhat differs from what was initially asked  use of AddTorque to make desired rotation. Btw, did you intentionally shifted center of mass?
Feb 14 at 09:35 PM
Alex_K98
You're quite right Alex, in my eagerness to share what I've done I confused this thread with another one over here... http://forum.unity3d.com/threads/isthisabuginthephysicsenginerotationalstability.259514 I'll modify it, thanks.
Feb 15 at 07:33 AM
BenTristem
(comments are locked)

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/myimages/706/explanationp.png My object is travelling at a fixed speed on a heading in a 2d 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 zaxis. 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. Thanks, BinaryX
(comments are locked)

Taking hellcats's answer and fixing the "wobbliness" reported by aleiby, I came up with this :
The fix is to substract the current angular velocity. I hope this will be useful for other people googling around for this. If you modify the inertia tensor yourself, the ForceMode can sometimes be changed to
(comments are locked)

Gentlemen, what is the sense of multiplying by 'inertiaTensorRotation' when it is always W=1 and XYZ=0, at least this is what Debug.Log shows me? The way the Unity engine reports inertia tensor is a bit "squiffy". It always deals with diagonalised tensors, and seems not to transform them into the frame of the rotating object. I found that by translating to the frame of the rotating object, we get the required behaviour. I'll be adding another section to my game physics course in March 2015 to go through this stepbystep. I've also updated my answer above to remove the link to my course.
Feb 14 at 07:24 PM
BenTristem
BenTristem, by saying 'transform them into the frame of the rotating object' you mean 'transform to local object coordinate system'?
Feb 14 at 07:48 PM
Alex_K98
That's right Alex, however I only looked at this briefly. I've attached my prototype above, and will be going through it and thinking about it much more carefully soon.
Feb 15 at 12:08 AM
BenTristem
(comments are locked)

1 2 next page »