rendering space ships

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

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 }

}

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.

Advertisement

thanks marc, anymore input?

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

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.

thanks for all the help but I am still stuck.

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

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

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 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.

do you have anymore input marc?

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.

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

This topic is closed to new replies.

Advertisement