Skip to content

SethuEapen/2D-Air-Hockey

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 

Repository files navigation

2D-Air-Hockey

Physics based Air Hockey

Requirements

Java JDK 15 is required to run this program

Physics

Trigonometry

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;

Transfer Of Momentum

Momentum was numericaly calculated for collisions in a previous implementaion. A proporianal relationship is now set to enable more accurate preditions.

Old Implementation:

//Code in Player Class
double compnt;
		
double top = 2*(m1 * v1) - (m1 * v2) + (m2 * v2);
double bottom = m1 + m2;
		
compnt = top / bottom;

New Implementation:

//Code in Player Class
double angle = Math.atan2(dify, difx);
tempObject.velX = (int) (Math.cos(angle) * totalVel);
tempObject.velY = (int) (Math.sin(angle) * totalVel);

Kinematics

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);

Misc.

Other implementaions of physics can be seen throughout the source code

AI

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.

Special Thanks

to RealTutsGML for Java game tutorials that helped with the strucure of this code.

and Mr. Pederson for being an amazing teacher! Im going to miss you next year!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages