Basically I'm creating a pong game for learning purposes and I have come to a point where I need to change direction of my "ball".
My question is, how could I calculate the angle that ball must bounce off when colliding with the defined edge and with platforms?
Right now I have something like this (inside function main):
//ball_dir is randomly generated value between 30 and 150.
//Move ball with a direction
ballx += spd*cosv(ball_dir)*time_step;
bally += spd*sinv(ball_dir)*time_step;
//Collision with "walls"
-------------------------------------
if ((ballx > game_minx & ballx+6 < game_maxx)){ //game_minx = 1/3 of screen_size.x and game_maxx = 2/3 screen_size.x
ball_dir = ball_dir; //Keep the direction
}else{//If out of boundaries
ball_dir = (180 - abs(ball_dir)); //Change the direction
}
//Checking collision with platforms!
if (rect_collision(P_ball.pos_x, P_ball.pos_y, P_ball.size_x, P_ball.size_y, P_ai.pos_x, P_ai.pos_y, P_ai.size_x, P_ai.size_y)
| rect_collision(P_ball.pos_x, P_ball.pos_y, P_ball.size_x, P_ball.size_y, P_player.pos_x, P_player.pos_y, P_player.size_x, P_player.size_y)){
ball_dir = (180 - abs(ball_dir)); //Trying to change dir here.
}
But (180 - dir) doesn't always work. I have also searched the internet for vectors to calculate new vector to move on, but I don't know how to implement them in LiteC or is it even practical in this case.
Any suggestion solving this?