载入中...
搜索中...
未找到
PhysicsWorldQueries.cpp
浏览该文件的文档.
6#include "physics/World3D.h"
7
8#include <algorithm>
9#include <vector>
10
11namespace eve::physics {
12namespace {
13class CameraObstructionQueryImpl final : public eve::ICameraObstructionQuery {
14public:
15 void add(World3D* world, std::weak_ptr<const void> lifetime) {
16 std::erase_if(worlds_, [](const RegisteredWorld& entry) { return entry.lifetime.expired(); });
17 const auto found = std::find_if(worlds_.begin(), worlds_.end(),
18 [world](const RegisteredWorld& entry) { return entry.world == world; });
19 if (world && found == worlds_.end()) worlds_.push_back({world, std::move(lifetime)});
20 }
21
22 bool sphereCast(float fromX, float fromY, float fromZ, float toX, float toY, float toZ, float radius,
23 uint64_t maskBits, int ignoredBodyId, eve::CameraObstructionHit* out) override {
25 if (!out) return false;
26 for (const RegisteredWorld& entry : worlds_) {
27 auto lifetime = entry.lifetime.lock();
28 World3D* world = entry.world;
29 if (!lifetime || !world || !world->isValid()) continue;
30 CameraSphereHit3D hit;
31 if (!world->sphereCast(fromX, fromY, fromZ, toX, toY, toZ, radius, maskBits, ignoredBodyId, &hit)) continue;
32 if (!out->hit || hit.fraction < out->fraction) {
33 out->hit = true;
34 out->bodyId = hit.bodyId;
35 out->fraction = hit.fraction;
36 out->x = hit.x;
37 out->y = hit.y;
38 out->z = hit.z;
39 out->normalX = hit.nx;
40 out->normalY = hit.ny;
41 out->normalZ = hit.nz;
42 }
43 }
44 return out->hit;
45 }
46
47private:
48 struct RegisteredWorld {
49 World3D* world = nullptr;
50 std::weak_ptr<const void> lifetime;
51 };
52
53 std::vector<RegisteredWorld> worlds_;
54};
55
56CameraObstructionQueryImpl& cameraObstructionQueryImpl();
57
58class ProcgenWorldQueryImpl final : public eve::IProcgenWorldQuery {
59public:
60 eve::Result<eve::ProcgenSurfaceHit> projectDown(float x, float z, float maxY, float minY,
61 std::uint64_t maskBits) override {
64 const bool found = cameraObstructionQueryImpl().sphereCast(x, maxY, z, x, minY, z, 0.f, maskBits, -1, &hit);
66 output.hit = true;
67 output.objectId = hit.bodyId;
68 output.x = hit.x;
69 output.y = hit.y;
70 output.z = hit.z;
71 output.normalX = hit.normalX;
72 output.normalY = hit.normalY;
73 output.normalZ = hit.normalZ;
75 }
76};
77
78CameraObstructionQueryImpl& cameraObstructionQueryImpl() {
79 static CameraObstructionQueryImpl impl;
80 return impl;
81}
82
83TargetingLineOfSightAdapter& targetingLineOfSightAdapterImpl() {
84 static TargetingLineOfSightAdapter impl;
85 return impl;
86}
87
88} // namespace
89
90eve::ICameraObstructionQuery& cameraObstructionQuery() { return cameraObstructionQueryImpl(); }
91TargetingLineOfSightAdapter& targetingLineOfSightAdapter() { return targetingLineOfSightAdapterImpl(); }
93 static ProcgenWorldQueryImpl query;
94 return query;
95}
96
98 if (world) cameraObstructionQueryImpl().add(world, world->queryLifetime_);
99}
101 return targetingLineOfSightAdapterImpl().addWorld(world);
102}
103
104} // namespace eve::physics
double z
double x
std::string output
void * impl
std::weak_ptr< const void > lifetime
double radius
Definition MeleeHit.cpp:42
bool hit
bool found
ScriptStructure out
Physics-to-sensing line-of-sight capability adapter.
Cross-module query used by camera without linking against physics.
Optional synchronous world-query capability for procedural generation.
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
Read-only 3D physics implementation of sensing's LOS capability.
Box3D rigid-body world. Script coordinates are meters (Box3D native), unlike 2D World which uses pixe...
Definition World3D.h:47
ForceVector add(ForceVector a, ForceVector b)
Definition ForceMath.h:13
Optional physics backend for vehicle mobility and body attach.
Definition Climbing.h:36
TargetingLineOfSightAdapter & targetingLineOfSightAdapter()
Borrow the process-owned LOS adapter on the simulation thread; worlds are weakly observed.
eve::Result< void > registerTargetingLineOfSightWorld(World3D *world)
Registers a borrowed live World3D with the targeting LOS adapter.
eve::IProcgenWorldQuery & procgenWorldQuery()
Borrow the process-owned terrain projection query; no world ownership is transferred.
eve::ICameraObstructionQuery & cameraObstructionQuery()
Borrow the process-owned camera query on the simulation thread; lifetime is process-wide.
void registerCameraObstructionWorld(World3D *world)
Registers camera obstruction world.
Closest hit returned by the optional camera-obstruction provider.
Value result for a procedural world-surface projection query.