Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8916627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:17:03+00:00 2026-06-15T05:17:03+00:00

I am having trouble displaying and animation multiple asteroids for my GLUT asteroids game.

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:17:05+00:00Added an answer on June 15, 2026 at 5:17 am

    You simply want to break your createAsteroid function in two, one which creates the Asteroid, setting its initial values, and another one that does the rendering.

    You seem to have a ready function 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:

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

    Also, you seem to recreate your player’s spaceship at every display too, 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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello i'm having some trouble in displaying multiple pushpins on maps. So far i've
My nested model form is now working, but I am having trouble displaying the
I'm having trouble displaying a Base64 image inline. How can I do it? <!DOCTYPE
Hello I'm having trouble displaying the output of a SP in a console window
I'm having some trouble with displaying numbers in apex, but only when i fill
OK, this is an amateur question but I am having the most trouble displaying
I have a simple login page but i am having trouble displaying the logged
guys am having some troubles with running out of memory when displaying an animation
I am having trouble displaying results recieved from asp.net WebMethod. I have a HTML
I am having trouble displaying a raw YUV file that it is in NV12

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.