vrecko
virtual reality framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PhysXManager.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <NxPhysics.h>
4 #include "SweepSurface.h"
5 #include "Utils.h"
6 #include "Hoop.h"
7 #include <Windows.h>
8 #include <ctime>
9 #include "DAEnvironment.h"
10 
11 #pragma comment(lib, "PhysXLoader.lib")
12 
13 namespace APDYNAMICART
14 {
15 
17 {
18  double detailCoef;
19  double hoopDensity;
20  double stringSpring;
21  double stringDamper;
22 
24  detailCoef(0.70),
25  hoopDensity(900),
26  stringSpring(0),
27  stringDamper(0)
28  {}
29 };
30 
32 {
33 
34 public:
35  static PhysXManager* get()
36  {
37  static PhysXManager manager;
38  return &manager;
39  }
40 
42  static inline NxQuat toNxQuat( const osg::Quat& qRot );
43  static inline NxVec3 toNxVec3( const osg::Vec3& point );
44  static inline osg::Vec3 toOsgVec3( const NxVec3& point);
45  static inline osg::Quat toOsgQuat( const NxQuat& qRot);
46  static inline osg::Matrix getDAMatrix();
47 
48 
49  void releasePhysics();
50  bool init();
51  bool createScene();
52  void getPhysicsResult();
53  void startPhysics();
54  NxActor* addHoopActorToScene( Hoop* hoop, NxVec3 linearVelocity = NxVec3(0.2,0.0,-1.0) );
55  void setStringJoints(HoopPtr first, HoopPtr second, Vec3ArrayPtr points);
56  void setActorPosition(HoopPtr hoop);
57  void setActorOrientation(HoopPtr hoop);
58  void addForceToHoop(HoopPtr hoop, const osg::Vec3& direction);
59  void resetScene();
60  void releaseHoopActor(HoopPtr hoop);
61  void releaseHoopJoints();
62  void createRope(HoopPtr hoop, const osg::Vec3& pos, double segHeight, double radius, unsigned int segments);
63  const std::vector<NxActor*>& getRopeActors() const;
65 
66  bool isRunning() const { return _simulationRunning; }
67  void setSimulationRun(bool val) { _simulationRunning = val; }
68 
69 protected:
70  PhysXManager();
71 
72  inline NxVec3 transformToLocalCoord( Hoop* hoop, const osg::Vec3& point );
73  NxSphericalJoint* createRopeSphericalJoint(NxActor* a0, NxActor* a1, const NxVec3& globalAnchor, const NxVec3& globalAxis);
74  NxActor* createCapsule(const NxVec3& pos, const NxReal height, const NxReal radius);
75 
76  NxPhysicsSDK* _physicsSDK;
77  NxScene* _scene;
78  float _deltaTime;
82 
83  std::vector<NxJoint*> _joints;
84  std::vector<NxActor*> _ropeSegs;
85  std::vector<NxSphericalJoint*> _ropeLinks;
86 };
87 
88 NxQuat PhysXManager::toNxQuat( const osg::Quat &qRot )
89 {
90  NxQuat result;
91  result.x=qRot.x();
92  result.y=qRot.y();
93  result.z=qRot.z();
94  result.w=qRot.w();
95 
96  return result;
97 }
98 
99 NxVec3 PhysXManager::toNxVec3( const osg::Vec3& point )
100 {
101  return point._v;
102 }
103 
104 osg::Vec3 PhysXManager::toOsgVec3( const NxVec3& point )
105 {
106  return osg::Vec3(point.x, point.y, point.z);
107 }
108 
109 osg::Quat PhysXManager::toOsgQuat( const NxQuat& qRot )
110 {
111  return osg::Quat(qRot.x, qRot.y, qRot.z, qRot.w);
112 }
113 
114 inline NxVec3 PhysXManager::transformToLocalCoord( Hoop* hoop, const osg::Vec3& point )
115 {
116  osg::Vec3 localPoint = hoop->getMatrix().getRotate().inverse() * (point - hoop->getPosition());
117  return toNxVec3(localPoint);
118 }
119 
121 {
123  return eo->getMatrix();
124 }
125 
126 }