Skip to content

Commit e34d654

Browse files
Fixing build issues.
1 parent b598f45 commit e34d654

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

src/main/java/com/thealgorithms/physics/ElasticCollision2D.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
package com.thealgorithms.physics;
22

3+
/**
4+
* 2D Elastic collision between two circular bodies
5+
* Based on principles of conservation of momentum and kinetic energy.
6+
*
7+
* @author [Yash Rajput](https://github.com/the-yash-rajput)
8+
*/
39
public final class ElasticCollision2D {
410

511
private ElasticCollision2D() {
6-
// utility class
12+
throw new AssertionError("No instances. Utility class");
713
}
814

915
public static class Body {
10-
public double x, y;
11-
public double vx, vy;
12-
public double mass, radius;
16+
public double x;
17+
public double y;
18+
public double vx;
19+
public double vy;
20+
public double mass;
21+
public double radius;
1322

1423
public Body(double x, double y, double vx, double vy, double mass, double radius) {
1524
this.x = x;
@@ -31,22 +40,28 @@ public static void resolveCollision(Body a, Body b) {
3140
double dx = b.x - a.x;
3241
double dy = b.y - a.y;
3342
double dist = Math.hypot(dx, dy);
34-
if (dist == 0) return; // overlapping
43+
44+
if (dist == 0) {
45+
return; // overlapping
46+
}
3547

3648
double nx = dx / dist;
3749
double ny = dy / dist;
3850

3951
// relative velocity along normal
4052
double rv = (b.vx - a.vx) * nx + (b.vy - a.vy) * ny;
41-
if (rv > 0) return; // moving apart
4253

43-
// Correct 2D impulse with masses
54+
if (rv > 0) {
55+
return; // moving apart
56+
}
57+
58+
// impulse with masses
4459
double m1 = a.mass;
4560
double m2 = b.mass;
4661

4762
double j = -(1 + 1.0) * rv / (1.0 / m1 + 1.0 / m2);
4863

49-
// impulse vector along normal
64+
// impulse vector
5065
double impulseX = j * nx;
5166
double impulseY = j * ny;
5267

0 commit comments

Comments
 (0)