I am having trouble displaying and animation multiple asteroids for my GLUT asteroids game. I can generate 1 ok but when I try to add more the output is not right, createasteroid keeps getting called and just flashing up asteroids for milliseconds in different parts of the screen for some reason. I believe it may be because the objects are getting destroyed when the display function ends but I cant figure out a workaround Here is my code so far:
asteroid.h
class asteroid
{
public:
asteroid(void); //constructer
~asteroid(void); //deconstructer
void createAsteroid();
float generateAsteroidLocation(float a, float b);
void animateAsteroid();
};
asteroid.cpp
#include "asteroid.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <ctime>
#include <GL/glut.h>
float asteroidX,asteroidY, V;
int numOfAsteroids=0;
asteroid::asteroid(void){
}
asteroid::~asteroid(void){
}
void asteroid::createAsteroid(){
// Translate to an area outside of the screen and then give a random velocity in the direction of the player
asteroidX = generateAsteroidLocation(0, 30);
asteroidY = generateAsteroidLocation(0, 30);
V = generateAsteroidLocation(0.000030, 0.000050);
printf("Asteroid Y Location %f \n", asteroidY);
printf("Asteroid X Location %f \n", asteroidX);
printf("Asteroid Velocity %f \n", V);
printf("Asteroid Created! \n");
glPushMatrix();
glTranslatef(asteroidX, asteroidY, 0.0);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINE_STRIP);
glVertex2f(1.0, 1.0);
glVertex2f(1.8, 0.0);
glVertex2f(1.4, -0.8);
glVertex2f(1.0, -1.7);
glVertex2f(-1.5, -1.0);
glVertex2f(-1.0, 0.0);
glVertex2f(0.0, 1.4);
glVertex2f(1.0, 1.0);
glEnd();
glPopMatrix();
}
float asteroid::generateAsteroidLocation(float a, float b){
float random = ((float) rand()) / (float) RAND_MAX;
float range = b - a;
return (random*range) + a;
}
void asteroid::animateAsteroid(){
float dt = 3500;
float Dx = 25 - asteroidX;
float Dy = 25 - asteroidY;
float Cx = asteroidX + Dx / sqrt(Dx*Dx+Dy*Dy) * V * dt;
float Cy = asteroidY + Dy / sqrt(Dx*Dx+Dy*Dy) * V * dt;
asteroidX = Cx;
asteroidY = Cy;
}
main.cpp ( the functions causing problems )
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <ctime>
#include "asteroid.h"
#include <GL/glut.h>
asteroid a1;
asteroid a2;
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
// check to see if the player is ready, if not, show the splashscreen
if(ready == false){
splashScreen();
showText();
}
else{
createSpaceship();
// PROBLEM HERE
a1.createAsteroid();
a2.createAsteroid();
}
// Setup the scene
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 50, 0, 50, 0, 50);
// call the needed functions
glutSwapBuffers();
}
void idle(void)
{
glutPostWindowRedisplay(glutGetWindow());
// PROBLEM HERE
a1.animateAsteroid();
a2.animateAsteroid();
}
Any help on this would be great. I’m stumped as to what approach to take. I can provide more information or a pastebin if needed.
Thanks! -Dan
You simply want to break your
createAsteroidfunction in two, one which creates theAsteroid, setting its initial values, and another one that does the rendering.You seem to have a
readyfunction to know when the loading is done, it’s around there that you should create and initialize your asteroids. In display you should call a function that does only this part:Also, you seem to recreate your player’s spaceship at every
displaytoo, it should only be created in the initialization phase like the Asteroids!Edit: If I’m not clear, when creating a game object you usually want at least 4 main methods: Create/Init, Update (where you would animate your object, do gameplay code like testing for bullet hits, etc…), Render and Clean up / Destroy. Sticking to that should really help you getting your game going!