View Full Version : random movement

09-16-2002, 11:45 AM
I am new here, and I am trying to learn OpenGL with the help of a textbook and no teacher! I am kind of following allong on a certain class offered at a school near me to structure my learning. However, I am trying to make a 2-D square that spins while moving randomly across the screen. I seem to be having trouble figuring out how to get the square top move about in a random fashoin. WHen the square hits the edge of the window, it is suppsed to turn into a triangle. At any rate, does any one have any ideas on how I can generate this random motion? Everyone seems very knowledgeable and I think I have found a great resource!!

09-16-2002, 02:32 PM
Well, if you haven't already paid a visit I suggest you go to nehe.gamedev.net and work through at least the first 5 tutorials to get the basics down...

With your particular problem in mind I would guess that the following are ideal tutorials to work through...

Lesson 2 - Polygons
Lesson 4 - Rotation
Lesson 30 - Collision Detection.. although perhaps not that necessary for your problem...

Set up x,y,z values for the start position of a square and randomize these values make sure they fall within chosen boundaries... then draw the square... it will automatically regenerate the new place for it...

I haven't played with collision detection myself yet but I would assume that somewhere you would test the xyz values and if any one of them are hitting the edges of the winow set the variable to draw a triangle up and repeat the render sequence but this time draw the triangle instead of the square.

Pseudocode of something you could do for this to work if you need it

global vars xpos=0,ypos=0,zpos=0,rotation=0,xmin=left,xmax=wid th,ymin=bottom,ymax=height,zmin=near,zmax=far,ds=t rue,dt=false
routine drawsquare which draws the square
routine drawtri which draws the triangle

render routine

x = randomnumber between xmin and xmax
y = randomnumber between ymin and ymax
z = randomnumber between zmin and zmax

if x=xmax or y=ymax then set dt=not dt and ds=not ds (effectively swapping which object to draw)

position object at x,y,z
rotate object using angle on selected axes

if dt drawtriangle
if ds drawsquare

increase rotation angle

end of render routine

Hope that helps

[This message has been edited by tinak (edited 09-17-2002).]