00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
#include "PhysicsSystem.h"
00013
#include <OpenCAL/PhysicalObject.h>
00014
#include <OpenCAL/Force.h>
00015
#include <OpenCAL/Constraint.h>
00016
#include <OpenCAL/EulerSolver.h>
00017
#include <OpenCAL/MidpointSolver.h>
00018
#include <OpenCAL/RungeKutta4Solver.h>
00019
using namespace OpenCAL;
00020
00021
using namespace std;
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 PhysicsSystem::PhysicsSystem(
System *parent)
00045 :
System(parent), m_solver(0), m_maxStepSize(0.3f)
00046 {
00047 }
00048
00049 PhysicsSystem::~PhysicsSystem()
00050 {
00051
if(m_solver)
00052
delete m_solver;
00053 }
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
void PhysicsSystem::addForce(
const Vector3 &force)
00066 {
00067
unsigned int i;
00068
00069
for(i = 0; i < m_systems.size(); ++i)
00070 ((
PhysicsSystem *) m_systems[i])->addForce(force);
00071
00072
PhysicalObject *object;
00073
for(i = 0; i < m_objects.size(); ++i)
00074 {
00075 object = (
PhysicalObject *) m_objects[i];
00076
if(!object->
isFixed())
00077 object->
addForce(force);
00078 }
00079 }
00080
00081
void PhysicsSystem::resetForces()
00082 {
00083
unsigned int i;
00084
00085
for(i = 0; i < m_systems.size(); ++i)
00086 ((
PhysicsSystem *) m_systems[i])->resetForces();
00087
00088
PhysicalObject *object;
00089
for(i = 0; i < m_objects.size(); ++i)
00090 {
00091 object = (
PhysicalObject *) m_objects[i];
00092
if(!object->
isFixed())
00093 object->
resetForce();
00094 }
00095 }
00096
00097
00098
00099
00100
00101
00102 void PhysicsSystem::fillState()
00103 {
00104
00105
00106
00107
00108
00110 m_state.
clear();
00111
00112
00113
for(
unsigned int i = 0; i < m_objects.size(); ++i)
00114 {
00115
PhysicalObject *object = (
PhysicalObject *) m_objects[i];
00116 object->
addToState(&m_state);
00118
00119
00120 }
00121 }
00122
00123 void PhysicsSystem::fillDerivative()
00124 {
00126
00127
00128
00129 m_derivative.
clear();
00130
00131
00132
for(
unsigned int i = 0; i < m_objects.size(); ++i)
00133 {
00134
PhysicalObject *object = (
PhysicalObject *) m_objects[i];
00135 object->
addToDerivative(&m_derivative);
00136
00137
00139
00140
00141 }
00142 }
00143
00144
void PhysicsSystem::fromState()
00145 {
00146
00147 m_state.
reset();
00148
00149
for(
unsigned int i = 0; i < m_objects.size(); ++i)
00150 {
00151
PhysicalObject *object = (
PhysicalObject *) m_objects[i];
00152 object->
fromState(&m_state);
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170 }
00171 }
00172
00173
00174
00175
00176
00177
00178
void PhysicsSystem::step(
float deltaSeconds)
00179 {
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
unsigned int steps = (
unsigned int) (deltaSeconds / m_maxStepSize) + 1;
00190 deltaSeconds /= steps;
00191
00192
for(
unsigned int i = 0; i < steps; ++i)
00193 {
00194 System::step(deltaSeconds);
00195
if(m_objects.size() > 0)
00196 m_solver->
solve(deltaSeconds,
this);
00197 }
00198 }
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262 void PhysicsSystem::initialize()
00263 {
00264
System::initialize();
00265
00267 m_solver =
new EulerSolver;
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281 }
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
#ifdef BLABLA
00330
void PhysicsSystem::deleteObject(
PhysicalObject *object)
00331 {
00337
if(isDestructing())
return;
00338
00339 forAllObjects(it)
00340 if(*it == object)
00341 {
00342 m_objects.erase(it);
00343
return;
00344 }
00345
00346
#ifdef VERBOSE
00347
Debug::print(
"Trying to delete an object that isn't in the list!");
00348
#endif // VERBOSE
00349
00351 }
00352
00353
void PhysicsSystem::deleteForce(
Force *force)
00354 {
00360
if(isDestructing())
return;
00361
00362 forAllForces(it)
00363 if(*it == force)
00364 {
00365 m_forces.erase(it);
00366
return;
00367 }
00368
00369
#ifdef VERBOSE
00370
Debug::print(
"Trying to delete a force that isn't in the list!");
00371
#endif // VERBOSE
00372
00374 }
00375
00376
void PhysicsSystem::deleteConstraint(
Constraint *constraint)
00377 {
00383
if(isDestructing())
return;
00384
00385 forAllConstraints(it)
00386 if(*it == constraint)
00387 {
00388 m_constraints.erase(it);
00389
return;
00390 }
00391
00392
#ifdef VERBOSE
00393
Debug::print(
"Trying to delete a constraint that isn't in the list!");
00394
#endif // VERBOSE
00395
00397 }
00398
#endif // BLABLA