-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollision.h
More file actions
46 lines (34 loc) · 1.09 KB
/
Copy pathcollision.h
File metadata and controls
46 lines (34 loc) · 1.09 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
#include <cmath>
// Particle structure
struct Particle {
double x, y; // position
double vx, vy; // velocity
double mass;
double radius;
};
// Compute distance between two particles
inline double distance(const Particle &a, const Particle &b) {
return std::sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));
}
// Resolve elastic collision between two particles
inline void resolveCollision(Particle &a, Particle &b) {
double dx = b.x - a.x;
double dy = b.y - a.y;
double dist = std::sqrt(dx*dx + dy*dy);
if (dist == 0) return; // avoid division by zero
double nx = dx / dist;
double ny = dy / dist;
double dvx = a.vx - b.vx;
double dvy = a.vy - b.vy;
double relVel = dvx*nx + dvy*ny;
if (relVel > 0) return; // moving apart
double e = 1.0; // restitution (1 = perfectly elastic)
double j = -(1 + e) * relVel;
j /= (1/a.mass + 1/b.mass);
double impulseX = j * nx;
double impulseY = j * ny;
a.vx += impulseX / a.mass;
a.vy += impulseY / a.mass;
b.vx -= impulseX / b.mass;
b.vy -= impulseY / b.mass;
}