How do I apply gravity to my bouncing ball applica

2019-02-02 02:25发布

I've written a fairly simple java application that allows you to drag your mouse and based on the length of the mouse drag you did, it will shoot a ball in that direction, bouncing off walls as it goes.

Here is a quick screenshot:
alt text http://img222.imageshack.us/img222/3179/ballbouncemf9.png

Each one of the circles on the screen is a Ball object. The balls movement is broken down into an x and y vector;

public class Ball {
    public int xPos;
    public int yPos;
    public int xVector;
    public int yVector;

    public Ball(int xPos, int yPos, int xVector, int yVector) {
        this.xPos = xPos;
        this.yPos = yPos;
        this.xVector = xVector;
        this.yVector = yVector;
    }

    public void step()
    {
        posX += xVector;
        posY += yVector;

        checkCollisions();
    }

    public void checkCollisions()
    {
        // Check if we have collided with a wall
        // If we have, take the negative of the appropriate vector
        // Depending on which wall you hit
    }

    public void draw()
    {
        // draw our circle at it's position
    }
}

This works great. All the balls bounce around and around from wall to wall.

However, I have decided that I want to be able to include the effects of gravity. I know that objects accelerate toward the earth at 9.8m/s but I don't directly know how this should translate into code. I realize that the yVector will be affected but my experimentation with this didn't have the desired effect I wanted.

Ideally, I would like to be able to add some gravity effect to this program and also allow the balls to bounce a few times before settling to the "ground."

How can I create this bouncing-elastic, gravity effect? How must I manipulate the speed vectors of the ball on each step? What must be done when it hits the "ground" so that I can allow it to bounce up again, but somewhat shorter then the previous time?

Any help is appreciated in pointing me in the right direction.


Thanks you for the comments everyone! It already is working great!

In my step() I am adding a gravity constant to my yVector like people suggested and this is my checkCollision():

public void checkCollision()
{
    if (posX - radius < 0)              // Left Wall?
    {
        posX = radius;              // Place ball against edge
        xVector = -(xVector * friction);
    }
    else if (posX + radius > rightBound) // Right Wall?
    {
        posX = rightBound - radius;     // Place ball against edge
        xVector = -(xVector * friction);
    }

    // Same for posY and yVector here.
}

However, the balls will continue to slide around/roll on the floor. I assume this is because I am simply taking a percentage (90%) of their vectors each bounce and it is never truly zero. Should I add in a check that if the xVector becomes a certain absolute value I should just change it to zero?

9条回答
迷人小祖宗
2楼-- · 2019-02-02 02:54

When the balls are all rolling around on the ground, yes, check to see if the velocity is below a certain minimum value and, if so, set it to zero. If you look at the physics behind this type of idealized motion and compare with what happens in the real world, you'll see that a single equation cannot be used to account for the fact that a real ball stops moving.

BTW, what you're doing is called the Euler method for numerical integration. It goes like this:

  • Start with the kinematic equations of motion:
    x(t) = x0 + vx*t + 0.5*axt^2
    y(t) = y0 + vy
    t + 0.5*ayt^2
    vx(t) = vx0 + ax
    t
    vy(t) = vy0 + ay*t
    Where x and y are position, vx and vy are velocity, ax and ay are acceleration, and t is time. x0, y0, vx0, and vy0 are the initial values. This describes the motion of an object in the absence of any outside force.

  • Now apply gravity:
    ay = -9.8 m/s^2
    To this point, there's no need to do anything tricky. We can solve for the position of each ball using this equation for any time.

  • Now add air friction: Since it's a spherical ball, we can assume it has a coefficient of friction c. There are typically two choices for how to model the air friction. It can be proportional to the velocity or to the square of velocity. Let's use the square:
    ax = -cvx^2
    ay = -c
    vy^2 - 9.8
    Because the acceleration is now dependent on the velocity, which is not constant, we must integrate. This is bad, because there's no way to solve this by hand. We'll have to integrate numerically.

  • We take discrete time steps, dt. For Euler's method, we simply replace all occurances of t in the above equations with dt, and use the value from the previous timestep in place of the initial values, x0, y0, etc. So now our equations look like this (in pseudocode):

    // Save previous values
    xold = x;
    yold = y;
    vxold = vx;
    vyold = vy;

    // Update acceleration
    ax = -cvxold^2;
    ay = -c
    vyold^2 - 9.8;

    // Update velocity
    vx = vxold + axdt;
    vy = vyold + ay
    dt;

    // Update position
    x = xold + vxold*dt + 0.5*axdt^2;
    y = yold + vyold
    dt + 0.5*ay*dt^2;

