载入中...
搜索中...
未找到
JoltShapeOperations.cpp
浏览该文件的文档.
1// Jolt requires its platform header before any other native header.
2#include <Jolt/Jolt.h>
3
4// Provider implementation dependencies.
5#include <Jolt/Physics/Body/MotionProperties.h>
6#include <Jolt/Physics/Collision/CollidePointResult.h>
7#include <Jolt/Physics/Collision/CollisionCollectorImpl.h>
10namespace eve::physics::jolt {
12 if (!isValid()) return rejected<ShapePlane3D>("Shape expired", eve::DiagnosticCode::StaleHandle);
13 const auto body = shape->body.lock();
14 const auto pose = body->api().GetWorldTransform(body->id);
15 const auto normal =
16 pose.Multiply3x3(JPH::Quat(g.localQx, g.localQy, g.localQz, g.localQw) * JPH::Vec3(n.x, n.y, n.z));
17 const auto point = pose * JPH::Vec3(g.localX, g.localY, g.localZ) + normal * offset;
19 {point.GetX(), point.GetY(), point.GetZ(), normal.GetX(), normal.GetY(), normal.GetZ()});
20}
21
23 using R [[maybe_unused]] = eve::Result<float>;
24 if (!isValid()) return rejected<float>("Object expired", eve::DiagnosticCode::StaleHandle);
25 return R::success(shape->density);
26}
28 if (!isValid()) return rejected<void>("Shape expired", eve::DiagnosticCode::StaleHandle);
29 if (!std::isfinite(density) || density < 0)
30 return rejected<void>("Invalid density", eve::DiagnosticCode::InvalidArgument);
31 if (density == shape->density) return success();
32 auto cooked = cook(shape->geometry, density, reinterpret_cast<JPH::uint64>(shape.get()));
33 if (!cooked) return eve::Result<void>::failure(cooked.status());
34 auto previous = shape->native;
35 shape->native = std::move(cooked).takeValue();
36 if (!updateMass) {
37 // Native compounds retain the old leaf until the next explicit mass/topology rebuild.
38 // Geometry and attribution are unchanged; only the pending density differs.
39 shape->density = density;
40 return success();
41 }
42 auto body = shape->body.lock();
43 auto replaced = replaceShapes(*body, body->shapes);
44 if (!replaced) {
45 shape->native = previous;
46 return replaced;
47 }
48 shape->density = density;
49 return success();
50}
51
53 using R [[maybe_unused]] = eve::Result<float>;
54 if (!isValid()) return rejected<float>("Object expired", eve::DiagnosticCode::StaleHandle);
55 return R::success(shape->material.friction);
56}
58 using R [[maybe_unused]] = eve::Result<void>;
59 if (!isValid()) return rejected<void>("Object expired", eve::DiagnosticCode::StaleHandle);
60 if (!std::isfinite(value) || value < 0)
61 return rejected<void>("Invalid material value", eve::DiagnosticCode::InvalidArgument);
62 shape->material.friction = value;
63 return success();
64}
66 using R [[maybe_unused]] = eve::Result<float>;
67 if (!isValid()) return rejected<float>("Object expired", eve::DiagnosticCode::StaleHandle);
68 return R::success(shape->material.restitution);
69}
71 using R [[maybe_unused]] = eve::Result<void>;
72 if (!isValid()) return rejected<void>("Object expired", eve::DiagnosticCode::StaleHandle);
73 if (!std::isfinite(value) || value < 0 || value > 1)
74 return rejected<void>("Invalid material value", eve::DiagnosticCode::InvalidArgument);
75 shape->material.restitution = value;
76 return success();
77}
79 using R [[maybe_unused]] = eve::Result<SurfaceMaterial3D>;
80 if (!isValid()) return rejected<SurfaceMaterial3D>("Object expired", eve::DiagnosticCode::StaleHandle);
81 return R::success(shape->material);
82}
84 if (!isFinite(value.friction, value.restitution, value.rollingResistance) || value.friction < 0 ||
85 value.restitution < 0 || value.restitution > 1 || value.rollingResistance < 0)
86 return rejected<void>("Invalid material", eve::DiagnosticCode::InvalidArgument);
87 if (!isFinite(value.tangentVelocity.x, value.tangentVelocity.y, value.tangentVelocity.z) ||
88 ((value.userMaterialId >> 32) & 7) > 4 || ((value.userMaterialId >> 35) & 7) > 4)
89 return rejected<void>("Invalid material policy", eve::DiagnosticCode::InvalidArgument);
90 return success();
91}
93 if (!isValid()) return rejected<void>("Shape expired", eve::DiagnosticCode::StaleHandle);
95 if (!checked) return checked;
96 shape->material = value;
97 return success();
98}
99
101 using R [[maybe_unused]] = eve::Result<ShapeFilter3D>;
102 if (!isValid()) return rejected<ShapeFilter3D>("Object expired", eve::DiagnosticCode::StaleHandle);
103 return R::success(shape->filter);
104}
106 using R [[maybe_unused]] = eve::Result<void>;
107 if (!isValid()) return rejected<void>("Object expired", eve::DiagnosticCode::StaleHandle);
108 (void)updatePairs;
109 shape->filter = value;
110 auto b = shape->body.lock();
111 b->api().InvalidateContactCache(b->id);
112 if (b->active) b->api().ActivateBody(b->id);
113 return success();
114}
116 using R [[maybe_unused]] = eve::Result<bool>;
117 if (!isValid()) return rejected<bool>("Object expired", eve::DiagnosticCode::StaleHandle);
118 return R::success(shape->sensor);
119}
121 using R [[maybe_unused]] = eve::Result<void>;
122 if (!isValid()) return rejected<void>("Object expired", eve::DiagnosticCode::StaleHandle);
123 (void)enabled;
124 return success();
125}
127 using R [[maybe_unused]] = eve::Result<void>;
128 if (!isValid()) return rejected<void>("Object expired", eve::DiagnosticCode::StaleHandle);
129 (void)enabled;
130 auto body = shape->body.lock();
131 body->api().InvalidateContactCache(body->id);
132 if (body->active) body->api().ActivateBody(body->id);
133 return success();
134}
136 if (!isValid()) return rejected<int>("Shape expired", eve::DiagnosticCode::StaleHandle);
137 return eve::Result<int>::success(static_cast<int>(shape->meshMaterials.size()));
138}
140 if (!isValid()) return rejected<SurfaceMaterial3D>("Shape expired", eve::DiagnosticCode::StaleHandle);
141 if (slot < 0 || size_t(slot) >= shape->meshMaterials.size())
142 return rejected<SurfaceMaterial3D>("Invalid material slot", eve::DiagnosticCode::InvalidArgument);
143 return eve::Result<SurfaceMaterial3D>::success(shape->meshMaterials[size_t(slot)]);
144}
146 if (!isValid()) return rejected<void>("Shape expired", eve::DiagnosticCode::StaleHandle);
147 if (slot < 0 || size_t(slot) >= shape->meshMaterials.size())
148 return rejected<void>("Invalid material slot", eve::DiagnosticCode::InvalidArgument);
150 if (!checked) return checked;
151 shape->meshMaterials[size_t(slot)] = value;
152 return success();
153}
154
155eve::Result<bool> ShapeBackend::testPoint(float x, float y, float z) const {
156 if (!isValid()) return rejected<bool>("Shape expired", eve::DiagnosticCode::StaleHandle);
157 if (!isFinite(x, y, z)) return rejected<bool>("Invalid point", eve::DiagnosticCode::InvalidArgument);
158 const auto body = shape->body.lock();
159 const auto& g = shape->geometry;
160 const auto pose = body->api().GetWorldTransform(body->id) *
161 JPH::RMat44::sRotationTranslation(JPH::Quat(g.localQx, g.localQy, g.localQz, g.localQw),
162 JPH::RVec3(g.localX, g.localY, g.localZ));
163 const auto local =
164 JPH::Vec3(pose.InversedRotationTranslation() * JPH::RVec3(x, y, z)) - shape->native->GetCenterOfMass();
165 JPH::AllHitCollisionCollector<JPH::CollidePointCollector> hits;
166 shape->native->CollidePoint(local, JPH::SubShapeIDCreator(), hits);
167 return eve::Result<bool>::success(!hits.mHits.empty());
168}
169} // namespace eve::physics::jolt
double z
double y
double x
eve::EntitySpatialPose pose
float density
tensor::Graph g
Definition GpuGraph.cpp:7
glm::vec3 n
Definition Grass.cpp:63
std::string local
size_t offset
MeleePoint3 b
Definition MeleeHit.cpp:41
Texture * normal
std::shared_ptr< const OceanSprayFrame > previous
std::string body
Object::Ptr value
std::string slot
glm::vec3 point
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
eve::Result< float > density() const override
density; synchronous owning-value operation under the class lifetime contract.
eve::Result< ShapeFilter3D > filter() const override
filter; synchronous owning-value operation under the class lifetime contract.
eve::Result< void > setDensity(float value, bool updateMass) override
setDensity; synchronous owning-value operation under the class lifetime contract.
std::shared_ptr< ShapeState > shape
Definition JoltShape.h:10
eve::Result< bool > testPoint(float x, float y, float z) const override
testPoint; synchronous owning-value operation under the class lifetime contract.
eve::Result< void > enablePreSolveEvents(bool enabled) override
enablePreSolveEvents; synchronous owning-value operation under the class lifetime contract.
eve::Result< SurfaceMaterial3D > material() const override
material; synchronous owning-value operation under the class lifetime contract.
eve::Result< void > setFriction(float value) override
setFriction; synchronous owning-value operation under the class lifetime contract.
eve::Result< void > enableHitEvents(bool enabled) override
enableHitEvents; synchronous owning-value operation under the class lifetime contract.
eve::Result< void > setRestitution(float value) override
setRestitution; synchronous owning-value operation under the class lifetime contract.
bool isValid() const noexcept override
Native shape liveness.
eve::Result< int > meshMaterialCount() const override
meshMaterialCount; synchronous owning-value operation under the class lifetime contract.
eve::Result< bool > isSensor() const override
isSensor; synchronous owning-value operation under the class lifetime contract.
eve::Result< SurfaceMaterial3D > meshMaterial(int slot) const override
meshMaterial; synchronous owning-value operation under the class lifetime contract.
eve::Result< float > friction() const override
friction; synchronous owning-value operation under the class lifetime contract.
eve::Result< void > setMaterial(SurfaceMaterial3D value) override
setMaterial; synchronous owning-value operation under the class lifetime contract.
eve::Result< void > setFilter(ShapeFilter3D value, bool updatePairs) override
setFilter; synchronous owning-value operation under the class lifetime contract.
eve::Result< ShapePlane3D > oneWayPlane(const ShapeGeometry3D &geometry, PhysicsVector3D normal, float offset) const override
Computes an owning world-space plane using the current body pose; no callbacks.
eve::Result< float > restitution() const override
restitution; synchronous owning-value operation under the class lifetime contract.
eve::Result< void > setMeshMaterial(SurfaceMaterial3D value, int slot) override
setMeshMaterial; synchronous owning-value operation under the class lifetime contract.
Result< ForceVector > checked(ForceVector value)
Definition ForceMath.h:21
eve::Result< void > success()
eve::Result< void > replaceShapes(BodyState &, const std::vector< std::shared_ptr< ShapeState > > &)
Definition JoltShape.cpp:77
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)
bool enabled
Allocation-free owning three-dimensional physics value.
Definition RigidTypes3D.h:4
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.