Euler angle
Formula of four-dollar calculation
The basic mathematical equations for the four-tuple are: q = cos (A/2) + i (x * sin (A/2)) + j (Y * sin (A/2)) + K (z * sin (A/2)) where a is the rotation angle, (x, Y, z) represents the axis of revolution
here's how to match the specific four-dollar number to the rotation axis and rotation angle. 1. Indicate the rotation axis and rotation angle, how to convert to four elements. assume that the axis of rotation is: Raxis = Z axis, converted into three-dimensional space unit vector is Raxis = [0 0 1], rotation 60 degreesthen the conversion to a four-dollar number isQ.w=cos (60°/2) = 0.866Q.x=raix.x*sin (60°/2) = 0*0.5=0Q.y=raix.y*sin (60°/2) = 0*0.5=0Q.z=raix.z*sin (60°/2) = 1*0.5=0.5Example validation: From a three-dimensional space, assume that the object point a=[0 1 0], around raxis = Z axis, rotation 30 degrees (assuming clockwise is positive, because MATLAB is clockwise, and the following QUAT2DCM function is the MATLAB own)then the coordinates of the object point a after rotation in the world coordinate system will be b=[0.866 0.5 0],how to calculate with a four-dollar number? The idea is this: any four-dollar number corresponds to a rotated 3*3 matrix. M=QUAT2DCM (q) *a ' =[0.866;0.5;0], about QUAT2DCM in software matlab inside has.
four meta-number programminga representation of a four-dollar number is: Q = xi + YJ + ZK + w
Here I, J, K can be seen as 3 coordinate column vectors in 3D space. Based on the four-tuple representation, so direct, he can be represented as a scalar W.
plus a 3-dimensional vector.
Q = [w, v]
Here V = xi + yj + ZK
naturally, someone with some programming experience will immediately put the little guy in the structure to show the following:
struct quaternion
{
double x, Y, Z, W;
}
here we don't need to know the addition and subtraction of the four-dollar algorithm, now to solve our goal (create a camera based on the four-dollar number) We need to know how to standardize him in the first step.
. The normalization of the four-dollar number is the same as the normalization of vectors. The goal is to turn the module into 1. | q| = sqrt (w^2 + x^2 +y^2 + z^2)
here is the code
Double GetLength (quaternion quat)
{
return sqrt (quat.x * quat.x + quat.y * quat.y + quat.z * quat.z + QUAT.W * quat.w);
}
)
normalized four-dollar q* = q/| q| = [w/| q|, v/| Q|];
Here is the code:
quaternion normalize (quaternion quat)
{
Double length = length (Quat);
quat.x/= Length;
quat.y/= Length;
quat.z/= lenght;
quat.w/= Length;
return quat;
}
down we need to know how to calculate a four-dollar conjugate four-dollar number, conjugate four yuan for the moment to use Q ' to replace it.
Q ' = [w,-V]
Here is the code that calculates the conjugate four-dollar number:
quaternion conjugate (quaternion quat)
{
quat.x =-quat.x;
quat.y =-quat.y;
quat.z =-quat.z;
QUAT.W =-QUAT.W;
return quat;
}
the next thing is to see how to calculate the score of four yuan, this seems a little bit complex.
Suppose there are four of dollars C, A, B now we want to calculate c= A * B;
the specific operation rules are as follows:
c.x = | A.W * b.x + a.x * B.W + a.y * b.z-a.z * b.y|
C.Y = | A.W * b.y-a.x * b.z + a.y * B.W + a.z * b.x|
C.z = | A.W * b.z + a.x * b.y-a.y * b.x + a.z * b.w|
C.W = | A.W * b.w-a.x * B.X-A.Y * b.y-a.z * b.z|
Here is the code:
quaternion mult (quaternion quat)
{
quaternion C;
c.x = a.w*b.x + A.X*B.W + a.y*b.z-a.z*b.y;
c.y = a.w*b.y-a.x*b.z + A.Y*B.W + a.z*b.x;
c.z = a.w*b.z + a.x*b.y-a.y*b.x + a.z*b.w;
C.W = a.w*b.w-a.x*b.x-a.y*b.y-a.z*b.z;
return C;
}
The main operation is finished, the following is going to get to the point. Now we're going to create the camera, please note. Obviously, it represents a camera in 3D space .
The camera needs to be positioned and directed. So in order to create the camera accurately, we use 3 3-D vectors to represent the camera's position position, to observe the direction view,
and the camera up (this is not necessary to introduce it). For a first-person camera, we now have to think about the camera's rotation,
using a four-dollar number we can rotate a vector around any bar, and for this we need to first convert the view vector to four, then define a rotation of four, and finally apply the rotation of the four-dollar number to the view four-dollar number. Below, please see the specific steps.
to get the view four, we're going to use [w, V] to represent it. Of course, the scalar W is set to 0. V is obviously the view vector.
so four Yuan V (the view we converted four yuan) v = [0, view]
and then, as I said above, we're going to create that spin four-dollar number, and in order to create this $ four, you have to be clear about which vector you want to rotate around, and the angle of rotation. We call this column vector (that is, around which vector rotation) is called a, the angle of rotation with theta, then the task is about to complete, we create a rotation of four R can be expressed as:
3DVector A = [x, y, z];
r.x = a.x * sin (THETA/2);
r.y = a.y * sin (THETA/2);
r.z = a.z * sin (THETA/2);
R.W = cos (THETA/2);
it will be rotated to calculate:
First look at the amount we know now:
1. We have a 3-dimensional view vector, we have a view of four yuan V = [0, view]
2. Suppose we are going to rotate the theta degree around a vector, we have a rotation of four yuan R to define this rotation.
3. Remember that what we get after the rotation is still a view of four yuan, this is naturally a new view four Yuan number. Let's say he's W .
so the rotation operation is:
W = R * V * r '
here R is the rotation of four yuan, V is the view four, R ' is the conjugate four-dollar number of rotation four-dollar R (its operation is preceded by speaking)
now we're just extracting the new vector portion of the view four-dollar W. Newview = [w.x, W.y, W.z]
so in summary we can create a function to perform a rotational transformation:
void Rotatecamera (float angle, float x, float y, float z)
{
quaternion temp, Quat_view, result;//Quat_view is the view $ four, temp is rotated four Yuan
//Three lines below use rotating column and angle to calculate rotation of four yuan
temp.x = x * sin (ANGLE/2);
temp.y = y * sin (ANGLE/2);
temp.z = z * sin (ANGLE/2);
temp.w = cos (ANGLE/2);
//Calculation of view four Yuan
quat_view.x = view.x;//view is a view vector
quat_view.y = view.y;
quat_view.z = view.z;
quat_view.w = 0;
//Rotation Transformation
result = mult (mult (temp, Qaut_view), conjugate (temp));
//Get a new view vector
view.x = result.x;
view.y = result.y;
view.z = result.z;
}
This completes. People who believe there is a certain OpenGL or D3D Foundation can easily translate this into code.
Euler's angle and four-dollar number (calculation, programming)