How to have a coordinate system that moves with the object

I am trying to rotate an object (airplane) on a 2D figure but I am having problems. The way I have my code now the roll works fine no matter the orientation of the object. Yaw and pitch rotate about a fixed axis. If I change the order of multiplication on the rotation matrix, I can get another direction to work as desired but the other 2 don't work as needed. I had tried putting in some code to change the order based on what parameter was changed and that works as far as the axii being "movable" but if I for example change the roll and then go to change the pitch, the object jumps back to some random orientation and from then on, the pitch will work. When I go to change the yaw, it will jump to some random position but then the yaw works correctly.
I did some searching and I think the answer is to use quaterions or some intrinsic rotation. I am lost on doing either of these but maybe someone can get me headed in the right direction. The big thing with quaterions is that my object matrix is m-by-3 but it needs to be m-by-4 in order to do any operations on it.
Below is a snippet of my code. I retyped from a computer without internet access so hopefully I didn't leave something out.
%define vertices (just a plate right now)
vertices = [-5 -5 0; -5 5 0; 5 5 0; 5 -5 0];
pitch = 15;
roll = 45;
yaw = 30;
vertices = rotateVertices(vertices, pitch, roll, yaw);
%run function to plot new vertices
%begin rotate function
function vertices = rotateVertices(vertices, pitch, roll, yaw)
pitch = pi/180*pitch;
roll = pi/180*pitch;
yaw = pi/180*pitch;
%rotation matrix
Rx = [1 0 0; 0 cos(pitch) -sin(pitch); 0 sin(pitch) cos(pitch)];
Ry = [cos(roll) 0 sin(roll); 0 1 0; -sin(roll) 0 cos(roll)];
Rz = [cos(yaw) -sin(yaw) 0; sin(yaw) cos(yaw) 0; 0 0 1];
Rt = Rz*Rx*Ry;
vertices = Rt*vertices;
%end function

Answers (1)

Mischa Kim
Mischa Kim on 20 Dec 2013
Edited: Mischa Kim on 20 Dec 2013
David, you are on the right track. See this MATLAB answer for reference.
Typically, for aircraft dynamics you would use a 3-2-1 (or z-y-x) rotation sequence for yaw-pitch-roll (your are using a y-x-z sequence in the example above). Also, careful with the correct location of negative signs in the individual rotation matrices.

2 Comments

Thanks Mischa. Doing it in the order that you suggested makes the pitch work correctly but the other 2 axes don't work correctly. I am also confused as to what you meant by the location of my negative signs. Do you just mean that it will rotate in a different direction based on those signs or is this what is messing up the axes?
Your other post is interesting and I think I need to get my rotation matrix converted to a quaterion. I imagine I would somehow have to convert my vertices matrix to a 4 element matrix as well though.
Please check the other post I am referring to. It shows the rotation matrices with the correct negative signs and the overall rotation matrix I believe you need for your application (z-y-x).
If you are only rotating vectors (or vertices, like in your case) the rotation matrix is the way to go. If on the other hand you would like to visualize the rotation of an object in a VR world you need to convert the rotation matrix into the corresponding rotation vector and angle (called Euler axis and angle).
The other thing I just noticed in your code is the conversion to rad where you use the pitch for all three angles.

Sign in to comment.

Categories

Asked:

on 19 Dec 2013

Edited:

on 20 Dec 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!