载入中...
搜索中...
未找到
JoltInternal.h
浏览该文件的文档.
1#pragma once
2// Jolt requires its platform header before any other native header.
3#include <Jolt/Jolt.h>
4
5// Provider implementation dependencies.
6#include <Jolt/Core/JobSystemSingleThreaded.h>
7#include <Jolt/Core/TempAllocator.h>
8#include <Jolt/Physics/Body/BodyCreationSettings.h>
9#include <Jolt/Physics/Collision/Shape/EmptyShape.h>
10#include <Jolt/Physics/Collision/Shape/StaticCompoundShape.h>
11#include <Jolt/Physics/PhysicsStepListener.h>
12#include <Jolt/Physics/PhysicsSystem.h>
13#include <cmath>
14#include <unordered_map>
15#include "physics/Body3D.h"
16#include "physics/Shape3D.h"
19namespace eve::physics::jolt {
20template <class T>
25inline bool isFinite(float x, float y, float z) { return std::isfinite(x) && std::isfinite(y) && std::isfinite(z); }
26struct Layers final : JPH::BroadPhaseLayerInterface, JPH::ObjectVsBroadPhaseLayerFilter, JPH::ObjectLayerPairFilter {
27 JPH::uint GetNumBroadPhaseLayers() const override { return 2; }
28 JPH::BroadPhaseLayer GetBroadPhaseLayer(JPH::ObjectLayer layer) const override {
29 return JPH::BroadPhaseLayer(static_cast<JPH::uint8>(layer));
30 }
31 bool ShouldCollide(JPH::ObjectLayer a, JPH::BroadPhaseLayer b) const override {
32 return a != 0 || b.GetValue() != 0;
33 }
34 bool ShouldCollide(JPH::ObjectLayer a, JPH::ObjectLayer b) const override { return a != 0 || b != 0; }
35};
40struct Listener;
52struct JointState;
55// Jolt SIMD members require alignment padding on MSVC ARM64.
56JPH_SUPPRESS_WARNING_PUSH
57JPH_MSVC_SUPPRESS_WARNING(4324)
58struct State {
60 JPH::TempAllocatorMalloc allocator;
61 JPH::JobSystemSingleThreaded jobs{JPH::cMaxPhysicsJobs};
62 JPH::PhysicsSystem system;
63 std::unique_ptr<Listener> listener;
64 std::unordered_map<JPH::uint32, std::shared_ptr<BodyState>> bodies;
65 std::vector<std::weak_ptr<JointState>> joints;
66 std::unordered_map<JPH::uint64, int> suppressedPairs;
67 float inverseStep = 0;
68 float maximumLinearSpeed = 500.f, stepMilliseconds = 0.f;
69 bool continuousCollision = true;
70 float hitThreshold = 1.f;
71 bool alive = true, sleep = true, faulted = false;
72 State(float, float, float, bool);
73 ~State();
74};
75struct BodyState {
76 std::weak_ptr<State> world;
77 JPH::MassProperties mass;
78 JPH::BodyID id;
79 Body3D* owner = nullptr;
80 std::vector<std::shared_ptr<ShapeState>> shapes;
81 bool alive = true, active = true, attached = true;
82 bool bullet = false;
83 bool customSleepThreshold = false, sleepEnabled = true;
84 float sleepThreshold = .03f, quietSeconds = 0.f;
85 JPH::EAllowedDOFs dofs = JPH::EAllowedDOFs::All;
86 bool valid() const {
87 auto w = world.lock();
88 return alive && w && w->alive;
89 }
90 JPH::BodyInterface& api() const { return world.lock()->system.GetBodyInterface(); }
91};
92JPH_SUPPRESS_WARNING_POP
93struct ShapeState {
94 std::weak_ptr<BodyState> body;
95 Shape3D* owner = nullptr;
96 JPH::RefConst<JPH::Shape> native;
99 std::vector<SurfaceMaterial3D> meshMaterials;
101 float density = 1.f;
102 bool alive = true, sensor = false;
103 bool valid() const {
104 auto b = body.lock();
105 return alive && b && b->valid();
106 }
107};
108struct Listener final : JPH::ContactListener, JPH::PhysicsStepListener {
109 void OnStep(const JPH::PhysicsStepListenerContext&) override;
110 State* state = nullptr; // Borrowed from owner; listener is destroyed before State.
111 std::vector<ContactPair> begins, ends;
112 std::unordered_map<JPH::SubShapeIDPair, ContactPair> contacts;
113 std::unordered_map<JPH::SubShapeIDPair, std::vector<SolvedPoint>> solved;
114 std::unordered_map<uint64_t, SolvedHit> hits;
115 std::unordered_map<uint64_t, float> normalTotals;
116 uint32_t substep = 0;
117 void OnContactSolved(const JPH::Body&, const JPH::SubShapeID&, const JPH::Body&, const JPH::SubShapeID&,
118 const JPH::ContactSolveResult&) override;
119 JPH::ValidateResult OnContactValidate(const JPH::Body&, const JPH::Body&, JPH::RVec3Arg,
120 const JPH::CollideShapeResult&) override;
121 void OnContactAdded(const JPH::Body&, const JPH::Body&, const JPH::ContactManifold&,
122 JPH::ContactSettings&) override;
123 void OnContactPersisted(const JPH::Body&, const JPH::Body&, const JPH::ContactManifold&,
124 JPH::ContactSettings&) override;
125 void OnContactRemoved(const JPH::SubShapeIDPair&) override;
126};
127[[nodiscard]] eve::Result<JPH::RefConst<JPH::Shape>> cook(const ShapeGeometry3D&, float density, JPH::uint64 userData);
128[[nodiscard]] eve::Result<JPH::RefConst<JPH::Shape>> cookMesh(const ShapeGeometry3D&, JPH::uint64 userData);
130int triangleIndex(const JPH::Body&, const JPH::SubShapeID&);
131SurfaceMaterial3D contactMaterial(const JPH::Body&, const JPH::SubShapeID&, const ShapeState&);
132[[nodiscard]] eve::Result<void> replaceShapes(BodyState&, const std::vector<std::shared_ptr<ShapeState>>&);
138inline ShapeState* child(const JPH::Body& body, const JPH::SubShapeID& id) {
139 return reinterpret_cast<ShapeState*>(body.GetShape()->GetSubShapeUserData(id));
140}
141} // namespace eve::physics::jolt
double z
double y
double x
ActionParameterOperation operation
float w
Definition AnimClip.cpp:738
float density
DiagnosticCode code
std::uint32_t alive
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
TileLayer * layer
std::string body
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
Backend-neutral 3D rigid body in meter-space coordinates (+Y up by convention). Owned by a World3D; c...
Definition Body3D.h:31
3D shape (box/sphere/capsule) attached to a Body3D with material settings. Created via Body3D::new*Sh...
Definition Shape3D.h:34
int triangleIndex(const JPH::Body &, const JPH::SubShapeID &)
Definition JoltMesh.cpp:89
eve::Result< T > rejected(const char *operation, eve::DiagnosticCode code=eve::DiagnosticCode::Unsupported)
eve::Result< void > success()
void initializeRuntime()
Initializes the process-owned allocator and type factory before constructing any native members.
Definition JoltState.cpp:84
eve::Result< void > replaceShapes(BodyState &, const std::vector< std::shared_ptr< ShapeState > > &)
Definition JoltShape.cpp:77
eve::Result< JPH::RefConst< JPH::Shape > > cookMesh(const ShapeGeometry3D &, JPH::uint64 userData)
Definition JoltMesh.cpp:10
SurfaceMaterial3D contactMaterial(const JPH::Body &, const JPH::SubShapeID &, const ShapeState &)
Definition JoltMesh.cpp:96
ShapeState * child(const JPH::Body &body, const JPH::SubShapeID &id)
Resolves a native child record for a synchronous callback or query. @ownership Borrowed from the worl...
eve::Result< JPH::RefConst< JPH::Shape > > cook(const ShapeGeometry3D &, float density, JPH::uint64 userData)
Definition JoltShape.cpp:16
bool isFinite(float x, float y, float z)
eve::Result< void > validateMaterial(SurfaceMaterial3D)
DiagnosticCode
Stable machine-readable diagnostic codes.
Definition Diagnostic.h:47
Owning contact point, oriented from the queried body toward its neighbour.
Collision filter; negative groups never collide, positive groups always collide.
Owning, backend-independent shape source; metres, local quaternion XYZW.
Owning surface values; userMaterialId contains the engine material ID and combine policy encoding.
JPH::BodyInterface & api() const
std::vector< std::shared_ptr< ShapeState > > shapes
std::weak_ptr< State > world
JPH::MassProperties mass
JPH::BroadPhaseLayer GetBroadPhaseLayer(JPH::ObjectLayer layer) const override
bool ShouldCollide(JPH::ObjectLayer a, JPH::BroadPhaseLayer b) const override
bool ShouldCollide(JPH::ObjectLayer a, JPH::ObjectLayer b) const override
JPH::uint GetNumBroadPhaseLayers() const override
std::unordered_map< JPH::SubShapeIDPair, ContactPair > contacts
std::unordered_map< JPH::SubShapeIDPair, std::vector< SolvedPoint > > solved
std::unordered_map< uint64_t, SolvedHit > hits
void OnContactSolved(const JPH::Body &, const JPH::SubShapeID &, const JPH::Body &, const JPH::SubShapeID &, const JPH::ContactSolveResult &) override
JPH::ValidateResult OnContactValidate(const JPH::Body &, const JPH::Body &, JPH::RVec3Arg, const JPH::CollideShapeResult &) override
void OnContactAdded(const JPH::Body &, const JPH::Body &, const JPH::ContactManifold &, JPH::ContactSettings &) override
std::vector< ContactPair > begins
std::vector< ContactPair > ends
void OnContactRemoved(const JPH::SubShapeIDPair &) override
std::unordered_map< uint64_t, float > normalTotals
void OnStep(const JPH::PhysicsStepListenerContext &) override
Definition JoltState.cpp:97
void OnContactPersisted(const JPH::Body &, const JPH::Body &, const JPH::ContactManifold &, JPH::ContactSettings &) override
std::weak_ptr< BodyState > body
JPH::RefConst< JPH::Shape > native
std::vector< SurfaceMaterial3D > meshMaterials
PhysicsShapeHandle shapeA
PhysicsShapeHandle shapeB
std::unordered_map< JPH::uint64, int > suppressedPairs
JPH::PhysicsSystem system
std::unordered_map< JPH::uint32, std::shared_ptr< BodyState > > bodies
std::unique_ptr< Listener > listener
JPH::TempAllocatorMalloc allocator
std::vector< std::weak_ptr< JointState > > joints