I tried creating a simple physics based "game" where you can spawn balls and they then get affected by different natural force, like gravity.
But I have a problem, when the ball reaches the floor, and it keeps getting affected by gravity. It kind of vibrates. Here's a quick example of how it looks.
And here is the code for collision detection and the gravity calculation.
public void CheckBounds(int maxHeight, int maxWidth)
{
if (this.position.Y + (radius + radius) > maxHeight)
this.velocity.Y = -Math.Abs(velocity.Y * 0.65f); // 0.65 is the "bouncyness"
}
Here is the main Update section
for (int i = 0; i < balls.Count; i++ )
{
Ball b = balls[i];
for (int k = 0; k < balls.Count; k++) // check for collision with other balls
{
if (balls[i] == balls[k])
continue;
if (balls[i].Colliding(balls[k]))
balls[i].Collide(balls[k]);
}
b.velocity.Y += gravitationalPull; // a gravity constant
b.CheckBounds();
b.position = Vector2.Add(b.position, b.velocity);
}
Does anyone know how to fix the vibration? I tried setting a minimum value of velocity that the gravity would be calculated (if velocity.y < some value then dont calculate), but then the ball stopped mid air at collisions and even at spawn.