rendering space ships

Started by
52 comments, last by phil67rpg 10 years, 9 months ago

I have drawn two space ships to the screen, they move up and down and left and right. I want them to move independently of each other. Right now they work in tandem. I am confused on how to use the pushmatrix and popmatrix commands. here is my main rendering code.

void
 
 
draw_ship()
{
glClear(
 
GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
 
glColor3f(0.0f,1.0f,0.0f);
glBegin(
 
GL_LINE_LOOP);
glVertex3f(-1.5f,-0.25f,0.0f);
glVertex3f(-1.75f,-0.5f,0.0f);
glVertex3f(-2.0f,-0.5f,0.0f);
glVertex3f(-1.5f,0.5f,0.0f);
glVertex3f(-1.0f,-0.5f,0.0f);
glVertex3f(-1.25f,-0.5f,0.0f);
glVertex3f(-1.5f,-0.25f,0.0f);
glEnd();
glColor3f(1.0f,0.0f,0.0f);
glBegin(
 
GL_LINE_LOOP);
glVertex3f(1.5f,-0.25f,0.0f);
glVertex3f(1.25f,-0.5f,0.0f);
glVertex3f(1.0f,-0.5f,0.0f);
glVertex3f(1.5f,0.5f,0.0f);
glVertex3f(2.0f,-0.5f,0.0f);
glVertex3f(1.75f,-0.5f,0.0f);
glVertex3f(1.5f,-0.25f,0.0f);
glEnd();
glColor3f(1.0f,0.0f,0.0f);
glPointSize(2.0f);
glBegin(
 
GL_POINTS);
glVertex3f(1.5f,up_two++,0.0f);
glEnd();
glColor3f(0.0f,1.0f,0.0f);
glPointSize(2.0f);
glBegin(
 
GL_POINTS);
glVertex3f(-1.5f,up++,0.0f);
glEnd();
 
glutSwapBuffers();
}

they also shoot bullets.

Advertisement

glPushMatrix();

Translate(ship_01_Position[0], ship_01_Position[1], ship_01_Position[2]);

glColor3f(0.0f,1.0f,0.0f); glBegin(GL_LINE_LOOP); glVertex3f(-1.5f,-0.25f,0.0f); glVertex3f(-1.75f,-0.5f,0.0f); glVertex3f(-2.0f,-0.5f,0.0f); glVertex3f(-1.5f,0.5f,0.0f); glVertex3f(-1.0f,-0.5f,0.0f); glVertex3f(-1.25f,-0.5f,0.0f); glVertex3f(-1.5f,-0.25f,0.0f); glEnd();

glPopMatrix();

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

glPushMatrix();

Translate(ship_02_Position[0], ship_02_Position[1], ship_02_Position[2]);

glColor3f(1.0f,0.0f,0.0f); glBegin(GL_LINE_LOOP); glVertex3f(1.5f,-0.25f,0.0f); glVertex3f(1.25f,-0.5f,0.0f); glVertex3f(1.0f,-0.5f,0.0f); glVertex3f(1.5f,0.5f,0.0f); glVertex3f(2.0f,-0.5f,0.0f); glVertex3f(1.75f,-0.5f,0.0f); glVertex3f(1.5f,-0.25f,0.0f); glEnd();

glPopMatrix();

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

Consider it pure joy, my brothers and sisters, whenever you face trials of many kinds, 3 because you know that the testing of your faith produces perseverance. 4 Let perseverance finish its work so that you may be mature and complete, not lacking anything.

well thanks for the above code, but I am still getting the space ships working in tandem.

Then you're either not pusing and popping the matrices, or you're translating the two ships with the same variables.

I don't see any code at all that would move the spaceships, there are movement variables for the bullets but nothing else unless I'm having a brain fart. I presume that there is a translation call before the draw_ship() function. If you post the whole Render function it will make it easier for someone to walk you through it. The way you have that function set up with the calls to glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); and SwapBuffers, it makes it seem as if that IS the render function. If this is the case, how are you getting any motion for the ships at all? You mentioned that you are having issues with PushMatrix and PopMatrix but they aren't even in the code you posted.

Consider it pure joy, my brothers and sisters, whenever you face trials of many kinds, 3 because you know that the testing of your faith produces perseverance. 4 Let perseverance finish its work so that you may be mature and complete, not lacking anything.

here is my movement code

void
 
 
ship_left_two()
{
glTranslatef(-1.5f,0.0f,0.0f);
glRotatef(angle,0.0f,0.0f,1.0f);
glTranslatef(1.5f,0.0f,0.0f);
}
void
 
 
ship_right_two()
{
glTranslatef(-1.5f,0.0f,0.0f);
glRotatef(-angle,0.0f,0.0f,1.0f);
glTranslatef(1.5f,0.0f,0.0f);
}
void
 
 
ship_up_two()
{
glTranslatef(0.0f,0.1f,0.0f);
}
void
 
 
ship_down_two()
{
glTranslatef(0.0f,-0.1f,0.0f);
}
void
 
 
ship_up()
{
glTranslatef(0.0f,0.1f,0.0f);
}
void
 
 
ship_down()
{
glTranslatef(0.0f,-0.1f,0.0f);
}
void
 
 
ship_left()
{
glTranslatef(1.5f,0.0f,0.0f);
glRotatef(angle,0.0f,0.0f,1.0f);
glTranslatef(-1.5f,0.0f,0.0f);
}
void
 
 
ship_right()
{
glTranslatef(1.5f,0.0f,0.0f);
glRotatef(-angle,0.0f,0.0f,1.0f);
glTranslatef(-1.5f,0.0f,0.0f);
}
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;
}
glutPostRedisplay();
glutTimerFunc(25,update,0);
}

