{SOLVED} 3D Rotations with Euler Angles and Matrices

X

Xenon

Guest
SOLVED: http://host-a.net/u/XenonFiles/RotationVisualizer.zip
  • vocab; Vertex (Any 3d point in space (x,y,z)) Vector (3d line representing angles, the line starts at (0,0,0) and ends 1 pixel distance in the direction the vector is representing, so if we are representing what is up in the game, we would use vector(0,0,1) because z is our up and down axis, and its positive one because negative one would be the bottom vector.)
Hey guys, I've been trying and trying and trying to get a pure math formula for 3d orientation based rotations. What i mean by this is, one without using super complicated scripts, dlls, and for now quaternions (that math just isn't easy to understand).
  • Also to clarify what i mean by orientation based rotations is that i am following the law that states that any 3d rotation can be performed only using 3 rotations (most notably Z -> X -> Z).
  • As you see we have 12 options to do so
Without considering the possibility of using two different conventions for the definition of the rotation axes (intrinsic or extrinsic), there exist twelve possible sequences of rotation axes, divided in two groups:
  • Proper Euler angles (z-x-z, x-y-x, y-z-y, z-y-z, x-z-x, y-x-y)
  • Tait–Bryan angles (x-y-z, y-z-x, z-x-y, x-z-y, z-y-x, y-x-z).
  • But don't worry guys i did the hard part for you! I have written 3 separate formulas to rotate a vertex(x,y,z) in 3d space. The formulas simply create two 3x3 matrices, one holding the rotation formula (There is X, Y, and Z) and one matrix is holding the 3d position of a vertex.
  • After we do that all we have to do is multiply them! It's truly simple what we're doing here, i am having more of a conceptual problem with the order of rotations and the comparison of angles.
  • Anyways these codes will only rotate a vector correctly if the vector itself is in the right plane, here is where it gets confusing.
From the top, we have created a 3d camera, initialized 3d mode, placed our camera at 32,32,32 looking at 0,0,0 with the up vector of course being Z+ (0,0,1). Then we draw our x,y, and z axes with 3 lines (the triad below in frame 1) After this we create another object that draws a line in 3d space from world origin (0,0,0) to x,y,z.
Pause
I will use visual aids because this isn't easy to understand or i think we'd have a definitive formula for 3d rotations already...

These are actual images of the program i will be sharing with you that i wrote to visualize 3d rotations using lines. So we have a vertex at (0,8,0) this is our starting point, we start here because we should be able to rotate Y, and then Z and then X, that is the whole idea of this and we are so close. I can do each rotation by itself but the moment i add 2 rotations it loses its origin,this happens because the vector isn't on the correct plane, i will explain below.

Anyways in the example this vertex is the x,y,z position of the line, remember we are drawing a line from origin(0,0,0) to position(x,y,z), this is the line above in frame 2 that the arrow is pointing at, the other line with a blue and green dot at the top and bottom is our invaluable UP vector. I'l get into UP in a second, but stay with me, the 3rd frame represents both of these lines being rotated 90 degrees on the Y axis (radians of course ;)) , the fourth frame represents both of these lines being rotated on the Z axis, and the fifth frame is them being rotated on the X axis. These are all separate instances, not a y rotation then a z then a x, each image is one rotation from frame2 x, y, or z.

So we got the idea of rotation on one axis, but why can't we just rotate by the z then y then x or how ever we want? The planes..

I googled 3d plane images, and got airplanes. but if the image provided doesn't make sense google what the 3d planes are, they're 2d grids one running on the x,y axes one running on the y,z axes and one running on the x,z axes.

So what are these planes have to do with rotation? Simple, to perform more than one rotation at a time we have to make sure the vector lies in the correct plane; for instance, all Z rotations must be performed while the vector(x,y,z) is lying on the YZ plane, in other words meaning that X = 0. If it is not the rotation will not be performed around the origin but it will be performed around the entire Z axis, simply put your vector will do a barrel roll around the Z axis instead of a spherical rotation around the origin. To recap
X rotations must be performed on vectors lying in the XZ plane or if Y = 0
Y rotations must be performed on vectors lying in the XY plane or if Z = 0
Z rotations must be performed on vectors lying in the YZ plane or if X = 0
So to perform three separate rotations we must first rotate the vector(0,1,0) on the Y axis, because 0,1,0 lies on the YZ plane, then we perform a Z rotation on the YZ plane, then we perform our final X rotation on the XZ plane.

