0
votes

I am trying to write a simple Physics simulator. However I am facing issues with how the ball bounces. Even though my RESTITUTION is set to 1, the ball bounces a little extra every time it touches the ground.

The Restitution variable:

public static double RESTITUTION=1d;

The code for handling bounce:

if(Y<c.getRadius()){
  VY=Math.abs(VY)*Utils.RESTITUTION;
  Y=c.getRadius();
  c.getCenter().setY(Y);
  c.getV().setY(VY);
  }

All variables use double as data type. Y is the center of the ball. VY is Y-component of velocity.

1
There's probably some violation of conservation of energy that happens. One way to sort that out is to add an invariant check to correct that with each time step. - duffymo
@duffymo The heat is being taken care of by the fan on the CPU, but the sound must be let out via the loudspeaker. ;-) - laune
Thank you for the lesson, @laune. 8) - duffymo
Shouldn't you be reversing the sign of the y-component of velocity? What about x-components? I consider it a bad sign that you aren't using a 2D Vector abstraction. - duffymo
Since the code is for handling the ball hitting the ground, I have used Math.abs(), it's just a test. getCenter(), getV() calls return a Vector2D object and I am setting the Y component of the Vector2D by using Vector2D#setY() setter method. - Madeyedexter

1 Answers

1
votes

If Y < c.getRadius() the ball has already entered the ground by c.getRadius() - Y, and this is added on the way back up.

I think you should keep Y and just invert the speed, etc.

If you bounce at an angle, you might compute the accurate point of impact and correct by that.