Advertisement

quaternions

Started by May 01, 2000 09:15 PM
36 comments, last by a2k 24 years, 6 months ago
If i understand it correctly, a matrix is just 3 vectors (first 3 columns). So if you convert the quaternion into a rotation matrix, you can use the 3 vectors.

You''d probably want to use the Z vector (column 3), depending on how the object is oriented in object space.

anyone correct me if i''m wrong?

===============================================
"Tell brave deeds of war."
Then they recounted tales, -- "There were stern stands And bitter runs for glory."

Ah, I think there were braver deeds.
This is my signature. There are many like it, but this one is mine. My signature is my best friend. It is my life. I must master it as I must master my life. My signature, without me, is useless. Without my signature, I am useless.
a2k,

I''m not sure about this, but I don''t think you can get a direction vector from quaternions. I add a direction vector from to my polygon objects.

I add 3 points to my game objects that I turn with quaternions. These points initialy, these points are (0, 0, 1) (my forward vector), (0, 1, 0) (my top vector) and (1, 0, 0) (my side vector). When my game object rotates, these points get rotated as well. The forward vector is the direction vector and the top vector is the "roll" orientation.
Advertisement
thanks, tc. yeah, i like your method the most out of all that i''ve read. a question i have about this is, do you really have to have those vectors part of your geometry? you said something about not screwing up the display list, or something. i thought those direction vectors would have nothing to do with display lists because they''re not being rendered....

a2k
------------------General Equation, this is Private Function reporting for duty, sir!a2k
a2k,

WickedMystic said he was having some kind of problem. I can''t find any problems in my engine(as you can see in my demo).

Adding it to the list of points makes it easier for me becuase when I rotate the object, my extra points get rotated automatically. It is a lot more simpler than having other lists of points and vectors I have to rotate.

My rendering process is not hindered by these extra points at all becuase I keep a list of polygons. My polygons use indexes into the point array.
My engine works fine now thanks to the piece of code from TC that calculates the euler angles from the 3 ''extra'' vertices.

So it basicly comes down to this.

Define those 3 extra vertices:

the vector that points forward (also the roll-axis)
the vector that points up ( yaw axis)
the vector that point to the right (pitch axis)

now if you want to pitch the camera(or ship):

create a quaternion with the pitch axis, and the pitch angle;

convert the quaternion to a rotation matrix;

multiply the yaw and roll axis by this matrix(thus rotating them)

etc etc


you can get the euler angles for rotating the ship from object space to world space using the code TC posted some posts up. Those angles are calculated from the current forward and top vector of the object.

so basicly I have a set of (unrotated)vertices for a ship + 3 vectors that define the ships orientation in space. Those 3 vectors actually get updated each time the user uses keys or a joystick to steer the ship.

After you get this to work, you might wanna look into BSP trees to draw the faces of the ship from back to front. A Z-buffer does this also, but they get inaccurate at greater distance.

I just finished my BSP code tonite and it works so sweeeet

WickedMystic
okay, thanks a lot again, guys. i''ve been talkin all theoretically, cuz i haven''t had a chance to code much. got other projects for school to do, and i''m about to head into finals, so i''ll see how all this stuff works in a few weeks. thanks again, and thanks all for your time.

a2k
------------------General Equation, this is Private Function reporting for duty, sir!a2k
Advertisement
okay, guys, i''m bringing this thread up from the dead. i started coding my quaternions again, and this is what''s goin on. i''ve taken wicked mystic''s approach to this, and this is what i have:

tQuaternion qroll;
tQuaternion qpitch;
tQuaternion qyaw;

rollangle = (int) (rollangle + fAngularVelocityZ) % 360;
pitchangle = (int) (pitchangle + fAngularVelocityX)% 360;
yawangle = (int) (yawangle + fAngularVelocityY) % 360;

// convert axis/angles to quaternions
AxisAngleToQuat(&rollaxis, rollangle, &qroll);
AxisAngleToQuat(&pitchaxis, pitchangle, &qpitch);
AxisAngleToQuat(&yawaxis, yawangle, &qyaw);


float tempx = 0;
float tempy = 0;
float tempz = 0;

rollaxis.x = 0;
rollaxis.y = 0;
rollaxis.z = -1;

yawaxis.x = 0;
yawaxis.y = 1;
yawaxis.z = 0;

pitchaxis.x = 1;
pitchaxis.y = 0;
pitchaxis.z = 0;

QuatToMatrix(&qpitch, matrix);

tempx = yawaxis.x * matrix[0][0]
+ yawaxis.y * matrix[1][0]
+ yawaxis.z * matrix[2][0];

tempy = yawaxis.x * matrix[0][1]
+ yawaxis.y * matrix[1][1]
+ yawaxis.z * matrix[2][1];

tempz = yawaxis.x * matrix[0][2]
+ yawaxis.y * matrix[1][2]
+ yawaxis.z * matrix[2][2];

//yawaxis.x = 1 * tempx;
yawaxis.y = 1 * tempy;
yawaxis.z = 1 * tempz;

tempx = rollaxis.x * matrix[0][0]
+ rollaxis.y * matrix[1][0]
+ rollaxis.z * matrix[2][0];

