Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ OpenGL
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 9:53am

1 solution

Rate this: bad
good
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.
  Permalink  
v2

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

  Print Answers RSS
0 Maciej Los 310
1 OriginalGriff 285
2 Afzaal Ahmad Zeeshan 200
3 Sergey Alexandrovich Kryukov 195
4 BillWoodruff 180
0 OriginalGriff 6,499
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,193
3 Manas Bhardwaj 4,657
4 Maciej Los 4,120


Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 23 Apr 2013
Copyright © CodeProject, 1999-2014
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