rendering space ships

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

I have incorporated your code, but I cannot get the ships to rotate around their centers and to follow their noses when moving up and down. here is my code so far.

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.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();
}
void
 
 
Render(void)
{
glClear(
 
GL_COLOR_BUFFER_BIT);
DrawShip(ship_01_POSITION_X,ship_01_POSITION_Y,ship_01_POSITION_Z,ship_01_ROTATION,ship_01_COLOR);
DrawShip(ship_02_POSITION_X,ship_02_POSITION_Y,ship_02_POSITION_Z,ship_02_ROTATION,ship_02_COLOR);
glutSwapBuffers();
}

here is my movement code


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;
}

Advertisement

thanks for all the help marc. do you have anymore input to give me?

I'm working on it right now, hold please.

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.

cool thanks

It looks like the ships points are being defined off-center. Once you've fixed this we can proceed to rotating them properly to face their trajectories
//=======================================================================================================
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();
//=======================================================================================================
The rest is looking good so far...
Edit: +1.5 to all the X values centers them on the Y-axis so they rotate properly.

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.

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.

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; }

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

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 code for my previous rotation routine

void
 
 
update(int value)
{
angle+=0.1f;
 
 
if(angle > 360.0f)
{
angle-=360;
}
glutPostRedisplay();
glutTimerFunc(25,update,0);
}

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.

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.


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.

This topic is closed to new replies.

Advertisement