Physics based Air Hockey
Java JDK 15 is required to run this program
Trig was used to calculate angles of deflection. Ex:
//Code in Player Class
double angle = Math.atan2(dify, difx);
int newX = (int) (Math.cos(angle) * distance) + x;
int newY = (int) (Math.sin(angle) * distance) + y;
Momentum was numericaly calculated for collisions in a previous implementaion. A proporianal relationship is now set to enable more accurate preditions.
//Code in Player Class
double compnt;
double top = 2*(m1 * v1) - (m1 * v2) + (m2 * v2);
double bottom = m1 + m2;
compnt = top / bottom;
//Code in Player Class
double angle = Math.atan2(dify, difx);
tempObject.velX = (int) (Math.cos(angle) * totalVel);
tempObject.velY = (int) (Math.sin(angle) * totalVel);
Calculations for speed of puck after a hit. Ex:
x = x + (int) ((velX * timePassed) + (.5 * accX * Math.pow(timePassed, 2)));
velX = velX + (accX * timePassed);
y = y + (int) ((velY * timePassed) + (.5 * accY * Math.pow(timePassed, 2)));
velY = velY + (accY * timePassed);
Other implementaions of physics can be seen throughout the source code
This AI is very basic and just moves towards the ball. To balance this, I made the AI overpowered by making it super fast and allowing it to basically prematurely end the game by slamming the puck through the wall.