Everything i try just doesn't work
Can anyone help?
I need the ball to rebound off the borders
Structs:
struct BallData
{
sf::Vector2f directionup;
sf::Vector2f directiondown;
sf::Vector2f directionleft;
sf::Vector2f directionright;
sf::Vector2f startpos;
float speed;
bool active;
BallData()
{
directiondown = sf::Vector2f(0.0f,1.0f);
directionup = sf::Vector2f(0.0f,-1.0f);
directionleft = sf::Vector2f(1.0f,0.0f);
directionright = sf::Vector2f(-1.0f,0.0f);
startpos = sf::Vector2f(0.0f,0.0f);
speed = 200.0f;
active = false;
}
};
First off you don't need to store 4 vectors you only need one of them and simply manipulate it to your heart's content.
Like this:
class BallData
{
sf::Vector2f direction;
sf::Vector2f startpos;
float speed;
bool active;
BallData(sf::Vector2f directioninput)
{
direction = directioninput; // Can be given a random direction in several ways.
startpos = sf::Vector2f(0.0f,0.0f);
speed = 200.0f;
active = false;
}
void setDirection(sf::Vector2f directioninput){/**/}
sf::Vector2f getDirection(){/**/}
};
// Some other file............
Pseudo Code
~Hit lower wall or upper wall
Reflect the Y axis
ball.setDirection(sf::Vector2f(ball.getDirection().x,ball.getDirection().y*-1)
~Hit Left or Right Paddle
Reflect the X axis
ball.setDirection(sf::Vector2f(ball.getDirection().x*-1,ball.getDirection().y)
Keep in mind depending on how the playing area is setup this could change a bit.
The general Idea is it is simpler than it looks. Also learning to use the debugger and the breakpoints of it would also help. If I remember right most IDEs will let you click in the margin to set breakpoints.