- Home /
how to combine 2 quaternion components?
Hi, I want to combine the rotation components of 2 quaternions, giving as result:
aQuat.x, bQuat.y, aQuat.z
how can I achieve this?
Thanks.
I'd say to start with what you want in physical terms. For example, quat#1 is a facing, and quat#2 is a "facing tweak" such as "10degs left and 5degs up from current facing."
Answer by Peter G · May 21, 2011 at 03:12 PM
I highly doubt that you want to do that.
Quaternions have 4 components so I don't know what you are even trying to do.
Each value does not necessarily store rotation around a single axis so editing a specific value might give you strange results.
Maybe you want Euler Angles:
var angleA : Vector3 = new Vector3 ( x , y , z );
var angleB : Vector3 = new Vector3 ( x , y, z );
var angleC : Vector3 = new Vector3 ( angleA.x , angleB.y , angleA.z);
transform.eulerAngles = angleC;
or if you want to use Quaternions use the "*" operator to multiply them together:
var rotationAngle : float = 90;
var quatA = Quaternion.AngleAxis(rotationAngle , Vector3.forward);
var quatB = Quaternion.AngleAxis(rotationAngle , Vector3.up);
var quatC = quatA * quatB;
//this will be rotate 90 degrees around the z axis and 90 degrees around the y axis.
transform.rotation = quatC;
Answer by Xav_m · May 21, 2011 at 04:37 PM
Thanks!
I think I missunderstood quaternions.
I have a function that lines a prefab with an irregular terrain. Works ok as long as terrain is flat, when slopes occur some geometry gets into terrain mesh, so I get some cuts. I need to align the prefab to the last positioned one, to form a continous skidmark, so I thought on getting the normal of the terrain and aligning X,Z to terrain and Y to lookat previous prefab.
So back to question, how do I get a component from a quaternion?
I tried:
Debug.Log ("rotx: "+rotationQ[ 0 ]);
and get different result from
inspector value: 331.4471
debug log: -0.2466001
Thanks again.
You should read Peter's answer carefully. A Quaternion is a 4-dimensional representation of a rotation in 3d-space. They are used to prevent errors or impossible rotations in some special cases. The components are parts of a complex number system. You don't have to deal with the 4 components at all. The 3 numbers that you can see in the inspector are the localEulerAngles
.
Oh, and don't post comments as answers... You can edit your question at any time or use comments to comment an answer.
Thanks for the explanation on quaternions as well as the localEulerAngles.
I'm sorry, missed the "comment button".
Cheap method: place an empty on the end of the skid mark. To make a new skidmark. place it on the empty for the old one, and do a lookAt to the tire. A lineRender also has possibilities.