mikeglaz
06-13-2010, 03:22 PM
Hi. I'm just starting my OpenGL adventure. Could someone help me with the code of how to make an object fly across the screen (i.e. a flying bullet). I have my spaceship flying around nicely but when I fire a bullet it only appears at its original location and then its ending location. I don't see it animate (or fly) through the screen. I mean shouldn't the glutPostRedisplay() function call in my keyboard() function render the bullet at each point? Here's some of my code:
void display(void)
{
int i=0;
glClear (GL_COLOR_BUFFER_BIT);
init();
glColor3f(1.0,1.0,1.0);
glLoadIdentity();
glPushMatrix();
//translate and rotate spaceship
glTranslatef(speedX, speedY, 0.0);
glRotatef(angle, 0.0, 0.0, 1.0);
//draw spaceship
glColor3f(1.0,1.0,1.0);
glBegin(GL_POLYGON);
glVertex2f(-1.0, 0.0);
glVertex2f(1.0, 1.0);
glVertex2f(1.0, -1.0);
glEnd();
glPopMatrix();
glPointSize(4.0);
glColor3f(1.0,0.0,0.0);
//translate and draw bullet
glTranslatef(bulletX, bulletY, 0.0);
glBegin(GL_POINTS);
glVertex2f(0.0,0.0);
glEnd();
}
glutSwapBuffers();
}
...
void specialKeys (int key, int x, int y)
{
switch (key) {
case GLUT_KEY_UP:
speedX -= .05*cos(angle*PI/180);
speedY -= .05*sin(angle*PI/180);
glutPostRedisplay();
break;
...
void keyboard(unsigned char key, int x, int y)
{
int i;
switch(key)
{
case ' ':
for(i=0; i<10; i++)
{
bulletX -= 0.5*cos(angle*PI/180);
bulletY -= 0.5*sin(angle*PI/180);
glutPostRedisplay();
}
break;
...
void display(void)
{
int i=0;
glClear (GL_COLOR_BUFFER_BIT);
init();
glColor3f(1.0,1.0,1.0);
glLoadIdentity();
glPushMatrix();
//translate and rotate spaceship
glTranslatef(speedX, speedY, 0.0);
glRotatef(angle, 0.0, 0.0, 1.0);
//draw spaceship
glColor3f(1.0,1.0,1.0);
glBegin(GL_POLYGON);
glVertex2f(-1.0, 0.0);
glVertex2f(1.0, 1.0);
glVertex2f(1.0, -1.0);
glEnd();
glPopMatrix();
glPointSize(4.0);
glColor3f(1.0,0.0,0.0);
//translate and draw bullet
glTranslatef(bulletX, bulletY, 0.0);
glBegin(GL_POINTS);
glVertex2f(0.0,0.0);
glEnd();
}
glutSwapBuffers();
}
...
void specialKeys (int key, int x, int y)
{
switch (key) {
case GLUT_KEY_UP:
speedX -= .05*cos(angle*PI/180);
speedY -= .05*sin(angle*PI/180);
glutPostRedisplay();
break;
...
void keyboard(unsigned char key, int x, int y)
{
int i;
switch(key)
{
case ' ':
for(i=0; i<10; i++)
{
bulletX -= 0.5*cos(angle*PI/180);
bulletY -= 0.5*sin(angle*PI/180);
glutPostRedisplay();
}
break;
...