载入中...
搜索中...
未找到
PhysicsCapabilities.cpp
浏览该文件的文档.
2#include "common/Capability.h"
7#include "physics/Physics.h"
10#include "physics/World.h"
11#include "physics/World3D.h"
14
15#include <algorithm>
16#include <string>
17#include <vector>
18
19namespace eve::physics {
20namespace {
21
22class PhysicsQueryImpl final : public eve::IPhysicsQuery {
23public:
24 int newWorld(float gravityX, float gravityY) override {
25 auto *ph = eve::ModuleManager::getInstance<Physics>("Physics");
26 if (!ph) return -1;
27 auto *w = ph->newWorld(gravityX, gravityY);
28 if (!w) return -1;
29 worlds_.push_back(w);
30 return static_cast<int>(worlds_.size()) - 1;
31 }
32
33 int worldCount() override {
34 int n = 0;
35 for (auto *w : worlds_)
36 if (w) ++n;
37 return n;
38 }
39
40 bool worldGravity(int id, float *gx, float *gy) override {
41 auto *w = world(id);
42 if (!w) return false;
43 if (gx) *gx = w->getGravityX();
44 if (gy) *gy = w->getGravityY();
45 return true;
46 }
47
48 bool rayCast(int id, float x1, float y1, float x2, float y2, RayHitInfo *out) override {
49 auto *w = world(id);
50 if (!w || !out) return false;
51 w->rayCast(x1, y1, x2, y2);
52 out->hit = w->hasRayHit();
53 if (out->hit) {
54 out->bodyId = w->getRayHitBodyId();
55 out->x = w->getRayHitX();
56 out->y = w->getRayHitY();
57 out->normalX = w->getRayHitNormalX();
58 out->normalY = w->getRayHitNormalY();
59 out->fraction = w->getRayHitFraction();
60 }
61 return true;
62 }
63
64 bool removeWorld(int id) override {
65 if (id < 0 || static_cast<size_t>(id) >= worlds_.size()) return false;
66 delete worlds_[static_cast<size_t>(id)];
67 worlds_[static_cast<size_t>(id)] = nullptr;
68 return true;
69 }
70
71private:
72 World *world(int id) const {
73 if (id < 0 || static_cast<size_t>(id) >= worlds_.size()) return nullptr;
74 return worlds_[static_cast<size_t>(id)];
75 }
76
77 std::vector<World *> worlds_;
78};
79
80} // namespace
81
83 static PhysicsQueryImpl impl;
84 eve::cap::provide<eve::IWorldSphereSweepFactory>(&worldSphereSweepFactory());
85 eve::cap::provide<eve::IPhysicsQuery>(&impl);
86 eve::cap::provide<eve::IProcgenWorldQuery>(&procgenWorldQuery());
87 eve::cap::provide<eve::ICameraObstructionQuery>(&cameraObstructionQuery());
88 // Claim World3D through the sensing router rather than owning the single-slot
89 // `ILineOfSightQuery` capability: a grid board can then claim its own space without the two
90 // providers replacing each other by load order. `ensureLineOfSightRouter` reports Conflict
91 // when some other provider already holds the capability; that registration is left alone on
92 // purpose, so this adapter simply does not take part and the project keeps its own provider.
94 if (router)
95 router.value()
97 .ignore("physics line-of-sight space claim was refused");
99}
100
101} // namespace eve::physics
float w
Definition AnimClip.cpp:738
void * impl
glm::vec3 n
Definition Grass.cpp:63
One registered line-of-sight provider that dispatches to backends by coordinate space.
std::string id
ScriptStructure out
Physics-to-sensing line-of-sight capability adapter.
2D physics world query surface (provided by the physics module).
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::IProcgenWorldQuery & procgenWorldQuery()
Borrow the process-owned terrain projection query; no world ownership is transferred.
void registerPhysicsArtifactProvider()
Register the physics provider in the common capability registry.
eve::ICameraObstructionQuery & cameraObstructionQuery()
Borrow the process-owned camera query on the simulation thread; lifetime is process-wide.
IWorldSphereSweepFactory & worldSphereSweepFactory()
Borrow the process-owned optional sweep factory on the simulation thread.
void registerPhysicsCapabilities()
Registers physics capabilities.
Result< LineOfSightRouter * > ensureLineOfSightRouter()
The process-wide router, registered as the ILineOfSightQuery capability.
Physics-owned CPU collider provider for generated artifacts.