small refactor

This commit is contained in:
Joseph Aquino 2025-12-19 16:00:48 -05:00
parent 197a3bcfd5
commit 17756834e0
1 changed files with 10 additions and 3 deletions

View File

@ -4,6 +4,7 @@
#include <Random.h> #include <Random.h>
sf::Vector2f getOverlap(const sf::FloatRect first, const sf::FloatRect second) sf::Vector2f getOverlap(const sf::FloatRect first, const sf::FloatRect second)
{ {
const float deltaX = std::abs(second.position.x - first.position.x); const float deltaX = std::abs(second.position.x - first.position.x);
@ -14,18 +15,24 @@ sf::Vector2f getOverlap(const sf::FloatRect first, const sf::FloatRect second)
return {resultX, resultY}; return {resultX, resultY};
} }
enum
{
left,
right
};
sf::Vector2f velocityInRandomDir(const float speed_in) sf::Vector2f velocityInRandomDir(const float speed_in)
{ {
const bool leftOrRight{(bool)Random::get(0,1)}; const int side{Random::get(0,1)};
// get angle within certain ranges so that the ball does not // get angle within certain ranges so that the ball does not
// start moving straight side to side or up and down // start moving straight side to side or up and down
if (leftOrRight) if (side == left)
{// left {// left
const sf::Angle angle = sf::degrees((float)Random::get(220, 255)); const sf::Angle angle = sf::degrees((float)Random::get(220, 255));
return sf::Vector2f{speed_in * std::cos(angle.asRadians()), speed_in * std::sin(angle.asRadians())}; return sf::Vector2f{speed_in * std::cos(angle.asRadians()), speed_in * std::sin(angle.asRadians())};
} }
else if (!leftOrRight) else if (side == right)
{// right {// right
const sf::Angle angle = sf::degrees((float)Random::get(285, 315)); const sf::Angle angle = sf::degrees((float)Random::get(285, 315));
return sf::Vector2f{speed_in * std::cos(angle.asRadians()), speed_in * std::sin(angle.asRadians())}; return sf::Vector2f{speed_in * std::cos(angle.asRadians()), speed_in * std::sin(angle.asRadians())};