I've made a simple code to get jumping ball positions, but I definitely missed something, because I get this:
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;
}
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With