Followers 0

# rendering space ships

## 53 posts in this topic

I fixed the off center problem. but I still cant get the ships to follow their nose, it just moves up and down regardless of where it is pointing at.

0

##### Share on other sites

Alright, this will be a bit more involved.  I've worked on a couple of different ways to do this over the years but sometimes these things can become overly complicated when you start running into oddities that require extra code to deal with all the weird errors that pop up when angles switch from 0 to 360 or when things switch from positive to negative so to start off with let's try just roughing out the proper angle for a given direction.

So to start off, let's just define an angle that is attached to a particular direction, this will cause the ship to snap instantly to the proper rotation but we can work on how to make it a smooth transition afterwards.

For UP,   the angle is 0.0

For LEFT,   the angle is 90.0

For DOWN, the angle is 180.0

For RIGHT,  the angle is 270.0

//=================================================================

ship_down_two() { ship_01_POSITION_Y -= 0.1;

ship_01_ROTATION   = 180.0; }

//=================================================================

0

##### Share on other sites

here is my code for my previous rotation routine

void

update(int value)
{
angle+=0.1f;

if(angle > 360.0f)
{
angle-=360;
}
glutPostRedisplay();
glutTimerFunc(25,update,0);
}

0

##### Share on other sites

I was helping someone else on another thread to get a model of his rotating towards where he touches the screen with his finger.  Since I already had your code pasted into a simple framework I decided to work on his using the ship code we have been working on.  Here are the results so far.  It is still in the preliminary stages but it is working.

//===========================================================================================================================

void Render(void)

{

//============================= - The next seven lines of code seems to have round 1 taken care of.
incrementTime += 0.001;
//--------------------------------

distanceBetweenModels[1] = ship_02_POSITION[1] - ship_01_POSITION[1];
distanceBetweenModels[0] = ship_02_POSITION[0] - ship_01_POSITION[0];

//------------------------------------------------------------------------------------------------------

targetRotation = atan2(distanceBetweenModels[0], distanceBetweenModels[1])* 180 / 3.14159;

blendedRotation = (startRotation * (1.0 - sliderTime)) + (targetRotation * sliderTime);

if(sliderTime <= 1.0) // This will prevent the rotation from over-shooting
{
sliderTime += incrementTime;
}

//=====================================================================================================================
glPushMatrix();

DrawShip(ship_01_POSITION_X, ship_01_POSITION_Y, ship_01_POSITION_Z, ship_01_ROTATION, ship_01_COLOR);

glPopMatrix();
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
glPushMatrix();

DrawShip(ship_02_POSITION_X, ship_02_POSITION_Y, ship_02_POSITION_Z, ship_02_ROTATION, ship_02_COLOR);

glPopMatrix();
//=========================================================================================

}

//===========================================================================================================================

What happens here is ship_01 rotates towards ship_02 when the program starts up.  There is nothing yet to update the program so that it will keep happening as the program parameters changes.  It is basically, for now,  a two frame animation that causes a smooth rotation to take place.  It has to be updated still so that it can be an infinite number of frames that take place one after the other as things progress.  We'll have to see how this goes but I think it will work better than my other solutions up to this point.

0

##### Share on other sites

For UP, the angle is 0.0

For LEFT, the angle is 90.0

For DOWN, the angle is 180.0

For RIGHT, the angle is 270.0

//=================================================================

ship_down_two()
{
ship_01_POSITION_Y -= 0.1;

ship_01_ROTATION = 180.0;
}

//=================================================================

well now it snaps to a particular direction but it still does not follow its nose smoothly.

0

##### Share on other sites

EDIT: Nope!  the following doesn't work yet, it just keeps spinning around.

EDIT_2: Now it works! Reversing the positive and negative signs that are highlighted green fixed it

I tested it with some fairly ridiculous numbers like 5000 and -5000 and it still worked, so I think it's good.

//==================================================================================================

We could do something like the following with a variable called "targetAngle"

//=================================================================

ship_down_two() { ship_01_POSITION_Y -= 0.1;

targetAngle   = 180.0; }

//=================================================================

update(int value)

