载入中...
搜索中...
未找到
JoltBody.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>
9bool BodyBackend::isValid() const noexcept { return body && body->attached && body->valid(); }
10void BodyBackend::bindOwner(Body3D& owner) noexcept { body->owner = &owner; }
11void BodyBackend::invalidate() noexcept {
12 body->owner = nullptr;
13 body->attached = false;
14}
15void BodyBackend::destroy() noexcept {
16 if (body && body->valid()) {
17 auto state = body->world.lock();
18 auto& api = body->api();
19 if (api.IsAdded(body->id)) api.RemoveBody(body->id);
20 api.DestroyBody(body->id);
21 state->bodies.erase(body->id.GetIndexAndSequenceNumber());
22 }
23 if (body) {
24 body->alive = false;
25 body->owner = nullptr;
26 body->attached = false;
27 }
28}
30 float friction, float restitution) {
31 if (!isValid()) return rejected<std::unique_ptr<IShapeBackend3D>>("Body expired", eve::DiagnosticCode::StaleHandle);
32 if (!isFinite(density, friction, restitution) || density < 0 || friction < 0 || restitution < 0 || restitution > 1)
33 return rejected<std::unique_ptr<IShapeBackend3D>>("Invalid material", eve::DiagnosticCode::InvalidArgument);
34 auto shape = std::make_shared<ShapeState>();
35 shape->body = body;
36 shape->geometry = geometry;
37 shape->density = density;
38 shape->material.friction = friction;
39 shape->material.restitution = restitution;
40 if (geometry.kind == ShapeKind3D::TriangleMesh) shape->meshMaterials.push_back(shape->material);
41 auto cooked = cook(geometry, density, reinterpret_cast<JPH::uint64>(shape.get()));
42 if (!cooked) return eve::Result<std::unique_ptr<IShapeBackend3D>>::failure(cooked.status());
43 shape->native = std::move(cooked).takeValue();
44 auto list = body->shapes;
45 list.push_back(shape);
46 auto result = replaceShapes(*body, list);
47 if (!result) return eve::Result<std::unique_ptr<IShapeBackend3D>>::failure(result.status());
48 body->shapes = std::move(list);
49 return eve::Result<std::unique_ptr<IShapeBackend3D>>::success(std::make_unique<ShapeBackend>(shape));
50}
51} // namespace eve::physics::jolt
float friction
float density
ShaderImageInput shape
Poco::JSON::Object::Ptr result
std::shared_ptr< OceanGpuSpectrum > owner
std::string body
std::string geometry
double restitution
Move-only operation result carrying either a value or Status.
Definition Result.h:176
Backend-neutral 3D rigid body in meter-space coordinates (+Y up by convention). Owned by a World3D; c...
Definition Body3D.h:31
void destroy() noexcept override
Releases the native body idempotently without callbacks; children must be detached first.
Definition JoltBody.cpp:15
eve::Result< std::unique_ptr< IShapeBackend3D > > createShape(const ShapeGeometry3D &geometry, float density, float friction, float restitution) override
Creates and owns one shape for this body; no input references are retained. @cost One geometry cook p...
Definition JoltBody.cpp:29
void invalidate() noexcept override
Detaches before its parent world destroys the native body.
Definition JoltBody.cpp:11
std::shared_ptr< BodyState > body
Definition JoltBody.h:10
void bindOwner(Body3D &owner) noexcept override
Binds its exclusive facade owner for internal attribution, on the simulation thread....
Definition JoltBody.cpp:10
bool isValid() const noexcept override
Whether the native body remains alive.
Definition JoltBody.cpp:9
eve::Result< T > rejected(const char *operation, eve::DiagnosticCode code=eve::DiagnosticCode::Unsupported)
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)
Owning, backend-independent shape source; metres, local quaternion XYZW.