Advertisement

Quaternions in Flight Sims

Started by June 22, 2003 12:09 AM
10 comments, last by FlyingDemon 21 years, 8 months ago
Can anyone tell me the order in which I?m supposed to do quaternion stuff (like: CreateFromAxisAngle, creatematrix, multiply, .ect) in a flight sim or give me some good references. please. .I just cant get them to work correctly. By flight sim I mean a normal airplane like a jet with two wings and an engine, nothing futuristic. thx.
Advertisement
what.. are u teasing me?

please - someone - anyone?
:/
please someone help i''ve been trying to figure this out but i cant find anything.

iv been looking for over a few days now.
Say something -- anything..i really need to know.
PLEASE -- PLEASE -- PLEASE -- PLEASE -- PLEASE -- PLEASE
PLEASE -- PLEASE -- PLEASE -- PLEASE -- PLEASE -- PLEASE
PLEASE -- PLEASE -- PLEASE -- PLEASE -- PLEASE -- PLEASE
PLEASE -- PLEASE -- PLEASE -- PLEASE -- PLEASE -- PLEASE
PLEASE -- PLEASE -- PLEASE -- PLEASE -- PLEASE -- PLEASE
PLEASE -- PLEASE -- PLEASE -- PLEASE -- PLEASE -- PLEASE
PLEASE -- PLEASE -- PLEASE -- PLEASE -- PLEASE -- PLEASE
PLEASE -- PLEASE -- PLEASE -- PLEASE -- PLEASE -- PLEASE
PLEASE -- PLEASE -- PLEASE -- PLEASE -- PLEASE -- PLEASE
PLEASE -- PLEASE -- PLEASE -- PLEASE -- PLEASE -- PLEASE
PLEASE -- PLEASE -- PLEASE -- PLEASE -- PLEASE -- PLEASE
PLEASE -- PLEASE -- PLEASE -- PLEASE -- PLEASE -- PLEASE
Advertisement
What are you talking about? What are you trying to accomplish? There is no answer to your question the way it stands now, hence no responses.
Search for the subject in google and read some of the articles on the subject like this which are pretty good.

Best of luck!
---CyberbrineDreamsSuspected implementation of the Windows idle loop: void idle_loop() { *((char*)rand()) = 0; }
well...i made a previous post about the same thing with a more detailed explination but no-one would reply..so i created this one.
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

here the other post....

[Airplane Orientation with Quaternions]

Im having trouble getting my aiplane to rotate correctly.
So far each of the airplanes axis have there own quaternion.

My Steps:
¬Adjust the rotation angles.
¬Adjust each quaternion every frame using axis to angle conversion.
¬Normalize each quaternion.
¬Now in the render code....right before i render the ship i create a matrix from the quaternions and use glMultMatrixf for each one..

The problem:
The orientation is messed up.. For some reason it messes up for the first two quaternions i do a glMultMatrixf for, but the last one always works even using the messed up orientation the the first two quaternions created. THe first two quaternions seem to rotate around world space not object space.

Please help me fix this or tell me if im doing this completly wrong. This is my first time using quaternions.

Here's some code:

in main.cpp:
//declare quaternions, angles and matrixquat qx;          //quat for x axis(attitude)quat qy;          //quat for y axis(yaw)quat qz;          //quat for z axis(roll)float y,a,r;      //angles (yaw, attitude, rol)float matrix[16]; // 4x4 OpenGL matrix//render codevoid render(){***     glLoadIdentity();							     qy.Quat2Matrix(matrix);		     glMultMatrixf(matrix);     qx.Quat2Matrix(matrix);     glMultMatrixf(matrix);     qz.Quat2Matrix(matrix);     glMultMatrixf(matrix);     ship.Draw();          //draws ship***}//stuff calculated each frame***// update each angle(y,a,r)******     // create quaternions from angles using axis angle conversion     qx.CreateFromAxisAngle(1,0,0,a);     q1.Normalize();     qy.CreateFromAxisAngle(0,1,0,y);     q2.Normalize();     qz.CreateFromAxisAngle(0,0,1,r);     q3.Normalize();****



