-
Notifications
You must be signed in to change notification settings - Fork 266
/
pfgen.cpp
235 lines (193 loc) · 6.66 KB
/
pfgen.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
* Implementation file for the particle force generators.
*
* Part of the Cyclone physics system.
*
* Copyright (c) Icosagon 2003. All Rights Reserved.
*
* This software is distributed under licence. Use of this software
* implies agreement with all terms and conditions of the accompanying
* software licence.
*/
#include <cyclone/pfgen.h>
using namespace cyclone;
void ParticleForceRegistry::updateForces(real duration)
{
Registry::iterator i = registrations.begin();
for (; i != registrations.end(); i++)
{
i->fg->updateForce(i->particle, duration);
}
}
void ParticleForceRegistry::add(Particle* particle, ParticleForceGenerator *fg)
{
ParticleForceRegistry::ParticleForceRegistration registration;
registration.particle = particle;
registration.fg = fg;
registrations.push_back(registration);
}
ParticleGravity::ParticleGravity(const Vector3& gravity)
: gravity(gravity)
{
}
void ParticleGravity::updateForce(Particle* particle, real duration)
{
// Check that we do not have infinite mass
if (!particle->hasFiniteMass()) return;
// Apply the mass-scaled force to the particle
particle->addForce(gravity * particle->getMass());
}
ParticleDrag::ParticleDrag(real k1, real k2)
: k1(k1), k2(k2)
{
}
void ParticleDrag::updateForce(Particle* particle, real duration)
{
Vector3 force;
particle->getVelocity(&force);
// Calculate the total drag coefficient
real dragCoeff = force.magnitude();
dragCoeff = k1 * dragCoeff + k2 * dragCoeff * dragCoeff;
// Calculate the final force and apply it
force.normalise();
force *= -dragCoeff;
particle->addForce(force);
}
ParticleSpring::ParticleSpring(Particle *other, real sc, real rl)
: other(other), springConstant(sc), restLength(rl)
{
}
void ParticleSpring::updateForce(Particle* particle, real duration)
{
// Calculate the vector of the spring
Vector3 force;
particle->getPosition(&force);
force -= other->getPosition();
// Calculate the magnitude of the force
real magnitude = force.magnitude();
magnitude = real_abs(magnitude - restLength);
magnitude *= springConstant;
// Calculate the final force and apply it
force.normalise();
force *= -magnitude;
particle->addForce(force);
}
ParticleBuoyancy::ParticleBuoyancy(real maxDepth,
real volume,
real waterHeight,
real liquidDensity)
:
maxDepth(maxDepth), volume(volume),
waterHeight(waterHeight), liquidDensity(liquidDensity)
{
}
void ParticleBuoyancy::updateForce(Particle* particle, real duration)
{
// Calculate the submersion depth
real depth = particle->getPosition().y;
// Check if we're out of the water
if (depth >= waterHeight + maxDepth) return;
Vector3 force(0,0,0);
// Check if we're at maximum depth
if (depth <= waterHeight - maxDepth)
{
force.y = liquidDensity * volume;
particle->addForce(force);
return;
}
// Otherwise we are partly submerged
force.y = liquidDensity * volume *
(depth - maxDepth - waterHeight) / (2 * maxDepth);
particle->addForce(force);
}
ParticleBungee::ParticleBungee(Particle *other, real sc, real rl)
: other(other), springConstant(sc), restLength(rl)
{
}
void ParticleBungee::updateForce(Particle* particle, real duration)
{
// Calculate the vector of the spring
Vector3 force;
particle->getPosition(&force);
force -= other->getPosition();
// Check if the bungee is compressed
real magnitude = force.magnitude();
if (magnitude <= restLength) return;
// Calculate the magnitude of the force
magnitude = springConstant * (restLength - magnitude);
// Calculate the final force and apply it
force.normalise();
force *= -magnitude;
particle->addForce(force);
}
ParticleFakeSpring::ParticleFakeSpring(Vector3 *anchor, real sc, real d)
: anchor(anchor), springConstant(sc), damping(d)
{
}
void ParticleFakeSpring::updateForce(Particle* particle, real duration)
{
// Check that we do not have infinite mass
if (!particle->hasFiniteMass()) return;
// Calculate the relative position of the particle to the anchor
Vector3 position;
particle->getPosition(&position);
position -= *anchor;
// Calculate the constants and check they are in bounds.
real gamma = 0.5f * real_sqrt(4 * springConstant - damping*damping);
if (gamma == 0.0f) return;
Vector3 c = position * (damping / (2.0f * gamma)) +
particle->getVelocity() * (1.0f / gamma);
// Calculate the target position
Vector3 target = position * real_cos(gamma * duration) +
c * real_sin(gamma * duration);
target *= real_exp(-0.5f * duration * damping);
// Calculate the resulting acceleration and therefore the force
Vector3 accel = (target - position) * ((real)1.0 / (duration*duration)) -
particle->getVelocity() * ((real)1.0/duration);
particle->addForce(accel * particle->getMass());
}
ParticleAnchoredSpring::ParticleAnchoredSpring()
{
}
ParticleAnchoredSpring::ParticleAnchoredSpring(Vector3 *anchor,
real sc, real rl)
: anchor(anchor), springConstant(sc), restLength(rl)
{
}
void ParticleAnchoredSpring::init(Vector3 *anchor, real springConstant,
real restLength)
{
ParticleAnchoredSpring::anchor = anchor;
ParticleAnchoredSpring::springConstant = springConstant;
ParticleAnchoredSpring::restLength = restLength;
}
void ParticleAnchoredBungee::updateForce(Particle* particle, real duration)
{
// Calculate the vector of the spring
Vector3 force;
particle->getPosition(&force);
force -= *anchor;
// Calculate the magnitude of the force
real magnitude = force.magnitude();
if (magnitude < restLength) return;
magnitude = magnitude - restLength;
magnitude *= springConstant;
// Calculate the final force and apply it
force.normalise();
force *= -magnitude;
particle->addForce(force);
}
void ParticleAnchoredSpring::updateForce(Particle* particle, real duration)
{
// Calculate the vector of the spring
Vector3 force;
particle->getPosition(&force);
force -= *anchor;
// Calculate the magnitude of the force
real magnitude = force.magnitude();
magnitude = (restLength - magnitude) * springConstant;
// Calculate the final force and apply it
force.normalise();
force *= magnitude;
particle->addForce(force);
}