载入中...
搜索中...
未找到
WorldSnapshotCodec.cpp
浏览该文件的文档.
37 eve::DiagnosticCode::InvariantViolation, "physics snapshot schema constant is invalid", "schema"));
41eve::Result<void> checkEnvelope(const eve::SnapshotEnvelope& snapshot, std::string_view type, std::string_view schema) {
68 if (duration.value() < 0 || revision.value() != stepCount.value() || tick.value() != snapshot.tick.value() ||
87bool validBodyType(const std::string& type) { return type == "static" || type == "kinematic" || type == "dynamic"; }
91 if (!object || !hasExactFields(*object, {"active", "angle", "awake", "bullet", "fixedRotation", "fixtures",
98 if (!id || id.value() == 0 || id.value() >= static_cast<std::uint64_t>(std::numeric_limits<int>::max()))
100 eve::DiagnosticCode::ParseError, "2D physics snapshot body id is invalid", "payload.bodies.id"));
105 eve::DiagnosticCode::InvalidArgument, "2D physics snapshot body type is invalid", "payload.bodies.type"));
139 return eve::Result<Body2DState>::failure(eve::Diagnostic::error(eve::DiagnosticCode::ParseError,
145 if (!d || !hasExactFields(*d, {"linearDamping", "angularDamping", "gravityScale", "allowSleep"}))
168 if (!object || !hasExactFields(*object, {"active", "awake", "bullet", "dynamics", "fixedRotation",
177 if (!id || id.value() == 0 || id.value() >= static_cast<std::uint64_t>(std::numeric_limits<int>::max()))
179 eve::DiagnosticCode::ParseError, "3D physics snapshot body id is invalid", "payload.bodies.id"));
184 eve::DiagnosticCode::InvalidArgument, "3D physics snapshot body type is invalid", "payload.bodies.type"));
226 !hasExactFields(*dynamics, {"linearDamping", "angularDamping", "gravityScale", "sleepThreshold", "allowSleep"}))
242 Diagnostic::error(DiagnosticCode::InvalidArgument, "3D damping and sleep threshold must be nonnegative"));
249 object.emplace("dynamics", Value(Value::Object{{"linearDamping", Value(body.raw()->GetLinearDamping())},
372 const b3Transform frameA = b3Joint_GetLocalFrameA(eve::physics::box3d::Access::jointId(*joint));
373 const b3Transform frameB = b3Joint_GetLocalFrameB(eve::physics::box3d::Access::jointId(*joint));
387eve::Value::Object observationFields(const SimulationObservation& observation, eve::SimulationTick tick) {
392 object.emplace("simulatedDurationNs", eve::Value(std::to_string(observation.simulatedDuration.nanoseconds())));
419eve::Value make3DPayload(const World3D& world, const std::vector<Body3D*>& bodies, const std::vector<Shape3D*>& shapes,
426 object.emplace("allowSleep", Value(b3World_IsSleepingEnabled(eve::physics::box3d::Access::worldId(world))));
445 eve::DiagnosticCode::ParseError, "physics snapshot bodies must be an array", "payload.bodies"));
461 eve::DiagnosticCode::ParseError, "physics snapshot bodies must be an array", "payload.bodies"));
472eve::Result<std::unique_ptr<World>> prepareWorld2D(const std::vector<Body2DState>& states, float gravityX,
558eve::Result<std::vector<float>> readFloatArray(const eve::Value::Object& object, std::string_view name) {
573 DiagnosticCode::InvalidArgument, "Snapshot number exceeds finite float range", std::string(name)));
579eve::Result<std::vector<std::int32_t>> readIntArray(const eve::Value::Object& object, std::string_view name) {
589 eve::DiagnosticCode::ParseError, "snapshot integer array item must be a string", std::string(name)));
608 auto prepared = WorldSnapshotAccess::makeDetachedWorld3D(gravityX, gravityY, gravityZ, instanceId, allowSleep);
672 if (bodyId.value() <= 0 || bodyId.value() >= std::numeric_limits<int>::max() || !id || id.value() <= 0 ||
677 eve::DiagnosticCode::Conflict, "3D shape references invalid identity or source", "payload.shapes"));
711 shape = body->newTriangleMeshShape(vertices.value(), indices.value(), weld.value(), tolerance.value(),
734 body->newHeightFieldShape(static_cast<int>(cx.value()), static_cast<int>(cz.value()), sx.value(),
739 eve::DiagnosticCode::Unsupported, "3D shape kind cannot be reconstructed", "payload.shapes.kind"));
791 eve::DiagnosticCode::Conflict, "3D joint references invalid bodies or frames", "payload.joints"));
809 joint = prepared->newDistanceJoint(aBody, bBody, anchorA.x, anchorA.y, anchorA.z, anchorB.x, anchorB.y,
815 prepared->newRevoluteJoint(aBody, bBody, anchorA.x, anchorA.y, anchorA.z, axis.x, axis.y, axis.z);
820 prepared->newPrismaticJoint(aBody, bBody, anchorA.x, anchorA.y, anchorA.z, axis.x, axis.y, axis.z);
825 prepared->newSphericalJoint(aBody, bBody, anchorA.x, anchorA.y, anchorA.z, axis.x, axis.y, axis.z);
846 eve::DiagnosticCode::Unsupported, "3D joint kind cannot be reconstructed", "payload.joints.kind"));
872 WorldSnapshotAccess::finishPrepared(*prepared, maxBodyId + 1, maxShapeId + 1, maxJointId + 1, observation);
#define EV_READ_BODY3D_BOOL(name)
#define EV_READ_BODY3D_FLOAT(name)
Backend-neutral, observable fixed-step contract for physics domains.
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Definition Diagnostic.cpp:21
constexpr std::int64_t nanoseconds() const noexcept
Return the exact signed nanosecond representation.
Definition Time.h:69
static constexpr Duration fromNanoseconds(std::int64_t nanoseconds) noexcept
Construct an exact duration from nanoseconds.
Definition Time.h:55
static std::optional< LogicalId > parse(std::string_view text)
Parses a scoped logical name.
Definition Identity.cpp:37
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
const T * getIf() const noexcept
Return a typed pointer, or nullptr when the kind differs.
Definition Value.h:209
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
2D fixture: a shape attached to a Body with material + filter settings. Also carries a string tag use...
Definition Fixture.h:18
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
Result< float > readFloat(const Value::Object &object, std::string_view name)
Reads a required finite number representable by float.
Definition WorldSnapshotParsing.cpp:66
eve::Result< std::unique_ptr< World3D > > prepareWorld3D(const std::vector< Body3DState > &states, const eve::Value::Array &shapes, const eve::Value::Array &joints, float gravityX, float gravityY, float gravityZ, const SimulationObservation &observation, eve::PersistentId instanceId, bool legacyParameters, bool allowSleep)
Definition WorldSnapshotCodec.cpp:601
constexpr std::uint64_t kSnapshotVersion
Definition WorldSnapshotCodec.h:27
Result< void > restoreFixtureMaterial(Fixture &fixture, const Value &value)
Validate material fields before applying them to a detached fixture.
Definition PlanarMaterialSnapshot.cpp:66
const Value * field(const Value::Object &object, std::string_view name)
Borrow an optional field only during the current synchronous codec call. @ownership Object owns the r...
Definition WorldSnapshotParsing.cpp:17
eve::Value shapesTopologyValue(const std::vector< Shape3D * > &shapes)
Definition WorldSnapshotCodec.cpp:350
eve::Value make2DPayload(const World &world, const std::vector< Body * > &bodies)
Definition WorldSnapshotCodec.cpp:397
eve::Value jointsTopologyValue(const std::vector< Joint3D * > &joints)
Definition WorldSnapshotCodec.cpp:360
eve::Value::Object observationFields(const SimulationObservation &observation, eve::SimulationTick tick)
Definition WorldSnapshotCodec.cpp:387
bool hasExactFields(const Value::Object &object, std::initializer_list< std::string_view > expected)
Internal exact-field schema predicate.
Definition WorldSnapshotParsing.cpp:9
eve::Value fixtureTopologyValue(const Body &body)
Definition WorldSnapshotCodec.cpp:298
Result< void > restoreJoint3DParameters(b3JointId id, const Value &value)
Validate the complete parameter object before updating a live candidate joint; no retained references...
Definition Joint3DParameters.cpp:251
constexpr std::string_view kWorld3DType
Definition WorldSnapshotCodec.h:25
Result< std::uint64_t > readUint64(const Value::Object &object, std::string_view name)
Reads a required unsigned decimal integer string.
Definition WorldSnapshotParsing.cpp:30
eve::Value make3DPayload(const World3D &world, const std::vector< Body3D * > &bodies, const std::vector< Shape3D * > &shapes, const std::vector< Joint3D * > &joints)
Definition WorldSnapshotCodec.cpp:419
constexpr std::string_view kWorld2DType
Definition WorldSnapshotCodec.h:23
Result< std::int64_t > readInt64(const Value::Object &object, std::string_view name)
Reads a required signed decimal integer string.
Definition WorldSnapshotParsing.cpp:42
Result< bool > readBool(const Value::Object &object, std::string_view name)
Reads a required boolean.
Definition WorldSnapshotParsing.cpp:75
eve::Result< std::vector< float > > readFloatArray(const eve::Value::Object &object, std::string_view name)
Read a finite float array, rejecting missing or malformed elements.
Definition WorldSnapshotCodec.cpp:558
eve::Result< std::vector< std::int32_t > > readIntArray(const eve::Value::Object &object, std::string_view name)
Definition WorldSnapshotCodec.cpp:579
eve::Result< Body2DState > parseBody2D(const eve::Value &value)
Definition WorldSnapshotCodec.cpp:89
eve::Result< eve::LogicalId > snapshotSchema(std::string_view text)
Definition WorldSnapshotCodec.cpp:33
Value captureJoint3DParameters(b3JointId id)
Capture solver-owned parameters on the physics owner thread into an owning value.
Definition Joint3DParameters.cpp:240
eve::Result< std::unique_ptr< World > > prepareWorld2D(const std::vector< Body2DState > &states, float gravityX, float gravityY, float meter, const SimulationObservation &observation, eve::PersistentId instanceId)
Definition WorldSnapshotCodec.cpp:472
eve::Result< std::vector< Body2DState > > parseBodies2D(const eve::Value::Object &object)
Definition WorldSnapshotCodec.cpp:440
eve::Result< void > checkEnvelope(const eve::SnapshotEnvelope &snapshot, std::string_view type, std::string_view schema)
Definition WorldSnapshotCodec.cpp:41
eve::Result< std::vector< Body3DState > > parseBodies3D(const eve::Value::Object &object)
Definition WorldSnapshotCodec.cpp:456
constexpr std::uint64_t kSnapshot2DVersion
Definition WorldSnapshotCodec.h:28
eve::Result< Body3DState > parseBody3D(const eve::Value &value)
Definition WorldSnapshotCodec.cpp:166
Value fixtureMaterialValue(const b2Fixture &fixture)
Owning material/filter projection from a synchronous backend observation.
Definition PlanarMaterialSnapshot.cpp:53
eve::Value bodyValue(const Body &body)
Definition WorldSnapshotCodec.cpp:246
bool validBodyType(const std::string &type)
Definition WorldSnapshotCodec.cpp:87
Result< std::string > readString(const Value::Object &object, std::string_view name)
Reads a required owning string.
Definition WorldSnapshotParsing.cpp:22
eve::Result< ObservationState > parseObservation(const eve::Value::Object &object, const eve::SnapshotEnvelope &snapshot)
Definition WorldSnapshotCodec.cpp:56
@ InvalidArgument
@ UnknownVersion
@ Unsupported
@ InvariantViolation
@ Failed
@ ParseError
@ Conflict
@ Applied
detail::StrongUint64< detail::SimulationTickTag > SimulationTick
Deterministic simulation time step; it is not wall-clock time.
Definition Time.h:31
Stable outer format shared by persistence and cross-process snapshots.
Definition Snapshot.h:46
Observable backend progress shared by CPU and accelerator providers.
Definition SimulationBackend.h:136
eve::Duration simulatedDuration
Accumulated logical simulation time, never wall-clock time.
Definition SimulationBackend.h:142
float lastDeltaSeconds
Duration consumed by the most recently completed step.
Definition SimulationBackend.h:146
std::uint64_t stepCount
Number of completed steps since backend creation.
Definition SimulationBackend.h:138
static void setJointId(Joint3D &joint, int id)
Definition WorldSnapshotAccess.h:116
static std::unique_ptr< World3D > makeDetachedWorld3D(float gx, float gy, float gz, eve::PersistentId instanceId, bool allowSleep)
Definition WorldSnapshotAccess.h:117
static void setShapeId(Shape3D &shape, int id)
Definition WorldSnapshotAccess.h:115
static Result< void > finishShapeMass(World3D &world)
Preflight candidate mass/inertia arithmetic before applying backend mass updates.
Definition Shape3DContactSnapshot.cpp:168
static void ownPreparedFixture(World &world, std::unique_ptr< Fixture > fixture)
Candidate owns a fixture wrapper after registering it with its backend.
Definition WorldSnapshotAccess.h:53
static eve::Result< void > finishPrepared(World &world, int nextId, const SimulationObservation &observation)
Definition WorldSnapshotAccess.h:121
static eve::Value shapeSource(const Shape3D &shape)
Definition WorldSnapshotAccess.h:68
static Result< float > restoreShapeContact(Shape3D &shape, const Value::Object &source)
Restore validated candidate contact state; return density for deferred mass preparation.
Definition Shape3DContactSnapshot.cpp:67
static void ownPreparedShape(World3D &world, std::unique_ptr< Shape3D > shape)
Transfer one reconstructed shape wrapper to its candidate world.
Definition WorldSnapshotAccess.h:61
static void ownPreparedJoint(World3D &world, std::unique_ptr< Joint3D > joint)
Transfer one reconstructed joint wrapper to its candidate world.
Definition WorldSnapshotAccess.h:65
static void ownPreparedBody(World &world, std::unique_ptr< Body > body)
Candidate owns newly reconstructed wrappers so rollback releases all allocations.
Definition WorldSnapshotAccess.h:49
static void setBodyId(Body &body, int id)
Definition WorldSnapshotAccess.h:113
static b3BodyId bodyId(const Body3D &body)
Borrows the native identifier synchronously; facade must belong to a Box3D world.
Definition Box3DBody.cpp:649
static b3WorldId worldId(const World3D &world)
Internal checked Box3D world ID; fails for another backend.
Definition Box3DWorld.cpp:13
static b3ShapeId shapeId(const Shape3D &shape)
Borrows a Box3D shape identifier for a synchronous native operation.
Definition Box3DShape.cpp:328
static b3JointId jointId(const Joint3D &joint)
Internal checked Box3D joint ID; fails for another backend.
Definition Box3DJoint.cpp:626
SimulationObservation value
Definition WorldSnapshotCodec.h:31