I’m writing my own physics engine in unity as part of a university assignment. I’m tasked to simulate the movement of a pool ball in a pool table where it moves and hits a wall then bounces. I implemented by writing an AABB class and whenever the ball collides with any of the wall I reflect the velocity vector. However for some reason the ball remains stuck to the wall. When I tried to debug the issue apparently the calculations are so fast that the balls keeps on reflecting the velocity vector making it stationary. Is there any way to fix this?
private bool HitWall()
{
AABB collider = gameObject.GetComponent<AABB>();
for (int i = 0; i < walls.Length; i++)
{
if (collider.IsColliding(gameObject, walls[i]))
return true;
}
return false;
}
private void WallReflection()
{
if (HitWall())
{
Vector3D newVelocity = Vector3D.Reflection(velocity, new Vector3D(-1, 0, 0));
velocity = newVelocity;
CalculatePosition();
}
}