- Home /
comparing an object rotation to a direction vector
i need to compare an object's rotation to a directionvector(gravity for now 0,-9.81,0) and to return the difference between the 2 so that i can apply force accordingly
right now im trying to figure out what should be compared , ive been trying to get a meaningfull vector out of quaternion.lookrotation altough it seems to work only for setting the object rotation and not reading it since the result is in local space and is always the same if the object moves
other than that ive looked at euler angles but cant see how to turn the vector to a rotation with them eighter
this is pretty much where im stuck, i need to figure out the equation and next ill just have to see how to work with the result , im working only on 2d to make that part simpler
thanks for the help
Answer by Jesse Anders · May 16, 2011 at 05:30 AM
You can't really compare an orientation to a vector in 3-d, but you can use the dot product to determine 'how aligned' two vectors are.
For example, assuming transform.up is up for your object, the dot product of transform.up and Vector3.up will tell you whether the object is more or less up-side-up or up-side-down, and to what degree.
Note that the dot product of transform.up and Vector3.up is simply transform.up.y. (I'm assuming the magnitude of the gravity vector doesn't matter here. Also, I realize your reference vector is pointing in the direction opposite Vector3.up, but that's just a matter of switching signs.)
so here's what come up with so far , im taking my forward vector and physics.gravity and doing the dot product but the result is always 0 so i guess the gravity vector isnt the right typr of expression , here my bit of code , what do you think ?
var gyro : float; var ward : Vector3;
function Start(){ grav = Physics.gravity;}
function Update () { ward = transform.TransformDirection(Vector3.forward); gyro = Vector3.Dot(ward,grav);}
Ins$$anonymous$$d of writing 'transform.TransformDirection(Vector3.forward)', you can just use transform.forward. As for the problem you mention, there's no problem with computing the dot product of the forward direction vector and the gravity vector. However, depending on what you need the result for, you may want to normalize the gravity vector first, or just use forward.y if the gravity vector is y-aligned. Also, a result of 0 would suggest that the forward vector is perpendicular to the gravity vector.