+9  A: 

What you have to do is constantly subtract a small constant (something that represents your 9.8 m/s) from your yVector. When the ball is going down (yVector is already negative), this would make it go faster. When it's going up (yVector is positive) it would slow it down.

This would not account for friction, so the things should bounce pretty much for ever.

edit1: To account for friction, whenever it reverses (and you reverse the sign), lower the absolute number a little. Like if it hits at yVector=-500, when you reverse the sign, make it +480 instead of +500. You should probably do the same thing to xVector to stop it from bouncing side-to-side.

edit2: Also, if you want it to react to "air friction", reduce both vectors by a very small amount every adjustment.

edit3: About the thing rolling around on the bottom forever--Depending on how high your numbers are, it could be one of two things. Either your numbers are large and it just seems to take forever to finish, or you are rounding and your Vectors are always 5 or something. (90% of 5 is 4.5, so it may round up to 5).

I'd print out a debug statement and see what the Vector numbers are like. If they go to somewhere around 5 and just stay there, then you can use a function that truncates your fraction to 4 instead of rounding back to 5. If it keeps on going down and eventually stops, then you might have to raise your friction coefficient.

If you can't find an easy "rounding" function, you could use (0.9 * Vector) - 1, subtracting 1 from your existing equation should do the same thing.

Bill K
@Bill K, could you take a look at my edit and comment on it?
Simucal
@Bill K, thanks Bill!
Simucal
+1  A: 

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".

Paul Tomblin
You need to lose velocity in both dimensions with every bounce, not just vertically with the floor.
erickson
@erickson, was just going to ask that, thank you!
Simucal
+1  A: 
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

torb
A: 

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.

jamuraa
What do you mean by giving the ground some sideways friction as well?
Simucal
A: 

I agree with what "Bill K" said, and would add that if you want them to "settle" you will need to reduce the x and y vectors over time (apply resistance). This will have to be a very small amount at a time, so you may have to change your vectors from int to a floating point type, or only reduce them by 1 every few seconds.

Bernhard Hofmann
A: 

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.

SoapBox
By multiplying the yVector in the step function, you are not representing a constant acceleration like gravity.
Paul Tomblin
By slowing the ball down in each step, you are going to make the balls look like they're moving through something extremely viscous. A more realistic effect would be to lose energy only when they bounce.
erickson
A: 

What must be done when it hits the "ground" so that I can allow it to bounce up again

If you assume a perfect collision (ie all the energy is conserved) all you have to do reverse the sign of one of the velocity scalar depending on which wall was hit.

For example if the ball hits the right or left walls revese the x scalar component and leave the the y scalar component the same:

 this.xVector = -this.xVector;

If the ball hits the top or bottom walls reverse the y scalar component and leave the x scalar component the same:

 this.yVector = -this.yVector;

but somewhat shorter then the previous time?

In this scenario some of the energy will be lost in the collision with the wall so just add in a loss factor to take of some of the velocity each time the wall is hit:

 double loss_factor = 0.99;
 this.xVector = -(loss_factor * this.xVector);
 this.yVector = -(loss_factor * this.yVector;
jussij
+3  A: 

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*ax*t^2
    y(t) = y0 + vy*t + 0.5*ay*t^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 = -c*vx^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 = -c*vxold^2;
    ay = -c*vyold^2 - 9.8;

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

    // Update position
    x = xold + vxold*dt + 0.5*ax*dt^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.

Scottie T