HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Moving in 3D Space

01-23-2007, 11:44 PM#1
wyrmlord
I'm attempting to make a game in C++ using OpenGL where some sort of ship is moved through space. I'm trying to get 3D movement, so when I would press up the ship would tilt upward and keep heading at that particular angle. Everything appears to work fine, but after a second or so of holding down an arrow, the angles seem to mess up. I figure it's something wrong with the math I'm using, but I can't be sure.

Here's the sections of code that I think are important:

All the classes being used.
Hidden information:
#define piover180 0.0174532925f

class Unit
{
protected:
float x, y, z, speed, acceleration, max_speed, life;
bool alive;
public:
virtual void Create(float xpos, float ypos, float zpos){x=xpos;y=ypos;z=zpos;alive=true;life=100;}
virtual inline void SetSpeed(float s){ speed = s; }
virtual inline void SetAcceleration(float a){ acceleration = a; }
virtual inline void SetMaxSpeed(float s){ max_speed = s; }
virtual void Move();
virtual void Destroy(){
x = 0;
y = 0;
z = 0;
speed = 0;
acceleration = 0;
max_speed = 0;
alive = false;
}
virtual inline float GetX(){ return x; }
virtual inline float GetY(){ return y; }
virtual inline float GetZ(){ return z; }
virtual inline float SetX(float xpos){ x = xpos; }
virtual inline float SetY(float ypos){ y = ypos; }
virtual inline float SetZ(float zpos){ z = zpos; }
virtual inline bool IsAlive(){ return alive; }
virtual inline bool SetAlive(bool living){ alive = living; }
virtual inline float GetLife(){ return life; }
};

class Ship : public Unit
{
public:
float angle1, angle2;
void Move();
};

void Ship::Move()
{
x += speed * sin(angle1*piover180) * cos(angle2);
z += speed * cos(angle1*piover180) * cos(angle2);
y += speed * sin(angle2);

if( speed < max_speed )
{
speed += acceleration;
}
}


Function to draw the scene. 'ship' is a variable of type 'Ship'.
Hidden information:
void DrawScene() //Called when the scene will be drawn
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

glRotatef(ship.angle2, 1.0f, 0.0f, 0.0f);
glRotatef(ship.angle1, 0.0f, 1.0f, 0.0f);

glTranslatef(0, 0, -200);

glTranslatef(ship.GetX(), ship.GetY(), ship.GetZ());

glBegin(GL_TRIANGLES);
glVertex3f(5, -5, 0);
glVertex3f(-5, -5, 0);
glVertex3f(0, 5, 0);
glEnd();
}

Function to handle Keys, and adjust angles of movement. Adjustment is set at 0.1 currently. 'keys' is a bool array that has values set to true when a key is pressed (handled in the window procedure)
Hidden information:
void KeyControls() //Called before a call to the DrawScene function
{
if( keys[VK_UP] && ship.angle2 > -45 )
{
ship.angle2 -= adjustment;
}
if( keys[VK_DOWN] && ship.angle2 < 45)
{
ship.angle2 += adjustment;
}
if( keys[VK_LEFT] )
{
ship.angle1 -= adjustment*2;
}
if( keys[VK_RIGHT] )
{
ship.angle1 += adjustment*2;
}
}


And lastly, the main function (though I'm fairly sure the problem isn't with this).
Hidden information:
int main(int argc, char* argv)
{
MSG msg;
BOOL done = FALSE;
CreateGLWindow("Asteroid Field", 640, 400, 16, false);
//^That function is in another file and properly working

ship.Create(0, 0, 100);
ship.SetSpeed(.001);
ship.SetAcceleration(.0001);
ship.SetMaxSpeed(.005);

while(!done)
{
if( PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) )
{
if( msg.message == WM_QUIT )
{
done = TRUE;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else
{
KeyControls();
ship.Move();
DrawScene();
SwapBuffers(hDC);
}
}
DestroyGLWindow();
//^That function is in another file and properly working
return msg.wParam;
}


If anyone has any ideas on what I did wrong, I would greatly appreciate some help. I'll also upload all the source files with the exe in a second.
Attached Files
File type: zipAsteroids.zip (11.8 KB)
01-24-2007, 01:10 AM#2
PipeDream
Go read about quaternions, linear algebra and euler angles.
01-24-2007, 01:30 AM#3
wyrmlord
Any specific sites or places I should look at? I'm clueless about what quaternions and eular angles are. I'm also fairly sure I don't know too much about linear algebra either. (I'm currently taking Algebra 1, and have a fairly good grasp of it)
01-24-2007, 03:48 AM#4
Guesst
This is my favorite site about quaternions: http://www.gamedev.net/reference/art...rticle1691.asp
02-04-2007, 09:15 AM#5
Jacek
nonono you don't need quaternions >_> they are useful but you don't need them really...

I suggest that you store the ship's rotation in matrices... they are good enough for full 3D rotations and don't suffer from problems Euler angles do (google for gimbal lock - probably this happens to you)