falling object

Started by
1 comment, last by APC 18 years ago
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]
Advertisement
//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;}


and delete this line from your code(or add t=0.01;):
zi = zf;
vi = vf;

[Edited by - Kambiz on March 27, 2006 8:52:33 AM]
Thanks it works great. [wow]

This topic is closed to new replies.

Advertisement