Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jumping ball physics in java, gravity

I've made a simple code to get jumping ball positions, but I definitely missed something, because I get this: http://i60.tinypic.com/2guxibn.png

Here's the code for getting x and y positions:

public Vector2f[] draw() {
    float x = 0, y = height; // height - float value from class constructor;
    ArrayList<Vector2f> graphic = new ArrayList<Vector2f>();
    for(;;){
        Vector2f a = new Vector2f(x, y);
        graphic.add(a);
        ySpeed -= 10;
        y += ySpeed*Math.cos(angle);
        x += xSpeed*Math.sin(angle);
        if(y <= 0){
            // float coef = -10 * y / ySpeed;
            // ySpeed = ((ySpeed + coef) * (-1) ) * bouncyness;
            ySpeed = (ySpeed * (-1)) * bouncyness;
            System.out.println(ySpeed + " " + y);
            y = 0;
        }
        if(x > Main.width)
            break;
    }
    Vector2f[] graphicArray = new Vector2f[graphic.size()];
    for (int i = 0; i < graphic.size(); i++) {
        graphicArray[i] = graphic.get(i);
    }
    return graphicArray;
}
like image 735
Fa2m Avatar asked Nov 01 '22 11:11

Fa2m


1 Answers

On its iteration y gets lower than the X axis on the first run. Then being zeroed,
So max height that you will get in that iteration is lower than original height.
The same happens later,
Until y will get to 0 without being set to it ( I think it will always converge to it ).
If you will set your height to be divided by 10 it should look alright.

For the bouncing case change if ( y <= 0) to if ( y<= 10 ) and remove y = 0 statement.
The correct situation ( not bouncing ), set y = Math.abs(y)

like image 60
shevski Avatar answered Nov 15 '22 04:11

shevski