Coordinate system or axis system
7 views (last 30 days)
Show older comments
I want to change the axis in the 3d graphical representation. Let's take I have a 3d car model on the graph. I want to change the X-axis to Y-axis and the Z-axis to X-axis to make it look according to a particular format. Please look at the normal graph pic I have attached with this and tell me the method to change it.
5 Comments
Answers (1)
Ashok
on 30 Aug 2024
Hey Sagar,
I see that you are trying to create a body (a plane) in a 3D axis and then moving the body along a trajectory.
You can change the orientation of the body using rotation matrices. A rotation matrix maps the original vector () to the rotated vector (). For further information on rotation matrices, you can refer the following link:
The procedural approach involves applying three rotation matrices () in sequence to the original vector (), where the sequence of rotation matrices depends on the sequence of intrinsic Euler rotation. For instance, if the sequence of Euler rotation is , one can calculate the rotated vector as,
A snippet of this implementation is shown below:
% Calculate ZYX Euler angles
yaw = atan2(forwardDir(2), forwardDir(1));
pitch = asin(-forwardDir(3));
roll = 0; % Assuming no roll for simplicity
% Create ZYX rotation matrix
Rz = [cos(yaw), -sin(yaw), 0;
sin(yaw), cos(yaw), 0;
0, 0, 1];
Ry = [cos(pitch), 0, sin(pitch);
0, 1, 0;
-sin(pitch), 0, cos(pitch)];
Rx = [1, 0, 0;
0, cos(roll), -sin(roll);
0, sin(roll), cos(roll)];
R = Rz * Ry * Rx; % ZYX rotation order
% Transform the vertices of the box
transformedVertices = (R * boxVertices')' + pos';
Here, the functions “atan2”, “asin”, “sin”, and “cos” are trigonometric relations documented in the following page.
However, in case of a continuous animation along a smooth trajectory, you can assume that “the orientation of the local axis of the body changes slowly” to formulate the rotation matrix incrementally. The same is demonstrated in the snippet below.
% Calculate the rotation matrix to align the box with the direction
up = [0; 0; 1]; % Define the up direction
right = cross(up, frontwardDir);
right = right / norm(right);
up = cross(frontwardDir, right);
R = [frontwardDir, right, up];
% Transform the vertices of the box
transformedVertices = (R * boxVertices')' + pos';
You can find the documentation of the functions “cross” and “norm” in the links below.
Feel free to check the attached live script for the complete code. You can also use the “rotx”, “roty”, and “rotz” functions in MATLAB to create the rotation matrices, for which you can refer:
You can also create the combined rotation matrix at one go using the “eul2rotm” function.
Moreover, one may also rotate graphical objects in a plot using the “rotate” function, mentioned in the link below.
Hope this helps!
0 Comments
See Also
Categories
Find more on Graphics Object Properties in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!