What undefined behaviour do you have?
If it's crashing, consider that you might be dividing by zero. These things happen when vectors might be zero.
Also, when comparing lengths of vectors, you don't actually need to do the costly square root.
For example,
float distanceSquared = dx * dx + dy * dy;
float radiusSum = circle1.getRadius() + circle2.getRadius();
float radiusSumSquared = radiusSum * radiusSum;
if (distanceSquared <= radiusSumSquared)
Squares/multiplying is less costly than square roots.
You can also re-use some of that inline code.
normalize:
return vec / length(vec);
distance:
return length(vec1 - vec2);
Also, if you create a dot function, you can re-use that too (in length and project).
e.g.
length:
return std::sqrt(dot(v));
project:
return (dot(vec1) / dot(vec2)) * vec2;
Note that my suggestion of "Distance Squared" is just the simple dot too.
Also, in addition, you're checking every
i ball with every
i+1 ball so
i should never be allowed to be the last ball...
Try:
for (std::size_t i = 0; i < balls.size() - 1u; i++)