# OpenGL falling object

This topic is 4495 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to create some artificial gravity using opengl. I made a simple cube that starts at some height 100. When the program runs its supposed to cause the cube to accelerate until the cube is at height zero. Once it hits the ground (at zero) i reversed the direction of the velocity and so it bounces back up till it stops at 100 again and repeates the process. I got the first part working but once it hits the ground it stays there and proceeds to do nothing. Can someone shed some light on why this may be happening?

//vi=velocity initial, vf=velocity final, zf=z-position final
//zi=z-position initial, g=0.98=gravity, t=time
// if we're moving in the -z direction, decrement the z position

if (direction)
{
zf = zi + vi*t - g*t*t/2.0;
t = t + 0.01;
vf = vi - g*t;

}
else  // we're moving in the +z direction, increment the z position
{
vf = vi - g*t;
zf = zi + vi*t - g*t*t/2.0;
t = t + 0.01;

}
if (zf >= 100.0)
direction = true;

else if (zf <= 0.0)
{
direction = false;
t = 0.0;
vi = -vf;
}


I also have zi = zf; vi = vf; after the picture is rendered. Any helpfull comments would be greatly appreciated. [looksaround]

##### Share on other sites
//this is the general solution for all times so you do not need to set//zi = zf;//vi = vf;//and if you do it you have to set t=0.01 too...//for both directions:vf = vi - g*t;zf = zi + vi*t - g*t*t/2.0;t = t + 0.01;//if you need to know the direction: direction = vi<0 ? true : false;if(zf<0){    vi = -vf;    zi =  0.0;    t  =  0;}

zi = zf;
vi = vf;

[Edited by - Kambiz on March 27, 2006 8:52:33 AM]

##### Share on other sites
Thanks it works great. [wow]

1. 1
2. 2
Rutin
22
3. 3
JoeJ
18
4. 4
5. 5

• 15
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631724
• Total Posts
3001903
×