RavEngine
Loading...
Searching...
No Matches
PhysicsSolver.hpp
1#pragma once
2//
3// PhysicsSolver.hpp
4// RavEngine_Static
5//
6// Copyright © 2020 Ravbug.
7//
8
9
10#include "PhysXDefines.h"
11#include <PxQueryReport.h>
12#include "Ref.hpp"
13#include "PhysicsCollider.hpp"
14#include "PhysicsTaskDispatcher.hpp"
15#include <PxPhysicsAPI.h>
16#include <PxFiltering.h>
17#include <cstdint>
18#include "Types.hpp"
19#include "Entity.hpp"
20
22 enum Enum {
23 L0 = (1 << 0),
24 L1 = (1 << 1),
25 L2 = (1 << 2),
26 L3 = (1 << 3),
27 };
28};
29
30namespace RavEngine {
31 struct Entity;
32 struct PhysicsBodyComponent;
33 struct World;
35 friend class World;
36 PhysicsTaskDispatcher taskDispatcher;
37 protected:
38 //static members must exist only once in the application
39 static physx::PxDefaultErrorCallback gDefaultErrorCallback;
40 static physx::PxDefaultAllocator gDefaultAllocatorCallback;
41 static physx::PxFoundation* foundation;
42 public:
43 static physx::PxPhysics* phys;
44 static physx::PxPvd* pvd;
45 static physx::PxCooking* cooking;
46 World* owner = nullptr;
47 physx::PxScene* scene;
48
49 void DeallocatePhysx();
50 protected:
51
52 void setupFiltering(physx::PxRigidActor* actor, physx::PxU32 filterGroup, physx::PxU32 filterMask);
53
54 // PxSimulationEventCallback overrides
55 virtual void onConstraintBreak(physx::PxConstraintInfo* constraints, physx::PxU32 count) override {}
56 virtual void onWake(physx::PxActor** actors, physx::PxU32 count) override {}
57 virtual void onSleep(physx::PxActor** actors, physx::PxU32 count) override {}
58 virtual void onContact(const physx::PxContactPairHeader& pairHeader, const physx::PxContactPair* pairs, physx::PxU32 nbPairs) override;
59 virtual void onTrigger(physx::PxTriggerPair* pairs, physx::PxU32 count) override;
60 virtual void onAdvance(const physx::PxRigidBody* const* bodyBuffer, const physx::PxTransform* poseBuffer, const physx::PxU32 count) override {}
61
62 // If deltatime > this value, the system will substep
63 constexpr static float max_step_time = 1.f/30;
64
65 friend class PhysicsBodyComponent;
66
67 public:
68 PhysicsSolver(World* world);
70 DeallocatePhysx();
71 }
72
73 void Spawn( PhysicsBodyComponent&);
74 void Destroy( PhysicsBodyComponent&);
75
76 void Tick(float deltaTime);
77
78 static void ReleaseStatics();
79
80 //scene query methods
81 struct RaycastHit {
82 RaycastHit() {}
84
85 bool hasBlocking{};
86 vector3 hitPosition{};
87 vector3 hitNormal{};
88 decimalType hitDistance{};
89 Entity getEntity() const;
90 private:
91 Entity hitObject;
92 };
93
102 bool Raycast(const vector3& origin, const vector3& direction, decimalType maxDistance, RaycastHit& out_hit);
103
104
105 struct OverlapHit {
106 OverlapHit() {}
107 //TODO: FIX
108 OverlapHit(const physx::PxOverlapBuffer& hit)/* : overlapObject( ((PhysicsBodyComponent*)hit.block.actor->userData)->GetOwner())*/{}
109 Ref<Entity> overlapObject;
110 };
111
120 bool BoxOverlap(const vector3& origin, const quaternion& rotation, const vector3& half_ext, OverlapHit& out_hit);
121
129 bool SphereOverlap(const vector3& origin, decimalType radius, OverlapHit& out_hit);
130
139 bool CapsuleOverlap(const vector3& origin, const quaternion& rotation, decimalType radius, decimalType halfheight, OverlapHit& out_hit);
140
141
142 protected:
144 vector3 pos;
145 quaternion rot;
146 PhysicsTransform(const vector3& p, const quaternion& q) : pos(p), rot(q) {}
147 };
148
156 };
157}
Definition PhysicsBodyComponent.hpp:41
Definition PhysicsSolver.hpp:34
void Tick(float deltaTime)
Definition PhysicsSolver.cpp:220
virtual void onContact(const physx::PxContactPairHeader &pairHeader, const physx::PxContactPair *pairs, physx::PxU32 nbPairs) override
This is called when certain contact events occur.
Definition PhysicsSolver.cpp:54
virtual void onAdvance(const physx::PxRigidBody *const *bodyBuffer, const physx::PxTransform *poseBuffer, const physx::PxU32 count) override
Provides early access to the new pose of moving rigid bodies.
Definition PhysicsSolver.hpp:60
virtual void onTrigger(physx::PxTriggerPair *pairs, physx::PxU32 count) override
This is called with the current trigger pair events.
Definition PhysicsSolver.cpp:108
virtual void onConstraintBreak(physx::PxConstraintInfo *constraints, physx::PxU32 count) override
This is called when a breakable constraint breaks.
Definition PhysicsSolver.hpp:55
bool SphereOverlap(const vector3 &origin, decimalType radius, OverlapHit &out_hit)
Definition PhysicsSolver.cpp:173
bool Raycast(const vector3 &origin, const vector3 &direction, decimalType maxDistance, RaycastHit &out_hit)
Definition PhysicsSolver.cpp:158
virtual void onWake(physx::PxActor **actors, physx::PxU32 count) override
This is called with the actors which have just been woken up.
Definition PhysicsSolver.hpp:56
void Spawn(PhysicsBodyComponent &)
Definition PhysicsSolver.cpp:196
void Destroy(PhysicsBodyComponent &)
Definition PhysicsSolver.cpp:210
bool BoxOverlap(const vector3 &origin, const quaternion &rotation, const vector3 &half_ext, OverlapHit &out_hit)
Definition PhysicsSolver.cpp:168
bool generic_overlap(const PhysicsTransform &transform, const physx::PxGeometry &geo, OverlapHit &out_hit)
Definition PhysicsSolver.cpp:183
virtual void onSleep(physx::PxActor **actors, physx::PxU32 count) override
This is called with the actors which have just been put to sleep.
Definition PhysicsSolver.hpp:57
bool CapsuleOverlap(const vector3 &origin, const quaternion &rotation, decimalType radius, decimalType halfheight, OverlapHit &out_hit)
Definition PhysicsSolver.cpp:178
Definition PhysicsTaskDispatcher.hpp:6
Definition World.hpp:181
Definition concurrentqueue.h:747
PxActor is the base class for the main simulation objects in the physics SDK.
Definition PxActor.h:201
Provides methods to cook (where cooking means to prepare the data and convert it to the right format ...
Definition PxCooking.h:337
default implementation of the allocator interface required by the SDK
Definition PxDefaultAllocator.h:88
default implementation of the error callback
Definition PxDefaultErrorCallback.h:52
Foundation SDK singleton class.
Definition PxFoundation.h:57
A geometry object.
Definition PxGeometry.h:79
Abstract singleton factory class used for instancing objects in the Physics SDK.
Definition PxPhysics.h:91
PxPvd is the top-level class for the PVD framework, and the main customer interface for PVD configura...
Definition PxPvd.h:114
PxRigidActor represents a base class shared between dynamic and static rigid bodies in the physics SD...
Definition PxRigidActor.h:53
PxRigidBody is a base class shared between dynamic rigid body objects.
Definition PxRigidBody.h:199
A scene is a collection of bodies and constraints which can interact.
Definition PxScene.h:178
An interface class that the user can implement in order to receive simulation events.
Definition PxSimulationEventCallback.h:792
class representing a rigid euclidean transform as a quaternion and a vector
Definition PxTransform.h:49
Definition Animation.hpp:6
Definition PhysicsSolver.hpp:21
Definition Entity.hpp:11
Definition PhysicsSolver.hpp:105
Definition PhysicsSolver.hpp:143
Definition PhysicsSolver.hpp:81
Definition type_quat.hpp:20
Definition qualifier.hpp:35
Descriptor for a broken constraint.
Definition PxSimulationEventCallback.h:765
An Instance of this class is passed to PxSimulationEventCallback.onContact().
Definition PxSimulationEventCallback.h:346
Contact report pair information.
Definition PxSimulationEventCallback.h:504
Descriptor for a trigger pair.
Definition PxSimulationEventCallback.h:745