RavEngine
Loading...
Searching...
No Matches
PhysicsBodyComponent.hpp
1#pragma once
2#include "PhysXDefines.h"
3#include "Function.hpp"
4#include "mathtypes.hpp"
5#include "PhysicsCallback.hpp"
6#include "Queryable.hpp"
7#include <PxScene.h>
8#include <PxRigidActor.h>
9#include "ComponentWithOwner.hpp"
10#include "DataStructures.hpp"
11#include "PhysicsCollider.hpp"
12
13namespace physx {
14 class PxRigidActor;
15 struct PxContactPairPoint;
16}
17
18namespace RavEngine {
19 struct Transform;
20 // stores contact points
22 vector3 position, normal, impulse;
23 decimalType separation;
24
26
27 // default constructor
29 };
30
32#if !RVE_SERVER
33public IDebugRenderable,
34#endif
35
36public Queryable<PhysicsBodyComponent
37#if !RVE_SERVER
38,IDebugRenderable
39#endif
40>
41 {
42 protected:
45 void CompleteConstruction();
46 friend class PhysicsLinkSystemRead;
47 friend class PhysicsLinkSystemWrite;
48 mutable bool setPoseNeedsSync = false;
49 public:
51#if !RVE_SERVER
53#endif
54 >::GetQueryTypes;
55
56 physx::PxRigidActor* rigidActor = nullptr;
57 physx::PxU32 filterGroup = -1;
58 physx::PxU32 filterMask = -1;
59
60 auto getScene() const{
61 return rigidActor->getScene();
62 }
63
65 virtual ~PhysicsBodyComponent();
66
67 void Destroy();
68
70 decltype(colliders)::iterator id;
71 };
72
73 template<typename T, typename ... A>
74 ColliderHandle EmplaceCollider(A&& ... args){
75 auto collider_iter = colliders.emplace(std::make_shared<T>(this,args...));
76
78 }
79
80 template<typename T>
81 void DestroyCollider(ColliderHandle handle){
82 colliders.erase(handle.id);
83 }
84
85 void OnDestroyDetatchCollider(RavEngine::PhysicsCollider& collider);
86
87
92 void AddReceiver(decltype(receivers)::value_type& obj);
93
98 void RemoveReceiver(decltype(receivers)::value_type& obj);
99
100 void RemoveReceiver(PhysicsCallback*);
101
106 std::pair<vector3,quaternion> getDynamicsWorldPose() const;
107
114 void setDynamicsWorldPose(const vector3& worldpos, const quaternion& worldrot) const;
115
116 void SetGravityEnabled(bool);
117
121 bool GetGravityEnabled() const;
122
124
128 bool GetSleepNotificationsEnabled() const;
129
130 void SetSimulationEnabled(bool);
131
135 bool GetSimulationEnabled() const;
136
143 void OnColliderEnter(PhysicsBodyComponent& other, const ContactPairPoint* contactPoints, size_t numContactPoints);
144
151 void OnColliderPersist(PhysicsBodyComponent& other, const ContactPairPoint* contactPoints, size_t numContactPoints);
152
159 void OnColliderExit(PhysicsBodyComponent& other, const ContactPairPoint* contactPoints, size_t numContactPoints);
160
165 void OnTriggerEnter(PhysicsBodyComponent& other);
166
171 void OnTriggerExit(PhysicsBodyComponent& other);
172
176 inline bool GetWantsContactData() const {
177 return wantsContactData;
178 }
183 inline void SetWantsContactData(bool state) {
184 wantsContactData = state;
185 }
186
188#if !RVE_SERVER
189 LockRead([&] {
190 for (const auto& collider : colliders) {
191 collider->DebugDraw(dbg, debug_color, tr);
192 }
193 });
194#endif
195 }
196 protected:
197 bool wantsContactData = false;
198
199 template<typename T>
200 inline void LockWrite(const T& func){
201 auto scene = rigidActor->getScene();
202 if (scene){
203 scene->lockWrite();
204 func();
205 scene->unlockWrite();
206 }
207 else{
208 func();
209 }
210 }
211
212 template<typename T>
213 inline void LockRead(const T& func) const{
214 auto scene = rigidActor->getScene();
215 if(scene){
216 scene->lockRead();
217 func();
218 scene->unlockRead();
219 }
220 else{
221 func();
222 }
223 }
224 private:
225 void setDynamicsWorldPoseNoLock(const vector3& worldpos, const quaternion& worldrot) const;
226 };
227
228 class RigidBodyDynamicComponent : public PhysicsBodyComponent, public QueryableDelta<PhysicsBodyComponent,RigidBodyDynamicComponent> {
229 public:
231 RigidBodyDynamicComponent(Entity owner, physx::PxU32 fg, physx::PxU32 fm) : RigidBodyDynamicComponent(owner) {
232 this->filterGroup = fg; this->filterMask = fm;
233 }
240
245
246 void SetLinearVelocity(const vector3&, bool);
247
248 void SetAngularVelocity(const vector3&, bool);
249
250 void SetKinematicTarget(const vector3& targetPos, const quaternion& targetRot);
251 std::pair<vector3, quaternion> GetKinematicTarget() const;
252
256 void Wake();
257
261 void Sleep();
262
266 bool IsSleeping() const;
267
268 enum AxisLock{
269 Linear_X = (1 << 0),
270 Linear_Y = (1 << 1),
271 Linear_Z = (1 << 2),
272 Angular_X = (1 << 3),
273 Angular_Y = (1 << 4),
274 Angular_Z = (1 << 5)
275 };
276
282 void SetAxisLock(uint16_t LockFlags);
283
288 uint16_t GetAxisLock() const;
289
294 void SetMass(decimalType mass);
295
299 decimalType GetMass() const;
300
304 decimalType GetMassInverse() const;
305
310 void AddForce(const vector3& force);
311
316 void AddTorque(const vector3& torque);
317
321 void ClearAllForces();
322
326 void ClearAllTorques();
327
328 // call underlying
329 void Destroy() {
330 PhysicsBodyComponent::Destroy();
331 }
332 };
333
334 struct RigidBodyStaticComponent : public PhysicsBodyComponent, public QueryableDelta<PhysicsBodyComponent,RigidBodyStaticComponent> {
338 RigidBodyStaticComponent(Entity owner, physx::PxU32 fg, physx::PxU32 fm) : RigidBodyStaticComponent(owner) {
339 this->filterGroup = fg; this->filterMask = fm;
340 }
341
342 // call underlying
343 void Destroy() {
344 PhysicsBodyComponent::Destroy();
345 }
346 };
347}
Definition ComponentWithOwner.hpp:6
Definition DebugDrawer.hpp:11
Definition PhysicsBodyComponent.hpp:41
void AddReceiver(decltype(receivers)::value_type &obj)
Definition PhysicsBodyComponent.cpp:66
void SetGravityEnabled(bool)
Definition PhysicsBodyComponent.cpp:109
void OnTriggerExit(PhysicsBodyComponent &other)
Definition PhysicsBodyComponent.cpp:269
bool GetSleepNotificationsEnabled() const
Definition PhysicsBodyComponent.cpp:130
bool GetGravityEnabled() const
Definition PhysicsBodyComponent.cpp:116
std::pair< vector3, quaternion > getDynamicsWorldPose() const
Definition PhysicsBodyComponent.cpp:90
void setDynamicsWorldPose(const vector3 &worldpos, const quaternion &worldrot) const
Definition PhysicsBodyComponent.cpp:98
void DebugDraw(RavEngine::DebugDrawer &dbg, const RavEngine::Transform &tr) const
Definition PhysicsBodyComponent.hpp:187
void SetWantsContactData(bool state)
Definition PhysicsBodyComponent.hpp:183
void OnColliderPersist(PhysicsBodyComponent &other, const ContactPairPoint *contactPoints, size_t numContactPoints)
Definition PhysicsBodyComponent.cpp:244
void RemoveReceiver(decltype(receivers)::value_type &obj)
Definition PhysicsBodyComponent.cpp:73
void OnColliderExit(PhysicsBodyComponent &other, const ContactPairPoint *contactPoints, size_t numContactPoints)
Definition PhysicsBodyComponent.cpp:253
bool GetSimulationEnabled() const
Definition PhysicsBodyComponent.cpp:145
void OnTriggerEnter(PhysicsBodyComponent &other)
Definition PhysicsBodyComponent.cpp:263
void SetSimulationEnabled(bool)
Definition PhysicsBodyComponent.cpp:140
bool GetWantsContactData() const
Definition PhysicsBodyComponent.hpp:176
void SetSleepNotificationsEnabled(bool)
Definition PhysicsBodyComponent.cpp:125
void OnColliderEnter(PhysicsBodyComponent &other, const ContactPairPoint *contactPoints, size_t numContactPoints)
Definition PhysicsBodyComponent.cpp:235
Definition PhysicsCollider.hpp:21
Definition PhysicsLinkSystem.hpp:37
Definition PhysicsLinkSystem.hpp:25
Definition PhysicsBodyComponent.hpp:228
vector3 GetLinearVelocity() const
Definition PhysicsBodyComponent.cpp:170
bool IsSleeping() const
Definition PhysicsBodyComponent.cpp:230
void SetMass(decimalType mass)
Definition PhysicsBodyComponent.cpp:281
uint16_t GetAxisLock() const
Definition PhysicsBodyComponent.cpp:216
void Sleep()
Definition PhysicsBodyComponent.cpp:225
void SetLinearVelocity(const vector3 &, bool)
Definition PhysicsBodyComponent.cpp:193
void AddTorque(const vector3 &torque)
Definition PhysicsBodyComponent.cpp:307
void ClearAllForces()
Definition PhysicsBodyComponent.cpp:313
void AddForce(const vector3 &force)
Definition PhysicsBodyComponent.cpp:301
void SetAngularVelocity(const vector3 &, bool)
Definition PhysicsBodyComponent.cpp:205
void ClearAllTorques()
Definition PhysicsBodyComponent.cpp:319
decimalType GetMassInverse() const
Definition PhysicsBodyComponent.cpp:293
void SetAxisLock(uint16_t LockFlags)
Definition PhysicsBodyComponent.cpp:212
void Wake()
Definition PhysicsBodyComponent.cpp:220
vector3 GetAngularVelocity() const
Definition PhysicsBodyComponent.cpp:179
decimalType GetMass() const
Definition PhysicsBodyComponent.cpp:285
Definition concurrentqueue.h:747
virtual PxScene * getScene() const =0
Retrieves the scene which this actor belongs to.
PxRigidActor represents a base class shared between dynamic and static rigid bodies in the physics SD...
Definition PxRigidActor.h:53
virtual void lockWrite(const char *file=NULL, PxU32 line=0)=0
Lock the scene for writing from this thread.
virtual void lockRead(const char *file=NULL, PxU32 line=0)=0
Lock the scene for reading from the calling thread.
Definition Animation.hpp:6
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
Definition PhysicsBodyComponent.hpp:21
Definition Entity.hpp:11
Definition IDebugRenderable.hpp:8
Definition PhysicsBodyComponent.hpp:69
Definition Queryable.hpp:38
Definition Queryable.hpp:21
Definition PhysicsBodyComponent.hpp:334
Definition Transform.hpp:20
Definition type_quat.hpp:20
Definition qualifier.hpp:35
A contact point as used by contact notification.
Definition PxSimulationEventCallback.h:462