Skip to content

Commit fd88f43

Browse files
committed
BallLogic: improved wall colision mechanism.
1 parent ae55e48 commit fd88f43

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

Logic/BallLogic.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,31 @@ public void setVelocity(Vector2 velocity)
5656

5757
public void setPosition()
5858
{
59-
_ball.Position += new Vector2(_ball.Velocity.X * _ball.Speed, _ball.Velocity.Y * _ball.Speed);
60-
if (_ball.Position.X < 0 || _ball.Position.X > _table.Width - _ball.Radius)
61-
_ball.Velocity *= -Vector2.UnitX;
59+
Vector2 newPosition = _ball.Position + new Vector2(_ball.Velocity.X * _ball.Speed, _ball.Velocity.Y * _ball.Speed);
6260

63-
if (_ball.Position.Y < 0 || _ball.Position.Y > _table.Height - _ball.Radius)
64-
_ball.Velocity *= -Vector2.UnitY;
61+
if (newPosition.X < 0)
62+
{
63+
_ball.Velocity = new Vector2(-_ball.Velocity.X, _ball.Velocity.Y);
64+
newPosition.X = 0;
65+
}
66+
else if (newPosition.X > _table.Width - _ball.Radius)
67+
{
68+
_ball.Velocity = new Vector2(-_ball.Velocity.X, _ball.Velocity.Y);
69+
newPosition.X = _table.Width - _ball.Radius;
70+
}
71+
72+
if (newPosition.Y < 0)
73+
{
74+
_ball.Velocity = new Vector2(_ball.Velocity.X, -_ball.Velocity.Y);
75+
newPosition.Y = 0;
76+
}
77+
else if (newPosition.Y > _table.Height - _ball.Radius)
78+
{
79+
_ball.Velocity = new Vector2(_ball.Velocity.X, -_ball.Velocity.Y);
80+
newPosition.Y = _table.Height - _ball.Radius;
81+
}
6582

83+
_ball.Position = newPosition;
6684
RaisePropertyChanged(nameof(X));
6785
RaisePropertyChanged(nameof(Y));
6886
}

Logic/LogicAPI.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public override void RunSimulation()
3232
{
3333
while (true)
3434
{
35-
Thread.Sleep(TimeSpan.FromSeconds(timeTravel));
35+
// Thread.Sleep(TimeSpan.FromSeconds(timeTravel));
3636
try
3737
{
3838
_cancellationToken.ThrowIfCancellationRequested();

0 commit comments

Comments
 (0)