Objects in 3D space move at a certain speed and sometimes require the object to be oriented in the same direction as the velocity,
In order to achieve this goal we generally use the rotation matrix M to rotate the object to the corresponding orientation.
For example, the velocity direction vector Spdv:vector3d (
The x-axis base vector is Axis_x:vector3d (1,0,0), and the direction of the vector is aligned with the default orientation of the 3D object without any rotation.
3D vector cross_x Records the results of the axis_x fork multiply SPDV.
Method one of the matrix M is calculated:
The radian value between the SPDV and the axis_x two vectors is calculated by RAD (computed by the cosine function), and then a unit matrix M0 is used.
Let M0 rotate rad around the axis cross_x to get the result matrix M.
method to figure out matrix M two(This method is simple to calculate, so it can be used in some places where matrices cannot be used directly, such as some shader calculations):
The base vector (axis_x,axis_y,axis_z) of the matrix M three orthogonal axes is calculated directly by the geometric algorithm.
How is it calculated? See the following code:
<span style= "White-space:pre" ></span>/** * Calculates the data of the three axes of the object toward the matrix by speed * */public static function Calcdirecmatbyvelocity (Spdv:vector3d):vector.<vector3d> {//record x-axis base vector var Axis_x:vector3d = new Vector3D (1, 0, 0) ;//Axis_x and SPDV fork by var cross_x:vector3d = axis_x.crossproduct (SPDV);//cross_x and SPDV fork multiply, and the result of cross-multiplication is recorded in MATHCALC.OUTCROSSV In this 3D vector, this function is used in order not to produce a new 3d vector object Mathcalc.crossv3d2 (cross_x, SPDV); Cross_x.normalize (); MathCalc.outCrossV.normalize ();//cross_x.x = 0.5 * (cross_x.x + mathcalc.outcrossv.x); cross_x.y = 0.5 * (cross_x.y + mathc ALC.OUTCROSSV.Y); cross_x.z = 0.5 * (cross_x.z + mathcalc.outcrossv.z);//At this point, a new z-axis cross_x.normalize () has been calculated;//Get new y-axis var Axis_y:vector3d = Cross_x.crossproduct (SPDV); axis_y.normalize ();//SPDV is the new x-axis, standardized Axis_x.setto (spdv.x,spdv.y,spdv.z ); Axis_x.normalize ();//var vs:vector.<vector3d> = new vector.<vector3d> (); Vs.push (Axis_x,axis_y,cross_ x); return vs;}
Explanation of principle:
First, the x-axis base vector of the rotation matrix M axis_x, spatially and SPDV coincident, so SPDV normalized, is the x-axis base vector axis_x
And the result of the cross_x axis and the SPDV fork is that the MATHCALC.OUTCROSSV falls on the yoz plane of the space represented by M,
The axis of rotation cross_x also falls on the yoz plane of the space represented by M.
And axis_z in the yoz plane is exactly the MATHCALC.OUTCROSSV and rotation axis cross_x angle of the middle line, so you can directly use the calculation
The midpoint method directly calculates the axis_z (base vector of the z-axis)
With Axis_z, the three base vectors are perpendicular to each other, so the axis_y is calculated by cross-multiplication.
At this point, the rotation matrix M is obtained, for example, this algorithm can be used to agal the velocity of particles toward
A calculation scheme of rotation matrix for the orientation of objects in 3D space and their velocity