载入中...
搜索中...
未找到
JoltWorldAabb.cpp
浏览该文件的文档.
1// Jolt requires its platform header before any other native header.
2#include <Jolt/Jolt.h>
3
4// Provider implementation dependencies.
5#include <algorithm>
6#include "physics/World3D.h"
9namespace eve::physics::jolt {
10eve::Result<BroadPhaseAabb3D> WorldBackend::queryAabbBroadPhaseOwned(float x1, float y1, float z1, float x2, float y2,
11 float z2, QueryFilter3D filter) {
12 if (!isValid()) return rejected<BroadPhaseAabb3D>("World expired", eve::DiagnosticCode::StaleHandle);
13 if (!isFinite(x1, y1, z1) || !isFinite(x2, y2, z2) || x1 > x2 || y1 > y2 || z1 > z2)
14 return rejected<BroadPhaseAabb3D>("Invalid AABB", eve::DiagnosticCode::InvalidArgument);
15 const JPH::AABox box(JPH::Vec3(x1, y1, z1), JPH::Vec3(x2, y2, z2));
16 std::vector<BroadPhaseHit3D> hits;
17 for (const auto& [id, body] : state->bodies) {
18 if (!body->owner || !body->active || body->owner->getId() == filter.ignoredBodyId) continue;
19 const auto transform = body->api().GetWorldTransform(body->id);
20 for (const auto& shape : body->shapes) {
21 if (!shape->owner || shape->sensor || shape->owner->getId() == filter.ignoredShapeId ||
22 !(shape->filter.categoryBits & filter.maskBits) || !(shape->filter.maskBits & filter.categoryBits))
23 continue;
24 const auto& g = shape->geometry;
25 auto local = JPH::Mat44::sRotationTranslation(
26 JPH::Quat(g.localQx, g.localQy, g.localQz, g.localQw).Normalized(), {g.localX, g.localY, g.localZ});
27 auto bounds = shape->native->GetWorldSpaceBounds(
28 (transform * local).PreTranslated(shape->native->GetCenterOfMass()), JPH::Vec3::sReplicate(1));
29 if (!box.Overlaps(bounds)) continue;
30 hits.push_back({body->owner->runtimeHandle(), shape->owner->runtimeHandle(), body->owner->getId(),
31 shape->owner->getId(), shape->owner->getTag(),
32 static_cast<int>(shape->material.userMaterialId & 0xffffffffu)});
33 }
34 }
35 std::sort(hits.begin(), hits.end(), [](const auto& a, const auto& b) {
36 return std::tie(a.bodyId, a.shapeId) < std::tie(b.bodyId, b.shapeId);
37 });
39 result.count = std::min(hits.size(), result.Capacity);
40 result.truncated = hits.size() > result.Capacity;
41 std::copy_n(hits.begin(), result.count, result.hits.begin());
43}
44eve::Result<int> WorldBackend::queryAABB(float x1, float y1, float z1, float x2, float y2, float z2) {
45 auto queried = queryAabbBroadPhaseOwned(
46 x1, y1, z1, x2, y2, z2,
47 {owner->queryCategoryBits_, owner->queryMaskBits_, owner->queryIgnoredBodyId_, owner->queryIgnoredShapeId_});
48 if (!queried) return eve::Result<int>::failure(queried.status());
49 // The legacy unbounded API must not silently report a truncated owning result.
50 if (queried.value().truncated)
51 return rejected<int>("AABB result exceeds the owning query capacity", eve::DiagnosticCode::Failed);
52 owner->queryBodyIds_.clear();
53 owner->queryShapes_.clear();
54 for (size_t i = 0; i < queried.value().count; ++i) {
55 const auto& hit = queried.value().hits[i];
56 owner->queryShapes_.emplace_back(hit.bodyId, hit.shapeId);
57 if (owner->queryBodyIds_.empty() || owner->queryBodyIds_.back() != hit.bodyId)
58 owner->queryBodyIds_.push_back(hit.bodyId);
59 }
60 return eve::Result<int>::success(static_cast<int>(owner->queryBodyIds_.size()));
61}
62} // namespace eve::physics::jolt
tensor::Graph g
Definition GpuGraph.cpp:7
ShaderImageInput shape
std::string local
Poco::JSON::Object::Ptr result
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
bool hit
std::string body
std::string filter
int count
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
eve::Result< int > queryAABB(float minX, float minY, float minZ, float maxX, float maxY, float maxZ) override
queryAABB; uses the class ownership, thread and failure contract.
std::shared_ptr< State > state
Definition JoltWorld.h:17
eve::Result< BroadPhaseAabb3D > queryAabbBroadPhaseOwned(float minX, float minY, float minZ, float maxX, float maxY, float maxZ, QueryFilter3D filter) override
queryAabbBroadPhaseOwned; uses the class ownership, thread and failure contract.
bool isValid() const noexcept override
Native world liveness.
Definition JoltWorld.cpp:90
bool isFinite(float x, float y, float z)
Fixed-capacity deterministic result of one AABB broad-phase query.
Query filter applied atomically for one owning query operation.
glm::vec4 bounds