00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00021
00022 #ifndef __VR3PHYSICSSIMULATOR_H__
00023 #define __VR3PHYSICSSIMULATOR_H__
00024
00025
00026 #if (defined(_WIN32) || defined(_WIN64)) && !defined(VR3CONF_NOPHYSICS)
00027
00028
00029 #include <set>
00030
00031 #include <map>
00032
00033 #include <unordered_map>
00034
00035 #include <VR3PhyMesh.h>
00036
00038
00205 class VR3PhysicsSimulator {
00207 friend class VR3Obj;
00209 friend class VR3Scene;
00210
00212 static VR3PhysicsSimulator* ms_activesimulator;
00213
00215
00221 int m_mode;
00222
00224 struct MapElement {
00226 VR3PhyMesh* phymesh;
00228 void* actor;
00230 void* material;
00232
00233 NxReal scaled_bbox[6];
00235 GLfloat init_transfmat[16];
00236 };
00237
00239 bool m_prepared;
00241 bool m_simulating;
00242
00244 int m_solveriter;
00245
00247 std::set<const VR3Obj*> m_objects;
00249 std::set<VR3PhyMesh*> m_phymeshes;
00251
00253 std::unordered_map<const VR3Obj*, MapElement> m_simobjects;
00255
00256 std::map<int, void*> m_joints;
00257
00259 void* m_SDK;
00261 void* m_scene;
00263 void* m_cookinterface;
00264
00266 long long m_freq;
00268 long long m_previoustime;
00269
00271 const VR3Obj* m_movobj;
00273 void* m_movmat;
00274
00276 VR3PhysicsSimulator(const VR3PhysicsSimulator&);
00278 VR3PhysicsSimulator& operator = (const VR3PhysicsSimulator&);
00279
00280
00281
00283
00284
00286
00296 void CookStaticActor(const VR3Obj* obj, MapElement* elem);
00297
00299
00311 void CookNonStaticActor(const VR3Obj* obj, MapElement* elem);
00312
00314
00315
00316
00318
00319
00321
00335 void CollectResults();
00336
00338
00350 bool GetModelMat(const VR3Obj* obj, GLfloat* modelmat);
00351
00353
00354 public:
00355
00356
00357
00359
00360
00362
00367 static VR3PhysicsSimulator* GetActiveSimulator()
00368 { return ms_activesimulator; }
00369
00371
00376 void Activate();
00377
00379
00384 void Deactivate();
00385
00387
00392 bool IsSimulating()
00393 { return m_simulating; }
00394
00396
00401 bool HasBeenPrepared()
00402 { return m_prepared; }
00403
00404
00405
00407
00408
00410
00433 VR3PhysicsSimulator( int mode = VR3PHYSIM_AUTOMATIC,
00434 int solveriter = VR3PHYSIM_DEF_ITERNUM,
00435 float timestep = VR3PHYSIM_DEF_TIMESTEP,
00436 int maxsteps = VR3PHYSIM_DEF_MAXSTEPS );
00437
00439
00443 ~VR3PhysicsSimulator();
00444
00446
00447
00448
00450
00451
00453
00456 void SetGravity(NxReal gx, NxReal gy, NxReal gz);
00457
00459
00460
00461
00480
00482
00489 int AddFixedJoint(const VR3Obj* o1, const VR3Obj* o2);
00490
00492
00499 int AddFixedJoint(const VR3Obj* o);
00500
00502
00516 int AddDistanceJoint( const VR3Obj* o1, const VR3Obj* o2,
00517 NxReal mindist, NxReal maxdist,
00518 bool collide);
00519
00521
00530 int AddDistanceJoint( const VR3Obj* o,
00531 NxReal pointX, NxReal pointY, NxReal pointZ,
00532 NxReal mindist, NxReal maxdist );
00533
00535
00554 int AddRevoluteJoint( const VR3Obj* o1, const VR3Obj* o2,
00555 NxReal anchorX, NxReal anchorY, NxReal anchorZ,
00556 NxReal axisX, NxReal axisY, NxReal axisZ,
00557 bool collide);
00558
00560
00571 int AddRevoluteJoint( const VR3Obj* o,
00572 NxReal anchorX, NxReal anchorY, NxReal anchorZ,
00573 NxReal axisX, NxReal axisY, NxReal axisZ );
00574
00576
00586 int AddSphericalJoint( const VR3Obj* o1, const VR3Obj* o2,
00587 NxReal anchorX, NxReal anchorY, NxReal anchorZ,
00588 bool collide );
00589
00591
00599 int AddSphericalJoint( const VR3Obj* o,
00600 NxReal anchorX, NxReal anchorY, NxReal anchorZ );
00601
00603
00614 int AddPrismaticJoint( const VR3Obj* o1, const VR3Obj* o2,
00615 NxReal axisX, NxReal axisY, NxReal axisZ,
00616 bool collide );
00617
00619
00627 int AddPrismaticJoint( const VR3Obj* o,
00628 NxReal axisX, NxReal axisY, NxReal axisZ );
00629
00631
00648 int AddCylindricalJoint( const VR3Obj* o1, const VR3Obj* o2,
00649 NxReal anchorX, NxReal anchorY, NxReal anchorZ,
00650 NxReal axisX, NxReal axisY, NxReal axisZ,
00651 bool collide );
00652
00654
00666 int AddCylindricalJoint( const VR3Obj* o,
00667 NxReal anchorX, NxReal anchorY, NxReal anchorZ,
00668 NxReal axisX, NxReal axisY, NxReal axisZ );
00669
00671
00690 int AddPulleyJoint( const VR3Obj* o1, const VR3Obj* o2,
00691 NxReal p1X, NxReal p1Y, NxReal p1Z,
00692 NxReal p2X, NxReal p2Y, NxReal p2Z,
00693 NxReal stiffness, NxReal ratio, bool collide );
00694
00696
00714 int AddPointInPlaneJoint( const VR3Obj* o1, const VR3Obj* o2,
00715 NxReal anchorX, NxReal anchorY, NxReal anchorZ,
00716 NxReal normX, NxReal normY, NxReal normZ,
00717 bool collide );
00718
00720
00731 int AddPointInPlaneJoint( const VR3Obj* o,
00732 NxReal anchorX, NxReal anchorY, NxReal anchorZ,
00733 NxReal normX, NxReal normY, NxReal normZ );
00734
00736
00751 int AddPointOnLineJoint( const VR3Obj* o1, const VR3Obj* o2,
00752 NxReal anchorX, NxReal anchorY, NxReal anchorZ,
00753 NxReal lineX, NxReal lineY, NxReal lineZ,
00754 bool collide );
00755
00757
00768 int AddPointOnLineJoint( const VR3Obj* o,
00769 NxReal anchorX, NxReal anchorY, NxReal anchorZ,
00770 NxReal lineX, NxReal lineY, NxReal lineZ );
00771
00773
00802 int AddGenericJoint( const VR3Obj* o1, const VR3Obj* o2,
00803 NxReal anchorX, NxReal anchorY, NxReal anchorZ,
00804 NxReal xaxisX, NxReal xaxisY, NxReal xaxisZ,
00805 NxReal yaxisX, NxReal yaxisY, NxReal yaxisZ,
00806 bool xtrans, bool ytrans, bool ztrans,
00807 bool xrot, bool yrot, bool zrot, bool collide );
00808
00810
00827 int AddGenericJoint( const VR3Obj* o,
00828 NxReal anchorX, NxReal anchorY, NxReal anchorZ,
00829 NxReal xaxisX, NxReal xaxisY, NxReal xaxisZ,
00830 NxReal yaxisX, NxReal yaxisY, NxReal yaxisZ,
00831 bool xtrans, bool ytrans, bool ztrans,
00832 bool xrot, bool yrot, bool zrot );
00833
00835
00841 void DeleteJoint(int jointID);
00842
00844
00845
00846
00870
00872
00884 void ObjAddForce( const VR3Obj* obj,
00885 NxReal dx, NxReal dy, NxReal dz,
00886 NxReal mag );
00887
00889
00901 void ObjAddForce( const VR3Obj* obj,
00902 NxReal px, NxReal py, NxReal pz,
00903 NxReal dx, NxReal dy, NxReal dz,
00904 NxReal mag );
00905
00907
00919 void ObjAddTorque( const VR3Obj* obj,
00920 NxReal dx, NxReal dy, NxReal dz,
00921 NxReal mag );
00922
00924
00925
00926
00962
00964
00970 void ObjBeginMove(const VR3Obj* obj);
00971
00973
00981 void ObjTranslate(NxReal x, NxReal y, NxReal z);
00982
00984
00993 void ObjRotate(NxReal ax, NxReal ay, NxReal az);
00994
00996
01005 void ObjRotate( NxReal cx, NxReal cy, NxReal cz,
01006 NxReal ax, NxReal ay, NxReal az );
01007
01009
01015 void ObjRestore();
01016
01018
01022 void ObjEndMove();
01023
01025
01026
01027
01029
01033
01035
01042 void ObjSetMass(const VR3Obj* obj, NxReal mass);
01043
01045
01058 void ObjSetLinearVelocity( const VR3Obj* obj,
01059 NxReal dx, NxReal dy, NxReal dz,
01060 NxReal vel );
01061
01063
01076 void ObjSetAngularVelocity( const VR3Obj* obj,
01077 NxReal dx, NxReal dy, NxReal dz,
01078 NxReal avel );
01079
01081
01090 void ObjSetLinearDamping(const VR3Obj* obj, NxReal damping);
01091
01093
01102 void ObjSetAngularDamping(const VR3Obj* obj, NxReal adamping);
01103
01105
01112 void ObjSetMaxAngularVelocity(const VR3Obj* obj, NxReal maxavel);
01113
01115
01120 void ObjSetStaticFriction(const VR3Obj* obj, NxReal sfri);
01121
01123
01128 void ObjSetDynamicFriction(const VR3Obj* obj, NxReal dfri);
01129
01131
01136 void ObjSetBounciness(const VR3Obj* obj, NxReal bou);
01137
01139
01155 void ObjSetSkinWidth(const VR3Obj* obj, NxReal swidth = -1.0f);
01156
01158
01164 void ObjAffectedByGravity(const VR3Obj* obj, bool flag);
01165
01167
01171 void ObjCollide(const VR3Obj* obj, bool flag);
01172
01174
01175
01176
01178
01182
01184
01195 int ObjGetType(const VR3Obj* obj);
01196
01198
01203 void ObjGetCenterOfMassPos(const VR3Obj* obj, NxReal* pos);
01204
01206
01216 NxReal ObjGetMass(const VR3Obj* obj);
01217
01219
01228 void ObjGetInertiaTensor(const VR3Obj* obj, NxReal* tens);
01229
01231
01238 void ObjGetLinearVelocity(const VR3Obj* obj, NxReal* vel);
01239
01241
01247 void ObjGetAngularVelocity(const VR3Obj* obj, NxReal* avel);
01248
01250
01259 void ObjGetPointVelocity( const VR3Obj* obj,
01260 NxReal x, NxReal y, NxReal z,
01261 NxReal* vel );
01262
01264
01271 void ObjGetLinearMomentum(const VR3Obj* obj, NxReal* mom);
01272
01274
01281 void ObjGetAngularMomentum(const VR3Obj* obj, NxReal* amom);
01282
01284
01291 NxReal ObjGetKineticEnergy(const VR3Obj* obj);
01292
01294
01299 NxReal ObjGetStaticFriction(const VR3Obj* obj);
01300
01302
01307 NxReal ObjGetDynamicFriction(const VR3Obj* obj);
01308
01310
01315 NxReal ObjGetBounciness(const VR3Obj* obj);
01316
01318
01328 NxReal ObjGetSkinWidth(const VR3Obj* obj);
01329
01331
01332
01333
01335
01336
01338
01353 void ObjLocalToWorld( const VR3Obj* obj,
01354 const GLfloat* inpoint, GLfloat* outpoint );
01355
01357
01372 bool ObjPointInBBox( const VR3Obj* obj,
01373 NxReal x, NxReal y, NxReal z );
01374
01376
01377
01378
01380
01381
01382 private:
01383
01385
01396 void AutoSimulateObject(const VR3Obj* obj, bool simulate);
01397
01398 public:
01399
01401
01411 void SimulateObject(const VR3Obj* obj, bool simulate);
01412
01414
01415
01416
01418
01419
01421
01437 void PrepareSimulation();
01438
01440
01445 void StartSimulation();
01446
01448
01451 void HaltSimulation();
01452
01454
01458 void ResetSimulation();
01459
01461
01466 void EndSimulation();
01467
01469
01470
01471
01473
01474
01476
01480 void ResetTimer();
01481
01483
01492 NxReal GetElapsedTime();
01493
01495
01496 };
01497
01498 #endif // windows only
01499
01500 #endif