Results 1 to 3 of 3

Thread: Asteroids

  1. #1
    Junior Member Newbie
    Join Date
    Jun 2010
    Posts
    28

    Asteroids

    Hi, I just started playing around with OpenGL. I'm creating the classic Asteroids game. I have a problem when creating the actual asteroids.

    here's my constructor
    Code :
    Asteroid::Asteroid()
    {
      radius = 2;
      numPts = 8;
      srand(time(0));
      angle = 1 + rand() % 360;
      posX = -10 + rand() % 20;
      posY = -10 + rand() % 20;
    }

    Then here's my draw code
    Code :
    void Asteroid::draw()
    {
     
      glTranslatef(posX, posY, 0.0);
      glBegin(GL_LINE_LOOP);
      for (int i=0; i < 360; i+=360/numPts)
      {
        float degInRad = i*DEG2RAD;
        glVertex2f(cos(degInRad)*radius, sin(degInRad)*radius);
      }
      glEnd();
    }
    and finally in main() in my display function
    Code :
      for(int i=0; i<numOfAsteroids; i++)
      {
        glPushMatrix();
          if(asteroids[i].getActive())
            asteroids[i].draw();
        glPopMatrix();
      }

    The problem is that I draw 10 asteroids in my display func. But their coordinates are all the same. I put that rand() function inside the constructor of each asteroid thinking it would create a set of different coordinates for each asteroid. But what it does instead, is create the same coordinates for all ten asteroids.

    help,
    mike

  2. #2
    Senior Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: Asteroids

    Move the srand(time(0)) in your main function or the function that you call once at init.
    Regards,
    Mobeen

  3. #3
    Junior Member Newbie
    Join Date
    Jun 2010
    Posts
    28

    Re: Asteroids

    thanx, that was it

Similar Threads

  1. asteroids
    By mikeglaz in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 06-17-2010, 12:53 PM
  2. Asteroids with OpenGL, matrix problem
    By in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 07-06-2003, 01:20 PM
  3. 2D asteroids help please
    By mat01 in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 04-24-2003, 09:09 AM
  4. 2D Asteroids Help Please
    By mat01 in forum OpenGL: User Software
    Replies: 1
    Last Post: 04-23-2003, 02:34 PM
  5. linked list-asteroids together
    By in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 06-14-2001, 08:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean