I have a JS function which is supposed to calculate the new dx and dy of my 'player' object once it bounces off of another (stationary) circular object. I can find the angle of the player to the 'goal post', but I'm having trouble handling the player once the collision actually happens. I can detect the collision using the distance between the player and the goal post by checking if it is less than or equal to the sum of the radii of the two objects. I can also calculate the angle at which the player comes into the collision using the player's dx and dy. I know that the angle of incidence should be equal to the angle of reflection, however I cannot seem to get it right.
My code is below:
var side_one = goal_posts[0].x - player.x;
var side_two = goal_posts[0].y - player.y;
var hypotenuse_player_to_post = Math.sqrt(Math.pow(side_one, 2) + Math.pow(side_two, 2));
var angle_player_to_post = Math.acos(side_two / hypotenuse_player_to_post) * (180 / Math.PI);
var player_travel_speed = Math.sqrt(Math.pow(player.dx, 2) + Math.pow(player.dy, 2));
var player_travel_angle = Math.acos(player.dy / player_travel_speed) * (180 / Math.PI);
var difference = angle_player_to_post - player_travel_angle;
var resulting_angle = angle_player_to_post + difference;
resulting_angle = resulting_angle / (180 / Math.PI);
player.dy = Math.cos(resulting_angle) * player_travel_speed;
player.dx = Math.sin(resulting_angle) * player_travel_speed;
Any help is much appreciated! :)
As suggested, the problem was solved using vectors instead of trigonometry.