-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamics.cpp
More file actions
195 lines (151 loc) · 3.84 KB
/
dynamics.cpp
File metadata and controls
195 lines (151 loc) · 3.84 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "dynamics.h"
#include "controls.h"
#ifdef _WIN32
#include "gl/glut.h"
#endif
#ifdef __linux__
#include <GL/glut.h>
#endif
float jm = 0.02; // jitter magnitude
int rand_sign()
{
if (rand() % 2) return 1;
else return -1;
}
particle::particle(float x, float y, float z)
{
pos = vector(x, y, z);
vel = vector(jm*rand_sign(), 0, jm*rand_sign());
}
void particle::update(float dt, vector g)
{
pos += dt*vel;
vel += dt*g;
}
particle_system::particle_system()
{
ready = 0;
}
void particle_system::store_frame()
{
std::vector<vector> current_positions;
// store frame
for(int i = 0; i < particles.size(); i++)
current_positions.push_back(particles[i].pos);
frames.push_back(current_positions);
}
particle_system::particle_system(vmodel vm)
{
current_frame = 0;
radius = vm.vsize/2;
// convert voxels to particles
for (int i = 0; i < vm.x; i++) {
for (int j = 0; j < vm.y; j++) {
for (int k = 0; k < vm.z; k++) {
if (vm.voxels[i][j][k])
particles.push_back(particle(vm.aabb_min.x + i*vm.vsize,
vm.aabb_min.y + j*vm.vsize,
vm.aabb_min.z + k*vm.vsize));
}
}
}
this->store_frame();
x_min = 1.1 * vm.aabb_min.x;
y_min = 1.1 * vm.aabb_min.y;
z_min = 1.1 * vm.aabb_min.z;
x_max = 1.1 * vm.aabb_max.x;
y_max = 1.1 * vm.aabb_max.y;
z_max = 1.1 * vm.aabb_max.z;
g = vector(0, -1, 0);
dt = radius / (2*sqrt(2*(y_max - y_min)));
ready = 1;
}
void collision(particle* p1, particle* p2, float r)
{
vector axis = p2->pos - p1->pos; // collision axis
float dist = axis.magn();
if (dist < 2*r) {
axis.normalize();
vector v1 = p1->vel;
vector v2 = p2->vel;
vector v1a, v1n, v2a, v2n;
v1a = (v1 * axis) * axis;
v2a = (v2 * axis) * axis;
// normal part remains the same
v1n = v1 - v1a;
v2n = v2 - v2a;
// assume equal masses for p1, p2 => exchange of velocities
p1->vel = v1n + v2a;
p2->vel = v2n + v1a;
// position adjustment
float adjustment = (2*r - dist)/2;
p1->pos -= adjustment*axis;
p2->pos += adjustment*axis;
}
}
void particle_system::floor_hit(particle* p)
{
vector pos = p->pos;
if (pos.j < y_min + radius) {
p->pos.j = y_min + radius;
p->vel.j = - p->vel.j;
}
}
void particle_system::update(int with_collisions)
{
// move for timestep
for(int i = 0; i < particles.size(); i++)
particles[i].update(dt, g);
// floor hit handling
for(int i = 0; i < particles.size(); i++)
floor_hit(&particles[i]);
if (with_collisions) {
// particle-particle collision handling
for(int i = 0; i < particles.size()-1; i++) {
for(int j = i+1; j < particles.size(); j++) {
collision(&particles[i], &particles[j], radius);
}
}
// floor hit handling again
for(int i = 0; i < particles.size(); i++)
floor_hit(&particles[i]);
}
this->store_frame();
}
void particle_system::draw()
{
if (ready) {
std::vector<vector> frame = frames[current_frame];
for (int i=0; i<frame.size(); i++) {
glPushMatrix();
glTranslatef(frame[i].i, frame[i].j, frame[i].k);
glutSolidSphere(radius, 8, 5);
glPopMatrix();
}
current_frame += frame_step;
// clamp to range 0 .. frames.size()-1
if (current_frame < 0)
current_frame = 0;
if (current_frame > (frames.size()-1))
current_frame = frames.size()-1;
}
}
void particle_system::run(int iterations, int with_collisions)
{
int last_progress_printed = 0;
int current_progress;
for (int i=0; i<iterations; i++) {
this->update(with_collisions);
current_progress = i*100/iterations;
if (current_progress != last_progress_printed &&
! (current_progress % 10)) {
printf("%d...", current_progress);
last_progress_printed = current_progress;
fflush(stdout);
}
}
printf("\nsimulation ready\n\n");
}