Random Astroids same coords

This is a discussion on Random Astroids same coords within the C++ Programming forums, part of the General Programming Boards category; I have a class astroid and when I create multiple objects each appears in the same location what am I ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    450

    Random Astroids same coords

    I have a class astroid and when I create multiple objects each appears in the same location what am I doing wrong?
    Code:
    // astroid.cpp
    // author: Troy Curless
    // Sunday September 5, 2004
    // Definition of Astroid class member functions
    #include <GL/glut.h>
    #include <cstdlib>
    #include <time.h>
    #include "astroid.h"
    
    extern const int SCREENWIDTH;
    extern const int SCREENHEIGHT;
    
    Astroid::Astroid()
    {
      srand(time(0));
      xcoord=rand()%SCREENWIDTH+1;
      ycoord=rand()%SCREENHEIGHT+1;
      velocity = .1*(2-rand()%4);
    }
    
    void Astroid::draw() const{
      glColor3f(256.0, 0.0 , 0.0);
      glRecti(xcoord, ycoord,xcoord+10,ycoord+10);
    
    }
    
    void Astroid::rotate(){}
    void Astroid::transform(){
      glTranslatef(velocity,velocity,0);
    }
    
    Astroid::~Astroid(){}
    I create the objects storing them in a vector of gameobjects
    for (int i = 1; i<10; i++)
    gameObjects[i] = new Astroid();

    Just Started learning opengl and I have a couple other issues. I haven't learned how to transform individual objects. For instance my Astroid.transform() function actualy translates all the gameobjects so I need to read a bit more in the redbook to figure this one out but if anyone wants to post a quick and dirty explanation super. I assume translate is moving the view camera and hence all the objects.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,200
    I dont know how much this helps but call srand only once in your program.

  3. #3
    Teenage Mutant Ninja Nerd MMD_Lynx's Avatar
    Join Date
    Aug 2004
    Posts
    65
    yes that should probably help a lil
    Code:
    bool init_rand = false;
    
    Asteroid::Asteroid
    {
       if(init_rand == false)
       {
          init_rand = true;
          srand(time(0));
       }
       //rest of the code
    }
    P.S. Post #50
    Stupid people are useful. You can make them do all the mindless tasks you are too lazy to do yourself.

    Sphynx cats are just bald and wrinkly, like old people, and we don't reject them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random to int?
    By psyadam in forum C# Programming
    Replies: 7
    Last Post: 07-22-2008, 08:09 PM
  2. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 08:16 AM
  3. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 04:51 PM
  4. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21