Skip to content

Commit 39c7c08

Browse files
committed
LogicAPI: changed method of creating balls to prevent generating balls in each other.
1 parent 243d75b commit 39c7c08

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

Logic/BallLogic.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ public float Y
2020
get { return _ball.Position.Y; }
2121
}
2222

23+
public Vector2 Position
24+
{
25+
get { return _ball.Position; }
26+
}
27+
2328
public int Radius
2429
{
2530
get { return _ball.Radius; }

Logic/LogicAPI.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,23 @@ public override void createBalls(int amount, int radius)
8888
Random random = new Random();
8989
for (int i = 0; i < amount; i++)
9090
{
91-
Vector2 position = new Vector2((float)random.Next(0, table.Width - radius), (float)random.Next(0, table.Height - radius));
91+
Vector2 position;
92+
bool collides;
93+
do
94+
{
95+
position = new Vector2((float)random.Next(0, table.Width - radius), (float)random.Next(0, table.Height - radius));
96+
collides = false;
97+
foreach (var existingBall in balls)
98+
{
99+
float distance = Vector2.Distance(position, existingBall.Position);
100+
if (distance < radius + existingBall.Radius)
101+
{
102+
collides = true;
103+
break;
104+
}
105+
}
106+
107+
} while (collides);
92108
BallLogic newBall = createBall(radius, position);
93109
balls.Add(newBall);
94110
}

0 commit comments

Comments
 (0)