-
Notifications
You must be signed in to change notification settings - Fork 266
/
particle.cpp
165 lines (131 loc) · 3.28 KB
/
particle.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
/*
* Implementation file for the particle class.
*
* 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 <assert.h>
#include <cyclone/particle.h>
using namespace cyclone;
/*
* --------------------------------------------------------------------------
* FUNCTIONS DECLARED IN HEADER:
* --------------------------------------------------------------------------
*/
void Particle::integrate(real duration)
{
// We don't integrate things with zero mass.
if (inverseMass <= 0.0f) return;
assert(duration > 0.0);
// Update linear position.
position.addScaledVector(velocity, duration);
// Work out the acceleration from the force
Vector3 resultingAcc = acceleration;
resultingAcc.addScaledVector(forceAccum, inverseMass);
// Update linear velocity from the acceleration.
velocity.addScaledVector(resultingAcc, duration);
// Impose drag.
velocity *= real_pow(damping, duration);
// Clear the forces.
clearAccumulator();
}
void Particle::setMass(const real mass)
{
assert(mass != 0);
Particle::inverseMass = ((real)1.0)/mass;
}
real Particle::getMass() const
{
if (inverseMass == 0) {
return REAL_MAX;
} else {
return ((real)1.0)/inverseMass;
}
}
void Particle::setInverseMass(const real inverseMass)
{
Particle::inverseMass = inverseMass;
}
real Particle::getInverseMass() const
{
return inverseMass;
}
bool Particle::hasFiniteMass() const
{
return inverseMass >= 0.0f;
}
void Particle::setDamping(const real damping)
{
Particle::damping = damping;
}
real Particle::getDamping() const
{
return damping;
}
void Particle::setPosition(const Vector3 &position)
{
Particle::position = position;
}
void Particle::setPosition(const real x, const real y, const real z)
{
position.x = x;
position.y = y;
position.z = z;
}
void Particle::getPosition(Vector3 *position) const
{
*position = Particle::position;
}
Vector3 Particle::getPosition() const
{
return position;
}
void Particle::setVelocity(const Vector3 &velocity)
{
Particle::velocity = velocity;
}
void Particle::setVelocity(const real x, const real y, const real z)
{
velocity.x = x;
velocity.y = y;
velocity.z = z;
}
void Particle::getVelocity(Vector3 *velocity) const
{
*velocity = Particle::velocity;
}
Vector3 Particle::getVelocity() const
{
return velocity;
}
void Particle::setAcceleration(const Vector3 &acceleration)
{
Particle::acceleration = acceleration;
}
void Particle::setAcceleration(const real x, const real y, const real z)
{
acceleration.x = x;
acceleration.y = y;
acceleration.z = z;
}
void Particle::getAcceleration(Vector3 *acceleration) const
{
*acceleration = Particle::acceleration;
}
Vector3 Particle::getAcceleration() const
{
return acceleration;
}
void Particle::clearAccumulator()
{
forceAccum.clear();
}
void Particle::addForce(const Vector3 &force)
{
forceAccum += force;
}