Quaternion Class:
#ifndef __QUAT_H__#define __QUAT_H__#include <math.h>#include <stdio.h>const float PIOVER180 = 0.01745329251994;const float PI		  = 3.14159265358979;class quat {	public:	float w,x,y,z;	quat(){};	~quat(){};		void Normalize(){		float magnitude = sqrt(w*w+x*x+y*y+z*z);		w = w/magnitude;		x = x/magnitude;		y = y/magnitude;		z = z/magnitude;	}	void CreateFromAxisAngle(float X, float Y, float Z, float degree) 	{ 		float angle = float((degree / 180.0f) * PI);		float result = (float)sin( angle / 2.0f );				w = (float)cos( angle / 2.0f );		x = float(X * result);		y = float(Y * result);		z = float(Z * result);	}		void Quat2Matrix(float *pMatrix)	{				// First row		pMatrix[ 0] = 1.0f - 2.0f * ( y * y + z * z ); 		pMatrix[ 1] = 2.0f * (x * y + z * w);		pMatrix[ 2] = 2.0f * (x * z - y * w);		pMatrix[ 3] = 0.0f;  		// Second row		pMatrix[ 4] = 2.0f * ( x * y - z * w );  		pMatrix[ 5] = 1.0f - 2.0f * ( x * x + z * z ); 		pMatrix[ 6] = 2.0f * (z * y + x * w );  		pMatrix[ 7] = 0.0f;  		// Third row		pMatrix[ 8] = 2.0f * ( x * z + y * w );		pMatrix[ 9] = 2.0f * ( y * z - x * w );		pMatrix[10] = 1.0f - 2.0f * ( x * x + y * y );  		pMatrix[11] = 0.0f;  		// Fourth row		pMatrix[12] = 0;  		pMatrix[13] = 0;  		pMatrix[14] = 0;  		pMatrix[15] = 1.0f;		// Now pMatrix[] is a 4x4 homogeneous matrix that can be applied to an OpenGL Matrix	}	void MultiplyWithQuat(quat q){		float wt,xt,yt,zt;		float w2,x2,y2,z2;		w2=q.w;		x2=q.x;		y2=q.y;		z2=q.z;		/*if(w2==0) w2=1;		if(x2==0) x2=1;		if(y2==0) y2=1;		if(z2==0) z2=1;*/		wt = (w*w2)-(x*x2)-(y*y2)-(z*z2);		xt = (w*x2)+(x*w2)+(y*z2)-(z*y2);		yt = (w*y2)-(x*z2)+(y*w2)+(z*x2);		zt = (w*z2)+(x*y2)-(y*x2)+(z*w2);		w=wt;		x=xt;		y=yt;		z=zt;	}	//debug	 	void Outq(){		FILE *file = fopen("quat.txt","wt");		fprintf(file,"%f\n%f\n%f\n%f",w,x,y,z);		fclose(file);			}	void Outm(float m[16]){		FILE *file = fopen("matrix.txt","wt");		fprintf(file,"%f , %f , %f , %f\n", m[0], m[1], m[2], m[3]);		fprintf(file,"%f , %f , %f , %f\n", m[4], m[5], m[6], m[7]);		fprintf(file,"%f , %f , %f , %f\n", m[8], m[9], m[10], m[11]);		fprintf(file,"%f , %f , %f , %f\n", m[12], m[13], m[14], m[15]);	}};#endif




[edited by - FlyingDemon on June 22, 2003 7:49:06 PM]

[edited by - FlyingDemon on June 22, 2003 8:22:56 PM]
Heres some that I use:
GLINLINE GLvoid GLQSetEular(GLQUATERNION *retQuat, GLfloat Yaw, GLfloat Pitch, GLfloat Roll){    #ifdef __cplusplus        GLfloat CosYaw   = cosf(Yaw / 2.0f);	GLfloat SinYaw   = sinf(Yaw / 2.0f);	GLfloat CosPitch = cosf(Pitch / 2.0f);	GLfloat SinPitch = sinf(Pitch / 2.0f);	GLfloat CosRoll  = cosf(Roll / 2.0f);	GLfloat SinRoll  = sinf(Roll / 2.0f);    #else	GLfloat CosYaw   = (GLfloat)cos(Yaw / 2.0f);	GLfloat SinYaw   = (GLfloat)sin(Yaw / 2.0f);	GLfloat CosPitch = (GLfloat)cos(Pitch / 2.0f);	GLfloat SinPitch = (GLfloat)sin(Pitch / 2.0f);	GLfloat CosRoll  = (GLfloat)cos(Roll / 2.0f);	GLfloat SinRoll  = (GLfloat)sin(Roll / 2.0f);    #endif    retQuat->X = CosRoll * SinPitch * CosYaw + SinRoll * CosPitch * SinYaw;    retQuat->Y = CosRoll * CosPitch * SinYaw - SinRoll * SinPitch * CosYaw;   retQuat->Z = SinRoll * CosPitch * CosYaw - CosRoll * SinPitch * SinYaw;    retQuat->W = CosRoll * CosPitch * CosYaw + SinRoll * CosPitch * SinYaw;}


Or:

     GLINLINE GLvoid GLQCreateFromEularAngles(GLQUATERNION *retQuat, GLfloat X, GLfloat Y, GLfloat Z){        GLfloat Roll  = DegreesToRadians(X);    GLfloat Pitch = DegreesToRadians(Y);    GLfloat Yaw   = DegreesToRadians(Z);    GLfloat CYaw       = cos(0.5f * Yaw);    GLfloat CPitch     = cos(0.5f * Pitch);    GLfloat CRoll      = cos(0.5f * Roll);    GLfloat SYaw       = sin(0.5f * Yaw);    GLfloat SPitch     = sin(0.5f * Pitch);    GLfloat SRoll      = sin(0.5f * Roll);    GLfloat CYawCPitch = CYaw * CPitch;    GLfloat SYawCPitch = SYaw * CPitch;    GLfloat CYawSPitch = CYaw * SPitch;    GLfloat SYawSPitch = SYaw * SPitch;    retQuat->X = (GLfloat)(CYawCPitch * SRoll - SYawSPitch * CRoll);    retQuat->Y = (GLfloat)(CYawSPitch * CRoll + SYawSPitch * SRoll);    retQuat->Z = (GLfloat)(SYawCPitch * CRoll - CYawSPitch * SRoll);    retQuat->W = (GLfloat)(CYawCPitch * CRoll + SYawSPitch * SRoll);}


One of them should do the trick.

-UltimaX-

"You wished for a white christmas... Now go shovel your wishes!"

[edited by - UltimaX on June 22, 2003 8:10:25 PM]

This topic is closed to new replies.

Advertisement