Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to make cubes that I am drawing appear to "jump" every few seconds. Here is my code:

for (int i=0; i<25; i++)
{
    if(j<rows)
    {

        //Timer used in vibration calculation when drawing cubes
        float time = (std::clock() - timer);
        //Calculate the amount of simulated vibration based on amount of distortion (intensity)
        float offset = sin(1.0f * 3.14159265359f * time) * shake;

        //Draw cubes right and left of centre point
        drawCube((x+xShift), y, (-300 +depth), 1.0f, cubeColour, offset);
        drawCube((x-xShift), y, (-300 +depth), 1.0f, cubeColour, offset);
        xShift -= gap;
    }
}


And the drawCube code is:

void drawCube(float x, float y, float z, float opacity, float col[], float offset)
{
    //Draw cube taking into account any offset caused by simulated vibration
    glTranslatef((-x+offset), (-y+offset), -z);
    glColor4f(col[0], col[1], col[2], opacity);
    glutWireCube(20);
    glTranslatef((x-offset), (y-offset), z);

}


I'm assuming that I need to use a timer that raises the y value every N seconds so the cube appears to jump but am unsure how to do this?
Basically every N seconds I want the row of cubes to rise and fall in a quick sequence, so it looks like they bounce. So i guess I want them to rise 10.0f or so on the y-axis then fall back down to their original position. I want this action to be triggered every N seconds.
Thanks
Posted

1 solution

You are trying to create an animation. You have not been sufficiently detailed in your explanation of "jump" so I will be general.

To get the OpenGL code working you can put one or more sleep()s in your main loop to create a delay.
When you are happy with your vizualization then create a waitable timer.

See this article: Timers Tutorial[^]

You can wait on this timer in your main thread which will prevent the thread from doing anything else or you can create another (worker) thread which is blocking on the timer event.

As you want the cubes to jump up and then return to original position there needs to be a delay beteween up and down (if not you won't see much more than flicker). If this is the same delay as that between the ups (periodic vibration) then thats easy - just use a state variable to keep track of position.

If not you will need a second timing mechanism.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900