views:

182

answers:

3
+2  Q: 

Free Fall Question

OK, I'm going to try my best to explain my problem. I have this program where you can select 5 balls. When you select one, you can drag it while you have the mouse button pressed and the cursor is within the ball's radius.

The problem is that I need a way to make the ball go up when the user stop pressing the mouse button, like he sent it to float in the air then made it fall down again. I have one way to know the time, velocity and thus the acceleration, but I don't know how to implement it.

Right now i have this:

void Circle::Fall(float velocity,float time)
{    
    if(this->posY >= 580)
    {
        this->posY = 580;
        this->vfall= 0.0f;
    }
    else if(this->posY < 580)
    {
        //this->distance=9.81f * 0.5f*time*time;
        this->vfall+= velocity;
        this->posY += this->vfall;
    } 
}

With it like this it just falls, and I can't make the effect I tried to explain.

Also, I'm calculating the time like this (just in case it helps):

difX=(x> event.motion.xrel)? x-event.motion.xrel : event.motion.xrel-x;
difY=(y> event.motion.yrel)? y-event.motion.yrel : event.motion.yrel-y;

And I'm using difY as the time variable


OK, sorry, I made it English now. And I'm going to try to make this easier to understand:

You need to make the ball float a little longer with the speed of the mouse at the moment it releases the click, like a hand throwing a ball into the air after taking some impulse. It does not have to make a U-turn or anything just go up a little on Y. I'm using SDL in case you need to know

Also you take the balls from the bottom of the window and when you release the click on them they return them automatically

+2  A: 

I think you need to set an initial velocity upward, by the sounds of it.

If you store the vertical velocity, you can set it to be a positive number. Then every frame, add the velocity to the position, and subtract a little bit from the velocity to make it act like there's gravity.

So have a this->yVelocity (or in your own language ;D) and a this->yPosition, as well as a gravityMagnitude, which could be something like -0.1.

Every frame, do something like this:

this->yVelocity += gravityMagnitude; //Subtracts 0.1
this->yPosition += this->yVelocity; //Make it actually move

This will realistically simulate gravity, since the motion of the ball will mimic a quadratic function.

Also, this way, if you want the ball to float up a bit at first, simply say this as soon as they let go:

this->yVelocity = 2.0; //Or some other positive number

And it will start with a little speed.

Chris Cooper
+1  A: 

This is really stretching my memory back to high school physics. What you need are the suvat formulae, specifically the one for displacement:

           1   2
s = ut  +  - at
           2
  • s is the displacement (from initial position).
  • u is the starting speed.
  • t is the elapsed time.
  • a is the acceleration.

So, given that you're throwing the ball up at (for example) 5 metres per second, and with acceleration being -9.8 metres per second squared (on Earth), its position after one second would be 0.2 metres above the starting height.

Note that you only have to worry about up and down in these equations (gravity) unless your application is going to be so realistic that it takes wind resistance into account.

All you need to do is to store the initial displacement (height) and velocity (vertical component only), then you can calculate the displacement at any given time value.

Keep in mind these equations are for constant acceleration situations. They may hold (mostly) around the Earth but may not be so accommodating where the gravity changes rapidly (like near a black hole).

paxdiablo
+2  A: 

I think you want this book: Physics for Game Developers.

Mike DeSimone
lol ty its going to be a really good book to use
Makenshi