载入中...
搜索中...
未找到
WorldSnapshotCodec.h
浏览该文件的文档.
1#pragma once
2// Internal snapshot codec, never exposed as a gameplay header.
3#include <initializer_list>
4#include <memory>
5#include <set>
6#include <string>
7#include <string_view>
8#include <vector>
9#include "common/Snapshot.h"
11class b2Fixture;
12namespace eve::physics {
13class World;
14class World3D;
15class Body;
16class Body3D;
17class Shape3D;
18class Joint3D;
19class Fixture;
20namespace snapshot_detail {
21
22
23constexpr std::string_view kWorld2DType = "physics.world2d";
24constexpr std::string_view kWorld2DSchema = "physics:world2d";
25constexpr std::string_view kWorld3DType = "physics.world3d";
26constexpr std::string_view kWorld3DSchema = "physics:world3d";
27constexpr std::uint64_t kSnapshotVersion = 3;
28constexpr std::uint64_t kSnapshot2DVersion = 3;
29
34 int id = 0;
35 std::string type;
36 float x = 0.f, y = 0.f, angle = 0.f;
37 float vx = 0.f, vy = 0.f, angularVelocity = 0.f;
38 bool active = false, bullet = false, awake = false, fixedRotation = false;
41 bool allowSleep = true;
42};
44 int id = 0;
45 std::string type;
46 float x = 0.f, y = 0.f, z = 0.f;
47 float qx = 0.f, qy = 0.f, qz = 0.f, qw = 1.f;
48 float vx = 0.f, vy = 0.f, vz = 0.f;
49 float wx = 0.f, wy = 0.f, wz = 0.f;
50 bool active = false, bullet = false, awake = false, fixedRotation = false;
52 bool allowSleep = true;
53};
54template <typename BodyState>
55eve::Result<void> matchBodyIds(const std::vector<BodyState>& states, std::set<int>& ids) {
56 for (const BodyState& state : states) {
57 if (!ids.insert(state.id).second)
59 eve::DiagnosticCode::Conflict, "physics snapshot contains a duplicate body id", "payload.bodies.id"));
60 }
62}
63
64
66[[nodiscard]] Result<std::vector<float>> readFloatArray(const Value::Object& object, std::string_view name);
68bool hasExactFields(const Value::Object& object, std::initializer_list<std::string_view> expected);
73const Value* field(const Value::Object& object, std::string_view name);
74[[nodiscard]] Result<std::uint64_t> readUint64(const Value::Object& object, std::string_view name);
75[[nodiscard]] Result<std::int64_t> readInt64(const Value::Object& object, std::string_view name);
76[[nodiscard]] Result<float> readFloat(const Value::Object& object, std::string_view name);
77[[nodiscard]] Result<bool> readBool(const Value::Object& object, std::string_view name);
78[[nodiscard]] Result<std::string> readString(const Value::Object& object, std::string_view name);
79[[nodiscard]] Result<LogicalId> snapshotSchema(std::string_view text);
80[[nodiscard]] Result<void> checkEnvelope(const SnapshotEnvelope& snapshot, std::string_view type,
81 std::string_view schema);
82[[nodiscard]] Result<ObservationState> parseObservation(const Value::Object& object, const SnapshotEnvelope& snapshot);
86Value fixtureMaterialValue(const b2Fixture& fixture);
88[[nodiscard]] Result<void> restoreFixtureMaterial(Fixture& fixture, const Value& value);
92Value shapesTopologyValue(const std::vector<Shape3D*>& shapes);
93Value jointsTopologyValue(const std::vector<Joint3D*>& joints);
94Value make2DPayload(const World& world, const std::vector<Body*>& bodies);
95Value make3DPayload(const World3D& world, const std::vector<Body3D*>& bodies, const std::vector<Shape3D*>& shapes,
96 const std::vector<Joint3D*>& joints);
97[[nodiscard]] Result<std::unique_ptr<World>> prepareWorld2D(const std::vector<Body2DState>& states, float gravityX,
98 float gravityY, float meter,
99 const SimulationObservation& observation,
101[[nodiscard]] Result<std::unique_ptr<World3D>> prepareWorld3D(const std::vector<Body3DState>& states,
102 const Value::Array& shapes, const Value::Array& joints,
103 float gravityX, float gravityY, float gravityZ,
104 const SimulationObservation& observation,
105 PersistentId instanceId, bool legacyParameters,
106 bool allowSleep);
107} // namespace snapshot_detail
108} // namespace eve::physics
Value::Object payload
std::string name
Definition ECS.cpp:154
int instanceId
std::string body
Object::Ptr value
std::string text
Backend-neutral, observable fixed-step contract for physics domains.
Versioned, integrity-checked persistence envelopes.
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 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
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
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
std::unordered_map< const void *, size_t > ids
Definition Snapshot.cpp:109
constexpr std::string_view kWorld2DSchema
Result< float > readFloat(const Value::Object &object, std::string_view name)
Reads a required finite number representable by float.
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)
constexpr std::uint64_t kSnapshotVersion
Result< void > restoreFixtureMaterial(Fixture &fixture, const Value &value)
Validate material fields before applying them to a detached fixture.
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...
eve::Value shapesTopologyValue(const std::vector< Shape3D * > &shapes)
eve::Value make2DPayload(const World &world, const std::vector< Body * > &bodies)
constexpr std::string_view kWorld3DSchema
eve::Value jointsTopologyValue(const std::vector< Joint3D * > &joints)
bool hasExactFields(const Value::Object &object, std::initializer_list< std::string_view > expected)
Internal exact-field schema predicate.
eve::Value fixtureTopologyValue(const Body &body)
constexpr std::string_view kWorld3DType
Result< std::uint64_t > readUint64(const Value::Object &object, std::string_view name)
Reads a required unsigned decimal integer string.
eve::Value make3DPayload(const World3D &world, const std::vector< Body3D * > &bodies, const std::vector< Shape3D * > &shapes, const std::vector< Joint3D * > &joints)
constexpr std::string_view kWorld2DType
Result< std::int64_t > readInt64(const Value::Object &object, std::string_view name)
Reads a required signed decimal integer string.
Result< bool > readBool(const Value::Object &object, std::string_view name)
Reads a required boolean.
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.
eve::Result< void > matchBodyIds(const std::vector< BodyState > &states, std::set< int > &ids)
eve::Result< eve::LogicalId > snapshotSchema(std::string_view text)
Result< void > migratePlanarV2(Value &payload)
Migrate previous schema with explicit defaults for previously omitted state.
eve::Result< std::unique_ptr< World > > prepareWorld2D(const std::vector< Body2DState > &states, float gravityX, float gravityY, float meter, const SimulationObservation &observation, eve::PersistentId instanceId)
eve::Result< std::vector< Body2DState > > parseBodies2D(const eve::Value::Object &object)
eve::Result< void > checkEnvelope(const eve::SnapshotEnvelope &snapshot, std::string_view type, std::string_view schema)
eve::Result< std::vector< Body3DState > > parseBodies3D(const eve::Value::Object &object)
constexpr std::uint64_t kSnapshot2DVersion
Value fixtureMaterialValue(const b2Fixture &fixture)
Owning material/filter projection from a synchronous backend observation.
Result< std::string > readString(const Value::Object &object, std::string_view name)
Reads a required owning string.
eve::Result< ObservationState > parseObservation(const eve::Value::Object &object, const eve::SnapshotEnvelope &snapshot)
Optional physics backend for vehicle mobility and body attach.
Definition Climbing.h:36
Stable outer format shared by persistence and cross-process snapshots.
Definition Snapshot.h:46
Observable backend progress shared by CPU and accelerator providers.