载入中...
搜索中...
未找到
World3DSimulation.cpp
浏览该文件的文档.
1#include <algorithm>
2#include <cmath>
3#include "common/Profile.h"
5#include "physics/Shape3D.h"
6#include "physics/World3D.h"
7
8namespace eve::physics {
9namespace {
10eve::Result<eve::SimulationStep> makeLegacyStep(float dt, eve::SimulationTick currentTick) {
11 if (!std::isfinite(dt) || dt < 0.f) {
14 "World3D update dt must be finite and non-negative", "physics.world3d.update.dt"));
15 }
16 const auto nextTick = currentTick.incremented();
17 if (!nextTick) {
20 "World3D simulation tick cannot be incremented", "physics.world3d.simulationTick"));
21 }
22 auto duration = eve::Duration::fromSeconds(static_cast<double>(std::min(dt, 0.05f)));
24 return eve::Result<eve::SimulationStep>::success({*nextTick, std::move(duration).takeValue()});
25}
26} // namespace
27void World3D::update(float dt) { updateFull(dt, 4); }
28
29void World3D::updateFull(float dt, int subStepCount) {
30 EV_PROFILE_MODULE("physics", "World3D::update");
31 auto legacyStep = makeLegacyStep(dt, simulationTick_);
32 if (!legacyStep) {
33 legacyStep.ignore("legacy World3D::updateFull cannot return a structured error");
34 return;
35 }
36 auto result = step(std::move(legacyStep).takeValue(), SimulationSettings{8, 3, subStepCount});
37 result.ignore("legacy World3D::updateFull cannot return a structured result");
38}
39
41 if (!isValid() || !simulation_ || couplingStepActive_) {
43 eve::DiagnosticCode::PreconditionViolation, "Cannot step a destroyed or uninitialized 3D physics world",
44 "physics.world3d.step"));
45 }
46 auto forceStep = validateForceStep(stepValue);
47 if (!forceStep) return forceStep;
48 auto valid = detail::validateSimulationStep(stepValue, settings, simulation_->observation());
49 if (!valid) return valid;
50 for (Mechanism3D *mechanism : mechanisms_) {
51 if (mechanism) mechanism->syncBeforeStep();
52 }
53 for (Shape3D *shape : shapes_) {
54 if (shape && shape->isOneWay()) shape->refreshOneWayWorldData();
55 }
56 auto result = stepCoupledBackend(stepValue, settings);
57 if (!result) return result;
58 simulationTick_ = stepValue.tick;
59 finishForceStep();
62 return result;
63}
64
66 return simulation_ ? simulation_->observation() : SimulationObservation{};
67}
68
70 return simulation_ ? simulation_->kind() : SimulationBackendKind::Cpu;
71}
72
74 return simulation_ ? simulation_->determinism() : SimulationDeterminism::ToleranceBounded;
75}
76
77eve::Status World3D::backendSelectionStatus() const { return backendSelectionStatus_; }
78
79} // namespace eve::physics
float duration
ShaderImageInput shape
Poco::JSON::Object::Ptr result
bool valid
OceanWakeSettings settings
double dt
#define EV_PROFILE_MODULE(module, name)
Profile the enclosing scope, tagged with a module for grouping.
Definition Profile.h:158
float step
Definition TreeMesh.cpp:451
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
static Result< Duration > fromSeconds(double seconds)
Convert finite seconds to the nearest nanosecond.
Definition Time.cpp:17
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
Structured status and zero or more diagnostics for an operation.
Definition Status.h:70
constexpr std::optional< StrongUint64 > incremented() const noexcept
Returns the next value, or empty instead of unsigned wraparound.
Composed mechanical assembly owned by a World3D.
Definition Mechanism3D.h:25
3D shape (box/sphere/capsule) attached to a Body3D with material settings. Created via Body3D::new*Sh...
Definition Shape3D.h:34
SimulationObservation simulationObservation() const noexcept
Snapshot of completed backend steps and logical simulation time.
void update(float dt)
Steps the simulation by dt seconds (default substep count).
eve::Result< void > step(const eve::SimulationStep &step, const SimulationSettings &settings={})
Advances the 3D domain with an injected deterministic step.
eve::Status backendSelectionStatus() const
Selection status, including a structured absent-capability warning.
bool isValid() const
True while the underlying Box3D world is alive.
Definition World3D.cpp:239
SimulationDeterminism backendDeterminism() const noexcept
Replay/numeric guarantee declared by the selected backend.
void updateFull(float dt, int subStepCount)
Steps with an explicit substep count.
void clearContactEvents()
Clears all contact and trigger buffers before the next step.
void emitContactEvents()
Internal: collects Box3D contact events into the event buffers.
Definition World3D.cpp:562
SimulationBackendKind backendKind() const noexcept
Selected CPU/GPU/mock backend family.
Optional physics backend for vehicle mobility and body attach.
Definition Climbing.h:36
SimulationBackendKind
Kind of implementation that owns a simulation step.
SimulationDeterminism
Determinism guarantee made by a simulation backend.
@ ToleranceBounded
Results are equivalent within a documented numeric tolerance.
One deterministic fixed-step emitted by SimulationClock.
Definition Time.h:158
SimulationTick tick
Tick reached after this step is applied.
Definition Time.h:160
Observable backend progress shared by CPU and accelerator providers.
Validated solver policy for one simulation step.