make an object move around randomly with openGL

Posted on 2006-04-22
Last Modified: 2012-05-05
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?  
Question by:spo7
    LVL 2

    Accepted Solution

    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

    LVL 25

    Expert Comment

    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?
    LVL 2

    Expert Comment

    Yeah apologies, I didn't think about the fact you wanted it floating around!
    LVL 25

    Expert Comment

    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 ;

    Author Comment

    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.  
    LVL 25

    Assisted Solution

    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 ?

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now