- Home /
Quaternion LookRotation issue
Hi! I am trying to simulate the movement of the left forearm with the positions of the joints of the left shoulder, left elbow and left wrist.
I am computing the difference vectors: leftShoulder-leftElbow and leftWrist-leftElbow. Then I make the cross product between both and I called it "x". Then I assume that the left forearm is always aligned with one vector so I assigned "y" to the vector leftWrist-leftElbow. Then I make again the cross product between "x" and "y" to obtain the orientation "z".
Now I have all the orientations that I want and I need the forearm to move like according to these. Therefore I used a Quaternion. Through some readings I try to use the function Quaternion.LookRotation with two inputs and it kinda works. I put the code below to better understanding.
public class LeftForeArmReferencial : MonoBehaviour {
public GameObject leftShoulder;
public GameObject leftElbow;
public GameObject leftWrist;
private Transform leftShouldertrans;
private Transform leftElbowtrans;
private Transform leftWristtrans;
private Vector3 leftShoulderLeftElbow;
private Vector3 leftElbowLeftWrist;
private Vector3 x;
private Vector3 y;
private Vector3 z;
private Vector3 normx;
private Vector3 normy;
private Vector3 normz;
private Transform trans;
private Matrix4x4 m;
void Start ()
{
trans = GetComponent<Transform> ();
}
void Update ()
{
leftShouldertrans = leftShoulder.transform;
leftElbowtrans = leftElbow.transform;
leftWristtrans = leftWrist.transform;
leftShoulderLeftElbow = leftShouldertrans.position - leftElbowtrans.position;
leftElbowLeftWrist = leftWristtrans.position - leftElbowtrans.position;
x = Vector3.Cross (leftElbowLeftWrist, leftShoulderLeftElbow);
y = leftElbowLeftWrist;
normx = Vector3.Normalize (x);
normy = Vector3.Normalize (y);
z = Vector3.Cross (normx, normy);
normz = Vector3.Normalize (z);
m.SetColumn (0, normx);
m.SetColumn (1, normy);
m.SetColumn (2, normz);
trans.rotation = QuaternionFromMatrix (m);
}
public static Quaternion QuaternionFromMatrix(Matrix4x4 m)
{
return Quaternion.LookRotation(m.GetColumn(2), m.GetColumn(1));
}
}
My question is: how and why does this quaternion function work? I put the "z" as the first parameter and "y" as the second because they suggested it here http://docs.unity3d.com/ScriptReference/Quaternion.LookRotation.html but why is this way? Wouldn't it work if I changed?
Your answer
Follow this Question
Related Questions
Turret on Tank rotating fine until I'm not flat on ground 1 Answer
Why is Quaternion.Eular only rotating in one direction? [Answered] 1 Answer
Rotate a cube by 90 degree 1 Answer
please help to rotate a cube around it's own axis 1 Answer
Vector3.forward only moves me in 2 directions when rotating 1 Answer