载入中...
搜索中...
未找到
JoltQueryInternal.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/Shape/CapsuleShape.h>
6#include <Jolt/Physics/Collision/Shape/SphereShape.h>
8namespace eve::physics::jolt {
9eve::Result<QueryVolume> capsuleVolume(float ax, float ay, float az, float bx, float by, float bz, float radius) {
10 if (!isFinite(ax, ay, az) || !isFinite(bx, by, bz) || !std::isfinite(radius) || radius <= 0)
11 return rejected<QueryVolume>("Invalid capsule", eve::DiagnosticCode::InvalidArgument);
12 const JPH::Vec3 axis(bx - ax, by - ay, bz - az);
13 const float length = axis.Length();
14 if (!std::isfinite(length)) return rejected<QueryVolume>("Capsule overflow", eve::DiagnosticCode::InvalidArgument);
16 result.shape = length < 1.e-6f ? JPH::RefConst<JPH::Shape>(new JPH::SphereShape(radius))
17 : JPH::RefConst<JPH::Shape>(new JPH::CapsuleShape(length * .5f, radius));
18 auto rotation = length < 1.e-6f ? JPH::Quat::sIdentity() : JPH::Quat::sFromTo(JPH::Vec3::sAxisY(), axis / length);
19 result.transform =
20 JPH::RMat44::sRotationTranslation(rotation, (JPH::RVec3(ax, ay, az) + JPH::RVec3(bx, by, bz)) * .5);
22}
23bool isAcceptedHit(const State& state, JPH::BodyID id, const JPH::SubShapeID& sub, QueryFilter3D filter) {
24 const auto* native = state.system.GetBodyLockInterfaceNoLock().TryGetBody(id);
25 if (!native) return false;
26 const auto* shape = child(*native, sub);
27 if (!shape || !shape->owner || shape->sensor) return false;
28 auto body = shape->body.lock();
29 return body && body->owner && body->owner->getId() != filter.ignoredBodyId &&
30 shape->owner->getId() != filter.ignoredShapeId && (shape->filter.categoryBits & filter.maskBits) &&
31 (shape->filter.maskBits & filter.categoryBits);
32}
33RayHit3D queryHit(const State& state, JPH::BodyID id, const JPH::SubShapeID& sub, JPH::RVec3Arg point,
34 JPH::Vec3Arg normal, float fraction) {
35 const auto& native = *state.system.GetBodyLockInterfaceNoLock().TryGetBody(id);
36 const auto& shape = *child(native, sub);
37 const auto& body = *shape.body.lock()->owner;
39 result.hit = true;
40 result.body = body.runtimeHandle();
41 result.shape = shape.owner->runtimeHandle();
42 result.bodyId = body.getId();
43 result.shapeId = shape.owner->getId();
44 result.shapeTag = shape.owner->getTag();
45 result.materialId = static_cast<int>(contactMaterial(native, sub, shape).userMaterialId & 0xffffffffu);
46 result.triangleIndex = triangleIndex(native, sub);
47 result.x = float(point.GetX());
48 result.y = float(point.GetY());
49 result.z = float(point.GetZ());
50 result.normalX = normal.GetX();
51 result.normalY = normal.GetY();
52 result.normalZ = normal.GetZ();
53 result.fraction = fraction;
54 return result;
55}
56JPH::AllHitCollisionCollector<JPH::CollideShapeCollector> overlapVolume(const State& state, const QueryVolume& volume,
57 float separation) {
58 JPH::AllHitCollisionCollector<JPH::CollideShapeCollector> hits;
59 JPH::CollideShapeSettings settings;
60 settings.mMaxSeparationDistance = separation;
61 state.system.GetNarrowPhaseQuery().CollideShape(volume.shape, JPH::Vec3::sReplicate(1), volume.transform, settings,
62 volume.transform.GetTranslation(), hits);
63 hits.Sort();
64 return hits;
65}
66JPH::AllHitCollisionCollector<JPH::CastShapeCollector> sweepVolume(const State& state, const QueryVolume& volume,
67 JPH::Vec3Arg delta) {
68 JPH::AllHitCollisionCollector<JPH::CastShapeCollector> hits;
69 JPH::ShapeCastSettings settings;
70 settings.mReturnDeepestPoint = true;
71 state.system.GetNarrowPhaseQuery().CastShape(
72 JPH::RShapeCast(volume.shape, JPH::Vec3::sReplicate(1), volume.transform, delta), settings,
73 volume.transform.GetTranslation(), hits);
74 hits.Sort();
75 return hits;
76}
77} // namespace eve::physics::jolt
double volume
ShaderImageInput shape
float length
JointPoint2D axis
std::array< float, 4 > rotation
Poco::JSON::Object::Ptr result
double radius
Definition MeleeHit.cpp:42
Texture * normal
OceanWakeSettings settings
std::string body
std::string filter
float separation
Definition TreeMesh.cpp:161
float fraction
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
int triangleIndex(const JPH::Body &, const JPH::SubShapeID &)
Definition JoltMesh.cpp:89
JPH::AllHitCollisionCollector< JPH::CollideShapeCollector > overlapVolume(const State &state, const QueryVolume &volume, float separation)
Collects owning native records for this synchronous query; no pointers escape.
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)
eve::Result< QueryVolume > capsuleVolume(float ax, float ay, float az, float bx, float by, float bz, float radius)
Cooks an ephemeral query capsule; inputs are endpoints in world space.
JPH::AllHitCollisionCollector< JPH::CastShapeCollector > sweepVolume(const State &state, const QueryVolume &volume, JPH::Vec3Arg delta)
Sweeps one ephemeral convex volume; results are local to its starting position.
Query filter applied atomically for one owning query operation.
Owning snapshot of the closest ray intersection.
JPH::RefConst< JPH::Shape > shape