0
\$\begingroup\$

I've been working on a pong clone, i've made games in the past but they have all been very hacky. This time I was trying to go for a better approach in terms of using OOP or patterns. So far the games not posed many issues, I am however having problems thinking about the collision system and ball physics. In my main loop the collision code is as follows:

if (ball->Y < 0)
{
    ball->direction = -ball->direction;

    if (ball->ballSpeed < 700)
    {
        ball->ballSpeed += 25;
    }
}

if ((ball->X + ball->width > player2->X)
    && (ball->X + ball->velocityX) + (ball->width) < player2->X + player2->width
    && (ball->Y + ball->velocityY) + (ball->height) > player2->Y
    && (ball->Y + ball->velocityY) + (ball->height) < player2->Y + player2->height)
{
    ball->direction -= 70;

    if (ball->ballSpeed < 700)
    {
        ball->ballSpeed += 25;
    }
}

if ((ball->X + ball->width) > player1->X
    && (ball->X + ball->velocityX) + (ball->width) < player1->X + player1->width
    && (ball->Y + ball->velocityY) + (ball->height) > player1->Y
    && (ball->Y + ball->velocityY) + (ball->height) < player1->Y + player1->height)
{
    ball->direction += 70;
    if (ball->ballSpeed < 700)
    {
        ball->ballSpeed += 25;
    }
}

The program checks the points of the objects rect's to see if they have passed any of the paddles and then applies changes to the balls public variables. Here is the ball update code:

if(move)
{
velocityX = int(cos((direction + 0.0) * M_PI / 180.0) * (ballSpeed *deltaTime));
velocityY = int(sin((direction + 0.0) * M_PI / 180.0) * (ballSpeed *deltaTime));

    rect.x += velocityX;
    rect.y += velocityY;
}

X = rect.x;
Y = rect.y;
width = rect.w;
height = rect.h;

This PARTIALLY works however the ball goes in weird directions sometimes, on top of that the ball can actually go through the bottom portion of the paddle (it seems that from the bottom of the paddle, there is a gap the ball can exactly fit through). Really the physics of the ball just seem weird and not fluid at all. I'm not brilliant at maths so I don't really know of any other way to go about this. Help would be greatly appreciated.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I think your code has two problems: incorrect X-coordinate bounces, and tunneling.

First the X-coordinate bounces. This code:

if (...)
{
    ball->direction -= 70;

    ...
}

is trying to reflect the ball in the opposite X-direction (I think), but all it's going to do is rotate the velocity by a fixed angle. You could fix that by doing the proper arithmetic, but I encourage you to try reimplementing this using vector math instead of using angles. This makes axis-aligned bounces much simpler.

Bouncing:

if (ball->Y < 0)
{
    // ball->direction = -ball->direction;
    ball->velocityY *= -1;
}

if (/* X-coordinate bounces*/)
{
    // ball->direction += 70;
    ball->velocityX *= -1;
}

Motion:

if(move)
{
    // velocityX = int(cos((direction + 0.0) * M_PI / 180.0) * (ballSpeed *deltaTime));
    // velocityY = int(sin((direction + 0.0) * M_PI / 180.0) * (ballSpeed *deltaTime));

    // rect.x += velocityX;
    // rect.y += velocityY;
    rect.x += velocityX * ballSpeed*deltaTime;
    rect.y += velocityY * ballSpeed*deltaTime;
}

As for tunneling, I won't cover too much because it can be a huge topic, with solutions ranging from small hacks (such as small increments, or slowing things down) to complex robust algorithms (search for swept volume). Without seeing your game in action it's hard to say what you can get away with.

\$\endgroup\$
2
  • \$\begingroup\$ I don't understand how this will work. I get that multiplying the coordinates causes them to 'flip' what I don't understand is how you work out the velocity. At the start the ball starts off with a certain amount of X velocity due to it moving right, once it hits a paddle it will only reflect the X coordinate. Y in this case is 0? so no matter how we flip Y it will be 0. \$\endgroup\$
    – Keeko
    Apr 29, 2014 at 10:22
  • \$\begingroup\$ Actually I figured out something that worked, thanks for the assistance. \$\endgroup\$
    – Keeko
    Apr 29, 2014 at 16:19

You must log in to answer this question.

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