12,406,342 members (29,690 online)
Rate this:
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 22-Apr-13 8:53am

Rate this:

## 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.

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.
v2

Top Experts
Last 24hrsThis month
 OriginalGriff 270 Karthik Bangalore 204 Richard MacCutchan 115 BillWoodruff 75 Afzaal Ahmad Zeeshan 75
 OriginalGriff 7,947 Karthik Bangalore 3,521 ppolymorphe 3,384 Richard MacCutchan 2,577 F-ES Sitecore 2,300