{

if(ship_01_ANGLE < targetAngle) { angle += 1.0; // When this was -= the ship kept spinning endlessly, this note is left here so people can see how a little thing can introduce bugs }

if(ship_01_ANGLE > targetAngle) { angle -= 1.0; // When this was += the ship kept spinning endlessly }

}

0

##### Share on other sites

thanks marc, anymore input?

0

##### Share on other sites

You're going to have to name one of the ships after me Phil.

1

##### Share on other sites

thanks for all the help but I am still stuck.

0

##### Share on other sites

I have almost solved my problem, I just need a little more help.

0

##### Share on other sites

Name it.  I've been having fun with this.

0

##### Share on other sites

I just need to get the ship to follow its nose, it has something to do with the translate or rotate command.  I have used all the code you have given me. thank once again for all  your help. I was thinking about using trigonometric matrices instead of the glRotatef command.

0

##### Share on other sites

do you have anymore input marc?

0

##### Share on other sites

Things are getting a bit confusing here with bits of code that have all been updated and changed.  I'm not sure where anything stands, controls, algorithms, etc...

We might have to start bouncing the full source code back and forth so that we are on the same page with everything, otherwise I'm having to guess at what's what.

0

##### Share on other sites

here is all of my code

#include

<glut.h>
using

namespace std;
GLfloat

angle=0.0f;
GLfloat

up=0.5f;
GLfloat

up_two=0.0f;
GLfloat

ship_01_POSITION_X=0.0f;
GLfloat

ship_01_POSITION_Y=0.0f;
GLfloat

ship_01_POSITION_Z=0.0f;
GLfloat

ship_01_ROTATION=0.0f;
GLfloat

ship_02_POSITION_X=1.5f;
GLfloat

ship_02_POSITION_Y=0.0f;
GLfloat

ship_02_POSITION_Z=0.0f;
GLfloat

ship_02_ROTATION=0.0f;
GLfloat

ship_01_COLOR=1.0f;
GLfloat

ship_02_COLOR=1.0f;
GLfloat

ship_01_bullet_POSITION_X=0.0f;
void

SetupRC()
{
glClearColor(0.0f,0.0f,0.0f,0.0f);
glOrtho(-10.0f,10.0f,-10.0f,10.0f,-1.0f,1.0f);
}
void

ship_left_two()
{
ship_01_ROTATION+=2.5f;
}
void

ship_right_two()
{
ship_01_ROTATION-=2.5f;
}
void

ship_up_two()
{
ship_01_POSITION_Y+=0.1f;
}
void

ship_down_two()
{
ship_01_POSITION_Y-=0.1f;
}
void

ship_up()
{
ship_02_POSITION_Y+=0.1f;
}
void

ship_down()
{
ship_02_POSITION_Y-=0.1f;
}
void

ship_left()
{
ship_02_ROTATION+=2.5f;
}
void

ship_right()
{
ship_02_ROTATION-=2.5f;
}
void

bullet()
{
up+=0.1f;

if(up >= 10.0f)
{
up=0.5f;
}
}
void

bullet_two()
{
up_two+=0.1f;

if(up_two >= 10.0f)
{
up_two=0.5f;
}
}
void

update(int value)
{
angle+=0.1f;

if(angle > 360.0f)
{
angle-=360.0f;
}
glutPostRedisplay();
glutTimerFunc(25,update,0);
}
void

DrawShip(GLfloat position_X,GLfloat position_Y, GLfloat position_Z, GLfloat rotation,GLfloat color)
{
glPushMatrix();
// glTranslatef(-position_X,-position_Y, -position_Z);
glRotatef(

rotation,0.0f,0.0f,1.0f);
glTranslatef(

position_X, position_Y, position_Z);
glColor3f(

color,0.0f,0.0f);
glBegin(

GL_LINE_LOOP);
glVertex3f(0.0f,-0.25f,0.0f);
glVertex3f(-0.25f,-0.5f,0.0f);
glVertex3f(-0.5f,-0.5f,0.0f);
glVertex3f(-0.0f,0.5f,0.0f);
glVertex3f(0.5f,-0.5f,0.0f);
glVertex3f(0.25f,-0.5f,0.0f);
glVertex3f(0.0f,-0.25f,0.0f);
glEnd();
glPopMatrix();
}
void

DrawBullet(GLfloat position_X,GLfloat position_Y,GLfloat position_Z,GLfloat rotation)
{
glPushMatrix();
glColor3f(1.0f,0.0f,0.0f);
glRotatef(

rotation,0.0f,0.0f,1.0f);
glTranslatef(

position_X,position_Y,position_Z);
glPointSize(2.0f);
glBegin(

GL_POINTS);
glVertex3f(0.0f,up++,0.0f);
glEnd();
glPopMatrix();
}
void

Render(void)
{
glClear(

GL_COLOR_BUFFER_BIT);

glPushMatrix();
DrawShip(ship_01_POSITION_X,ship_01_POSITION_Y,ship_01_POSITION_Z,ship_01_ROTATION,ship_01_COLOR);
glPopMatrix();
glPushMatrix();
DrawShip(ship_02_POSITION_X,ship_02_POSITION_Y,ship_02_POSITION_Z,ship_02_ROTATION,ship_02_COLOR);
glPopMatrix();

glPushMatrix();
DrawBullet(ship_01_bullet_POSITION_X,ship_01_POSITION_Y,ship_01_POSITION_Z, ship_01_ROTATION);
glPopMatrix();

glutSwapBuffers();
}
?
?
void

handleKeypress(unsigned char key, int x, int y)
{

switch(key)
{

case 27:
exit(0);

break;

case 32:
bullet();

break;

case 'w':
ship_up();

break;

case 'x':
ship_down();

break;

case 'a':
ship_left();

break;

case 'd':
ship_right();

break;

case 's':
bullet_two();

break;
}
}
void

mySpecialKeys(int key, int x, int y)
{

switch(key)
{

case GLUT_KEY_LEFT:
{
ship_left_two();

break;
}

case GLUT_KEY_RIGHT:
{
ship_right_two();

break;
}

case GLUT_KEY_UP:
{
ship_up_two();

break;
}

case GLUT_KEY_DOWN:
{
ship_down_two();

break;
}
}
}
void

main(int argc, char* argv[])
{

glutInit(&argc, argv);
glutInitDisplayMode(

GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(800,600);
glutInitWindowPosition(300,100);

glutCreateWindow("Asteroids");
glutDisplayFunc(Render);
glutKeyboardFunc(handleKeypress);
glutSpecialFunc(mySpecialKeys);
SetupRC();
glutTimerFunc(25,update,0);
glutMainLoop();
}

0

##### Share on other sites

I've made a change to how the colors are initialized and passed into DrawShip(), the changes allow the color to vary from ship to ship by using an array for the colors.

It's also a cleaner to pass the other variables to DrawShip() in this manner but not required.

//======================================================================================================================

GLfloat ship_01_COLOR[] = {1.0f, 0.0f, 0.0f};
GLfloat ship_02_COLOR[] = {0.0f, 1.0f, 0.0f};

//-----------------------------------------------------------------------------------------------------------------------------------------------

void DrawShip(GLfloat position_X, GLfloat position_Y, GLfloat position_Z, GLfloat rotation, GLfloat *color)
{
glPushMatrix();
//glTranslatef(-position_X,-position_Y, -position_Z);
glRotatef(rotation,0.0f,0.0f,1.0f);
glTranslatef(position_X, position_Y, position_Z);
glColor3f(color[0], color[1], color[2]);
glBegin(GL_LINE_LOOP);
glVertex3f(0.0f,-0.25f,0.0f);
glVertex3f(-0.25f,-0.5f,0.0f);
glVertex3f(-0.5f,-0.5f,0.0f);
glVertex3f(-0.0f,0.5f,0.0f);
glVertex3f(0.5f,-0.5f,0.0f);
glVertex3f(0.25f,-0.5f,0.0f);
glVertex3f(0.0f,-0.25f,0.0f);
glEnd();
glPopMatrix();
}

//-----------------------------------------------------------------------------------------------------------------------------------------------

void Render()

{

//=============================================================================================================
glPushMatrix();
DrawShip(ship_01_POSITION_X,ship_01_POSITION_Y,ship_01_POSITION_Z,ship_01_ROTATION, ship_01_COLOR);
glPopMatrix();
//=============================================================================================================
glPushMatrix();
DrawShip(ship_02_POSITION_X,ship_02_POSITION_Y,ship_02_POSITION_Z,ship_02_ROTATION, ship_02_COLOR);
glPopMatrix();
//=============================================================================================================

}

0

##### Share on other sites

thanks marc but I still cant get the ship follow its nose when moving up and down.

0

##### Share on other sites

Hopefully you will be able to make some sense of this.  The ship does a bit of a round about when the rotation transitions across 0-360 and 360-0 but aside from this it's working.  What I mean by round-about is the ship is not rotating by the shortest path possible.  It will require extra logic to fix this.  Sorry for not posting it in the format you are using(GLUT) and with all those control functions.

       if(ship_01_ROTATION < targetAngle)       {               ship_01_ROTATION += 5.0;                                     }       if(ship_01_ROTATION > targetAngle)       {               ship_01_ROTATION -= 5.0;                                     }       //else if              //=======================================================================================================         glDisable(GL_CULL_FACE);     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );glViewport(0, 0, nWidth, nHeight);            glMatrixMode( GL_PROJECTION );glLoadIdentity();       glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);                  glScalef(.1, .1, .1);        //=============================================================================================================        glPushMatrix();                DrawShip(ship_01_POSITION_X,ship_01_POSITION_Y,ship_01_POSITION_Z,ship_01_ROTATION, ship_01_COLOR);        glPopMatrix();        //=============================================================================================================        glPushMatrix();                DrawShip(ship_02_POSITION_X,ship_02_POSITION_Y,ship_02_POSITION_Z,ship_02_ROTATION, ship_02_COLOR);        glPopMatrix();        //=============================================================================================================        glPushMatrix();                DrawBullet(ship_01_bullet_POSITION_X,ship_01_POSITION_Y,ship_01_POSITION_Z, ship_01_ROTATION);        glPopMatrix();        //=============================================================================================================            SwapBuffers( g_hDC );}//=====================================================================================================================//---------------------------------------------------------------------------------------------------------------------//---------------------------------------------------------------------------------------------------------------------//=====================================================================================================================void ProcessKeyboard(void){    //=======================================================================================    if (keys[VK_UP] && !keys[VK_LEFT]  && !keys[VK_RIGHT])    {       ship_01_POSITION_Y +=   0.05;       targetAngle         =   0.0;    }    if (keys[VK_DOWN] && !keys[VK_LEFT]  && !keys[VK_RIGHT])    {       ship_01_POSITION_Y -=   0.05;       targetAngle         = 180.0;    }    if (keys[VK_LEFT] && !keys[VK_DOWN]  && !keys[VK_UP])    {       ship_01_POSITION_X -=   0.05;       targetAngle         =  90.0;    }    if (keys[VK_RIGHT] && !keys[VK_DOWN]  && !keys[VK_UP])    {       ship_01_POSITION_X +=   0.05;       targetAngle         = 270.0;    }    //=======================================================================================    if (keys[VK_UP] && keys[VK_LEFT])    {       ship_01_POSITION_X -=   0.05*0.7;        ship_01_POSITION_Y +=   0.05*0.7;       targetAngle         =  45.0;    }    if (keys[VK_DOWN] && keys[VK_LEFT])    {       ship_01_POSITION_X -=   0.05*0.7;              ship_01_POSITION_Y -=   0.05*0.7;       targetAngle         = 120.0;    }    if (keys[VK_DOWN] && keys[VK_RIGHT])    {       ship_01_POSITION_X +=   0.05*0.7;              ship_01_POSITION_Y -=   0.05*0.7;       targetAngle         = 225.0;    }            if (keys[VK_UP] && keys[VK_RIGHT])    {       ship_01_POSITION_X +=   0.05*0.7;        ship_01_POSITION_Y +=   0.05*0.7;       targetAngle         =  315.0;    }
0

##### Share on other sites

well I tried your code but it is still rotating as if its on a swing arm.

0

##### Share on other sites

That's because you have glRotatef() before the glTranslatef().

I should have made that correction above when I changed how color is being handled when I re-posted your code for DrawShip.

//=============================================================================================== You have the following...

void DrawShip(GLfloat position_X, GLfloat position_Y, GLfloat position_Z, GLfloat rotation, GLfloat *color)
{
glPushMatrix();
//glTranslatef(-position_X,-position_Y, -position_Z);
glRotatef(rotation,0.0f,0.0f,1.0f);
glTranslatef(position_X, position_Y, position_Z);
glColor3f(color[0], color[1], color[2]);
glBegin(GL_LINE_LOOP);
glVertex3f(0.0f,-0.25f,0.0f);
glVertex3f(-0.25f,-0.5f,0.0f);
glVertex3f(-0.5f,-0.5f,0.0f);
glVertex3f(-0.0f,0.5f,0.0f);
glVertex3f(0.5f,-0.5f,0.0f);
glVertex3f(0.25f,-0.5f,0.0f);
glVertex3f(0.0f,-0.25f,0.0f);
glEnd();
glPopMatrix();
}

//============================================================= Instead, it should look like this

void DrawShip(GLfloat position_X, GLfloat position_Y, GLfloat position_Z, GLfloat rotation, GLfloat *color)
{
glPushMatrix();
glTranslatef(position_X, position_Y, position_Z);
glRotatef(rotation,0.0f,0.0f,1.0f);
glColor3f(color[0], color[1], color[2]);
glBegin(GL_LINE_LOOP);
glVertex3f(0.0f,-0.25f,0.0f);
glVertex3f(-0.25f,-0.5f,0.0f);
glVertex3f(-0.5f,-0.5f,0.0f);
glVertex3f(-0.0f,0.5f,0.0f);
glVertex3f(0.5f,-0.5f,0.0f);
glVertex3f(0.25f,-0.5f,0.0f);
glVertex3f(0.0f,-0.25f,0.0f);
glEnd();
glPopMatrix();
}
0

##### Share on other sites

well I put translate before rotate and it moves up and down but does not follow its nose, it moves straight up and down.

0

##### Share on other sites

thanks for all the help marc I have almost solved my problem. I just need a little more help.

0

keep going

0

##### Share on other sites

I am still stuck, any advice?

0

##### Share on other sites

any more hints or tips?

0

## Create an account

Register a new account