-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticle.cpp
More file actions
62 lines (57 loc) · 1.41 KB
/
Particle.cpp
File metadata and controls
62 lines (57 loc) · 1.41 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
#include "Vec3.h"
#include "Particle.h"
// Distance between particles
double Particle::distance(const Particle *other) const {
// d = ||a - b||
return (pos - other->pos).length();
}
// Calculate movement
void Particle::move(double dt) {
// x = 0.5 * a * dt^2 + v0 * dt * x0
// v = a * dt + v0
pos += accel * 0.5 * dt * dt + velocity * dt;
velocity += accel * dt;
// Delay after creation
if ( pos.z > 0 ) {
pos -= Vec3(0, 0, 1000 * dt);
} else {
pos.z = 0;
}
}
// Flexible collision
Vec3 Particle::collideFlex(Particle *other) {
// u1 = ( v1 * ( m1 - m2 ) + 2 * v2 * m2 ) / ( m1 + m2 )
return ( velocity * ( radius - other->radius ) + other->velocity * ( 2 * other->radius ) ) * ( 1 / ( radius + other->radius ) );
}
// Calculate collision
Particle * Particle::collide(Particle *other, bool rec) {
if ( collided(other) ) {
Vec3 new_velocity = collideFlex(other);
Particle *p = 0;
if ( rec ) {
p = other->collide(this, false);
}
velocity = new_velocity;
return p;
}
return 0;
}
// Calculate collision with the frame border
void Particle::collide(int xmin, int ymin, int xmax, int ymax) {
if ( pos.x <= xmin ) {
pos.x = 1;
velocity.x *= -1;
}
if ( pos.x >= xmax ) {
pos.x = xmax - 1;
velocity.x *= -1;
}
if ( pos.y <= ymin ) {
pos.y = 1;
velocity.y *= -1;
}
if ( pos.y >= ymax ) {
pos.y = ymax - 1;
velocity.y *= -1;
}
}