-
Notifications
You must be signed in to change notification settings - Fork 260
/
fgen.cpp
184 lines (155 loc) · 5.15 KB
/
fgen.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
/*
* Implementation file for the rigid body force generators.
*
* Part of the Cyclone physics system.
*
* Copyright (c) Icosagon 2003. All Rights Reserved.
*
* This software is distributed under license. Use of this software
* implies agreement with all terms and conditions of the accompanying
* software license.
*/
#include <cyclone/fgen.h>
using namespace cyclone;
void ForceRegistry::updateForces(real duration)
{
Registry::iterator i = registrations.begin();
for (; i != registrations.end(); i++)
{
i->fg->updateForce(i->body, duration);
}
}
void ForceRegistry::add(RigidBody *body, ForceGenerator *fg)
{
ForceRegistry::ForceRegistration registration;
registration.body = body;
registration.fg = fg;
registrations.push_back(registration);
}
Buoyancy::Buoyancy(const Vector3 &cOfB, real maxDepth, real volume,
real waterHeight, real liquidDensity /* = 1000.0f */)
{
centreOfBuoyancy = cOfB;
Buoyancy::liquidDensity = liquidDensity;
Buoyancy::maxDepth = maxDepth;
Buoyancy::volume = volume;
Buoyancy::waterHeight = waterHeight;
}
void Buoyancy::updateForce(RigidBody *body, real duration)
{
// Calculate the submersion depth
Vector3 pointInWorld = body->getPointInWorldSpace(centreOfBuoyancy);
real depth = pointInWorld.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;
body->addForceAtBodyPoint(force, centreOfBuoyancy);
return;
}
// Otherwise we are partly submerged
force.y = liquidDensity * volume *
(depth - maxDepth - waterHeight) / 2 * maxDepth;
body->addForceAtBodyPoint(force, centreOfBuoyancy);
}
Gravity::Gravity(const Vector3& gravity)
: gravity(gravity)
{
}
void Gravity::updateForce(RigidBody* body, real duration)
{
// Check that we do not have infinite mass
if (!body->hasFiniteMass()) return;
// Apply the mass-scaled force to the body
body->addForce(gravity * body->getMass());
}
Spring::Spring(const Vector3 &localConnectionPt,
RigidBody *other,
const Vector3 &otherConnectionPt,
real springConstant,
real restLength)
: connectionPoint(localConnectionPt),
otherConnectionPoint(otherConnectionPt),
other(other),
springConstant(springConstant),
restLength(restLength)
{
}
void Spring::updateForce(RigidBody* body, real duration)
{
// Calculate the two ends in world space
Vector3 lws = body->getPointInWorldSpace(connectionPoint);
Vector3 ows = other->getPointInWorldSpace(otherConnectionPoint);
// Calculate the vector of the spring
Vector3 force = lws - ows;
// 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;
body->addForceAtPoint(force, lws);
}
Aero::Aero(const Matrix3 &tensor, const Vector3 &position, const Vector3 *windspeed)
{
Aero::tensor = tensor;
Aero::position = position;
Aero::windspeed = windspeed;
}
void Aero::updateForce(RigidBody *body, real duration)
{
Aero::updateForceFromTensor(body, duration, tensor);
}
void Aero::updateForceFromTensor(RigidBody *body, real duration,
const Matrix3 &tensor)
{
// Calculate total velocity (windspeed and body's velocity).
Vector3 velocity = body->getVelocity();
velocity += *windspeed;
// Calculate the velocity in body coordinates
Vector3 bodyVel = body->getTransform().transformInverseDirection(velocity);
// Calculate the force in body coordinates
Vector3 bodyForce = tensor.transform(bodyVel);
Vector3 force = body->getTransform().transformDirection(bodyForce);
// Apply the force
body->addForceAtBodyPoint(force, position);
}
AeroControl::AeroControl(const Matrix3 &base, const Matrix3 &min, const Matrix3 &max,
const Vector3 &position, const Vector3 *windspeed)
:
Aero(base, position, windspeed)
{
AeroControl::minTensor = min;
AeroControl::maxTensor = max;
controlSetting = 0.0f;
}
Matrix3 AeroControl::getTensor()
{
if (controlSetting <= -1.0f) return minTensor;
else if (controlSetting >= 1.0f) return maxTensor;
else if (controlSetting < 0)
{
return Matrix3::linearInterpolate(minTensor, tensor, controlSetting+1.0f);
}
else if (controlSetting > 0)
{
return Matrix3::linearInterpolate(tensor, maxTensor, controlSetting);
}
else return tensor;
}
void AeroControl::setControl(real value)
{
controlSetting = value;
}
void AeroControl::updateForce(RigidBody *body, real duration)
{
Matrix3 tensor = getTensor();
Aero::updateForceFromTensor(body, duration, tensor);
}
void Explosion::updateForce(RigidBody* body, real duration)
{
}