Overte C++ Documentation
EntityMotionState.h
1 //
2 // EntityMotionState.h
3 // libraries/entities/src
4 //
5 // Created by Andrew Meadows on 2014.11.06
6 // Copyright 2013 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_EntityMotionState_h
13 #define hifi_EntityMotionState_h
14 
15 #include <EntityItem.h>
16 #include <EntityTypes.h>
17 #include <AACube.h>
18 #include <workload/Region.h>
19 
20 #include "ObjectMotionState.h"
21 
22 
23 // From the MotionState's perspective:
24 // Inside = physics simulation
25 // Outside = external agents (scripts, user interaction, other simulations)
26 
27 class EntityMotionState : public ObjectMotionState {
28 public:
29  enum class OwnershipState {
30  NotLocallyOwned = 0,
31  PendingBid,
32  LocallyOwned,
33  Unownable
34  };
35 
36  EntityMotionState() = delete;
37  EntityMotionState(btCollisionShape* shape, EntityItemPointer item);
38  virtual ~EntityMotionState();
39 
40  void handleDeactivation();
41  virtual void handleEasyChanges(uint32_t& flags) override;
42 
44  virtual PhysicsMotionType computePhysicsMotionType() const override;
45 
46  virtual bool isMoving() const override;
47 
48  // this relays incoming position/rotation to the RigidBody
49  virtual void getWorldTransform(btTransform& worldTrans) const override;
50 
51  // this relays outgoing position/rotation to the EntityItem
52  virtual void setWorldTransform(const btTransform& worldTrans) override;
53 
54  bool shouldSendUpdate(uint32_t simulationStep);
55  void sendBid(OctreeEditPacketSender* packetSender, uint32_t step);
56  void sendUpdate(OctreeEditPacketSender* packetSender, uint32_t step);
57 
58  virtual uint32_t getIncomingDirtyFlags() const override;
59  virtual void clearIncomingDirtyFlags(uint32_t mask = DIRTY_PHYSICS_FLAGS) override;
60 
61  virtual float getObjectRestitution() const override { return _entity->getRestitution(); }
62  virtual float getObjectFriction() const override { return _entity->getFriction(); }
63  virtual float getObjectLinearDamping() const override { return _entity->getDamping(); }
64  virtual float getObjectAngularDamping() const override { return _entity->getAngularDamping(); }
65 
66  virtual glm::vec3 getObjectPosition() const override { return _entity->getWorldPosition() - ObjectMotionState::getWorldOffset(); }
67  virtual glm::quat getObjectRotation() const override { return _entity->getWorldOrientation(); }
68  virtual glm::vec3 getObjectLinearVelocity() const override { return _entity->getWorldVelocity(); }
69  virtual glm::vec3 getObjectAngularVelocity() const override { return _entity->getWorldAngularVelocity(); }
70  virtual glm::vec3 getObjectGravity() const override { return _entity->getGravity(); }
71  virtual glm::vec3 getObjectLinearVelocityChange() const override;
72 
73  virtual const QUuid getObjectID() const override { return _entity->getID(); }
74 
75  virtual uint8_t getSimulationPriority() const override;
76  virtual QUuid getSimulatorID() const override;
77  virtual void bump(uint8_t priority) override;
78 
79  // getEntity() returns a smart-pointer by reference because it is only ever used
80  // to insert into lists of smart pointers, and the lists will make their own copies
81  const EntityItemPointer& getEntity() const { return _entity; }
82 
83  void resetMeasuredBodyAcceleration();
84  void measureBodyAcceleration();
85 
86  virtual QString getName() const override;
87  ShapeType getShapeType() const override { return _entity->getShapeType(); }
88 
89  virtual void computeCollisionGroupAndMask(int32_t& group, int32_t& mask) const override;
90 
91  bool shouldSendBid() const;
92 
93  bool isLocallyOwned() const override;
94  bool isLocallyOwnedOrShouldBe() const override; // aka shouldEmitCollisionEvents()
95 
96  friend class PhysicalEntitySimulation;
97  OwnershipState getOwnershipState() const { return _ownershipState; }
98 
99  void setRegion(uint8_t region);
100  void saveKinematicState(btScalar timeStep) override;
101 
102 protected:
103  void setRigidBody(btRigidBody* body) override;
104 
105  uint8_t computeFinalBidPriority() const;
106  void updateSendVelocities();
107  uint64_t getNextBidExpiry() const { return _nextBidExpiry; }
108  void initForBid();
109  void initForOwned();
110  void clearOwnershipState();
111  void updateServerPhysicsVariables();
112  bool remoteSimulationOutOfSync(uint32_t simulationStep);
113 
114  void slaveBidPriority(); // computeNewBidPriority() with value stored in _entity
115 
116  void clearObjectVelocities() const;
117 
118  bool isInPhysicsSimulation() const { return _body != nullptr; }
119  bool shouldBeInPhysicsSimulation() const;
120  void setMotionType(PhysicsMotionType motionType) override;
121 
122  // EntityMotionState keeps a SharedPointer to its EntityItem which is only set in the CTOR
123  // and is only cleared in the DTOR
124  EntityItemPointer _entity;
125 
126  // These "_serverFoo" variables represent what we think the server knows.
127  // They are used in two different modes:
128  //
129  // (1) For remotely owned simulation: we store the last values recieved from the server.
130  // When the body comes to rest and goes inactive we slam its final transforms to agree with the last server
131  // update. This to reduce state synchronization errors when the local simulation deviated from remote.
132  //
133  // (2) For locally owned simulation: we store the last values sent to the server, integrated forward over time
134  // according to how we think the server doing it. We calculate the error between the true local transform
135  // and the remote to decide whether to send another update or not.
136  //
137  glm::vec3 _serverPosition; // in simulation-frame (not world-frame)
138  glm::quat _serverRotation;
139  glm::vec3 _serverVelocity;
140  glm::vec3 _serverAngularVelocity; // radians per second
141  glm::vec3 _serverGravity;
142  glm::vec3 _serverAcceleration;
143  QByteArray _serverActionData;
144 
145  glm::vec3 _lastVelocity;
146  glm::vec3 _measuredAcceleration;
147  quint64 _nextBidExpiry { 0 };
148 
149  float _measuredDeltaTime;
150  uint32_t _lastMeasureStep;
151  uint32_t _lastStep; // last step of server extrapolation
152 
153  OwnershipState _ownershipState { OwnershipState::NotLocallyOwned };
154  uint8_t _loopsWithoutOwner;
155  mutable uint8_t _accelerationNearlyGravityCount;
156  uint8_t _numInactiveUpdates { 1 };
157  uint8_t _bumpedPriority { 0 }; // the target simulation priority according to collision history
158  uint8_t _region { workload::Region::INVALID };
159 
160  bool isServerlessMode();
161 };
162 
163 #endif // hifi_EntityMotionState_h
Utility for processing, packing, queueing and sending of outbound edit messages.
Definition: OctreeEditPacketSender.h:23