Does anyone know why when my sprite intersects with a border it doesn't rebound in the correct direction it just stops
THE PROBLEM : The problem i have is that when the ball hits the top border it just stops, if i change the values sometimes it stops or just carries on going past the border. I need it so the ball bounces and rebounds off them so the ball is still in play
Everything i try just doesn't work
Can anyone help?
I need the ball to rebound off the borders
The ball:
sf::Vector2u basize = this->_wndw.getSize();
this->_ball.setTexture(this->_sprite_res.get(71));
this->_ball_data.startpos = this->_player.getPosition() + sf::Vector2f(-8,-40);
this->_ball.setPosition(this->_ball_data.startpos);
this->_ball.setScale(sf::Vector2f(0.5f,0.5f));
LJMUGame::setOriginCentre(this->_ball);
Borders:
this->_top_edge = sf::FloatRect(50.0f,128.0f,tscreenx,10.0f);
this->_btm_edge = sf::FloatRect(0.0f,tscreeny,tscreenx,10.0f);
this->_left_edge = sf::FloatRect(-10.0f,0.0f,10.0f,tscreeny);
this->_right_edge = sf::FloatRect(tscreenx,0.0f,10.0f,tscreeny);
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;
}
};
Update ball:(what i've done so far, fails)
//-------UPDATE THE BALL-----------------------------------------
if(this->_ball_data.active)
{
sf::Vector2f balldirection = this->_ball_data.directionup * this->_ball_data.speed * ptpf.asSeconds();
this->_ball.move(balldirection);
//-------UPDATE THE BALL COLLISIONS TOP EDGE------------------------------
if(this->_ball.getGlobalBounds().intersects(this->_top_edge))
{
sf::Vector2f balldirection = this->_ball_data.directiondown * this->_ball_data.speed * ptpf.asSeconds();
this->_game_data.score += 10;
this->_ball.move(balldirection);
}