make an object move around randomly with openGL

I have a textured polygon that rotates.  I used GL_QUADS and glRotatef in openGL to draw the cube.  I would like to make this into a screensacer by coding a function that makes the box float around as it rotates, and possibly bounce off of the borders of the window.  Can someone please solve this?  
spo7Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SammoCommented:
Hi there,

You can just position the box randomly using rand() to generate a random number and then position the box via that. You'll have to set some upper and lower bounds for the random number, probably best to just have a play around with a few values and see which ones work best.

rand() % 100

This would give you a random number 0 to 100.

If you need more assistance post a bit of code up and Ill have a play around with it.

Hope this helps

Sam

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
InteractiveMindCommented:
The easiest solution is to break the motion of the cube up into 2 components—horizontal and vertical (x, and y).

You assign the cube an initially random direction vector, by giving it random x and y components (see Sam's comment above).

You then have a seperate thread iterate infinitely, pause for say 30 milliseconds, then repaint the display.

When the display is repainted, you use the current position, and the direction components of the cube to calculate the new position for it.

You merely need to paint the cube in it's new position.

Next, you check to see if the x or y components exceed the window limits; if it does, then you INVERT them (*-1), so that they appear to bounce off the screen's edges..

Does that help?
SammoCommented:
Yeah apologies, I didn't think about the fact you wanted it floating around!
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

InteractiveMindCommented:
An example:


// define the x,y position of the cube on the screen (where the origin 0,0 is the center of the screen)

int x = 0 ;
int y = 0 ;

// define the random direction vectors for each axis

int dx = rand() % 30 ;
int dy = rand() % 30 ;

.
.

// in your seperate thread

for( ;; )
{
    Sleep( 30 ) ;  // 30 ms
    // invoke a repaint() here
}

.
.

// when you repaint the display:

// -- render the cube here, with the coordinates x,y -- //

// now update the x,y position based on the dx,dy values:

x += dx ;
y += dy ;

// now check to see if there's any collision with the edges of the screen, with width and height of w and h:
// (where 'c' is the width (or height) of the cube)

if ( x-c-w/2 <= 0  ||  x+c >= w/2 )
{
    dx = -dx ;
}

if ( y-c-h/2 <= 0  ||  y+c >= w/2 )
{
    dy = -dy ;
}
spo7Author Commented:
This is helpful - although I'm having trouble getting it working properly.  Since I'm very new to openGL, I was hoping for an answer more specific to openGL functions etc.  
InteractiveMindCommented:
The OpenGL functions are really just for the rendering side of things. Performing the actual transformations are down to the language code itself.



glRotatef( a, ax, ay, az ) ;  // rotate some amount around an arbitrary axis
glTranslate3i( x, y, 0 ) ; // translate to the position

// -- render the cube here -- //


That's about as much work as you've got to do in terms of OpenGL ;)


Is there something in particular that you wish to do purely with OpenGL ?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.