This is an approximation, so it won't be exactly correct, but it'll look OK. The problem is that for bigger timesteps, the error increases, so if we want to accurately model how a real ball would move, we'd have to use very tiny values for dt, which would cause problems with accuracy on a computer. To solve that, there are more complicated techniques. But if you just want to see behavior that looks like gravity and friction at the same time, then Euler's method is ok.

查看更多
Evening l夕情丶
3楼-- · 2019-02-02 03:02

You really want to simulate what gravity does - all it does is create force that acts over time to change the velocity of an object. Every time you take a step, you change the velocity of your ball a little bit in order to "pull" it towards the bottom of the widget.

In order to deal with the no-friction / bouncing ball settles issue, you need to make the "ground" collision exert a different effect than just strict reflection - it should remove some amount of energy from the ball, making it bounce back at a smaller velocity after it hits the ground than it would otherwise.

Another thing that you generally want to do in these types of bouncy visualizations is give the ground some sideways friction as well, so that when it's hitting the ground all the time, it will eventually roll to a stop.

查看更多
疯言疯语
4楼-- · 2019-02-02 03:03

Every time slice you have to apply the effects of gravity by accelerating the ball in teh y downwards direction. As Bill K suggested, that's as simple as making a subtraction from your "yVector". When the ball hits the bottom, yVector = -yVector, so now it's moving upwards but still accelarating downwards. If you want to make the balls eventually stop bouncing, you need to make the collisions slightly inelastic, basically by removing some speed in the y-up direction, possibly by instead of "yVector = -yVector", make it "yVector = -0.9 * yVector".

查看更多
The star\"
5楼-- · 2019-02-02 03:05

What you want to do is change the values of xVector and yVector to simulate gravity and friction. This is really pretty simple to do. (Need to change all of your variables to floats. When it comes time to draw, just round the floats.)

In your step function, after updating the ball's position, you should do something like this:

yVector *= 0.95;
xVector *= 0.95;
yVector -= 2.0;

This scales the X and Y speed down slightly, allowing your balls to eventually stop moving, and then applies a constant downward "acceleration" to the Y value, which will accumulate faster than the "slowdown" and cause the balls to fall.

This is an approximation of what you really want to do. What you really want is to keep a vector representing the acceleration of your balls. Every step you would then dot product that vector with a constant gravity vector to slightly change the ball's acceleration. But I think that my be more complex than you want to get, unless you're looking for a more realistic physics simulation.

查看更多
时光不老,我们不散
6楼-- · 2019-02-02 03:06

It's a ballistic movement. So you got a linear movement on x-axis and an uniform accelerated movement on y-axis.

The basic idea is that the y-axis will follow the equation:

y = y0 + v0 * t + (0.5)*a*t^2

Or, in C code, for example:

float speed = 10.0f, acceleration = -9.8f, y = [whatever position];

y += speed*t + 0.5f*acceleration*t^2;

Where here I use tiem parametrization. But you could use Torricelli:

v = sqrt(v0^2 + 2*acceleration*(y-y0));

And, on this model, you must maintain the last values of v and y.

Finally, I've done something similar using the first model with dt (time's differential) being fixed at 1/60 second (60 FPS).

Well, both models give good real-like results, but sqrt(), for example, is expensive.

查看更多
倾城 Initia
7楼-- · 2019-02-02 03:08
public void step()
{
    posX += xVector;
    posY += yVector;

    yVector += g //some constant representing 9.8

    checkCollisions();
}

in checkCollisions(), you should invert and multiply yVector by a number between 0 and 1 when it bounces on the ground. This should give you the desired effect

查看更多
登录 后发表回答