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.