Implementing Circle Physics in Java
- by Shijima
I am working on a simple physics based game where 2 balls bounce off each other. I am following a tutorial, 2-Dimensional Elastic Collisions Without Trigonometry, for the collision reactions. I am using Vector2 from the LIBGDX library to handle vectors. I am a bit confused on how to implement step 6 in Java from the tutorial.
Below is my current code, please note that the code strictly follows the tutorial and there are redundant pieces of code which I plan to refactor later.
Note: refrences to this refer to ball 1, and ball refers to ball 2.
/*
* Step 1
*
* Find the Normal, Unit Normal and Unit Tangential vectors
*/
Vector2 n = new Vector2(this.position[0] - ball.position[0], this.position[1] - ball.position[1]);
Vector2 un = n.normalize();
Vector2 ut = new Vector2(-un.y, un.x);
/*
* Step 2
*
* Create the initial (before collision) velocity vectors
*/
Vector2 v1 = this.velocity;
Vector2 v2 = ball.velocity;
/*
* Step 3
*
* Resolve the velocity vectors into normal and tangential components
*/
float v1n = un.dot(v1);
float v1t = ut.dot(v1);
float v2n = un.dot(v2);
float v2t = ut.dot(v2);
/*
* Step 4
*
* Find the new tangential Velocities after collision
*/
float v1tPrime = v1t;
float v2tPrime = v2t;
/*
* Step 5
*
* Find the new normal velocities
*/
float v1nPrime = v1n * (this.mass - ball.mass) + (2 * ball.mass * v2n) / (this.mass + ball.mass);
float v2nPrime = v2n * (ball.mass - this.mass) + (2 * this.mass * v1n) / (this.mass + ball.mass);
/*
* Step 6
*
* Convert the scalar normal and tangential velocities into vectors???
*/