Changing sprite direction with sfml! HELP

Does anyone know why when my sprite intersects with a border it doesn't rebound in the correct direction it just stops

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  	//-------UPDATE THE BALL-----------------------------------------
	if(this->_ball_data.active)
	{
	sf::Vector2f balldirection = this->_ball_data.directionup * ptpf.asSeconds();
	this->_ball.move(balldirection);
	

	//-------UPDATE THE BALL COLLISIONS BOTTOM EDGE------------------------------
	
	if(this->_ball.getGlobalBounds().intersects(this->_top_edge))
	{
	sf::Vector2f balldirectiondown = this->_ball_data.directiondown * this->_ball_data.speed * ptpf.asSeconds() - balldirection;
	this->_game_data.score += 10;
	this->_ball.move(balldirectiondown);
	}
	}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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,200.0f);
		directionup = sf::Vector2f(0.0f,-200.0f);
		directionleft = sf::Vector2f(200.0f,0.0f);
		directionright = sf::Vector2f(-200.0f,0.0f);
		startpos = sf::Vector2f(0.0f,0.0f);
		speed    = 200.0f;
		active   = false;
	}
};
bump
The ball update code always seem to use the directionup vector to move the ball up.
I made a breakout clone a while back. My ball object had a single trajectory object (x,y) and a speed multiplier, and if I hit a boundary I would just invert the trajectory specific to the boundary orientation (If I hit the left or right wall, I would invert the x value in the trajectory and if I hit the top wall or the paddle I would invert the y value in the trajectory.

Ball going up and to the right trajectory: (1,1)
then after hitting top wall (1,-1)
then after hitting left wall (-1,-1)
then bouncing off paddle at bottom (-1,1)

and so on...
Last edited on
Could you give me an example Texan,

How would i make the ball change direction when it hits border i've tried everything i can think of or get my head around
This is the function in my Breakout clone that dealt with handling the ball:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
    void game::updateBallPath() // update ball in scene
    {
      if(gameBall->Moving()) // is the ball in motion?
      {
        if(gameBall->collisionBoundaryCheck(LEFT_WALL) || gameBall->collisionBoundaryCheck(RIGHT_WALL)) // have we hit a vertical boundary?
        {
          gameBall->flipX(); // we hit a vertical wall, invert x direction
          return;
        }
        if(gameBall->collisionBoundaryCheck(TOP_WALL)) // have we hit the top boundary?
        {
          gameBall->flipY(); // we hit the roof, invert y direction
          return;
        }
        if(gameBall->collisionBoundaryCheck(gamePaddle->Rect())) // did we hit the paddle?
        {
          if(gameBall->Right() < gamePaddle->Left() + half(gameBall->Width()) && gameBall->VelocityX() > 0.0) gameBall->flipX(); // hit left corner, ball coming right
          if(gameBall->Left() > gamePaddle->Right() - half(gameBall->Width()) && gameBall->VelocityX() < 0.0) gameBall->flipX(); // hit right corner, ball coming left
          gameBall->flipY();
          return;
        }
        if(!gameBall->collisionBoundaryCheck(BRICK_AREA))
        {
          return;
        } else {
          if(brickWall->Remove(gameBall->Rect())) // we hit a brick
          {
            gameBall->flipY(); // invert y trajectory
          }
        }
      }
    }

Note that this code only updates the ball members for trajectory.
This is the collisionBoundaryCheck function:
bool collisionBoundaryCheck(sf::FloatRect b) { return image.getGlobalBounds().intersects(b); }


This in the function inside the ball object that dealt with recalculating the ball position based on trajectory and speed:
1
2
3
4
5
6
7
void ball::Move()
    {
      if(this->Moving())
      {
        this->setPosition(this->Left() + this->VelocityX(),this->Top() + this->VelocityY());
      }
    }


Then in the main game loop, I had a Pulse() function that would:

updateBallPath();
gameBall->Move();
gameBall->Draw();
Last edited on
Topic archived. No new replies allowed.