Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Free Fall Question

Tags:

c++

physics

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

like image 846
Makenshi Avatar asked May 07 '26 05:05

Makenshi


2 Answers

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

like image 196
Mike DeSimone Avatar answered May 09 '26 05:05

Mike DeSimone


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.

like image 35
Chris Cooper Avatar answered May 09 '26 03:05

Chris Cooper