Jump to content
  • Advertisement
Sign in to follow this  
APC

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!