PDA

View Full Version : Rotational mathematics... rotating cartesian coordinates



ax_deimos
11-20-2001, 06:22 AM
My program rotates 3D (x,y,z) cartesian coordinate systems. I'm trying to find an algorithm that can rotate a shape (in this case a wireframe sphere) which was originally placed in one coordinate system (lets say coordinate systam 1) so that it will be oriented along the previous coordinate system when placed inside a new coordinate system (called coordinate system 2)


example of coordinate systems:

(Both coordinate systems are RHS unit orthogonal coordinate systems. RHS means right hand side (right thumb points along x, right index finger points along y, right middle finger points along z)

coordinate system 1:
x1 = [(sqrt(3)/2)i, (1/2)j, 0k]
y1 = [(1/2)i, -(sqrt(3)/2)j, 0k]
z1 = [0i, 0j, 1k]


coordinate system 2:
x2 = [1i, 0j, 0k]
y2 = [0i, (sqrt(2)/2)j, (sqrt(2)/2)k]
z2 = [0i, -(sqrt(2)/2)j, (sqrt(2)/2)k]
*Note that sqrt means square root
*Note that these vectors are defined as i,j,k

I've got a partial algorithm which works half way. (It rotates my shape about the x2 axis)

glPushMatrix();
{
//Defines a vector
struct ijk {
// double vect[3];
double i;
double j;
double k;
};

//coordinate system 1
ijk x1 = { (sqrt(3)/2), (1/2), 0};
ijk y1 = {(1/2), -(sqrt(3)/2), 0};
ijk z1 = {0, 0, 1};

//coordinate system 2:
ijk x2 = {1, 0, 0};
ijk y2 = {0, (sqrt(2)/2), (sqrt(2)/2)};
ijk z2 = {0, -(sqrt(2)/2), (sqrt(2)/2)};

//A normal vector generated by crossing
//two equivalent coordinates
ijk normal = CrossProduct(x1, x2);

//Remember that a, b are unit vectors
double cosine_of_a_dot_b = DotProduct(x1,x2);
int angle = ArcCos(cosine_of_a_dot_b);

glRotated(angle,
normal.i, normal.j, normal.k);
glutWireSphere(22, 8,8);
}
glPopMatrix()
//END OF CODE


Try changing the normal from
CrossProduct(x1,x2)
to
CrossProduct(y1,y2)
or
CrossProduct(z1,z2)

ALSO If anyone can come up with a good arccos function for vectors for returning the angle from 0 to 360 degrees CCW I would appreciate that

email me at ax_deimos@yahoo.com

ax_deimos
11-20-2001, 06:25 AM
minor thing, I suggest you try to create a function which rotates the second coordinate axis to see how this works in dynamic systems.

djbuzzkill
11-21-2001, 12:16 AM
Are you just trying to rotate an object around an axis? I think you are making things harder than they really are. if you want to rotate you object around an axis all you need to do is use glRotatef with the modelview matrix and then draw your shape:

float rot; // the rotation value
.
.
.
glMatrixMode( GL_MODELVIEW );
glRotatef( rot, 1.0f, 0.0f, 0.0f );

// then draw your shape.



Originally posted by ax_deimos:
My program rotates 3D (x,y,z) cartesian coordinate systems. I'm trying to find an algorithm that can rotate a shape (in this case a wireframe sphere) which was originally placed in one coordinate system (lets say coordinate systam 1) so that it will be oriented along the previous coordinate system when placed inside a new coordinate system (called coordinate system 2)


example of coordinate systems:

(Both coordinate systems are RHS unit orthogonal coordinate systems. RHS means right hand side (right thumb points along x, right index finger points along y, right middle finger points along z)

coordinate system 1:
x1 = [(sqrt(3)/2)i, (1/2)j, 0k]
y1 = [(1/2)i, -(sqrt(3)/2)j, 0k]
z1 = [0i, 0j, 1k]


coordinate system 2:
x2 = [1i, 0j, 0k]
y2 = [0i, (sqrt(2)/2)j, (sqrt(2)/2)k]
z2 = [0i, -(sqrt(2)/2)j, (sqrt(2)/2)k]
*Note that sqrt means square root
*Note that these vectors are defined as i,j,k

I've got a partial algorithm which works half way. (It rotates my shape about the x2 axis)

glPushMatrix();
{
//Defines a vector
struct ijk {
// double vect[3];
double i;
double j;
double k;
};

//coordinate system 1
ijk x1 = { (sqrt(3)/2), (1/2), 0};
ijk y1 = {(1/2), -(sqrt(3)/2), 0};
ijk z1 = {0, 0, 1};

//coordinate system 2:
ijk x2 = {1, 0, 0};
ijk y2 = {0, (sqrt(2)/2), (sqrt(2)/2)};
ijk z2 = {0, -(sqrt(2)/2), (sqrt(2)/2)};

//A normal vector generated by crossing
//two equivalent coordinates
ijk normal = CrossProduct(x1, x2);

//Remember that a, b are unit vectors
double cosine_of_a_dot_b = DotProduct(x1,x2);
int angle = ArcCos(cosine_of_a_dot_b);

glRotated(angle,
normal.i, normal.j, normal.k);
glutWireSphere(22, 8,8);
}
glPopMatrix()
//END OF CODE


Try changing the normal from
CrossProduct(x1,x2)
to
CrossProduct(y1,y2)
or
CrossProduct(z1,z2)

ALSO If anyone can come up with a good arccos function for vectors for returning the angle from 0 to 360 degrees CCW I would appreciate that

email me at ax_deimos@yahoo.com

ax_deimos
11-21-2001, 06:50 AM
Solved the problem, you don't need to bother

I created a function which generates a matrix that transforms orthogonal unit coordinate system #1 into unit orthogonal coordinate system#2. Afterwards, all I do is pop this matrix on the stack and my coordinates are rotated properly.