- Home /
Set rotation based on 1,1,1 style vector? How to convert vector3 to quaternion?
Okay, I'm up against something of a mathematical/conversion issue and I'm not experienced enough switching between Vector3, Euler, and Quaternions to know the best way to do this:
I have an angle (x, y, z) determined not as Euler angles, but in the 0-1 float format [I'm not sure what the term for it is, but I'm using normal Vector x/y/z where 1.0 = 90 degrees, ie up = (0, 1, 0), ie (0 x, 90 y, 0 z)]
I need to set a new GameObject's rotation based on these Vector3 values... how could I do that? (I am doing this based on x/y cartesian coordinates on a place so I can, of course, use trigonometry to figure out the euler angles, but it will be much faster and cleaner if I can do it non-Euler)
From the sound of it you have unit vectors, not angles. Are you measuring the angle between them and another vector?
I would imagine he is either misspeaking and meant 0-360 or he's using some kind of shortest angle assumption where he only works within one quadrant so 127 is actually represent as 127 - 90 = 37 degrees which is roughly .34 or so in a particular quadrant.
Answer by sparkzbarca · Aug 09, 2015 at 06:07 AM
you can't safely convert vector3 to quat and back.
the best you can do is quanterion.euler(...);
The issue is quanternions have 4 elements and vectors have 3 and there simply is no way to go from quan to vector without losing some data and no way to from vector 3 to quan without picking one of multiple valid answers.
http://blog.preoccupiedgames.com/quaternions-not-satan/
Notice it says there can be multiple euler representations for one rotation.
that 0, 180, 0 is the same as 180, 0 , 180.
YOu might say well just pick one, except while those two rotations are the same, adding something to them doesn't both produce the same rotation.
So that's the problem, quats have exactly one answer for any rotation and adding them always gets the same answer.
but 0,180,0 + 90,0,0 to 90,180,0 isn't facing the same direction as 180,0,180 + 90,0,0 to 270,0,180
even though 0, 180,0 is 180,0,180. adding 90,0,0 to both gives different results. if your rotated this way in one you might be standing facing someone, in the other your butt is facing them and your on your head.
that's why you can't really convert because when your converting down from quan do you convert to 180,0,180 or 0,180,0.
Quaternions are better, they don't suffer from gimbal lock for example is the big thing.
basically eulers have 3 angles and 3 axes.
quaternions have a single axis and rotate around it.
(as i understand it, quats are complex numbers, there great for computers, horrible for humsies)
as such your best thing to do is use quaternions.
otherwise convert to 0-90 from 0-1 and then feed into quaternions.euler I suppose.