Now I'm very confused. In the first code you posted you have a function called draw_ship that has all the draw calls in it. There is nothing about this function in the most recent code that you posted.

In the recent code there are no Push and Pop function calls to isolate the matrix math. And in the draw_ship function you have all the draw calls together.

The following is how you would isolate model transformations and then move them around.

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

To move ship_01 to the right you could do the following

if(keyPressed_right_arrow) //I'm using pseudo code here since I don't use GLUT and I'll confuse the issue if I write the syntax I use

{

ship_01_POSITION_X += 0.01f;

}

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

To move ship_02 right you could do the following

if(keyPressed_D)

{

ship_02_POSITION_X += 0.01f;

}

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

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

glPushMatrix()

glTranslatef(ship_01_POSITION_X, ship_01_POSITION_Y, 0.0);

glRotatef(ship_01_ANGLE, 0.0f, 0.0f, 1.0f);

glColor3f(0.0f,1.0f,0.0f); glBegin(GL_LINE_LOOP); glVertex3f(-1.5f,-0.25f,0.0f); //if both ships are the same then they should also both have the same vertex info.

glVertex3f(-1.75f,-0.5f,0.0f); //Use glTranslatef to move the models to their respective positions glVertex3f(-2.0f,-0.5f,0.0f); glVertex3f(-1.5f,0.5f,0.0f); glVertex3f(-1.0f,-0.5f,0.0f); glVertex3f(-1.25f,-0.5f,0.0f); glVertex3f(-1.5f,-0.25f,0.0f); glEnd();

glPopMatrix()

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

glPushMatrix()

glTranslatef(ship_02_POSITION_X, ship_02_POSITION_Y, 0.0);

glRotatef(ship_02_ANGLE, 0.0f, 0.0f, 1.0f);

glColor3f(0.0f, 1.0f, 0.0f); glBegin(GL_LINE_LOOP); glVertex3f(-1.5f,-0.25f,0.0f); glVertex3f(-1.75f,-0.5f,0.0f); glVertex3f(-2.0f,-0.5f,0.0f); glVertex3f(-1.5f,0.5f,0.0f); glVertex3f(-1.0f,-0.5f,0.0f); glVertex3f(-1.25f,-0.5f,0.0f); glVertex3f(-1.5f,-0.25f,0.0f); glEnd();

glPopMatrix()

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

Bullets should be outside of the ships' Push and Pop calls but started from the ships' position at the time they are fired.

//######################################################################################################################

If you wanted both models to move as a fleet but still have the ability to move them with respect to each other then you could do the following.

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

glPushMatrix(); //The purple Push and Pops isolate other model transformations that come after the fleet transforms but allow both ships to be moved as a group

glTranslatef(moveFleet_POSITION_X, moveFleet_POSITION_Y, 0.0);

glRotatef(fleet_ANGLE, 0.0f, 0.0f, 1.0f);

glPushMatrix();

glTranslatef(ship_01_POSITION_X, ship_01_POSITION_Y, 0.0);

glRotatef(ship_01_ANGLE, 0.0f, 0.0f, 1.0f);

glColor3f(0.0f,1.0f,0.0f); glBegin(GL_LINE_LOOP); glVertex3f(-1.5f,-0.25f,0.0f);

glVertex3f(-1.75f,-0.5f,0.0f); glVertex3f(-2.0f,-0.5f,0.0f); glVertex3f(-1.5f,0.5f,0.0f); glVertex3f(-1.0f,-0.5f,0.0f); glVertex3f(-1.25f,-0.5f,0.0f); glVertex3f(-1.5f,-0.25f,0.0f); glEnd();

glPopMatrix();

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

glPushMatrix();

glTranslatef(ship_02_POSITION_X, ship_02_POSITION_Y, 0.0);

glRotatef(ship_02_ANGLE, 0.0f, 0.0f, 1.0f);

glColor3f(0.0f, 1.0f, 0.0f); glBegin(GL_LINE_LOOP); glVertex3f(-1.5f,-0.25f,0.0f); glVertex3f(-1.75f,-0.5f,0.0f); glVertex3f(-2.0f,-0.5f,0.0f); glVertex3f(-1.5f,0.5f,0.0f); glVertex3f(-1.0f,-0.5f,0.0f); glVertex3f(-1.25f,-0.5f,0.0f); glVertex3f(-1.5f,-0.25f,0.0f); glEnd();

glPopMatrix();

glPopMatrix();

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

Caution! treat glPushMatrix and glPopMatrix like awful bugs waiting to happen.

Always check and recheck that an equal number of Pops always follow the same number of Pushes. If you have 10 Pushes every frame, make sure you have 10 Pops every frame. If you miss this you will most certainly regret it when errors start to add up in consecutive frames.

Consider it pure joy, my brothers and sisters, whenever you face trials of many kinds, 3 because you know that the testing of your faith produces perseverance. 4 Let perseverance finish its work so that you may be mature and complete, not lacking anything.

Once again marcClinton, you saved a life ! I mean by that that your answers are always clear and well explained.

Thanks shocobenn.

Consider it pure joy, my brothers and sisters, whenever you face trials of many kinds, 3 because you know that the testing of your faith produces perseverance. 4 Let perseverance finish its work so that you may be mature and complete, not lacking anything.

well I have implemented the above code and it all works well except that when I rotate the ship it still moves up and down but does not move in the direction the ship is oriented.

in other words it does not follow its own nose. thanks for all the help. let me if you need additional code.

This topic is closed to new replies.

Advertisement