View Full Version : Random Placement on a grid?????

01-11-2002, 08:56 AM
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.

Here is the code that I used to try to place the boxes in a random position.
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);

This causes it to not display the boxes at all. I'm pretty sure I'm making a stupid mistake but I can't see it. Need more help

01-11-2002, 11:07 AM
Translations, rotations, etc are cummulative unless you load the identity matrix before the translation, either through poping the last matrix or loading the identity matrix before the translation.

What may be happening is, because of the accumulation of translations, objects are being placed out of view or at random locations.

Review Matrix manipulation (Push, Pop), glLoadIdentity and Translation, Rotations, etc. These topics are grouped together because they are related to one another.

Hope this helps.


[This message has been edited by lobstah (edited 01-11-2002).]

[This message has been edited by lobstah (edited 01-11-2002).]