3D Theory - Forum Question

By: HJL (hjl2) - 2007-10-13 05:27
Hi 
 
I'm neither a programmer not a mathematician (although I do have a science background) and I need basic help with 3D geometry. I've read all of the excellent Martin Baker site but still find the maths a bit much. 
 
In my spare time I program in C++ to write simple games for my smartphone. I decided to try and do something with wireframe graphics. I am struggling to control the rotation of objects in the way that I want. 
 
The space I am modelling uses 3D Cartesian coordinates and vectors, with the positive directions being x=right, y=forward, z=up. Each entity in space is a list of triangles, and each triangle has an origin and two vectors (variables named “edge1” and “edge2”) to find the vertices; from there I draw lines between the three vertices. The starting point, the origin, is variable "position" which is a 3D coordinate. 
 
Each entity also has an "angle", made up of x,y and z parts. All the vectors (from origin to vertex) within the entity are rotated according to this angle. I modified some code I found on the web to do this: 
 
//START OF CODE 
 
double sx=sin(angle.x), sy=sin(angle.y), sz=sin(angle.z); 
double cx=cos(angle.x), cy=cos(angle.y), cz=cos(angle.z); 
 
//create a 3x3 matrix “mat” with 1’s down the diagonal and the rest 0’s 
identity(); 
 
//note there is a fourth column in the matrix which stores the “origin” 
 
//rotate each triangle using rotate formula matrix (all triangles rotated to same angle) 
//to convert the end of each edge into a vertex position 
mat[0][0]=cy*cz+sy*sx*sz; mat[1][0]=-cy*sz+cz*sy*sx; mat[2][0]=cx*sy; 
mat[0][1]=cx*sz; mat[1][1]=cx*cz; mat[2][1]=-sx; 
mat[0][2]=-cz*sy+cy*sx*sz; mat[1][2]=sy*sz+cy*cz*sx; mat[2][2]=cy*cx; 
 
for (int loop=0; loop<num_triangs; loop++) 

//enter the triangle position into the matrix 
//(for current test-run all triangles have same position) 
triang = &triang_list[loop]; 
mat[3][0]=triang->position.x;  
mat[3][1]=triang->position.y;  
mat[3][2]=triang->position.z; 
 
//instead of looping through 3 vertices 
//just reiterate each corner, starting with the origin 
triang->origin.x = mat[3][0];//(as if vector is all zeroes) 
triang->origin.y = mat[3][1]; 
triang->origin.z = mat[3][2]; 
 
//now vertex1 
triang->vertex1.x = triang->edge1.x * mat[0][0] + triang->edge1.y * mat[1][0] + triang->edge1.z * mat[2][0] + mat[3][0]; 
triang->vertex1.y = triang->edge1.x * mat[0][1] + triang->edge1.y * mat[1][1] + triang->edge1.z * mat[2][1] + mat[3][1]; 
triang->vertex1.z = triang->edge1.x * mat[0][2] + triang->edge1.y * mat[1][2] + triang->edge1.z * mat[2][2] + mat[3][2]; 
 
//now vertex2 
triang->vertex2.x = triang->edge2.x * mat[0][0] + triang->edge2.y * mat[1][0] + triang->edge2.z * mat[2][0] + mat[3][0]; 
triang->vertex2.y = triang->edge2.x * mat[0][1] + triang->edge2.y * mat[1][1] + triang->edge2.z * mat[2][1] + mat[3][1]; 
triang->vertex2.z = triang->edge2.x * mat[0][2] + triang->edge2.y * mat[1][2] + triang->edge2.z * mat[2][2] + mat[3][2]; 
 
//and flag the triangle as having been rotated, 
triang->rotated = true; 
//to allow the main routine to call getX,Y and Z 

 
//END OF CODE 
 
So far, so good? Actually no because the x,y and z angles behave rather strangely. I want to be able to rotate objects using pitch, yaw and roll, and hoped that the x,y, and z would correspond to these, but they only work at the base position of angle(0,0,0) [equivalent to orientation along unit vector (0,1,0)]. They appear to behave as follows: 
 
Angle.z is consistent whatever the heading. It always acts as a yaw, i.e. the orientation swings left and right relative to the present orientation. 
 
Angle.x rotates about the x axis at the start (0,0,0) in a pitching motion. However if I yaw 90 deg to the left or right, angle.x becomes a roll. So it is still rotating around the x axis but I want it to now rotate around the y axis. Even more confusing, if I first pitch down (to the “south pole”) using angle.y and then change angle.x, this x rotation becomes a yaw about the y axis. 
 
Angle.y does something similar to angle.x. It starts as a roll about the y axis. If I dip down to the south pole it becomes a yaw, rotating about the y axis still. If I start from (0,0,0), yaw to the right, then angle.y continues to move around the y axis, now acting as a pitch.  
 
In between the 90 deg directions, various intermediate things occur. 
 
So, can anyone help?? As I said I’ve tried to understand the problem by reading on the web but I am stuck. My thoughts are that maybe I shouldn’t be using this angle system in the first place because it is so dependent on where your object is oriented and how it got there. (Something to do with Euler angles?) But I don’t know what to use instead of the code above to rotate the vectors. 
 
Maybe there is a simple way of converting pitch, yaw and roll controls into the angles that I am using? 
 
Maybe, when obeying key commands, I need to convert the current orientation into a vector, rotate about that vector according to the instruction, then convert back into the angle format? But I don’t know how to do the rotation about the vector /or/ the conversion back to the angle system. 
 
I would be very appreciative of any advice. 
 
Thanks, 
Lewie 



 


metadata block
see also:
Correspondence about this page

This site may have errors. Don't use for critical systems.

Copyright (c) 1998-2023 Martin John Baker - All rights reserved - privacy policy.