forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElasticCollision2D.java
More file actions
73 lines (60 loc) · 1.8 KB
/
ElasticCollision2D.java
File metadata and controls
73 lines (60 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package com.thealgorithms.physics;
/**
* 2D Elastic collision between two circular bodies
* Based on principles of conservation of momentum and kinetic energy.
*
* @author [Yash Rajput](https://github.com/the-yash-rajput)
*/
public final class ElasticCollision2D {
private ElasticCollision2D() {
throw new AssertionError("No instances. Utility class");
}
public static class Body {
public double x;
public double y;
public double vx;
public double vy;
public double mass;
public double radius;
public Body(double x, double y, double vx, double vy, double mass, double radius) {
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
this.mass = mass;
this.radius = radius;
}
}
/**
* Resolve instantaneous elastic collision between two circular bodies.
*
* @param a first body
* @param b second body
*/
public static void resolveCollision(Body a, Body b) {
double dx = b.x - a.x;
double dy = b.y - a.y;
double dist = Math.hypot(dx, dy);
if (dist == 0) {
return; // overlapping
}
double nx = dx / dist;
double ny = dy / dist;
// relative velocity along normal
double rv = (b.vx - a.vx) * nx + (b.vy - a.vy) * ny;
if (rv > 0) {
return; // moving apart
}
// impulse with masses
double m1 = a.mass;
double m2 = b.mass;
double j = -(1 + 1.0) * rv / (1.0 / m1 + 1.0 / m2);
// impulse vector
double impulseX = j * nx;
double impulseY = j * ny;
a.vx -= impulseX / m1;
a.vy -= impulseY / m1;
b.vx += impulseX / m2;
b.vy += impulseY / m2;
}
}