Jump physics ball in java, gravity

I made a simple code to get the ball's bouncing position, but I definitely missed something because I get the following: http://i60.tinypic.com/2guxibn.png

Here is the code to get the 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;
}
+4
source share
2 answers

y , X . ,
, max height, , height.
,
y 0, ( , ).
height 10 10, .

if ( y <= 0) if ( y<= 10 ) y = 0.
( ), y = Math.abs(y)

+1

  • , angle. . .
  • , .

    x += v + acc * ∆time * ∆time * 0.5;
    v += acc * ∆time;
    
  • y = -y, y < 0.

∆time - 1, acc - -10, .

+1

Source: https://habr.com/ru/post/1570281/


All Articles