-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforces.h
More file actions
289 lines (232 loc) · 6.28 KB
/
forces.h
File metadata and controls
289 lines (232 loc) · 6.28 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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//
// Forces.h
// MSIM495
//
// Created by Aaron Ruel on 2/13/18.
// Copyright (c) 2018 AAR. All rights reserved.
//
#ifndef __MSIM495__Forces__
#define __MSIM495__Forces__
#include <stdio.h>
#include "core.h"
namespace Physics {
/**
* Interface for derived classes to apply forces
* to particles in a more constructed manner
*/
class ParticleForceGenerator {
public:
virtual void update_force(Particle *p, real time) = 0;
};
/*
* Container and manager for link between particle
* and force generators
*/
class ParticleForceRegistrar {
protected:
/**
* Bundles particle with force generator
*/
struct ParticleForceLink {
Particle * particle;
ParticleForceGenerator * fg;
};
/*
* Link Container
*/
typedef std::vector<ParticleForceLink> Registry;
Registry links;
public:
/*
* Methods
*/
/**
* Add link between particle and force generator
*/
void add(Particle * particle, ParticleForceGenerator * fg);
/**
* Remove link between particle and force generator
*/
void remove(Particle * particle, ParticleForceGenerator * fg);
/**
* Clear all connections
*/
void clear();
/**
* Check if link between particle and force generator
* exists
*/
bool check_force_registered(
Particle * particle,
ParticleForceGenerator * fg
);
/**
* Updates all connections for one time step
*/
void update_forces(real duration);
};
/**
* General gravitational force generator
*/
class ParticleGravity : public ParticleForceGenerator {
/*
* force of gravity constant
*/
Vector3 gravity;
public:
/*
* Constructors
*/
ParticleGravity(Vector3 v) : gravity(v) {}
/*
* Getters / Setters
*/
void set_gravity(Vector3 g) { gravity = g; }
/**
* Particle force generator implementation
*/
void update_force(Particle * particle, real duration);
};
/*
* Attached spring force generator
*/
class ParticleSpring : public ParticleForceGenerator {
/*
* particle at one end of the spring
*/
Particle * end;
/*
* Spring Constant k
*/
real spring_constant;
/**
* Resting length of spring
*/
real rest_length;
public:
/*
* Constructors
*/
ParticleSpring(Particle * e, real s_c, real r_l) :
end(e), spring_constant(s_c), rest_length(r_l){}
/**
* Particle force generator implementation
*/
void update_force(Particle * particle, real duration);
};
/*
* Similar to spring with damping factor
*/
class ParticleStiffSpring : public ParticleForceGenerator {
/*
* particle at one end of the spring
*/
Particle * end;
/*
* Spring Constant k
*/
real spring_constant;
/*
* Resting length of spring
*/
real damping;
public:
/*
* Constructors
*/
ParticleStiffSpring(Particle * e, real s_c, real d) :
end(e), spring_constant(s_c), damping(d){}
/**
* Particle force generator implementation
*/
void update_force(Particle * particle, real duration);
};
///////////////////////// RigidBody /////////////////////////
/**
* Interface for derived classes to apply forces
* to RigidBodies in a more constructed manner
*/
class ForceGenerator {
public:
/**
* RigidBody force generation interface
*/
virtual void update_force(RigidBody * body, real duration) = 0;
};
class Gravity : public ForceGenerator {
/**
* Gravity constant
*/
Vector3 gravity;
public:
/**
* Constructor
*/
Gravity(Vector3 &gravity);
/**
* RigidBody force generation gravity implementation
*/
virtual void update_force(RigidBody * body, real duration);
};
/**
* RigidBody attached spring force generator
*/
class Spring : public ForceGenerator {
Vector3 connection_point_left;
Vector3 connection_point_right;
RigidBody * other;
real spring_constant;
real rest_length;
public:
/**
* Constructor
*/
Spring(
Vector3 &left_connection_point,
Vector3 &right_connection_point,
RigidBody * other,
real spring_constant,
real rest_length
);
/**
* RigidBody force generation spring implementation
*/
virtual void update_force(RigidBody * body, real duration);
};
class ForceRegistry {
protected:
/**
* Bundles RigidBody with force generator
*/
struct ForceRegistration {
RigidBody * body;
ForceGenerator * fg;
};
/*
* Link Container
*/
typedef std::vector<ForceRegistration> Registry;
Registry links;
public:
/*
* Methods
*/
/**
* Add link between RigidBody and force generator
*/
void add(RigidBody * RigidBody, ForceGenerator * fg);
/**
* Remove link between RigidBody and force generator
*/
void remove(RigidBody * RigidBody, ForceGenerator * fg);
/**
* Clear all connections
*/
void clear();
/**
* Updates all connections for one time step
*/
void update_forces(real duration);
};
};
#endif /* defined(__MSIM495__Forces__) */