Coordinate system or axis system

7 views (last 30 days)
Sagar Chawla
Sagar Chawla on 15 Mar 2022
Answered: Ashok on 30 Aug 2024
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
Sagar Chawla
Sagar Chawla on 16 Mar 2022
Okay, I will try to give you the code. Thanks for the answer.
Sagar Chawla
Sagar Chawla on 1 Apr 2022
Here is the code, it is not for the graph shown above. % prepare environment and generate aircraft trajectory clear all; close all; clc; data = zeros(400,6); % x, y and z coordinates data(:,1) = 0; data(:,2) = .5:.5:200; data(:,3) = 500; % pitch, yaw and roll angle values data(:,4) = 0; data(:,5) = 0; data(21:380,6) = (1:1:360)/57.3; % prepare aircraft simulation model new_object('aircraft.mat',data,... 'model','f-16.mat', 'edge',[.1 .1 .1],'face',[.1 .1 .1],... 'path','on','pathcolor', [.89 .0 .27],'pathwidth',1,'scale',1); % generate the scene and save the result as gif file flypath('aircraft.mat',... 'animate','on','step',3,... 'font','Georgia','fontsize',10,... 'view',[35 45],'window',[700 700],... 'output','aileron_roll.gif',... 'xlim',[-20 20],'ylim',[0 200], 'zlim',[480 520]); The image shows the diagram on a z, x & y axis. I want to change coordinates as z in place of x and y in place of z and x in place of y.

Sign in to comment.

Answers (1)

Ashok
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!

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!