That is just our theory, and technically it holds true, but here is our code.
SYNTAX: script_z(x,y,z,angle,axis)
Code:
Angle = degtorad(argument3);//angle of z rotation
if(argument4 = 0){
//Formula Y
a[0] = cos(angle);
a[1] = 0;
a[2] = sin(angle);
a[3] = 0;
a[4] = 1;
a[5] = 0;
a[6] = -sin(angle);
a[7] = 0;
a[8] = cos(angle);}
if(argument7 = 1){
//Formula Z
a[0] = cos(angle);
a[1] = -sin(angle);
a[2] = 0;
a[3] = sin(angle);
a[4] = cos(angle);
a[5] = 0;
a[6] = 0;
a[7] = 0;
a[8] = 1;}
if(argument7 = 2){
//Formula X
a[0] = 1;
a[1] = 0;
a[2] = 0;
a[3] = 0;
a[4] = cos(angle);
a[5] = -sin(angle);
a[6] = 0;
a[7] = sin(angle);
a[8] = cos(angle);}

b[0] = argument0; b[1] = argument0; b[2] = argument0
b[3] = argument1; b[4] = argument1; b[5] = argument1
b[6] = argument2; b[7] = argument2; b[8] = argument2

c[0] = (a[0] * b[0]) + (a[1] * b[3]) + (a[2] * b[6]);
c[1] = (a[0] * b[1]) + (a[1] * b[4]) + (a[2] * b[7]);
c[2] = (a[0] * b[2]) + (a[1] * b[5]) + (a[2] * b[8]);
c[3] = (a[3] * b[0]) + (a[4] * b[3]) + (a[5] * b[6]);
c[4] = (a[3] * b[1]) + (a[4] * b[4]) + (a[5] * b[7]);
c[5] = (a[3] * b[2]) + (a[4] * b[5]) + (a[5] * b[8]);
c[6] = (a[6] * b[0]) + (a[7] * b[3]) + (a[8] * b[6]);
c[7] = (a[6] * b[1]) + (a[7] * b[4]) + (a[8] * b[7]);
c[8] = (a[6] * b[2]) + (a[7] * b[5]) + (a[8] * b[8]);

global.xx = c[0];
global.yy = c[3];
global.zz = c[6]
The formulas at the beginning represent the rotation matrices. Please wikipedia them and euler angles!

Now if you get a 3d vector at 0,1,0 you can plug the code in and change the last argument to change what axis the vector rotates on. Its a pretty close version of what we're trying to here.
It's 2:30 am, so i will come back and focus on performing the actual rotation but for now you know what i know, and you have the formulas i have, can you make 3d rotation a possibility? Any collaboration, comments, concerns, ideas or corrections please post them but for now my question being is, how do i combine my three rotation formulas into one?! :bash:

edit: the 3d rotation visualizer is not ready for public sharing but will be posted ASAP
edit2: It seems once laying in bed i have epiphanies... Anyways a very simple way to visualize a 3d rotation is, all 3d rotations are basically performed in 2d, this the reason being the vector must lay on a certain plane. That is a X rotation is performed as if we are in 2d looking at XZ, instead of the usual XY, thus our position(x,Y,z) - y is never affected in a X rotation, so a spherical rotation will perform on the XZ plane so if Y isn't 0, we will barrel roll around the y axis while the rotation performs.
 
Last edited by a moderator:

FoxyOfJungle

Kazan Games
Hi Xenon, the link is down, I was wondering if you still have the file or project, I will be very grateful.
That user's account doesn't even exist on the forum anymore, maybe that will never happen unless he creates another one and comes back to that topic again. ¯\_(ツ)_/¯
 
Top