tempy = rollaxis.x * matrix[0][1]
+ rollaxis.y * matrix[1][1]
+ rollaxis.z * matrix[2][1];

tempz = rollaxis.x * matrix[0][2]
+ rollaxis.y * matrix[1][2]
+ rollaxis.z * matrix[2][2];

//rollaxis.x = tempx;
rollaxis.y = tempy;
rollaxis.z = tempz;

/*
tempx = pitchaxis.x * matrix[0][0]
+ pitchaxis.y * matrix[1][0]
+ pitchaxis.z * matrix[2][0];

tempy = pitchaxis.x * matrix[0][1]
+ pitchaxis.y * matrix[1][1]
+ pitchaxis.z * matrix[2][1];

tempz = pitchaxis.x * matrix[0][2]
+ pitchaxis.y * matrix[1][2]
+ pitchaxis.z * matrix[2][2];

pitchaxis.x = 1 * tempx;
pitchaxis.y = 1 * tempy;
pitchaxis.z = 1 * tempz;
*/

QuatToMatrix(&qyaw, matrix);

tempx = pitchaxis.x * matrix[0][0]
+ pitchaxis.y * matrix[1][0]
+ pitchaxis.z * matrix[2][0];

tempy = pitchaxis.x * matrix[0][1]
+ pitchaxis.y * matrix[1][1]
+ pitchaxis.z * matrix[2][1];

tempz = pitchaxis.x * matrix[0][2]
+ pitchaxis.y * matrix[1][2]
+ pitchaxis.z * matrix[2][2];

pitchaxis.x = 1 * tempx;
//pitchaxis.y = 1 * tempy;
pitchaxis.z = 1 * tempz;

tempx = rollaxis.x * matrix[0][0]
+ rollaxis.y * matrix[1][0]
+ rollaxis.z * matrix[2][0];

tempy = rollaxis.x * matrix[0][1]
+ rollaxis.y * matrix[1][1]
+ rollaxis.z * matrix[2][1];

tempz = rollaxis.x * matrix[0][2]
+ rollaxis.y * matrix[1][2]
+ rollaxis.z * matrix[2][2];

rollaxis.x = tempx;
//rollaxis.y = tempy;
rollaxis.z = tempz;

/*
tempx = yawaxis.x * matrix[0][0]
+ yawaxis.y * matrix[1][0]
+ yawaxis.z * matrix[2][0];

tempy = yawaxis.x * matrix[0][1]
+ yawaxis.y * matrix[1][1]
+ yawaxis.z * matrix[2][1];

tempz = yawaxis.x * matrix[0][2]
+ yawaxis.y * matrix[1][2]
+ yawaxis.z * matrix[2][2];

yawaxis.x = 1 * tempx;
yawaxis.y = 1 * tempy;
yawaxis.z = 1 * tempz;
*/

QuatToMatrix(&qroll, matrix);

tempx = yawaxis.x * matrix[0][0]
+ yawaxis.y * matrix[1][0]
+ yawaxis.z * matrix[2][0];

tempy = yawaxis.x * matrix[0][1]
+ yawaxis.y * matrix[1][1]
+ yawaxis.z * matrix[2][1];

tempz = yawaxis.x * matrix[0][2]
+ yawaxis.y * matrix[1][2]
+ yawaxis.z * matrix[2][2];

yawaxis.x = 1 * tempx;
yawaxis.y = 1 * tempy;
//yawaxis.z = 1 * tempz;

tempx = pitchaxis.x * matrix[0][0]
+ pitchaxis.y * matrix[1][0]
+ pitchaxis.z * matrix[2][0];

tempy = pitchaxis.x * matrix[0][1]
+ pitchaxis.y * matrix[1][1]
+ pitchaxis.z * matrix[2][1];

tempz = pitchaxis.x * matrix[0][2]
+ pitchaxis.y * matrix[1][2]
+ pitchaxis.z * matrix[2][2];

pitchaxis.x = 1 * tempx;
pitchaxis.y = 1 * tempy;
//pitchaxis.z = 1 * tempz;

/*
tempx = rollaxis.x * matrix[0][0]
+ rollaxis.y * matrix[1][0]
+ rollaxis.z * matrix[2][0];

tempy = rollaxis.x * matrix[0][1]
+ rollaxis.y * matrix[1][1]
+ rollaxis.z * matrix[2][1];

tempz = rollaxis.x * matrix[0][2]
+ rollaxis.y * matrix[1][2]
+ rollaxis.z * matrix[2][2];

rollaxis.x = tempx;
rollaxis.y = tempy;
rollaxis.z = tempz;
*/

okay, now what it''s doing is if i rotate farther than like 45 degrees (roughly) in any direction, it starts going all funky on me. if i move them, they kind of look like they "spring" back into place, kinda cartoony, but if i really give it an extreme angle, eventually, the axes will point in random directions and start moving around on their own, wildly. some things that i think are causing this problem is the fact that i''m reassigning the axis vectors multiple times in one pass, which may actually "compound" the value. any other ideas? suggestions on how to fix this thing?

a2k
wouldn''t i have to multiply the 3 matrices together (or quaternions) to get the right rotation for all angles?

a2k
------------------General Equation, this is Private Function reporting for duty, sir!a2k

This topic is closed to new replies.

Advertisement