Hello everyone, I am trying to create a bounding box around a Sprite in my simple Pong game. The bounding box needs to be a simple rectangle around the sprite so that it can act as a collision detector between the ball and paddle.

I have already tried to create the bounding box but it's not quite working as expected.

    float current_x = paddle_right->xPos();
    float current_y = paddle_right->yPos();
    float current_x2 = paddle_left->xPos();
    float current_y2 = paddle_left->yPos();

    float width_x = current_x + 16;
    float width_y = current_y + 150;
    float width_x2 = current_x2 + 16;
    float width_y2 = current_y2 + 150;

    if((x_pos > current_x && x_pos < width_x && y_pos > current_y && y_pos < width_y)||
        (x_pos > current_x2 && x_pos < width_x2 && y_pos > current_y2 && y_pos < width_y2))
        dir_vector.setX(dir_vector.getX() * -1);

The above code is the code I have used to create said bounding box, but like I said, it's simply not working correctly (As in the ball will sometimes go through the Sprite or have an abnormal reaction to it such as bouncing the wrong way).

I hope I have explained this well, if not please ask me which part to explain again. Basically, I just want do create a bounding box around a Sprite (in this case, the Paddle) to which then the ball can collide from.

For reference I am using the AwesomeSauce Game Engine.

  • 3
    \$\begingroup\$ Is there a reason you aren't using the appropriate classes for points and rectangles? You shouldn't have to do all of this stuff manually. \$\endgroup\$ Commented Dec 10, 2017 at 6:55

2 Answers 2


Couple of things stand out.

Firstly i'm assuming a bunch of things just by your variable names. Here goes..

if your pong ball travels faster than 16 pixels per update then there is a chance that the ball can pass through the bat. You could verify this by limiting the ball speed.

On collision, it looks like you invert the x component of the ball direction vector. As the collision occurred inside the bat then even after you invert the direction the ball could still end up inside the bat after it is updated if its speed is low enough. To verify you could move the ball outside the bat in the direction it will travel. So if the new ball x direction is positive then place the ball on the right side of the bat. If new direction is negative place the ball on the left side of the bat.


For reference I am using the AwesomeSauce Game Engine.

First is there a particular reason why you aren't using the primitives for a square in that engine? They've undoubtedly solved this problem for you.

It looks like you check if the ball position is within the bounds of either right or left paddle, then reverse the x direction of travel if it hits. With paddles of such small width, you have the potential that the ball will be fast enough that it passes through the paddle (causing it to miss).

Alternatively, if your ball is moving just right, you ball could jump into the paddle upon next movement, you would evaluate the ball colliding on the next check, the ball x reverses like normal, but on the following check the ball might still be inside and it would reverse directions again.

To solve these issues you need to take into account these edge cases:

  1. you can make sure you evaluate the position of the ball before evaluating the change and direction (and immediately change the position again to reflect a collision). The ball will never be stuck inside the paddle this way and bounce out the wrong direction. This will not solve collision phasing however.

  2. To solve the no collision on impact issue, you need to see if the line segment between your starting and final position of the ball intersected with the paddle then change direction.

enter image description here

Evaluating line-segment intersection in both scenarios will also work. You have to evaluate the balls possible next position in both before you figure out the true final position however.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .