载入中...
搜索中...
未找到
JoltGeometryQueries.cpp
浏览该文件的文档.
1// eve-hot-path
2// Jolt requires its platform header before any other native header.
3#include <Jolt/Jolt.h>
4
5// Provider implementation dependencies.
6#include <Jolt/Geometry/GJKClosestPoint.h>
7#include <Jolt/Physics/Collision/Shape/ConvexShape.h>
8#include "physics/World3D.h"
11namespace eve::physics::jolt {
12JPH::RVec3 closestConvexPoint(const ShapeState& shape, JPH::RVec3Arg target) {
13 const auto body = shape.body.lock();
14 const auto& g = shape.geometry;
15 const auto pose = body->api().GetWorldTransform(body->id) *
16 JPH::RMat44::sRotationTranslation(JPH::Quat(g.localQx, g.localQy, g.localQz, g.localQw),
17 JPH::RVec3(g.localX, g.localY, g.localZ));
18 const auto center = shape.native->GetCenterOfMass();
19 const auto local = JPH::Vec3(pose.InversedRotationTranslation() * target) - center;
20 struct Point {
21 JPH::Vec3 value;
22 JPH::Vec3 GetSupport(JPH::Vec3Arg) const { return value; }
23 } point{local};
24 JPH::ConvexShape::SupportBuffer buffer;
25 const auto* support =
26 static_cast<const JPH::ConvexShape*>(shape.native.GetPtr())
27 ->GetSupportFunction(JPH::ConvexShape::ESupportMode::IncludeConvexRadius, buffer, JPH::Vec3::sReplicate(1));
28 JPH::GJKClosestPoint gjk;
29 JPH::Vec3 direction = local.NormalizedOr(JPH::Vec3::sAxisX()), a, b;
30 const float distance = gjk.GetClosestPoints(*support, point, 1.e-6f, FLT_MAX, direction, a, b);
31 if (distance <= 1.e-12f) return target;
32 return pose * (a + center);
33}
34eve::Result<int> WorldBackend::closestPoint(float x, float y, float z, float maxDistance) {
35 if (!isValid()) return rejected<int>("World expired", eve::DiagnosticCode::StaleHandle);
36 if (!isFinite(x, y, z) || !std::isfinite(maxDistance) || maxDistance < 0 ||
37 double(maxDistance) * maxDistance > FLT_MAX)
38 return rejected<int>("Invalid closest point query", eve::DiagnosticCode::InvalidArgument);
39 owner->closestBodyId_ = owner->closestShapeId_ = -1;
40 owner->closestShapeTag_ = 0;
41 owner->closestX_ = owner->closestY_ = owner->closestZ_ = 0;
42 owner->closestNormalX_ = owner->closestNormalY_ = owner->closestNormalZ_ = owner->closestDistance_ = 0;
43 const JPH::RVec3 target(x, y, z);
44 double best = double(maxDistance) * maxDistance;
45 for (const auto& [id, body] : state->bodies) {
46 if (!body->active || !body->owner || body->owner->getId() == owner->queryIgnoredBodyId_) continue;
47 for (const auto& shape : body->shapes) {
48 if (!shape->owner || shape->sensor || shape->native->GetType() != JPH::EShapeType::Convex ||
49 shape->owner->getId() == owner->queryIgnoredShapeId_ ||
50 !(shape->filter.categoryBits & owner->queryMaskBits_) ||
51 !(shape->filter.maskBits & owner->queryCategoryBits_))
52 continue;
53 const auto point = closestConvexPoint(*shape, target);
54 const auto distance = (point - target).LengthSq();
55 const int bid = body->owner->getId(), sid = shape->owner->getId();
56 if (distance > best + 1.e-7 ||
57 (std::abs(distance - best) <= 1.e-7 && owner->closestBodyId_ >= 0 &&
58 (bid > owner->closestBodyId_ || (bid == owner->closestBodyId_ && sid >= owner->closestShapeId_))))
59 continue;
60 best = distance;
61 owner->closestBodyId_ = bid;
62 owner->closestShapeId_ = sid;
63 owner->closestShapeTag_ = shape->owner->getTag();
64 owner->closestX_ = float(point.GetX());
65 owner->closestY_ = float(point.GetY());
66 owner->closestZ_ = float(point.GetZ());
67 owner->closestDistance_ = float(std::sqrt(distance));
68 const auto normal = JPH::Vec3(target - point).NormalizedOr(JPH::Vec3::sZero());
69 owner->closestNormalX_ = normal.GetX();
70 owner->closestNormalY_ = normal.GetY();
71 owner->closestNormalZ_ = normal.GetZ();
72 }
73 }
74 return eve::Result<int>::success(owner->closestBodyId_);
75}
77 uint64_t mask) const {
78 if (!isValid()) return rejected<ClothContact3D>("World expired", eve::DiagnosticCode::StaleHandle);
79 if (!isFinite(x, y, z) || !std::isfinite(radius) || radius < 0)
80 return rejected<ClothContact3D>("Invalid point probe", eve::DiagnosticCode::InvalidArgument);
82 for (const auto& [id, body] : state->bodies) {
83 if (!body->owner) continue;
84 for (const auto& shape : body->shapes) {
85 if (shape->sensor || shape->native->GetType() != JPH::EShapeType::Convex ||
86 !(shape->filter.categoryBits & mask) || !(shape->filter.maskBits & category))
87 continue;
88 const auto delta = JPH::Vec3(JPH::RVec3(x, y, z) - closestConvexPoint(*shape, {x, y, z}));
89 const float depth = radius - delta.Length();
90 if (depth <= result.depth) continue;
91 const auto normal = delta.NormalizedOr(JPH::Vec3::sAxisY());
92 result.hit = true;
93 result.depth = depth;
94 result.nx = normal.GetX();
95 result.ny = normal.GetY();
96 result.nz = normal.GetZ();
97 result.body = body->owner;
98 result.friction = shape->material.friction;
99 result.restitution = shape->material.restitution;
100 }
101 }
103}
104} // namespace eve::physics::jolt
double z
double y
double x
LogicalId target
eve::EntitySpatialPose pose
int mask
tensor::Graph g
Definition GpuGraph.cpp:7
ShaderImageInput shape
std::string local
Poco::JSON::Object::Ptr result
MeleePoint3 b
Definition MeleeHit.cpp:41
double radius
Definition MeleeHit.cpp:42
MeleePoint3 a
Definition MeleeHit.cpp:40
float distance
Texture * normal
glm::dvec2 center
std::unique_ptr< gpgpu::GpuBuffer > buffer
Definition OnnxGpgpu.cpp:26
std::string body
Object::Ptr value
RoadLaneDirection direction
std::map< Cell, int > best
const char * category
std::uint32_t depth
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
std::shared_ptr< State > state
Definition JoltWorld.h:17
eve::Result< ClothContact3D > pointProbeFiltered(float x, float y, float z, float radius, uint64_t categoryBits, uint64_t maskBits) const override
pointProbeFiltered; uses the class ownership, thread and failure contract.
bool isValid() const noexcept override
Native world liveness.
Definition JoltWorld.cpp:90
eve::Result< int > closestPoint(float x, float y, float z, float maxDistance) override
closestPoint; uses the class ownership, thread and failure contract.
bool isFinite(float x, float y, float z)
JPH::RVec3 closestConvexPoint(const ShapeState &shape, JPH::RVec3Arg target)
Returns a convex leaf's world closest point; shape/body are borrowed only during this idle-thread cal...
Result of a 3D point probe: deepest non-sensor shape within radius. Normal points from the shape towa...