forked from PrototypeX29A/norbit
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathphysics.cpp
More file actions
297 lines (228 loc) · 8.22 KB
/
physics.cpp
File metadata and controls
297 lines (228 loc) · 8.22 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
290
291
292
293
294
295
296
297
/*----------------------------------------------------------------------------
quite heavily adapted from Chris Hecker's - checker@d6.com - http://www.d6.com/users/checker
*/
/*----------------------------------------------------------------------------
physics.cpp - This file implements the 2D physics simulator.
10/15/96 - checker
july 2010 ptr_here
*/
#include <assert.h>
#include <stdio.h>
#include <vector>
#include "physics.h"
real const
simulation_world::Kws; // Hooke's spring constant
real const
simulation_world::Kwd; // damping constant
real const
simulation_world::Kbs; // Hooke's spring constant
real const
simulation_world::Kbd; // damping constant
real const
simulation_world::Kdl; // linear damping factor
real const
simulation_world::Kda; // angular damping factor
//helper func
rigid_body *
simulation_world::add_body (real Mass)
{
rigid_body *B = new rigid_body ();
rigid_body & Body = *B;
Body.Mass = Mass;
Body.OneOverMass = r (1) / Mass;
// integrate over the body to find the moment of inertia
const float Width = 50.0; // TODO: parametrize this instead of using const values
const float Height = 1.0;
Body.OneOverCMMomentOfInertia = r (1) / ((Mass / r (12)) *
(Width * Width + Height * Height));
// 0-out non-vector quantities
Body.aConfigurations[0].Orientation = r (0);
Body.aConfigurations[0].AngularVelocity = r (0);
Body.aConfigurations[0].Torque = r (0);
aBodies.push_back (B);
return B;
}
/*----------------------------------------------------------------------------
simulation_world ctor
*/
simulation_world::simulation_world ()
{
WorldSpringAnchor.X = 0.0f;
WorldSpringAnchor.Y = 0.0f;
}
/*----------------------------------------------------------------------------
simulation_world dtor
*/
simulation_world::~simulation_world (void)
{
}
/*----------------------------------------------------------------------------
Render - render the source configurations
*/
void
simulation_world::Render (void)
{
// draw bodies
int i = 0;
for (std::vector < rigid_body * >::iterator it = aBodies.begin ();
it != aBodies.end (); ++it, ++i)
{
rigid_body::configuration & config =
(*it)->aConfigurations[(*it)->SourceConfigurationIndex];
printf ("body %d: position: %0.4f %0.4f orientation: %0.4f \n",
i, config.CMPosition.X, config.CMPosition.Y,
config.Orientation);
}
}
/*----------------------------------------------------------------------------
Simulate - Integrate forward DeltaTime seconds.
@todo do I need to store the last simulation time?
*/
void
simulation_world::Simulate (real DeltaTime)
{
real CurrentTime = r (0);
real TargetTime = DeltaTime;
while (CurrentTime < DeltaTime)
{
//ComputeForces();
Integrate (TargetTime - CurrentTime);
// we made a successful step, so swap configurations
// to "save" the data for the next step
CurrentTime = TargetTime;
TargetTime = DeltaTime;
ResetForces ();
for (std::vector < rigid_body * >::iterator it = aBodies.begin ();
it != aBodies.end (); ++it)
{
(*it)->SourceConfigurationIndex =
(*it)->SourceConfigurationIndex ? 0 : 1;
(*it)->TargetConfigurationIndex =
(*it)->TargetConfigurationIndex ? 0 : 1;
}
}
}
/*----------------------------------------------------------------------------
ComputeForces - compute forces for gravity, spring, etc.
*/
void
simulation_world::ResetForces ()
{
for (std::vector < rigid_body * >::iterator it = aBodies.begin ();
it != aBodies.end (); ++it)
{
rigid_body & Body = **it;
rigid_body::configuration & Configuration =
Body.aConfigurations[(*it)->SourceConfigurationIndex];
// clear forces
Configuration.Torque = r (0);
Configuration.CMForce = vector_2 (r (0), r (0));
}
/*
// BodySpring
{
rigid_body *Body0 = aBodies.at(0);
rigid_body::configuration &Configuration0 = Body0->aConfigurations[Body0->SourceConfigurationIndex];
matrix_2x2 const Rotation0(Configuration0.Orientation);
vector_2 Position0 = Configuration0.CMPosition + Rotation0 * vector_2( 1.0, 0.0);
vector_2 U0 = Position0 - Configuration0.CMPosition;
vector_2 VU0 = Configuration0.CMVelocity + Configuration0.AngularVelocity * GetPerpendicular(U0);
rigid_body *Body1 = aBodies.at(1);
rigid_body::configuration &Configuration1 = Body1->aConfigurations[Body1->SourceConfigurationIndex];
matrix_2x2 const Rotation1(Configuration1.Orientation);
vector_2 Position1 = Configuration1.CMPosition + Rotation1 * vector_2( -1.0, 0.0);
vector_2 U1 = Position1 - Configuration1.CMPosition;
vector_2 VU1 = Configuration1.CMVelocity + Configuration1.AngularVelocity * GetPerpendicular(U1);
// spring goes from 0 to 1
vector_2 SpringVector = Position1 - Position0;
vector_2 Spring = -Kbs * SpringVector;
vector_2 RelativeVelocity = VU1 - VU0;
// project velocity onto spring to get damping vector
// this is basically a Gram-Schmidt projection
vector_2 DampingForce =
-Kbd * (DotProduct(RelativeVelocity,SpringVector)/
DotProduct(SpringVector,SpringVector)) * SpringVector;
Spring += DampingForce;
Configuration0.CMForce -= Spring;
Configuration0.Torque -= PerpDotProduct(U0,Spring);
Configuration1.CMForce += Spring;
Configuration1.Torque += PerpDotProduct(U1,Spring) ;
}
*/
/*
// WorldSpring
{
// apply spring to body 0's vertex 0 to anchor
rigid_body &Body = *aBodies.at(0);
rigid_body::configuration &Configuration =
Body.aConfigurations[ConfigurationIndex];
vector_2 Position = Configuration.CMPosition + Configuration.Orientation * vector_2( 10.0, 0.0);
vector_2 U = Position - Configuration.CMPosition;
vector_2 VU = Configuration.CMVelocity + Configuration.AngularVelocity * GetPerpendicular(U);
vector_2 Spring = -Kws * (Position - WorldSpringAnchor);
// project velocity onto spring to get damping vector
// this is basically a Gram-Schmidt projection
vector_2 DampingForce =
-Kwd * (DotProduct(VU,Spring)/DotProduct(Spring,Spring)) * Spring;
Spring += DampingForce;
Configuration.CMForce += Spring;
Configuration.Torque += PerpDotProduct(U,Spring);
}
*/
}
/*----------------------------------------------------------------------------
Integrate - integrate the rigid body configurations forward in time from
source config to target config
@todo calculate initial derivatives _once_
@todo use something better than Euler's method
*/
void
simulation_world::Integrate (real DeltaTime)
{
for (std::vector < rigid_body * >::iterator it = aBodies.begin ();
it != aBodies.end (); ++it)
{
rigid_body::configuration & Source =
(*it)->aConfigurations[(*it)->SourceConfigurationIndex];
rigid_body::configuration & Target =
(*it)->aConfigurations[(*it)->TargetConfigurationIndex];
Target.CMPosition = Source.CMPosition + DeltaTime * Source.CMVelocity;
Target.Orientation = Source.Orientation +
DeltaTime * Source.AngularVelocity;
Target.CMVelocity = Source.CMVelocity +
(DeltaTime * (*it)->OneOverMass) * Source.CMForce;
Target.AngularVelocity = Source.AngularVelocity +
(DeltaTime * (*it)->OneOverCMMomentOfInertia) * Source.Torque;
}
}
rigid_body::rigid_body ():SourceConfigurationIndex (0),
TargetConfigurationIndex (1)
{
}
//apply force specified in local ref
void
rigid_body::apply_force (vector_2 const &F, vector_2 const &Pl)
{
matrix_2x2 const Rotation (aConfigurations[SourceConfigurationIndex].
Orientation);
aConfigurations[SourceConfigurationIndex].CMForce += Rotation * F;
aConfigurations[SourceConfigurationIndex].Torque += PerpDotProduct (Pl, F);
/* printf("applying total torque: %0.4f F: %0.4f %0.4f \n",
aConfigurations[SourceConfigurationIndex].Torque,
aConfigurations[SourceConfigurationIndex].CMForce.X,
aConfigurations[SourceConfigurationIndex].CMForce.Y);
*/
}
//apply the force, specified in global ref
void
rigid_body::apply_force_G (vector_2 const &F, vector_2 const &Pl)
{
aConfigurations[SourceConfigurationIndex].CMForce += F;
aConfigurations[SourceConfigurationIndex].Torque += PerpDotProduct (Pl, F);
/*
printf("applying total torque: %0.4f F: %0.4f %0.4f \n",
aConfigurations[SourceConfigurationIndex].Torque,
aConfigurations[SourceConfigurationIndex].CMForce.X,
aConfigurations[SourceConfigurationIndex].CMForce.Y);
*/
}