OpenMW
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
physicssystem.hpp
Go to the documentation of this file.
1 #ifndef OPENMW_MWPHYSICS_PHYSICSSYSTEM_H
2 #define OPENMW_MWPHYSICS_PHYSICSSYSTEM_H
3 
4 #include <memory>
5 #include <map>
6 #include <set>
7 
8 #include <osg/Quat>
9 #include <osg/ref_ptr>
10 
11 #include "../mwworld/ptr.hpp"
12 
13 #include "collisiontype.hpp"
14 
15 namespace osg
16 {
17  class Group;
18 }
19 
20 namespace MWRender
21 {
22  class DebugDrawer;
23 }
24 
25 namespace Resource
26 {
27  class BulletShapeManager;
28  class ResourceSystem;
29 }
30 
31 namespace SceneUtil
32 {
33  class UnrefQueue;
34 }
35 
36 class btCollisionWorld;
37 class btBroadphaseInterface;
38 class btDefaultCollisionConfiguration;
39 class btCollisionDispatcher;
40 class btCollisionObject;
41 class btCollisionShape;
42 
43 namespace MWPhysics
44 {
45  typedef std::vector<std::pair<MWWorld::Ptr,osg::Vec3f> > PtrVelocityList;
46 
47  class HeightField;
48  class Object;
49  class Actor;
50 
52  {
53  public:
54  PhysicsSystem (Resource::ResourceSystem* resourceSystem, osg::ref_ptr<osg::Group> parentNode);
55  ~PhysicsSystem ();
56 
57  void setUnrefQueue(SceneUtil::UnrefQueue* unrefQueue);
58 
60 
61  void enableWater(float height);
62  void setWaterHeight(float height);
63  void disableWater();
64 
65  void addObject (const MWWorld::Ptr& ptr, const std::string& mesh, int collisionType = CollisionType_World);
66  void addActor (const MWWorld::Ptr& ptr, const std::string& mesh);
67 
68  void updatePtr (const MWWorld::Ptr& old, const MWWorld::Ptr& updated);
69 
70  Actor* getActor(const MWWorld::Ptr& ptr);
71  const Actor* getActor(const MWWorld::ConstPtr& ptr) const;
72 
73  const Object* getObject(const MWWorld::ConstPtr& ptr) const;
74 
75  // Object or Actor
76  void remove (const MWWorld::Ptr& ptr);
77 
78  void updateScale (const MWWorld::Ptr& ptr);
79  void updateRotation (const MWWorld::Ptr& ptr);
80  void updatePosition (const MWWorld::Ptr& ptr);
81 
82 
83  void addHeightField (const float* heights, int x, int y, float triSize, float sqrtVerts);
84 
85  void removeHeightField (int x, int y);
86 
87  bool toggleCollisionMode();
88 
89  void stepSimulation(float dt);
90  void debugDraw();
91 
92  std::vector<MWWorld::Ptr> getCollisions(const MWWorld::ConstPtr &ptr, int collisionGroup, int collisionMask) const;
93  osg::Vec3f traceDown(const MWWorld::Ptr &ptr, float maxHeight);
94 
95  std::pair<MWWorld::Ptr, osg::Vec3f> getHitContact(const MWWorld::ConstPtr& actor,
96  const osg::Vec3f &origin,
97  const osg::Quat &orientation,
98  float queryDistance);
99 
100 
105  float getHitDistance(const osg::Vec3f& point, const MWWorld::ConstPtr& target) const;
106 
107  struct RayResult
108  {
109  bool mHit;
110  osg::Vec3f mHitPos;
111  osg::Vec3f mHitNormal;
113  };
114 
116  RayResult castRay(const osg::Vec3f &from, const osg::Vec3f &to, MWWorld::ConstPtr ignore = MWWorld::ConstPtr(), int mask =
118 
119  RayResult castSphere(const osg::Vec3f& from, const osg::Vec3f& to, float radius);
120 
122  bool getLineOfSight(const MWWorld::ConstPtr& actor1, const MWWorld::ConstPtr& actor2) const;
123 
124  bool isOnGround (const MWWorld::Ptr& actor);
125 
126  bool canMoveToWaterSurface (const MWWorld::ConstPtr &actor, const float waterlevel);
127 
129  osg::Vec3f getHalfExtents(const MWWorld::ConstPtr& actor) const;
130 
132  osg::Vec3f getRenderingHalfExtents(const MWWorld::ConstPtr& actor) const;
133 
136  osg::Vec3f getCollisionObjectPosition(const MWWorld::ConstPtr& actor) const;
137 
140  void queueObjectMovement(const MWWorld::Ptr &ptr, const osg::Vec3f &velocity);
141 
143  const PtrVelocityList& applyQueuedMovement(float dt);
144 
146  void clearQueuedMovement();
147 
151  bool isActorStandingOn(const MWWorld::Ptr& actor, const MWWorld::ConstPtr& object) const;
152 
154  void getActorsStandingOn(const MWWorld::ConstPtr& object, std::vector<MWWorld::Ptr>& out) const;
155 
158  bool isActorCollidingWith(const MWWorld::Ptr& actor, const MWWorld::ConstPtr& object) const;
159 
161  void getActorsCollidingWith(const MWWorld::ConstPtr& object, std::vector<MWWorld::Ptr>& out) const;
162 
163  bool toggleDebugRendering();
164 
167  void markAsNonSolid (const MWWorld::ConstPtr& ptr);
168 
169  bool isOnSolidGround (const MWWorld::Ptr& actor) const;
170 
171  private:
172 
173  void updateWater();
174 
175  osg::ref_ptr<SceneUtil::UnrefQueue> mUnrefQueue;
176 
177  btBroadphaseInterface* mBroadphase;
178  btDefaultCollisionConfiguration* mCollisionConfiguration;
179  btCollisionDispatcher* mDispatcher;
180  btCollisionWorld* mCollisionWorld;
181 
182  std::auto_ptr<Resource::BulletShapeManager> mShapeManager;
184 
185  typedef std::map<MWWorld::ConstPtr, Object*> ObjectMap;
187 
188  std::set<Object*> mAnimatedObjects; // stores pointers to elements in mObjects
189 
190  typedef std::map<MWWorld::ConstPtr, Actor*> ActorMap;
192 
193  typedef std::map<std::pair<int, int>, HeightField*> HeightFieldMap;
195 
197 
198  // Tracks standing collisions happening during a single frame. <actor handle, collided handle>
199  // This will detect standing on an object, but won't detect running e.g. against a wall.
200  typedef std::map<MWWorld::Ptr, MWWorld::Ptr> CollisionMap;
202 
203  // replaces all occurrences of 'old' in the map by 'updated', no matter if it's a key or value
204  void updateCollisionMapPtr(CollisionMap& map, const MWWorld::Ptr &old, const MWWorld::Ptr &updated);
205 
208 
209  float mTimeAccum;
210 
213 
214  std::auto_ptr<btCollisionObject> mWaterCollisionObject;
215  std::auto_ptr<btCollisionShape> mWaterCollisionShape;
216 
217  std::auto_ptr<MWRender::DebugDrawer> mDebugDrawer;
218 
219  osg::ref_ptr<osg::Group> mParentNode;
220 
221  PhysicsSystem (const PhysicsSystem&);
223  };
224 }
225 
226 #endif
std::vector< std::pair< MWWorld::Ptr, osg::Vec3f > > PtrVelocityList
Definition: physicssystem.hpp:45
void disableWater()
Definition: physicssystem.cpp:1470
Resource::ResourceSystem * mResourceSystem
Definition: physicssystem.hpp:183
void updatePtr(const MWWorld::Ptr &old, const MWWorld::Ptr &updated)
Definition: physicssystem.cpp:1198
Wrapper class that constructs and provides access to the most commonly used resource subsystems...
Definition: resourcesystem.hpp:24
RayResult castRay(const osg::Vec3f &from, const osg::Vec3f &to, MWWorld::ConstPtr ignore=MWWorld::ConstPtr(), int mask=CollisionType_World|CollisionType_HeightMap|CollisionType_Actor|CollisionType_Door, int group=0xff) const
Definition: physicssystem.cpp:915
std::auto_ptr< btCollisionShape > mWaterCollisionShape
Definition: physicssystem.hpp:215
std::set< Object * > mAnimatedObjects
Definition: physicssystem.hpp:188
osg::ref_ptr< osg::Group > mParentNode
Definition: physicssystem.hpp:219
std::auto_ptr< Resource::BulletShapeManager > mShapeManager
Definition: physicssystem.hpp:182
HeightFieldMap mHeightFields
Definition: physicssystem.hpp:194
void addActor(const MWWorld::Ptr &ptr, const std::string &mesh)
Definition: physicssystem.cpp:1300
float mWaterHeight
Definition: physicssystem.hpp:211
btCollisionDispatcher * mDispatcher
Definition: physicssystem.hpp:179
float mTimeAccum
Definition: physicssystem.hpp:209
void getActorsStandingOn(const MWWorld::ConstPtr &object, std::vector< MWWorld::Ptr > &out) const
Get the handle of all actors standing on object in this frame.
Definition: physicssystem.cpp:1449
void enableWater(float height)
Definition: physicssystem.cpp:1479
void setUnrefQueue(SceneUtil::UnrefQueue *unrefQueue)
Definition: physicssystem.cpp:737
btCollisionWorld * mCollisionWorld
Definition: physicssystem.hpp:180
bool mDebugDrawEnabled
Definition: physicssystem.hpp:196
btDefaultCollisionConfiguration * mCollisionConfiguration
Definition: physicssystem.hpp:178
const Object * getObject(const MWWorld::ConstPtr &ptr) const
Definition: physicssystem.cpp:1237
ObjectMap mObjects
Definition: physicssystem.hpp:186
PtrVelocityList mMovementQueue
Definition: physicssystem.hpp:206
ActorMap mActors
Definition: physicssystem.hpp:191
Definition: collisiontype.hpp:8
osg::ref_ptr< SceneUtil::UnrefQueue > mUnrefQueue
Definition: physicssystem.hpp:175
bool mHit
Definition: physicssystem.hpp:109
std::map< std::pair< int, int >, HeightField * > HeightFieldMap
Definition: physicssystem.hpp:193
Definition: collisiontype.hpp:11
const PtrVelocityList & applyQueuedMovement(float dt)
Apply all queued movements, then clear the list.
Definition: physicssystem.cpp:1344
void markAsNonSolid(const MWWorld::ConstPtr &ptr)
Definition: physicssystem.cpp:762
Definition: physicssystem.hpp:51
Definition: collisiontype.hpp:9
bool toggleDebugRendering()
Definition: physicssystem.cpp:747
~PhysicsSystem()
Definition: physicssystem.cpp:707
std::map< MWWorld::Ptr, MWWorld::Ptr > CollisionMap
Definition: physicssystem.hpp:200
void addHeightField(const float *heights, int x, int y, float triSize, float sqrtVerts)
Definition: physicssystem.cpp:1122
osg::Vec3f getHalfExtents(const MWWorld::ConstPtr &actor) const
Get physical half extents (scaled) of the given actor.
Definition: physicssystem.cpp:1043
Definition: collisiontype.hpp:10
bool isOnGround(const MWWorld::Ptr &actor)
Definition: physicssystem.cpp:1002
void updateRotation(const MWWorld::Ptr &ptr)
Definition: physicssystem.cpp:1264
void getActorsCollidingWith(const MWWorld::ConstPtr &object, std::vector< MWWorld::Ptr > &out) const
Get the handle of all actors colliding with object in this frame.
Definition: physicssystem.cpp:1464
std::pair< MWWorld::Ptr, osg::Vec3f > getHitContact(const MWWorld::ConstPtr &actor, const osg::Vec3f &origin, const osg::Quat &orientation, float queryDistance)
Definition: physicssystem.cpp:828
void updatePosition(const MWWorld::Ptr &ptr)
Definition: physicssystem.cpp:1282
void clearQueuedMovement()
Clear the queued movements list without applying.
Definition: physicssystem.cpp:1338
PtrVelocityList mMovementResults
Definition: physicssystem.hpp:207
bool toggleCollisionMode()
Definition: physicssystem.cpp:1309
std::auto_ptr< MWRender::DebugDrawer > mDebugDrawer
Definition: physicssystem.hpp:217
osg::Vec3f traceDown(const MWWorld::Ptr &ptr, float maxHeight)
Definition: physicssystem.cpp:1113
Pointer to a const LiveCellRef.
Definition: ptr.hpp:90
std::auto_ptr< btCollisionObject > mWaterCollisionObject
Definition: physicssystem.hpp:214
void stepSimulation(float dt)
Definition: physicssystem.cpp:1422
bool isActorCollidingWith(const MWWorld::Ptr &actor, const MWWorld::ConstPtr &object) const
Definition: physicssystem.cpp:1458
osg::Vec3f getRenderingHalfExtents(const MWWorld::ConstPtr &actor) const
Definition: physicssystem.cpp:1052
float mWaterEnabled
Definition: physicssystem.hpp:212
std::vector< MWWorld::Ptr > getCollisions(const MWWorld::ConstPtr &ptr, int collisionGroup, int collisionMask) const
get handles this object collides with
Definition: physicssystem.cpp:1096
void updateCollisionMapPtr(CollisionMap &map, const MWWorld::Ptr &old, const MWWorld::Ptr &updated)
Definition: physicssystem.cpp:1182
Definition: physicssystem.cpp:561
osg::Vec3f mHitPos
Definition: physicssystem.hpp:110
btBroadphaseInterface * mBroadphase
Definition: physicssystem.hpp:177
void removeHeightField(int x, int y)
Definition: physicssystem.cpp:1131
Actor * getActor(const MWWorld::Ptr &ptr)
Definition: physicssystem.cpp:1221
void debugDraw()
Definition: physicssystem.cpp:1433
RayResult castSphere(const osg::Vec3f &from, const osg::Vec3f &to, float radius)
Definition: physicssystem.cpp:952
void setWaterHeight(float height)
Definition: physicssystem.cpp:1489
bool isActorStandingOn(const MWWorld::Ptr &actor, const MWWorld::ConstPtr &object) const
Definition: physicssystem.cpp:1439
void updateScale(const MWWorld::Ptr &ptr)
Definition: physicssystem.cpp:1245
bool isOnSolidGround(const MWWorld::Ptr &actor) const
Definition: physicssystem.cpp:771
CollisionMap mStandingCollisions
Definition: physicssystem.hpp:201
osg::Vec3f getCollisionObjectPosition(const MWWorld::ConstPtr &actor) const
Definition: physicssystem.cpp:1061
std::map< MWWorld::ConstPtr, Object * > ObjectMap
Definition: physicssystem.hpp:185
Definition: physicssystem.hpp:107
PhysicsSystem & operator=(const PhysicsSystem &)
bool getLineOfSight(const MWWorld::ConstPtr &actor1, const MWWorld::ConstPtr &actor2) const
Return true if actor1 can see actor2.
Definition: physicssystem.cpp:976
Handles unreferencing of objects through the WorkQueue. Typical use scenario would be the main thread...
Definition: unrefqueue.hpp:19
Pointer to a LiveCellRef.
Definition: ptr.hpp:19
PhysicsSystem(Resource::ResourceSystem *resourceSystem, osg::ref_ptr< osg::Group > parentNode)
Definition: physicssystem.cpp:685
osg::Vec3f mHitNormal
Definition: physicssystem.hpp:111
std::map< MWWorld::ConstPtr, Actor * > ActorMap
Definition: physicssystem.hpp:190
Resource::BulletShapeManager * getShapeManager()
Definition: physicssystem.cpp:742
void updateWater()
Definition: physicssystem.cpp:1498
bool canMoveToWaterSurface(const MWWorld::ConstPtr &actor, const float waterlevel)
Definition: physicssystem.cpp:1029
MWWorld::Ptr mHitObject
Definition: physicssystem.hpp:112
Definition: physicssystem.cpp:509
Definition: bulletshapemanager.hpp:25
void queueObjectMovement(const MWWorld::Ptr &ptr, const osg::Vec3f &velocity)
Definition: physicssystem.cpp:1323
Definition: actor.hpp:48
void addObject(const MWWorld::Ptr &ptr, const std::string &mesh, int collisionType=CollisionType_World)
Definition: physicssystem.cpp:1142
float getHitDistance(const osg::Vec3f &point, const MWWorld::ConstPtr &target) const
Definition: physicssystem.cpp:866