Overte C++ Documentation
PhysicalEntitySimulation.h
1 //
2 // PhysicalEntitySimulation.h
3 // libraries/physics/src
4 //
5 // Created by Andrew Meadows 2015.04.27
6 // Copyright 2015 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_PhysicalEntitySimulation_h
13 #define hifi_PhysicalEntitySimulation_h
14 
15 #include <stdint.h>
16 #include <map>
17 #include <set>
18 
19 #include <btBulletDynamicsCommon.h>
20 #include <BulletCollision/CollisionDispatch/btGhostObject.h>
21 
22 #include <EntityDynamicInterface.h>
23 #include <EntityItem.h>
24 #include <EntitySimulation.h>
25 #include <workload/Space.h>
26 
27 #include "PhysicsEngine.h"
28 #include "EntityMotionState.h"
29 
30 class PhysicalEntitySimulation;
31 using PhysicalEntitySimulationPointer = std::shared_ptr<PhysicalEntitySimulation>;
32 using SetOfEntityMotionStates = QSet<EntityMotionState*>;
33 
34 class VectorOfEntityMotionStates: public std::vector<EntityMotionState*> {
35 public:
36  void remove(uint32_t index) {
37  assert(index < size());
38  if (index < size() - 1) {
39  (*this)[index] = back();
40  }
41  pop_back();
42  }
43  void removeFirst(EntityMotionState* state) {
44  for (uint32_t i = 0; i < size(); ++i) {
45  if ((*this)[i] == state) {
46  remove(i);
47  break;
48  }
49  }
50  }
51 };
52 
53 class PhysicalEntitySimulation : public EntitySimulation {
54  Q_OBJECT
55 public:
56  PhysicalEntitySimulation();
57  ~PhysicalEntitySimulation();
58 
59  void init(EntityTreePointer tree, PhysicsEnginePointer engine, EntityEditPacketSender* packetSender);
60  void setWorkloadSpace(const workload::SpacePointer space) { _space = space; }
61 
62  void addDynamic(EntityDynamicPointer dynamic) override;
63  void removeDynamic(const QUuid dynamicID) override;
64  void applyDynamicChanges() override;
65 
66  void takeDeadAvatarEntities(SetOfEntities& deadEntities);
67 
68  virtual void clearEntities() override;
69  void queueEraseDomainEntity(const QUuid& id) const override;
70 
71 signals:
72  void entityCollisionWithEntity(const EntityItemID& idA, const EntityItemID& idB, const Collision& collision);
73 
74 protected: // only called by EntitySimulation
75  // overrides for EntitySimulation
76  void addEntityToInternalLists(EntityItemPointer entity) override;
77  void removeEntityFromInternalLists(EntityItemPointer entity) override;
78  void processChangedEntity(const EntityItemPointer& entity) override;
79  void processDeadEntities() override;
80 
81  void removeOwnershipData(EntityMotionState* motionState);
82  void clearOwnershipData();
83 
84 public:
85  virtual void prepareEntityForDelete(EntityItemPointer entity) override;
86  void removeDeadEntities();
87 
88  void buildPhysicsTransaction(PhysicsEngine::Transaction& transaction);
89  void handleProcessedPhysicsTransaction(PhysicsEngine::Transaction& transaction);
90 
91  void handleDeactivatedMotionStates(const VectorOfMotionStates& motionStates);
92  void handleChangedMotionStates(const VectorOfMotionStates& motionStates);
93  void handleCollisionEvents(const CollisionEvents& collisionEvents);
94 
95  EntityEditPacketSender* getPacketSender() { return _entityPacketSender; }
96 
97  void addOwnershipBid(EntityMotionState* motionState);
98  void addOwnership(EntityMotionState* motionState);
99  void sendOwnershipBids(uint32_t numSubsteps);
100  void sendOwnedUpdates(uint32_t numSubsteps);
101 
102 private:
103  void buildMotionStatesForEntitiesThatNeedThem();
104 
105  class ShapeRequest {
106  public:
107  ShapeRequest() { }
108  ShapeRequest(const EntityItemPointer& e) : entity(e) { }
109  bool operator<(const ShapeRequest& other) const { return entity.get() < other.entity.get(); }
110  bool operator==(const ShapeRequest& other) const { return entity.get() == other.entity.get(); }
111  EntityItemPointer entity { nullptr };
112  mutable uint64_t shapeHash { 0 };
113  };
114  SetOfEntities _entitiesToAddToPhysics; // we could also call this: _entitiesThatNeedMotionStates
115  SetOfEntities _entitiesToRemoveFromPhysics;
116  SetOfEntityMotionStates _incomingChanges; // EntityMotionStates changed by external events
117  SetOfMotionStates _physicalObjects; // MotionStates of entities in PhysicsEngine
118 
119  using ShapeRequests = std::set<ShapeRequest>;
120  ShapeRequests _shapeRequests;
121 
122  PhysicsEnginePointer _physicsEngine = nullptr;
123  EntityEditPacketSender* _entityPacketSender = nullptr;
124 
125  VectorOfEntityMotionStates _owned;
126  VectorOfEntityMotionStates _bids;
127  SetOfEntities _deadAvatarEntities; // to remove from Avatar's lists
128  std::vector<EntityItemPointer> _entitiesToDeleteLater;
129 
130  QList<EntityDynamicPointer> _dynamicsToAdd;
131  QSet<QUuid> _dynamicsToRemove;
132  QRecursiveMutex _dynamicsMutex;
133 
134  workload::SpacePointer _space;
135  uint64_t _nextBidExpiry;
136  uint32_t _lastStepSendPackets { 0 };
137  uint32_t _lastWorkDeliveryCount { 0 };
138 };
139 
140 
141 typedef std::shared_ptr<PhysicalEntitySimulation> PhysicalEntitySimulationPointer;
142 
143 #endif // hifi_PhysicalEntitySimulation_h
Utility for processing, packing, queueing and sending of outbound edit voxel messages.
Definition: EntityEditPacketSender.h:25
Abstract ID for editing model items. Used in EntityItem JS API.
Definition: EntityItemID.h:28