Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SFML 2.0 Getting ball to collide with paddle in a pong game  (Read 2712 times)

0 Members and 1 Guest are viewing this topic.

Rabbel

  • Newbie
  • *
  • Posts: 1
    • View Profile
Hi everybody, first time poster here. I am trying to make a pong type game and I am having trouble with getting the ball that i have bouncing around to collide with the paddle correctly. I have seen a few methods online that I must not be implementing right, the ball with either go through the paddle and bounce back a little later, strange movement etc. If anyone can look at my code and give some suggestions that would be great. Just the snippet of the if statement, xMovement is a float var.

 if(ballSprite.GetPosition().x + ballSprite.GetScale().x + ballSprite.GetPosition().y + ballSprite.GetScale().y <= bluePaddleSprite.GetPosition().x + bluePaddleSprite.GetScale().x + bluePaddleSprite.GetPosition().y + bluePaddleSprite.GetScale().y)
                 {
                         xMovement = -xMovement;
                       
                 }

Haze

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Github Profile
Re: SFML 2.0 Getting ball to collide with paddle in a pong game
« Reply #1 on: June 25, 2012, 05:59:50 pm »
What are you trying to do? You're adding x-axis position, y-axis position, and scale factors, which doesn't make sense.
You can perform a simple collision test using bounding boxes:

1. build bounding boxes for your sprites, using FloatRect:
sf::FloatRect rect;
rect.Top = sprite.GetPosition().y;
rect.Left = sprite.GetPosition().x;
rect.Bottom = rect.Top + sprite.GetSize().y;
rect.Right = rect.Left + sprite.GetSize().x;
 

2. Test if two bounding boxes are overlapping:
if (rect.Intersects(rect2))
{
    // collision
}