载入中...
搜索中...
未找到
WorldSnapshotAccess.h
浏览该文件的文档.
2#pragma once
3// Internal, narrowly scoped access for detached topology construction.
4#include "physics/World.h"
5
6#include "physics/Body.h"
7#include "physics/Body3D.h"
8#include "physics/Fixture.h"
9#include "physics/Joint3D.h"
10#include "physics/Shape3D.h"
11#include "physics/World3D.h"
13
14#include <Box2D/Box2D.h>
15#include <box3d/box3d.h>
16
17#include <algorithm>
18#include <charconv>
19#include <cmath>
20#include <cstdint>
21#include <exception>
22#include <initializer_list>
23#include <limits>
24#include <set>
25#include <string>
26#include <string_view>
27#include <utility>
28#include <vector>
29
30namespace eve::physics {
33 static Value shapeContact(const Shape3D& shape);
37 [[nodiscard]] static Result<void> finishShapeMass(World3D& world);
39 [[nodiscard]] static Result<Value> captureCouplings3D(const World3D& world);
41 [[nodiscard]] static Result<void> restoreCouplings3D(World3D& world, const Value::Array& values);
43 [[nodiscard]] static Result<Value> captureJoints2D(const World& world);
45 [[nodiscard]] static Result<void> restoreJoints2D(World& candidate, const Value::Array& joints,
46 std::uint32_t firstHandleIndex);
47
49 static void ownPreparedBody(World& world, std::unique_ptr<Body> body) {
50 world.restoredBodies_.push_back(std::move(body));
51 }
53 static void ownPreparedFixture(World& world, std::unique_ptr<Fixture> fixture) {
54 world.restoredFixtures_.push_back(std::move(fixture));
55 }
57 static void ownPreparedBody(World3D& world, std::unique_ptr<Body3D> body) {
58 world.restoredBodies_.push_back(std::move(body));
59 }
61 static void ownPreparedShape(World3D& world, std::unique_ptr<Shape3D> shape) {
62 world.restoredShapes_.push_back(std::move(shape));
63 }
65 static void ownPreparedJoint(World3D& world, std::unique_ptr<Joint3D> joint) {
66 world.restoredJoints_.push_back(std::move(joint));
67 }
70 object.emplace("bodyId", eve::Value(std::to_string(shape.body_ ? shape.body_->getId() : -1)));
71 object.emplace("id", eve::Value(std::to_string(shape.id_)));
72 object.emplace("kind", eve::Value(shape.getKind()));
73 object.emplace("contact", shapeContact(shape));
74 object.emplace("localTransform",
75 Value(Value::Array{Value(shape.getLocalX()), Value(shape.getLocalY()), Value(shape.getLocalZ()),
76 Value(shape.getLocalRotX()), Value(shape.getLocalRotY()),
77 Value(shape.getLocalRotZ()), Value(shape.getLocalRotW())}));
78 object.emplace("a", eve::Value(shape.geometry_.a));
79 object.emplace("b", eve::Value(shape.geometry_.b));
80 object.emplace("c", eve::Value(shape.geometry_.c));
81 auto floats = [](const std::vector<float>& source) {
83 values.reserve(source.size());
84 for (float value : source) values.emplace_back(value);
85 return eve::Value(std::move(values));
86 };
87 auto integers = [](const auto& source) {
89 values.reserve(source.size());
90 for (auto value : source) values.emplace_back(std::to_string(static_cast<std::int64_t>(value)));
91 return eve::Value(std::move(values));
92 };
93 object.emplace("hullVertices", floats(shape.geometry_.hullVertices));
94 object.emplace("hullMaxVertices", eve::Value(std::to_string(shape.geometry_.hullMaxVertices)));
95 object.emplace("meshVertices", floats(shape.geometry_.meshVertices));
96 object.emplace("meshIndices", integers(shape.geometry_.meshIndices));
97 object.emplace("meshWeldVertices", eve::Value(shape.geometry_.meshWeldVertices));
98 object.emplace("meshWeldTolerance", eve::Value(shape.geometry_.meshWeldTolerance));
99 object.emplace("meshIdentifyEdges", eve::Value(shape.geometry_.meshIdentifyEdges));
100 object.emplace("meshUseMedianSplit", eve::Value(shape.geometry_.meshUseMedianSplit));
101 object.emplace("meshMaterialIndices", integers(shape.geometry_.meshMaterialIndices));
102 object.emplace("heightValues", floats(shape.geometry_.heightValues));
103 object.emplace("heightCountX", eve::Value(std::to_string(shape.geometry_.heightCountX)));
104 object.emplace("heightCountZ", eve::Value(std::to_string(shape.geometry_.heightCountZ)));
105 object.emplace("heightCellSizeX", eve::Value(shape.geometry_.heightCellSizeX));
106 object.emplace("heightCellSizeZ", eve::Value(shape.geometry_.heightCellSizeZ));
107 object.emplace("heightGlobalMin", eve::Value(shape.geometry_.heightGlobalMin));
108 object.emplace("heightGlobalMax", eve::Value(shape.geometry_.heightGlobalMax));
109 object.emplace("heightClockwise", eve::Value(shape.geometry_.heightClockwise));
110 return eve::Value(std::move(object));
111 }
112
113 static void setBodyId(Body& body, int id) { body.id_ = id; }
114 static void setBodyId(Body3D& body, int id) { body.id_ = id; }
115 static void setShapeId(Shape3D& shape, int id) { shape.id_ = id; }
116 static void setJointId(Joint3D& joint, int id) { joint.id_ = id; }
117 static std::unique_ptr<World3D> makeDetachedWorld3D(float gx, float gy, float gz, eve::PersistentId instanceId,
118 bool allowSleep) {
119 return std::unique_ptr<World3D>(new World3D(gx, gy, gz, allowSleep, instanceId, false, RigidBackend3D::Box3D));
120 }
121 static eve::Result<void> finishPrepared(World& world, int nextId, const SimulationObservation& observation) {
122 world.nextId_ = nextId;
123 auto restored = world.simulation_->restoreObservation(observation);
124 if (!restored) return restored;
125 world.simulationTick_ = observation.lastTick;
127 }
128 static eve::Result<void> finishPrepared(World3D& world, int nextBodyId, int nextShapeId, int nextJointId,
129 const SimulationObservation& observation) {
130 world.nextId_ = nextBodyId;
131 world.nextShapeId_ = nextShapeId;
132 world.nextJointId_ = nextJointId;
133 auto restored = world.simulation_->restoreObservation(observation);
134 if (!restored) return restored;
135 world.simulationTick_ = observation.lastTick;
137 }
138
140 [[nodiscard]] static Result<void> prepareRuntimeLinks(const World3D& live, World3D& prepared) {
141 try {
142 prepared.nextBodyHandleIndex_ = live.nextBodyHandleIndex_;
143 prepared.nextShapeHandleIndex_ = live.nextShapeHandleIndex_;
144 prepared.nextJointHandleIndex_ = live.nextJointHandleIndex_;
145 prepared.refreshRuntimeHandlesAfterRestore();
146 return Result<void>::success();
147 } catch (const std::exception& error) {
149 DiagnosticCode::Failed, std::string("Cannot prepare restored 3D links: ") + error.what(),
150 "physics.world3d.restore.links"));
151 }
152 }
153
154 static void adopt(World& live, World& prepared) { live.adoptPreparedTopology(prepared); }
155 static void adopt(World3D& live, World3D& prepared) { live.adoptPreparedTopology(prepared); }
156};
157} // namespace eve::physics
std::vector< std::unique_ptr< ScriptClimbingEventSink > > live
int instanceId
ShaderImageInput shape
std::string id
ScriptMotionSinkStore< ScriptMotionFloatSink > floats
std::string error
Definition Package.cpp:174
std::set< std::string > values
std::string body
Object::Ptr value
std::string_view source
Backend-neutral, observable fixed-step contract for physics domains.
Json object
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
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
Definition Status.cpp:19
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
std::map< std::string, Value > Object
Definition Value.h:34
std::vector< Value > Array
Definition Value.h:33
Id128 public API.
Definition Identity.h:111
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
Observable backend progress shared by CPU and accelerator providers.
eve::SimulationTick lastTick
Tick of the most recently completed step.
static void setJointId(Joint3D &joint, int id)
static std::unique_ptr< World3D > makeDetachedWorld3D(float gx, float gy, float gz, eve::PersistentId instanceId, bool allowSleep)
static void setShapeId(Shape3D &shape, int id)
static Result< void > finishShapeMass(World3D &world)
Preflight candidate mass/inertia arithmetic before applying backend mass updates.
static void setBodyId(Body3D &body, int id)
static Value shapeContact(const Shape3D &shape)
Capture native shape contact state and wrapper-owned collision settings.
static void ownPreparedFixture(World &world, std::unique_ptr< Fixture > fixture)
Candidate owns a fixture wrapper after registering it with its backend.
static eve::Result< void > finishPrepared(World3D &world, int nextBodyId, int nextShapeId, int nextJointId, const SimulationObservation &observation)
static Result< void > restoreJoints2D(World &candidate, const Value::Array &joints, std::uint32_t firstHandleIndex)
Rebuild validated joint definitions inside a detached candidate, allocating fresh runtime handles.
static Result< void > restoreCouplings3D(World3D &world, const Value::Array &values)
Rebuild transmissions after candidate handles and native joints are ready.
static Result< void > prepareRuntimeLinks(const World3D &live, World3D &prepared)
Allocate fresh 3D links entirely in the detached candidate before publishing topology.
static eve::Result< void > finishPrepared(World &world, int nextId, const SimulationObservation &observation)
static eve::Value shapeSource(const Shape3D &shape)
static Result< float > restoreShapeContact(Shape3D &shape, const Value::Object &source)
Restore validated candidate contact state; return density for deferred mass preparation.
static Result< Value > captureCouplings3D(const World3D &world)
Capture transmissions with stable source ids and current unwrapped phase.
static void adopt(World3D &live, World3D &prepared)
static Result< Value > captureJoints2D(const World &world)
Capture owning joint definitions; rejects unregistered backend joints.
static void ownPreparedShape(World3D &world, std::unique_ptr< Shape3D > shape)
Transfer one reconstructed shape wrapper to its candidate world.
static void adopt(World &live, World &prepared)
static void ownPreparedJoint(World3D &world, std::unique_ptr< Joint3D > joint)
Transfer one reconstructed joint wrapper to its candidate world.
static void ownPreparedBody(World &world, std::unique_ptr< Body > body)
Candidate owns newly reconstructed wrappers so rollback releases all allocations.
static void setBodyId(Body &body, int id)
static void ownPreparedBody(World3D &world, std::unique_ptr< Body3D > body)
Detached 3D candidates own all reconstructed wrappers through commit or rollback.