Overte C++ Documentation
ObjectMotionState.h
1 //
2 // ObjectMotionState.h
3 // libraries/physics/src
4 //
5 // Created by Andrew Meadows 2014.11.05
6 // Copyright 2014 High Fidelity, Inc.
7 //
8 // Distributed under the Apache License, Version 2.0.
9 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10 //
11 
12 #ifndef hifi_ObjectMotionState_h
13 #define hifi_ObjectMotionState_h
14 
15 #include <btBulletDynamicsCommon.h>
16 #include <glm/glm.hpp>
17 
18 #include <QSet>
19 #include <QVector>
20 
21 #include <SimulationFlags.h>
22 
23 #include "ContactInfo.h"
24 #include "ShapeManager.h"
25 
26 enum PhysicsMotionType {
27  MOTION_TYPE_STATIC, // no motion
28  MOTION_TYPE_DYNAMIC, // motion according to physical laws
29  MOTION_TYPE_KINEMATIC // keyframed motion
30 };
31 
32 /*@jsdoc
33  * <p>An entity's physics motion type may be one of the following:</p>
34  * <table>
35  * <thead>
36  * <tr><th>Value</th><th>Description</th></tr>
37  * </thead>
38  * <tbody>
39  * <tr><td><code>"static"</code></td><td>There is no motion because the entity is locked &mdash; its <code>locked</code>
40  * property is set to <code>true</code>.</td></tr>
41  * <tr><td><code>"kinematic"</code></td><td>Motion is applied without physical laws (e.g., damping) because the entity is
42  * not locked and has its <code>dynamic</code> property set to <code>false</code>.</td></tr>
43  * <tr><td><code>"dynamic"</code></td><td>Motion is applied according to physical laws (e.g., damping) because the entity
44  * is not locked and has its <code>dynamic</code> property set to <code>true</code>.</td></tr>
45  * </tbody>
46  * </table>
47  * @typedef {string} Entities.PhysicsMotionType
48  */
49 inline QString motionTypeToString(PhysicsMotionType motionType) {
50  switch(motionType) {
51  case MOTION_TYPE_STATIC: return QString("static");
52  case MOTION_TYPE_DYNAMIC: return QString("dynamic");
53  case MOTION_TYPE_KINEMATIC: return QString("kinematic");
54  }
55  return QString("unknown");
56 }
57 
58 enum MotionStateType {
59  MOTIONSTATE_TYPE_INVALID,
60  MOTIONSTATE_TYPE_ENTITY,
61  MOTIONSTATE_TYPE_AVATAR,
62  MOTIONSTATE_TYPE_DETAILED
63 };
64 
65 // The update flags trigger two varieties of updates: "hard" which require the body to be pulled
66 // and re-added to the physics engine and "easy" which just updates the body properties.
67 const uint32_t HARD_DIRTY_PHYSICS_FLAGS = (uint32_t)(Simulation::DIRTY_MOTION_TYPE | Simulation::DIRTY_SHAPE |
68  Simulation::DIRTY_COLLISION_GROUP);
69 const uint32_t EASY_DIRTY_PHYSICS_FLAGS = (uint32_t)(Simulation::DIRTY_TRANSFORM | Simulation::DIRTY_VELOCITIES |
70  Simulation::DIRTY_MASS | Simulation::DIRTY_MATERIAL |
71  Simulation::DIRTY_SIMULATOR_ID | Simulation::DIRTY_SIMULATION_OWNERSHIP_PRIORITY |
72  Simulation::DIRTY_PHYSICS_ACTIVATION);
73 
74 
75 // These are the set of incoming flags that the PhysicsEngine needs to hear about:
76 const uint32_t DIRTY_PHYSICS_FLAGS = (uint32_t)(HARD_DIRTY_PHYSICS_FLAGS | EASY_DIRTY_PHYSICS_FLAGS);
77 
78 // These are the outgoing flags that the PhysicsEngine can affect:
79 const uint32_t OUTGOING_DIRTY_PHYSICS_FLAGS = Simulation::DIRTY_TRANSFORM | Simulation::DIRTY_VELOCITIES;
80 
81 
83 class PhysicsEngine;
84 
85 class ObjectMotionState : public btMotionState {
86 public:
87  // These properties of the PhysicsEngine are "global" within the context of all ObjectMotionStates
88  // (assuming just one PhysicsEngine). They are cached as statics for fast calculations in the
89  // ObjectMotionState context.
90  static void setWorldOffset(const glm::vec3& offset);
91  static const glm::vec3& getWorldOffset();
92 
93  static void setWorldSimulationStep(uint32_t step);
94  static uint32_t getWorldSimulationStep();
95 
96  static void setShapeManager(ShapeManager* manager);
97  static ShapeManager* getShapeManager();
98 
99  ObjectMotionState(const btCollisionShape* shape);
100  virtual ~ObjectMotionState();
101 
102  virtual void handleEasyChanges(uint32_t& flags);
103 
104  void updateBodyMaterialProperties();
105  void updateBodyVelocities();
106  void updateLastKinematicStep();
107 
108  virtual void updateBodyMassProperties();
109 
110  MotionStateType getType() const { return _type; }
111  virtual PhysicsMotionType getMotionType() const { return _motionType; }
112 
113  virtual void setMass(float mass);
114  virtual float getMass() const;
115 
116  void setBodyLinearVelocity(const glm::vec3& velocity) const;
117  void setBodyAngularVelocity(const glm::vec3& velocity) const;
118  void setBodyGravity(const glm::vec3& gravity) const;
119 
120  glm::vec3 getBodyLinearVelocity() const;
121  glm::vec3 getBodyLinearVelocityGTSigma() const;
122  glm::vec3 getBodyAngularVelocity() const;
123  virtual glm::vec3 getObjectLinearVelocityChange() const;
124 
125  virtual uint32_t getIncomingDirtyFlags() const = 0;
126  virtual void clearIncomingDirtyFlags(uint32_t mask = DIRTY_PHYSICS_FLAGS) = 0;
127 
128  virtual PhysicsMotionType computePhysicsMotionType() const = 0;
129 
130  virtual bool needsNewShape() const { return _shape == nullptr || getIncomingDirtyFlags() & Simulation::DIRTY_SHAPE; }
131  const btCollisionShape* getShape() const { return _shape; }
132  btRigidBody* getRigidBody() const { return _body; }
133 
134  virtual bool isMoving() const = 0;
135 
136  // These pure virtual methods must be implemented for each MotionState type
137  // and make it possible to implement more complicated methods in this base class.
138 
139  virtual float getObjectRestitution() const = 0;
140  virtual float getObjectFriction() const = 0;
141  virtual float getObjectLinearDamping() const = 0;
142  virtual float getObjectAngularDamping() const = 0;
143 
144  virtual glm::vec3 getObjectPosition() const = 0;
145  virtual glm::quat getObjectRotation() const = 0;
146  virtual glm::vec3 getObjectLinearVelocity() const = 0;
147  virtual glm::vec3 getObjectAngularVelocity() const = 0;
148  virtual glm::vec3 getObjectGravity() const = 0;
149 
150  virtual const QUuid getObjectID() const = 0;
151 
152  virtual uint8_t getSimulationPriority() const { return 0; }
153  virtual QUuid getSimulatorID() const = 0;
154  virtual void bump(uint8_t priority) {}
155 
156  virtual QString getName() const { return ""; }
157  virtual ShapeType getShapeType() const = 0;
158 
159  virtual void computeCollisionGroupAndMask(int32_t& group, int32_t& mask) const = 0;
160 
161  bool isActive() const { return _body ? _body->isActive() : false; }
162 
163  bool hasInternalKinematicChanges() const { return _hasInternalKinematicChanges; }
164 
165  // these methods are declared const so they can be called inside other const methods
166  void dirtyInternalKinematicChanges() const { _hasInternalKinematicChanges = true; }
167  void clearInternalKinematicChanges() const { _hasInternalKinematicChanges = false; }
168 
169  virtual bool isLocallyOwned() const { return false; }
170  virtual bool isLocallyOwnedOrShouldBe() const { return false; } // aka shouldEmitCollisionEvents()
171  virtual void saveKinematicState(btScalar timeStep);
172 
173  friend class PhysicsEngine;
174 
175 protected:
176  virtual void setMotionType(PhysicsMotionType motionType);
177  void updateCCDConfiguration();
178 
179  virtual void setRigidBody(btRigidBody* body);
180  virtual void setShape(const btCollisionShape* shape);
181 
182  MotionStateType _type { MOTIONSTATE_TYPE_INVALID }; // type of MotionState
183  PhysicsMotionType _motionType { MOTION_TYPE_STATIC }; // type of motion: KINEMATIC, DYNAMIC, or STATIC
184 
185  const btCollisionShape* _shape { nullptr };
186  btRigidBody* _body { nullptr };
187  float _density { 1.0f };
188 
189  // ACTION_CAN_CONTROL_KINEMATIC_OBJECT_HACK: These data members allow an Action
190  // to operate on a kinematic object without screwing up our default kinematic integration
191  // which is done in the MotionState::getWorldTransform().
192  mutable uint32_t _lastKinematicStep;
193  mutable bool _hasInternalKinematicChanges { false };
194 };
195 
196 using SetOfMotionStates = QSet<ObjectMotionState*>;
197 using VectorOfMotionStates = QVector<ObjectMotionState*>;
198 
199 #endif // hifi_ObjectMotionState_h
Utility for processing, packing, queueing and sending of outbound edit messages.
Definition: OctreeEditPacketSender.h:23