载入中...
搜索中...
未找到
PhysicsLink.cpp
浏览该文件的文档.
2
3#include "physics/Body.h"
4#include "physics/Body3D.h"
5#include "physics/Joint3D.h"
6#include "physics/Shape3D.h"
7#include "physics/World.h"
8#include "physics/World3D.h"
9
10namespace eve::physics {
11namespace {
12
13eve::Diagnostic invalidLinkDiagnostic(const char* path, const char* message) {
15}
16
17eve::Diagnostic staleLinkDiagnostic(const char* path) {
19 "Physics link does not resolve to a live object owned by this world", path);
20}
21
22template <typename Link, typename Handle>
23eve::Result<Link> attachLink(PhysicsWorldHandle world, Handle handle, const char* path, const char* message) {
24 if (world.isInvalid() || handle.isInvalid())
25 return eve::Result<Link>::failure(invalidLinkDiagnostic(path, message));
26 return eve::Result<Link>::success(Link{world, handle});
27}
28
29} // namespace
30
32 if (world.isInvalid() || body.isInvalid()) {
34 invalidLinkDiagnostic("physics.link", "PhysicsLink requires valid world and body handles"));
35 }
37}
38
40 const World* world = body.getWorld();
41 if (!body.isValid() || !world || !world->isValid()) {
42 return eve::Result<PhysicsLink>::failure(staleLinkDiagnostic("physics.link.body"));
43 }
44 return attach(world->runtimeHandle(), body.runtimeHandle());
45}
46
48 const World3D* world = body.getWorld();
49 if (!body.isValid() || !world || !world->isValid()) {
50 return eve::Result<PhysicsLink>::failure(staleLinkDiagnostic("physics.link.body3d"));
51 }
52 return attach(world->runtimeHandle(), body.runtimeHandle());
53}
54
56 if (!isValid() || world.runtimeHandle() != this->world) {
57 return eve::Result<Body*>::failure(staleLinkDiagnostic("physics.link.resolve"));
58 }
59 Body* resolved = world.findBody(body);
60 if (!resolved) {
61 return eve::Result<Body*>::failure(staleLinkDiagnostic("physics.link.resolve"));
62 }
64}
65
67 if (!isValid() || world.runtimeHandle() != this->world) {
68 return eve::Result<Body3D*>::failure(staleLinkDiagnostic("physics.link.resolve3d"));
69 }
70 Body3D* resolved = world.findBody(body);
71 if (!resolved) {
72 return eve::Result<Body3D*>::failure(staleLinkDiagnostic("physics.link.resolve3d"));
73 }
75}
76
78 return attachLink<PhysicsShapeLink>(world, shape, "physics.shapeLink",
79 "PhysicsShapeLink requires valid world and shape handles");
80}
81
83 const Body3D* body = shape.getBody();
84 const World3D* world = body ? body->getWorld() : nullptr;
85 if (!shape.isValid() || !body || !world || !world->isValid())
86 return eve::Result<PhysicsShapeLink>::failure(staleLinkDiagnostic("physics.shapeLink.shape"));
87 return attach(world->runtimeHandle(), shape.runtimeHandle());
88}
89
91 if (!isValid() || world.runtimeHandle() != this->world)
92 return eve::Result<Shape3D*>::failure(staleLinkDiagnostic("physics.shapeLink.resolve"));
93 Shape3D* resolved = world.findShape(shape);
94 if (!resolved) return eve::Result<Shape3D*>::failure(staleLinkDiagnostic("physics.shapeLink.resolve"));
96}
97
99 return attachLink<PhysicsJointLink>(world, joint, "physics.jointLink",
100 "PhysicsJointLink requires valid world and joint handles");
101}
102
104 World3D* world = joint.getWorld();
105 if (!joint.isValid() || !world || !world->isValid())
106 return eve::Result<PhysicsJointLink>::failure(staleLinkDiagnostic("physics.jointLink.joint"));
107 return attach(world->runtimeHandle(), joint.runtimeHandle());
108}
109
111 if (!isValid() || world.runtimeHandle() != this->world)
112 return eve::Result<Joint3D*>::failure(staleLinkDiagnostic("physics.jointLink.resolve"));
113 Joint3D* resolved = world.findJoint(joint);
114 if (!resolved) return eve::Result<Joint3D*>::failure(staleLinkDiagnostic("physics.jointLink.resolve"));
116}
117
118} // namespace eve::physics
std::string message
ShaderImageInput shape
std::string path
Definition PlayHost.cpp:111
PrimitiveHandle handle
std::string body
RoadNetwork resolved
A structured explanation of a failed, degraded, or noteworthy result.
Definition Diagnostic.h:94
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
constexpr bool isInvalid() const noexcept
Returns whether this value is the invalid sentinel.
constexpr bool isValid() const noexcept
Returns whether both coordinates are usable handle values.
Backend-neutral 3D rigid body in meter-space coordinates (+Y up by convention). Owned by a World3D; c...
Definition Body3D.h:31
2D rigid body (Box2D) in pixel-space coordinates. Owned by a World; create shapes with newRectangleFi...
Definition Body.h:21
Script-facing joint owned by a World3D. Legacy scalar/void methods adapt the checked backend contract...
Definition Joint3D.h:27
3D shape (box/sphere/capsule) attached to a Body3D with material settings. Created via Body3D::new*Sh...
Definition Shape3D.h:34
Box3D rigid-body world. Script coordinates are meters (Box3D native), unlike 2D World which uses pixe...
Definition World3D.h:47
Box2D world wrapper (2D physics) with pixel-space coordinates. Handles stepping, gravity,...
Definition World.h:44
Optional physics backend for vehicle mobility and body attach.
Definition Climbing.h:36
eve::RuntimeHandle< PhysicsWorldHandleTag > PhysicsWorldHandle
Generation-qualified runtime identity for a physics world.