Skip to content

Commit ba03d6f

Browse files
committed
BallLogic: added overlap distance into collision method to prevent balls stuck in each other.
1 parent 39c7c08 commit ba03d6f

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

Logic/BallLogic.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ public void handleBallColission(BallLogic other)
9595
float collisionPulse = 2 * this.Mass * other.Mass * Vector2.Dot(relativeVelocity, collisionNormal) / (this.Mass + other.Mass);
9696
this.Velocity += collisionPulse / this.Mass * collisionNormal;
9797
other.Velocity -= collisionPulse / other.Mass * collisionNormal;
98+
99+
float overlapDistance = (this.Radius / 2 + other.Radius / 2) - Vector2.Distance(this._ball.Position, other._ball.Position);
100+
if (overlapDistance > 0)
101+
{
102+
this._ball.Position -= collisionNormal * overlapDistance / 2;
103+
other._ball.Position += collisionNormal * overlapDistance / 2;
104+
}
98105
}
99106
}
100107
}

0 commit comments

Comments
 (0)