载入中...
搜索中...
未找到
JoltWorldQueries.cpp
浏览该文件的文档.
1// Jolt requires its platform header before any other native header.
2#include <Jolt/Jolt.h>
3
4// Provider implementation dependencies.
5#include <Jolt/Physics/Collision/CastResult.h>
6#include <Jolt/Physics/Collision/CollisionCollectorImpl.h>
7#include <Jolt/Physics/Collision/RayCast.h>
8#include <algorithm>
9#include "physics/World3D.h"
12namespace eve::physics::jolt {
13eve::Result<RayHit3D> WorldBackend::rayCastOwned(float x1, float y1, float z1, float x2, float y2, float z2,
15 if (!isValid()) return rejected<RayHit3D>("World expired", eve::DiagnosticCode::StaleHandle);
16 if (!isFinite(x1, y1, z1) || !isFinite(x2, y2, z2))
17 return rejected<RayHit3D>("Invalid ray", eve::DiagnosticCode::InvalidArgument);
20 JPH::RRayCast ray({x1, y1, z1}, {x2 - x1, y2 - y1, z2 - z1});
21 JPH::AllHitCollisionCollector<JPH::CastRayCollector> hits;
22 state->system.GetNarrowPhaseQuery().CastRay(ray, JPH::RayCastSettings(), hits);
23 hits.Sort();
24 for (const auto& hit : hits.mHits) {
25 auto i = state->bodies.find(hit.mBodyID.GetIndexAndSequenceNumber());
26 if (i == state->bodies.end() || !i->second->owner) continue;
27 const auto* native = state->system.GetBodyLockInterfaceNoLock().TryGetBody(hit.mBodyID);
28 if (!native) continue;
29 auto* shape = child(*native, hit.mSubShapeID2);
30 if (!shape || !shape->owner || shape->sensor) continue;
31 auto* body = i->second->owner;
32 if (body->getId() == filter.ignoredBodyId || shape->owner->getId() == filter.ignoredShapeId ||
33 !(shape->filter.categoryBits & filter.maskBits) || !(shape->filter.maskBits & filter.categoryBits))
34 continue;
35 auto point = ray.GetPointOnRay(hit.mFraction);
36 auto normal = native->GetWorldSpaceSurfaceNormal(hit.mSubShapeID2, point);
37 result.hit = true;
38 result.body = body->runtimeHandle();
39 result.shape = shape->owner->runtimeHandle();
40 result.bodyId = body->getId();
41 result.shapeId = shape->owner->getId();
42 result.shapeTag = shape->owner->getTag();
43 result.materialId =
44 static_cast<int>(contactMaterial(*native, hit.mSubShapeID2, *shape).userMaterialId & 0xffffffffu);
45 result.triangleIndex = triangleIndex(*native, hit.mSubShapeID2);
46 result.x = static_cast<float>(point.GetX());
47 result.y = static_cast<float>(point.GetY());
48 result.z = static_cast<float>(point.GetZ());
49 result.normalX = normal.GetX();
50 result.normalY = normal.GetY();
51 result.normalZ = normal.GetZ();
52 result.fraction = hit.mFraction;
53 break;
54 }
56}
57eve::Result<int> WorldBackend::rayCastFiltered(float x1, float y1, float z1, float x2, float y2, float z2,
58 uint64_t maskBits) {
59 if (!isValid()) return rejected<int>("World expired", eve::DiagnosticCode::StaleHandle);
60 if (!isFinite(x1, y1, z1) || !isFinite(x2, y2, z2))
61 return rejected<int>("Invalid ray", eve::DiagnosticCode::InvalidArgument);
62 JPH::RRayCast ray({x1, y1, z1}, {x2 - x1, y2 - y1, z2 - z1});
63 JPH::AllHitCollisionCollector<JPH::CastRayCollector> hits;
64 state->system.GetNarrowPhaseQuery().CastRay(ray, JPH::RayCastSettings(), hits);
65 hits.Sort();
66 RayHit3D r;
67 for (const auto& hit : hits.mHits) {
68 const auto* native = state->system.GetBodyLockInterfaceNoLock().TryGetBody(hit.mBodyID);
69 if (!native) continue;
70 const auto* shape = child(*native, hit.mSubShapeID2);
71 if (!shape || !shape->owner || shape->sensor || !(shape->filter.categoryBits & maskBits) ||
72 !(shape->filter.maskBits & owner->queryCategoryBits_) ||
73 shape->owner->getId() == owner->queryIgnoredShapeId_)
74 continue;
75 auto body = shape->body.lock();
76 if (!body || !body->owner || body->owner->getId() == owner->queryIgnoredBodyId_) continue;
77 auto point = ray.GetPointOnRay(hit.mFraction);
78 r = queryHit(*state, hit.mBodyID, hit.mSubShapeID2, point,
79 native->GetWorldSpaceSurfaceNormal(hit.mSubShapeID2, point), hit.mFraction);
80 break;
81 }
82 owner->rayHitBodyId_ = r.bodyId;
83 owner->rayHitShapeId_ = r.shapeId;
84 owner->rayHitShapeTag_ = r.shapeTag;
85 owner->rayHitMaterialId_ = r.materialId;
86 owner->rayHitTriangleIndex_ = r.triangleIndex;
87 owner->rayHitX_ = r.x;
88 owner->rayHitY_ = r.y;
89 owner->rayHitZ_ = r.z;
90 owner->rayHitNormalX_ = r.normalX;
91 owner->rayHitNormalY_ = r.normalY;
92 owner->rayHitNormalZ_ = r.normalZ;
93 owner->rayHitFraction_ = r.fraction;
94 return eve::Result<int>::success(r.bodyId);
95}
96eve::Result<int> WorldBackend::rayCastAll(float x1, float y1, float z1, float x2, float y2, float z2, int maxHits) {
97 if (!isValid()) return rejected<int>("World expired", eve::DiagnosticCode::StaleHandle);
98 if (!isFinite(x1, y1, z1) || !isFinite(x2, y2, z2) || maxHits < 1 || maxHits > 4096)
99 return rejected<int>("Invalid ray", eve::DiagnosticCode::InvalidArgument);
100 const JPH::RRayCast ray({x1, y1, z1}, {x2 - x1, y2 - y1, z2 - z1});
101 JPH::AllHitCollisionCollector<JPH::CastRayCollector> hits;
102 state->system.GetNarrowPhaseQuery().CastRay(ray, JPH::RayCastSettings(), hits);
103 const QueryFilter3D filter{owner->queryCategoryBits_, owner->queryMaskBits_, owner->queryIgnoredBodyId_,
104 owner->queryIgnoredShapeId_};
105 std::vector<World3D::RayResult> values;
106 for (const auto& hit : hits.mHits) {
107 if (!isAcceptedHit(*state, hit.mBodyID, hit.mSubShapeID2, filter)) continue;
108 const auto& native = *state->system.GetBodyLockInterfaceNoLock().TryGetBody(hit.mBodyID);
109 const auto point = ray.GetPointOnRay(hit.mFraction);
110 auto r = queryHit(*state, hit.mBodyID, hit.mSubShapeID2, point,
111 native.GetWorldSpaceSurfaceNormal(hit.mSubShapeID2, point), hit.mFraction);
112 values.push_back({r.bodyId, r.shapeId, r.shapeTag, r.materialId, r.triangleIndex, r.x, r.y, r.z, r.normalX,
113 r.normalY, r.normalZ, r.fraction});
114 }
115 std::sort(values.begin(), values.end(), [](const auto& a, const auto& b) {
116 return std::tie(a.fraction, a.shapeId, a.triangleIndex) < std::tie(b.fraction, b.shapeId, b.triangleIndex);
117 });
118 if (values.size() > size_t(maxHits)) values.resize(size_t(maxHits));
119 const auto r = values.empty() ? World3D::RayResult{} : values.front();
120 owner->rayResults_ = std::move(values);
121 owner->rayHitBodyId_ = r.bodyId;
122 owner->rayHitShapeId_ = r.shapeId;
123 owner->rayHitShapeTag_ = r.shapeTag;
124 owner->rayHitMaterialId_ = r.materialId;
125 owner->rayHitTriangleIndex_ = r.triangleIndex;
126 owner->rayHitX_ = r.x;
127 owner->rayHitY_ = r.y;
128 owner->rayHitZ_ = r.z;
129 owner->rayHitNormalX_ = r.normalX;
130 owner->rayHitNormalY_ = r.normalY;
131 owner->rayHitNormalZ_ = r.normalZ;
132 owner->rayHitFraction_ = r.fraction;
133 return eve::Result<int>::success(static_cast<int>(owner->rayResults_.size()));
134}
135} // namespace eve::physics::jolt
ShaderImageInput shape
double r
Poco::JSON::Object::Ptr result
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
Texture * normal
bool hit
std::set< std::string > values
std::string body
std::string filter
glm::vec3 point
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
PhysicsWorldHandle runtimeHandle() const noexcept
Process-local identity used by PhysicsLink; invalid after destruction.
Definition World3D.h:136
eve::Result< int > rayCastFiltered(float x1, float y1, float z1, float x2, float y2, float z2, uint64_t maskBits) override
rayCastFiltered; uses the class ownership, thread and failure contract.
std::shared_ptr< State > state
Definition JoltWorld.h:17
eve::Result< RayHit3D > rayCastOwned(float x1, float y1, float z1, float x2, float y2, float z2, QueryFilter3D filter) override
rayCastOwned; uses the class ownership, thread and failure contract.
bool isValid() const noexcept override
Native world liveness.
Definition JoltWorld.cpp:90
eve::Result< int > rayCastAll(float x1, float y1, float z1, float x2, float y2, float z2, int maxHits) override
rayCastAll; uses the class ownership, thread and failure contract.
int triangleIndex(const JPH::Body &, const JPH::SubShapeID &)
Definition JoltMesh.cpp:89
bool isAcceptedHit(const State &state, JPH::BodyID id, const JPH::SubShapeID &sub, QueryFilter3D filter)
Filters a synchronous native hit, borrowing state only for the call.
SurfaceMaterial3D contactMaterial(const JPH::Body &, const JPH::SubShapeID &, const ShapeState &)
Definition JoltMesh.cpp:96
ShapeState * child(const JPH::Body &body, const JPH::SubShapeID &id)
Resolves a native child record for a synchronous callback or query. @ownership Borrowed from the worl...
RayHit3D queryHit(const State &state, JPH::BodyID id, const JPH::SubShapeID &sub, JPH::RVec3Arg point, JPH::Vec3Arg normal, float fraction)
Projects a validated native hit into an owning result on the idle simulation thread.
bool isFinite(float x, float y, float z)
Query filter applied atomically for one owning query operation.
Owning snapshot of the closest ray intersection.
PhysicsWorldHandle world