载入中...
搜索中...
未找到
WorldSphereSweep.cpp
浏览该文件的文档.
1// eve-hot-path
3#include <charconv>
4#include <cmath>
5#include <limits>
6#include "common/Diagnostic.h"
8#include "physics/Body3D.h"
9#include "physics/World3D.h"
10
11namespace eve::physics {
12namespace {
13class Sweep final : public IWorldSphereSweep {
14public:
15 Sweep(PhysicsWorldHandle world, std::uint64_t epoch, PhysicsBodyHandle ignored, WorldSphereSweepSettings settings)
16 : world_(world), epoch_(epoch), ignored_(ignored), settings_(settings) {}
17 Result<std::vector<WorldSphereContact>> query(double x, double y, double z, double endX, double endY,
18 double endZ) const override {
19 auto* world = World3D::findWorld(world_);
20 if (!world || !world->isValid() || world->queryEpoch() != epoch_)
21 return Result<std::vector<WorldSphereContact>>::failure(Diagnostic::error(
22 DiagnosticCode::NotFound, "Bound World3D was destroyed or restored; rebind the sweep", "world"));
23 if (settings_.ignoredBodyId >= 0) {
24 auto* body = world->findBodyById(settings_.ignoredBodyId);
25 if (!body || body->runtimeHandle() != ignored_)
26 return Result<std::vector<WorldSphereContact>>::failure(
28 "Ignored body was destroyed or replaced; rebind the sweep", "ignoredBody"));
29 }
30 return world->sweepSphereOwned(x, y, z, endX, endY, endZ, settings_);
31 }
32
33private:
34 PhysicsWorldHandle world_;
35 std::uint64_t epoch_;
36 PhysicsBodyHandle ignored_;
37 WorldSphereSweepSettings settings_;
38};
39class Factory final : public IWorldSphereSweepFactory {
40public:
41 Result<std::unique_ptr<IWorldSphereSweep>> bind(std::string_view token,
42 WorldSphereSweepSettings settings) const override {
43 const auto separator = token.find(':');
44 std::uint64_t packed = 0, epoch = 0;
45 if (separator == std::string_view::npos)
46 return Result<std::unique_ptr<IWorldSphereSweep>>::failure(
47 Diagnostic::error(DiagnosticCode::InvalidArgument, "Invalid World3D query token", "world"));
48 const auto first = token.substr(0, separator), second = token.substr(separator + 1);
49 auto parse = [](std::string_view part, std::uint64_t& result) {
50 const auto parsed = std::from_chars(part.data(), part.data() + part.size(), result, 16);
51 return !part.empty() && parsed.ec == std::errc{} && parsed.ptr == part.data() + part.size();
52 };
53 if (!parse(first, packed) || !parse(second, epoch) || !std::isfinite(settings.radius) || settings.radius <= 0 ||
54 settings.ignoredBodyId < -1)
55 return Result<std::unique_ptr<IWorldSphereSweep>>::failure(Diagnostic::error(
56 DiagnosticCode::InvalidArgument, "Invalid world token, radius or ignored body", "worldSweep"));
57 const auto handle = PhysicsWorldHandle::fromPacked(packed);
58 auto* world = World3D::findWorld(handle);
59 if (!world || !world->isValid() || world->queryEpoch() != epoch)
60 return Result<std::unique_ptr<IWorldSphereSweep>>::failure(
61 Diagnostic::error(DiagnosticCode::NotFound, "World3D query token is stale", "world"));
63 if (settings.ignoredBodyId >= 0) {
64 auto* body = world->findBodyById(settings.ignoredBodyId);
65 if (!body)
66 return Result<std::unique_ptr<IWorldSphereSweep>>::failure(Diagnostic::error(
67 DiagnosticCode::NotFound, "Ignored body does not exist in this world", "ignoredBody"));
68 ignored = body->runtimeHandle();
69 }
70 return Result<std::unique_ptr<IWorldSphereSweep>>::success(
71 std::make_unique<Sweep>(handle, epoch, ignored, settings));
72 }
73};
74} // namespace
76 static Factory factory;
77 return factory;
78}
79std::string World3D::queryToken() const {
80 char buffer[17]{};
81 const auto encoded = std::to_chars(buffer, buffer + 16, runtimeHandle().packed(), 16);
82 std::string token(buffer, encoded.ptr);
83 const auto epoch = std::to_chars(buffer, buffer + 16, queryEpoch(), 16);
84 return token + ":" + std::string(buffer, epoch.ptr);
85}
86} // namespace eve::physics
double z
double y
double x
Stable, structured diagnostics shared by engine modules.
std::int32_t second
std::int32_t first
Poco::JSON::Object::Ptr result
OceanWakeSettings settings
OceanGpuFrameToken token
uint64_t epoch
Definition OnnxGpgpu.cpp:28
std::unique_ptr< gpgpu::GpuBuffer > buffer
Definition OnnxGpgpu.cpp:26
PrimitiveHandle handle
std::string body
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Optional binding capability; physics owns the factory for process lifetime.
static constexpr RuntimeHandle fromPacked(std::uint64_t value) noexcept
Reconstructs a handle from an explicit packed boundary value.
static constexpr RuntimeHandle invalid() noexcept
Returns the canonical invalid handle.
PhysicsWorldHandle runtimeHandle() const noexcept
Process-local identity used by PhysicsLink; invalid after destruction.
Definition World3D.h:136
std::string queryToken() const
Opaque process-local query token; re-fetch after restore, never persist in save data.
static World3D * findWorld(PhysicsWorldHandle handle) noexcept
Resolve a live process-local world handle, or null after teardown. @thread Owning physics thread only...
Definition World3D.cpp:296
std::uint64_t queryEpoch() const noexcept
Monotonic query binding revision; successful restore invalidates prior sessions.
Definition World3D.h:645
I * query()
Queries .
Definition Capability.h:88
Optional physics backend for vehicle mobility and body attach.
Definition Climbing.h:36
IWorldSphereSweepFactory & worldSphereSweepFactory()
Borrow the process-owned optional sweep factory on the simulation thread.
eve::RuntimeHandle< PhysicsWorldHandleTag > PhysicsWorldHandle
Generation-qualified runtime identity for a physics world.
eve::RuntimeHandle< PhysicsBodyHandleTag > PhysicsBodyHandle
Generation-qualified runtime identity for a physics body.
ModelData parse(std::span< const uint8_t > bytes)
Parse.
WidgetDesc separator(std::string id)
Horizontal separator line.
Definition Widget.cpp:379