-
Notifications
You must be signed in to change notification settings - Fork 0
/
ElasticBodySimulator.cpp
159 lines (120 loc) · 4.11 KB
/
ElasticBodySimulator.cpp
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
#include <math.h>
#include "ElasticBodySimulator.h"
#include <stdio.h>
using namespace Pirogronian;
ElasticBodySimulator::ElasticBodySimulator(int x, int y, int z, double granMass, double linkMass, double couple, double space, double initspace, double time) {
_couple = couple;
_timeSlice = time;
_granMass = granMass;
_linkMass = linkMass;
_eqspace = space;
_xSize = x;
_ySize = y;
_zSize = z;
// printf("_gMatrix.size: %d\n", _gMatrix.size());
for (int i = 0; i < x; i++) {
_gMatrix.append(QVector<QVector<Granule> >());
for (int j = 0; j < y; j++) {
_gMatrix[i].append(QVector<Granule>(z));
for (int k = 0; k < z; k++) {
// printf("%d, %d, %d, _gMatrix.size: %d\n", i, j, k, _gMatrix.size());
ElasticBodySimulator::Granule g;
g.speed.setX(0);
g.speed.setY(0);
g.speed.setZ(0);
g.position.setX(initspace * i);
g.position.setY(initspace * j);
g.position.setZ(initspace * k);
g.prevpos.setX(g.position.x());
g.prevpos.setY(g.position.y());
g.prevpos.setZ(g.position.z());
_gMatrix[i][j][k] = g;
}
}
}
}
ElasticBodySimulator::~ElasticBodySimulator() { }
QVector3D ElasticBodySimulator::forces(ElasticBodySimulator::Granule a, ElasticBodySimulator::Granule b) {
QVector3D dis, fp;
dis = distances(a, b);
qreal f = _couple * (dis.length() - _eqspace) / dis.length();
fp.setX(f * dis.x());
fp.setY(f * dis.y());
fp.setZ(f * dis.z());
return fp;
}
void ElasticBodySimulator::updateForce(int i, int j, int k) {
ElasticBodySimulator::Granule &g = _gMatrix[i][j][k];
//printf("_gMatrix.size: %d\n", _gMatrix.size());
// printf("_gMatrix.size: %d\n", _gMatrix.size());
// printf("_gMatrix[%d].size: %d\n", i+1, _gMatrix[i+1].size());
// printf("_gMatrix[%d][%d].size: %d\n", i+1, j, _gMatrix[i+1][j].size());
// printf("_gMatrix[%d][%d][%d] mass:%f\n", i+1, j, k, _gMatrix[i+1][j][k].mass);
// printf("_lMatrix[%d][%d][%d].mass:%f\n", i, j, k, _lMatrix[i][j][k].mass);
g.force.setX(0);
g.force.setY(0);
g.force.setZ(0);
if (i < _xSize - 1) g.force += forces(g, _gMatrix[i+1][j][k]);
if (j < _ySize - 1) g.force += forces(g, _gMatrix[i][j+1][k]);
if (k < _zSize - 1) g.force += forces(g, _gMatrix[i][j][k+1]);
if (i > 0) {
g.force += forces(g, _gMatrix[i-1][j][k]);
}
if (j > 0) {
g.force += forces(g, _gMatrix[i][j-1][k]);
}
if (k > 0) {
g.force += forces(g, _gMatrix[i][j][k-1]);
}
printf("Granule[%d][%d][%d]: f(%f, %f, %f)\n", i, j, k, g.force.x(), g.force.y(), g.force.z());
}
void ElasticBodySimulator::updatePosition(ElasticBodySimulator::Granule &g) {
QVector3D accel, newspeed, movement, newpos;
accel = g.force / granMass();
newspeed = g.speed + accel * _timeSlice;
movement = (g.speed + newspeed) / 2 * _timeSlice;
newpos = 2 * g.position - g.prevpos + g.force * timeSlice() * timeSlice() / granMass();
// g.position += movement;
g.speed = newspeed;
g.prevpos = g.position;
g.position = newpos;
}
void ElasticBodySimulator::doStep() {
for (int i = 0; i < _xSize; i++)
for (int j = 0; j < _ySize; j++)
for (int k = 0; k < _zSize; k++)
updateForce(i, j, k);
//correctForces();
for (int i = 0; i < _xSize; i++)
for (int j = 0; j < _ySize; j++)
for (int k = 0; k < _zSize; k++)
updatePosition(_gMatrix[i][j][k]);
}
qreal ElasticBodySimulator::calcForces() {
qreal sum = 0;
for (int i = 0; i < _xSize; i++)
for (int j = 0; j < _ySize; j++)
for (int k = 0; k < _zSize; k++)
sum += _gMatrix[i][j][k].force.length();
return sum;
}
void ElasticBodySimulator::sumForces() {
_totForce = calcForces();
}
void ElasticBodySimulator::correctForces() {
qreal a = (calcForces() - _totForce) / (_xSize * _ySize * _zSize);
qreal l, lp, cosx, cosy, cosz;
for (int i = 0; i < _xSize; i++)
for (int j = 0; j < _ySize; j++)
for (int k = 0; k < _zSize; k++) {
QVector3D f = _gMatrix[i][j][k].force;
lp = f.length() - a;
cosx = f.x() / f.length();
cosy = f.y() / f.length();
cosz = f.z() / f.length();
f.setX(cosx * lp);
f.setY(cosy * lp);
f.setZ(cosz * lp);
_gMatrix[i][j][k].force = f;
}
}