Random placement on a grid??

Started by
3 comments, last by jon723 22 years, 3 months ago
Hello. I was wondering if someone could tell me how I would go about placeing objects in a random position on a grid. Actually this is what I did. I created a display list that contained the points for a cube. Then, inside another function I created a grid of points on the xz plane. Now the problem I have is that I want to take a specific number of the box I created and randomly place them on different points on the grid. When I tried doing this it showed me the boxes moving around the different points. Is there a way to stop this so that they remain in a fixed location??? Someone please help me out. Thanx, Jon
www.lefthandinteractive.net
Advertisement
You should generate the points once, store them in some fashion (array, list, something else), and then use those stored points.

It is possible, however, to guarantee the same series of random points each frame. This would achieve the same effect. To do this, seed the random number generator immediately before placing your cubes or whatever on the grid with a constant number. Assuming you''re using the stdlib, then you would call srand(some_constant) to do this.
Hey, I seeded the random number generator with a variable equal to 6. I then created two "GLfloat" numbers and inside a "for loop" which ran 4 times (just to test it) I added the following code:

for(int r = 0; r <= 3; r++)
{
pointx = rand(); // make float number a random number
pointz = rand(); // make float number a random number

glTranslatef(pointx, 0.0f, pointz);
Addbox();
}



Now it won''t even display the boxes at all. I''m pretty sure I''m making a stupid mistake but I can''t see it. Need more help

Thanx, Jon.
www.lefthandinteractive.net
My only guess is that your cubes are outside the FOV. If I were you, I''d temporarily disable lighting, texturing, vertex colors, and backface culling, and add code to rotate and move the "camera." This way you should be able to figure out where your cubes are really being placed. I''m afraid I can''t offer too much advice beyond that.
What''s the size of your box?

try (float)(rand()%[num]) where [num] is the value you want to go from 0 to.
http://www.deakin.edu.au/~bradleyj/unsanity/

This topic is closed to new replies.

Advertisement