Click here to Skip to main content
13,044,543 members (58,568 online)
Rate this:
Please Sign up or sign in to vote.
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++)
                    //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);
    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.
Posted 22-Apr-13 8:53am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 23 Apr 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100