Plasma Engine  2.0
Loading...
Searching...
No Matches
JoltWorldModule.h
1#pragma once
2
3#include <Core/Interfaces/NavmeshGeoWorldModule.h>
4#include <Core/Interfaces/PhysicsWorldModule.h>
5#include <Core/World/Declarations.h>
6#include <Core/World/WorldModule.h>
7#include <Foundation/Types/UniquePtr.h>
8#include <JoltPlugin/Declarations.h>
9#include <JoltPlugin/JoltPluginDLL.h>
10#include <JoltPlugin/System/JoltCollisionFiltering.h>
11#include <JoltPlugin/Utilities/JoltUserData.h>
12#include <RendererCore/Meshes/DynamicMeshBufferResource.h>
13
18class plView;
19
20namespace JPH
21{
22 class Body;
23 class TempAllocator;
24 class PhysicsSystem;
25 class GroupFilter;
26} // namespace JPH
27
28class PL_JOLTPLUGIN_DLL plJoltWorldModule : public plPhysicsWorldModuleInterface
29{
30 PL_DECLARE_WORLD_MODULE();
31 PL_ADD_DYNAMIC_REFLECTION(plJoltWorldModule, plPhysicsWorldModuleInterface);
32
33public:
36
37 virtual void Initialize() override;
38 virtual void Deinitialize() override;
39 virtual void OnSimulationStarted() override;
40
41 JPH::PhysicsSystem* GetJoltSystem() { return m_pSystem.get(); }
42 const JPH::PhysicsSystem* GetJoltSystem() const { return m_pSystem.get(); }
43
44 plUInt32 CreateObjectFilterID();
45 void DeleteObjectFilterID(plUInt32& ref_uiObjectFilterID);
46
47 plUInt32 AllocateUserData(plJoltUserData*& out_pUserData);
48 void DeallocateUserData(plUInt32& ref_uiUserDataId);
49 const plJoltUserData& GetUserData(plUInt32 uiUserDataId) const;
50
51 void SetGravity(const plVec3& vObjectGravity, const plVec3& vCharacterGravity);
52 virtual plVec3 GetGravity() const override { return plVec3(0, 0, -10); }
53 plVec3 GetCharacterGravity() const { return m_Settings.m_vCharacterGravity; }
54
56 // plPhysicsWorldModuleInterface
57 //
58
59 virtual plUInt32 GetCollisionLayerByName(plStringView sName) const override;
60
61 virtual bool Raycast(plPhysicsCastResult& out_result, const plVec3& vStart, const plVec3& vDir, float fDistance, const plPhysicsQueryParameters& params, plPhysicsHitCollection collection = plPhysicsHitCollection::Closest) const override;
62
63 virtual bool RaycastAll(plPhysicsCastResultArray& out_results, const plVec3& vStart, const plVec3& vDir, float fDistance, const plPhysicsQueryParameters& params) const override;
64
65 virtual bool SweepTestSphere(plPhysicsCastResult& out_result, float fSphereRadius, const plVec3& vStart, const plVec3& vDir, float fDistance, const plPhysicsQueryParameters& params, plPhysicsHitCollection collection = plPhysicsHitCollection::Closest) const override;
66
67 virtual bool SweepTestBox(plPhysicsCastResult& out_result, plVec3 vBoxExtends, const plTransform& transform, const plVec3& vDir, float fDistance, const plPhysicsQueryParameters& params, plPhysicsHitCollection collection = plPhysicsHitCollection::Closest) const override;
68
69 virtual bool SweepTestCapsule(plPhysicsCastResult& out_result, float fCapsuleRadius, float fCapsuleHeight, const plTransform& transform, const plVec3& vDir, float fDistance, const plPhysicsQueryParameters& params, plPhysicsHitCollection collection = plPhysicsHitCollection::Closest) const override;
70
71 virtual bool OverlapTestSphere(float fSphereRadius, const plVec3& vPosition, const plPhysicsQueryParameters& params) const override;
72
73 virtual bool OverlapTestCapsule(float fCapsuleRadius, float fCapsuleHeight, const plTransform& transform, const plPhysicsQueryParameters& params) const override;
74
75 virtual void QueryShapesInSphere(plPhysicsOverlapResultArray& out_results, float fSphereRadius, const plVec3& vPosition, const plPhysicsQueryParameters& params) const override;
76
77 virtual void AddStaticCollisionBox(plGameObject* pObject, plVec3 vBoxSize) override;
78
80
81 virtual plBoundingBoxSphere GetWorldSpaceBounds(plGameObject* pOwner, plUInt32 uiCollisionLayer, plBitflags<plPhysicsShapeType> shapeTypes, bool bIncludeChildObjects) const override;
82
83 plDeque<plComponentHandle> m_RequireUpdate;
84
85 const plSet<plJoltDynamicActorComponent*>& GetActiveActors() const { return m_ActiveActors; }
86 const plMap<plJoltRagdollComponent*, plInt32>& GetActiveRagdolls() const { return m_ActiveRagdolls; }
87 const plMap<plJoltRopeComponent*, plInt32>& GetActiveRopes() const { return m_ActiveRopes; }
88 plArrayPtr<plJoltRagdollComponent*> GetRagdollsPutToSleep() { return m_RagdollsPutToSleep.GetArrayPtr(); }
89
91 plUInt32 QueueBodyToAdd(JPH::Body* pBody, bool bAwake);
92
97 bool IsBodyStillQueuedToAdd(plUInt32 uiBodiesAddCounter) const { return uiBodiesAddCounter == m_uiBodiesAddCounter; }
98
99 JPH::GroupFilter* GetGroupFilter() const { return m_pGroupFilter; }
100 JPH::GroupFilter* GetGroupFilterIgnoreSame() const { return m_pGroupFilterIgnoreSame; }
101
102 void EnableJoinedBodiesCollisions(plUInt32 uiObjectFilterID1, plUInt32 uiObjectFilterID2, bool bEnable);
103
104 JPH::TempAllocator* GetTempAllocator() const { return m_pTempAllocator.get(); }
105
106 void ActivateCharacterController(plJoltCharacterControllerComponent* pCharacter, bool bActivate);
107
108 plJoltContactListener* GetContactListener()
109 {
110 return reinterpret_cast<plJoltContactListener*>(m_pContactListener);
111 }
112
113 void CheckBreakableConstraints();
114
115 plSet<plComponentHandle> m_BreakableConstraints;
116
117 void QueryGeometryInBox(const plPhysicsQueryParameters& params, plBoundingBox box, plDynamicArray<plNavmeshTriangle>& out_triangles) const;
118
119private:
120 bool SweepTest(plPhysicsCastResult& out_Result, const JPH::Shape& shape, const JPH::Mat44& transform, const plVec3& vDir, float fDistance, const plPhysicsQueryParameters& params, plPhysicsHitCollection collection) const;
121 bool OverlapTest(const JPH::Shape& shape, const JPH::Mat44& transform, const plPhysicsQueryParameters& params) const;
122
123 void FreeUserDataAfterSimulationStep();
124
125 void StartSimulation(const plWorldModule::UpdateContext& context);
126 void FetchResults(const plWorldModule::UpdateContext& context);
127
128 void Simulate();
129
130 void UpdateSettingsCfg();
131 void ApplySettingsCfg();
132
133 void UpdateConstraints();
134
135 plTime CalculateUpdateSteps();
136
137 void DebugDrawGeometry();
138 void DebugDrawGeometry(const plVec3& vCenter, float fRadius, plPhysicsShapeType::Enum shapeType, const plTag& tag);
139
140 struct DebugGeo
141 {
142 plGameObjectHandle m_hObject;
143 plUInt32 m_uiLastSeenCounter = 0;
144 bool m_bMutableGeometry = false;
145 };
146
147 struct DebugGeoShape
148 {
150 plBoundingBox m_Bounds;
151 plUInt32 m_uiLastSeenCounter = 0;
152 };
153
154 struct DebugBodyShapeKey
155 {
156 plUInt32 m_uiBodyID;
157 const void* m_pShapePtr;
158
159 bool operator<(const DebugBodyShapeKey& rhs) const
160 {
161 if (m_uiBodyID == rhs.m_uiBodyID)
162 return m_pShapePtr < rhs.m_pShapePtr;
163
164 return m_uiBodyID < rhs.m_uiBodyID;
165 }
166
167 bool operator==(const DebugBodyShapeKey& rhs) const
168 {
169 return (m_uiBodyID == rhs.m_uiBodyID) && (m_pShapePtr == rhs.m_pShapePtr);
170 }
171 };
172
173 plUInt32 m_uiDebugGeoLastSeenCounter = 0;
174 plMap<DebugBodyShapeKey, DebugGeo> m_DebugDrawComponents;
175 plMap<const void*, DebugGeoShape> m_DebugDrawShapeGeo;
176
177 plUInt32 m_uiNextObjectFilterID = 1;
178 plDynamicArray<plUInt32> m_FreeObjectFilterIDs;
179
180 plDeque<plJoltUserData> m_AllocatedUserData;
181 plDynamicArray<plUInt32> m_FreeUserData;
182 plDynamicArray<plUInt32> m_FreeUserDataAfterSimulationStep;
183
184 plTime m_AccumulatedTimeSinceUpdate;
185
186 plJoltSettings m_Settings;
187
188 plSharedPtr<plTask> m_pSimulateTask;
189 plTaskGroupID m_SimulateTaskGroupId;
190 plTime m_SimulatedTimeStep;
191
192 std::unique_ptr<JPH::PhysicsSystem> m_pSystem;
193 std::unique_ptr<JPH::TempAllocator> m_pTempAllocator;
194
195 plJoltObjectToBroadphaseLayer m_ObjectToBroadphase;
196 plJoltObjectVsBroadPhaseLayerFilter m_ObjectVsBroadphaseFilter;
197 plJoltObjectLayerPairFilter m_ObjectLayerPairFilter;
198
199 void* m_pContactListener = nullptr;
200 void* m_pActivationListener = nullptr;
204 plDynamicArray<plJoltRagdollComponent*> m_RagdollsPutToSleep;
205
206 JPH::GroupFilter* m_pGroupFilter = nullptr;
207 JPH::GroupFilter* m_pGroupFilterIgnoreSame = nullptr;
208
209 plUInt32 m_uiBodiesAddedSinceOptimize = 100;
210 plUInt32 m_uiBodiesAddCounter = 1; // increased every time bodies get added, can be used to check whether a queued body is still queued
211 plDeque<plUInt32> m_BodiesToAdd;
212 plDeque<plUInt32> m_BodiesToAddAndActivate;
213
214 plHybridArray<plTime, 4> m_UpdateSteps;
216};
217
218
222{
223 PL_DECLARE_WORLD_MODULE();
225
226public:
228
229 virtual void RetrieveGeometryInArea(plUInt32 uiCollisionLayer, const plBoundingBox& box, plDynamicArray<plNavmeshTriangle>& out_triangles) const override;
230
231private:
232 plJoltWorldModule* m_pJoltModule = nullptr;
233};
This class encapsulates an array and it's size. It is recommended to use this class instead of plain ...
Definition ArrayPtr.h:37
Definition Deque.h:270
Definition DynamicArray.h:81
This class represents an object inside the world.
Definition GameObject.h:32
A hybrid array uses in-place storage to handle the first few elements without any allocation....
Definition HybridArray.h:12
Base class for character controllers (CC).
Definition JoltCharacterControllerComponent.h:27
Definition JoltContacts.h:66
Implementation of the plNavmeshGeoWorldModuleInterface that uses Jolt physics to retrieve the geometr...
Definition JoltWorldModule.h:222
Definition JoltCollisionFiltering.h:95
Definition JoltCollisionFiltering.h:46
Definition JoltCollisionFiltering.h:87
Creates a physics ragdoll for an animated mesh and creates animation poses from the physics simulatio...
Definition JoltRagdollComponent.h:75
Creates a physically simulated rope that is made up of multiple segments.
Definition JoltRopeComponent.h:57
Definition JoltUserData.h:18
Definition JoltWorldModule.h:29
bool IsBodyStillQueuedToAdd(plUInt32 uiBodiesAddCounter) const
Checks whether the last QueueBodyToAdd() has been processed already, or not.
Definition JoltWorldModule.h:97
Definition Map.h:408
A world module that retrieves triangle data that should be used for building navmeshes at runtime.
Definition NavmeshGeoWorldModule.h:22
Definition PhysicsWorldModule.h:109
virtual void AddStaticCollisionBox(plGameObject *pOwner, plVec3 vBoxSize)
Adds a static actor with a box shape to pOwner.
Definition PhysicsWorldModule.h:150
virtual plUInt32 GetCollisionLayerByName(plStringView sName) const =0
Searches for a collision layer with the given name and returns its index.
virtual plBoundingBoxSphere GetWorldSpaceBounds(plGameObject *pOwner, plUInt32 uiCollisionLayer, plBitflags< plPhysicsShapeType > shapeTypes, bool bIncludeChildObjects) const
Gets world space bounds of a physics object if its shape type is included in shapeTypes and its colli...
Definition PhysicsWorldModule.h:168
virtual void AddFixedJointComponent(plGameObject *pOwner, const plPhysicsWorldModuleInterface::FixedJointConfig &cfg)
Adds a fixed joint to pOwner.
Definition PhysicsWorldModule.h:165
Definition Set.h:238
A Shared ptr manages a shared object and destroys that object when no one references it anymore....
Definition SharedPtr.h:10
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
The tag class stores the necessary lookup information for a single tag which can be used in conjuncti...
Definition Tag.h:16
Given out by plTaskSystem::CreateTaskGroup to identify a task group.
Definition TaskSystemDeclarations.h:103
Encapsulates a view on the given world through the given camera and rendered with the specified Rende...
Definition View.h:21
A world encapsulates a scene graph of game objects and various component managers and their component...
Definition World.h:22
virtual void OnSimulationStarted()
This method is called at the start of the next world update when the world is simulated....
Definition WorldModule.h:105
virtual void Initialize()
This method is called after the constructor. A derived type can override this method to do initializa...
Definition WorldModule.h:98
virtual void Deinitialize()
This method is called before the destructor. A derived type can override this method to do deinitiali...
Definition WorldModule.h:101
The plBitflags class allows you to work with type-safe bitflags.
Definition Bitflags.h:82
A handle to a game object.
Definition Declarations.h:76
Definition Declarations.h:75
Definition PhysicsWorldModule.h:43
Used for raycast and sweep tests.
Definition PhysicsWorldModule.h:23
Definition PhysicsWorldModule.h:63
Definition PhysicsWorldModule.h:86
Definition PhysicsWorldModule.h:161
The time class encapsulates a double value storing the time in seconds.
Definition Time.h:12
Definition WorldModule.h:33