Basic network play: Quaternions -> Torque
I'm developing a *very* simple P2P network play system, where the opponent's position is simply updated over a socket, and forces applied to correct the position of the opponent on the player's screen.
Forces are no problem, but I also need to apply torque to correct the orientation of the object. Can anyone tell me what f(q1, q2) would be in the equation:
Vector3 currTorque = f(currentOrientation, targetOrientation)?
Please help, I've always sucked at geometry and this one has me stumped.
Well good, we have something in common. We both suck at geometry.
As you probably know, the torque is basically a rotation force and it is given by the cross product of two vectors r and F. You also already probably know the right-hand-rule that helps remember those 2 vectors.
r is the axis of rotation, imagine it is a screw or a bolt you are trying to screw/unscrew and this is your thumb.. F is the direction you would pull or push if you were using a wrench to screw/unscrew and this is your other 4 fingers.
The cross product of these 2 vectors r x F will give you the torque that you are applying and it also can be vizualized as the area of the parallelogram spanned by these two vectors. So, the longer wrench and the more perpendicular you push, the easier it will be to unscrew that bolt.
So, where do quaternions fit into that ? I am not sure. There is some information missing. I assume they represent your 2 vector or directions. If it is the case, you could transform some unit vector with the 2 quaternions a use the cross product between those 2 vectors. But still, where is the mass, the inertia tensor, etc. ? I would need more info.
In MOGRE, torques are represented as a Vector3. This means rotation around three axes, IIRC around the X, Y and Z axes, since to apply the torque to your object in-situ you apply a torque of [objectorientation]*[torque vector].
Quaternions represent a full rotation operation with pitch yaw and roll, which cannot be represented by a single rotation. I believe they can be decomposed into three axes and three angles.
Since two quaternions can have different axes, I cannot simply calculate the difference in angles between two quaternions. If I could make them use common axes or something I might be able to get started.
Mass, MOI are unimportant, since Newton will take care of that. I just need a rough idea of what torque to apply this frame to get the object rotating towards the orientation required.
I figured it out. For the benefit of anyone using my search terms here is the solution
Quaternion rotError = theBody.Orientation.UnitInverse() * targetOri;
Matrix3 rotMat = rotError.ToRotationMatrix();
Radian r1, r2, r3;
rotMat.ToEulerAnglesXYZ(out r1, out r2, out r3);
new Vector3(r1.ValueRadians, r2.ValueRadians, r3.ValueRadians);
To explain, the difference between two quaternions is given by multiplying one by the inverse of the other. I discovered that quite by accident almost simultaneously to working out the other part.
We can get a rotation matrix from a quaternion. This can then be reoriented to the 'euler' axes, X, Y, and Z. By reading the angles from here and simply creating a torque vector proportional to it, we can achieve the desired effect.
For a full solution we'd need to do some more work on minimum paths, but this works just fine in a situation where the errors between orientations should be small.