12 #ifndef hifi_PhysicalEntitySimulation_h
13 #define hifi_PhysicalEntitySimulation_h
19 #include <btBulletDynamicsCommon.h>
20 #include <BulletCollision/CollisionDispatch/btGhostObject.h>
22 #include <EntityDynamicInterface.h>
23 #include <EntityItem.h>
24 #include <EntitySimulation.h>
25 #include <workload/Space.h>
27 #include "PhysicsEngine.h"
28 #include "EntityMotionState.h"
30 class PhysicalEntitySimulation;
31 using PhysicalEntitySimulationPointer = std::shared_ptr<PhysicalEntitySimulation>;
32 using SetOfEntityMotionStates = QSet<EntityMotionState*>;
34 class VectorOfEntityMotionStates:
public std::vector<EntityMotionState*> {
36 void remove(uint32_t index) {
37 assert(index < size());
38 if (index < size() - 1) {
39 (*this)[index] = back();
43 void removeFirst(EntityMotionState* state) {
44 for (uint32_t i = 0; i < size(); ++i) {
45 if ((*
this)[i] == state) {
53 class PhysicalEntitySimulation :
public EntitySimulation {
56 PhysicalEntitySimulation();
57 ~PhysicalEntitySimulation();
60 void setWorkloadSpace(
const workload::SpacePointer space) { _space = space; }
62 void addDynamic(EntityDynamicPointer dynamic)
override;
63 void removeDynamic(
const QUuid dynamicID)
override;
64 void applyDynamicChanges()
override;
66 void takeDeadAvatarEntities(SetOfEntities& deadEntities);
68 virtual void clearEntities()
override;
69 void queueEraseDomainEntity(
const QUuid&
id)
const override;
76 void addEntityToInternalLists(EntityItemPointer entity)
override;
77 void removeEntityFromInternalLists(EntityItemPointer entity)
override;
78 void processChangedEntity(
const EntityItemPointer& entity)
override;
79 void processDeadEntities()
override;
81 void removeOwnershipData(EntityMotionState* motionState);
82 void clearOwnershipData();
85 virtual void prepareEntityForDelete(EntityItemPointer entity)
override;
86 void removeDeadEntities();
88 void buildPhysicsTransaction(PhysicsEngine::Transaction& transaction);
89 void handleProcessedPhysicsTransaction(PhysicsEngine::Transaction& transaction);
91 void handleDeactivatedMotionStates(
const VectorOfMotionStates& motionStates);
92 void handleChangedMotionStates(
const VectorOfMotionStates& motionStates);
93 void handleCollisionEvents(
const CollisionEvents& collisionEvents);
97 void addOwnershipBid(EntityMotionState* motionState);
98 void addOwnership(EntityMotionState* motionState);
99 void sendOwnershipBids(uint32_t numSubsteps);
100 void sendOwnedUpdates(uint32_t numSubsteps);
103 void buildMotionStatesForEntitiesThatNeedThem();
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 };
114 SetOfEntities _entitiesToAddToPhysics;
115 SetOfEntities _entitiesToRemoveFromPhysics;
116 SetOfEntityMotionStates _incomingChanges;
117 SetOfMotionStates _physicalObjects;
119 using ShapeRequests = std::set<ShapeRequest>;
120 ShapeRequests _shapeRequests;
122 PhysicsEnginePointer _physicsEngine =
nullptr;
125 VectorOfEntityMotionStates _owned;
126 VectorOfEntityMotionStates _bids;
127 SetOfEntities _deadAvatarEntities;
128 std::vector<EntityItemPointer> _entitiesToDeleteLater;
130 QList<EntityDynamicPointer> _dynamicsToAdd;
131 QSet<QUuid> _dynamicsToRemove;
132 QRecursiveMutex _dynamicsMutex;
134 workload::SpacePointer _space;
135 uint64_t _nextBidExpiry;
136 uint32_t _lastStepSendPackets { 0 };
137 uint32_t _lastWorkDeliveryCount { 0 };
141 typedef std::shared_ptr<PhysicalEntitySimulation> PhysicalEntitySimulationPointer;
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