载入中...
搜索中...
未找到
JoltSolvedContacts.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 <algorithm>
7#include <tuple>
8#include "physics/World3D.h"
11
12namespace eve::physics::jolt {
13void Listener::OnContactSolved(const JPH::Body& a, const JPH::SubShapeID& subA, const JPH::Body& b,
14 const JPH::SubShapeID& subB, const JPH::ContactSolveResult& result) {
15 auto* sa = child(a, subA);
16 auto* sb = child(b, subB);
17 if (!sa || !sb || !sa->owner || !sb->owner || sa->sensor || sb->sensor) return;
18 auto ba = sa->body.lock(), bb = sb->body.lock();
19 if (!ba || !bb || !ba->owner || !bb->owner) return;
20 const bool flip = sa->owner->getId() > sb->owner->getId();
21 const JPH::SubShapeIDPair key = flip ? JPH::SubShapeIDPair(b.GetID(), subB, a.GetID(), subA)
22 : JPH::SubShapeIDPair(a.GetID(), subA, b.GetID(), subB);
23 auto* first = flip ? sb->owner : sa->owner;
24 auto* second = flip ? sa->owner : sb->owner;
25 auto* bodyA = flip ? bb->owner : ba->owner;
26 auto* bodyB = flip ? ba->owner : bb->owner;
27 const auto normal = flip ? -result.mNormal : result.mNormal;
28 const auto posA = flip ? result.mPosition2 : result.mPosition1;
29 const auto posB = flip ? result.mPosition1 : result.mPosition2;
30 auto& points = solved[key];
31 // CCD solves a single averaged manifold point; use a separate feature from discrete points.
32 const uint32_t index = result.mCCD ? JPH::ContactConstraintManager::MaxContactPoints : result.mPointIndex;
33 if (points.size() <= index) points.resize(size_t(index) + 1);
34 auto& point = points[index];
35 const float total = point.pointA.totalNormalImpulse + result.mNormalImpulse;
36 const uint32_t feature = index;
37 point.bodyA = bodyA->runtimeHandle();
38 point.bodyB = bodyB->runtimeHandle();
39 point.shapeA = first->runtimeHandle();
40 point.shapeB = second->runtimeHandle();
41 point.substep = substep;
42 point.pointA = {first->getId(), first->getTag(), bodyB->getId(),
43 second->getId(), second->getTag(), feature,
44 float(posA.GetX()), float(posA.GetY()), float(posA.GetZ()),
45 normal.GetX(), normal.GetY(), normal.GetZ(),
46 result.mSeparation, result.mNormalImpulse, total,
47 result.mNormalVelocity, result.mPersisted};
48 point.pointB = {second->getId(), second->getTag(), bodyA->getId(),
49 first->getId(), first->getTag(), feature,
50 float(posB.GetX()), float(posB.GetY()), float(posB.GetZ()),
51 -normal.GetX(), -normal.GetY(), -normal.GetZ(),
52 result.mSeparation, result.mNormalImpulse, total,
53 result.mNormalVelocity, result.mPersisted};
54 const uint64_t hitKey = (uint64_t(first->getId()) << 32) | uint32_t(second->getId());
55 const float pairTotal = (normalTotals[hitKey] += result.mNormalImpulse);
56 const float speed = -result.mNormalVelocity;
57 auto hit = hits.find(hitKey);
58 if ((first->areHitEventsEnabled() || second->areHitEventsEnabled()) && result.mNormalImpulse > 0 &&
59 speed > state->hitThreshold && (hit == hits.end() || speed > hit->second.speed)) {
60 const auto position = .5 * (posA + posB);
62 point.shapeB,
63 {bodyA->getId(), first->getId(), first->getTag(), bodyB->getId(), second->getId(),
64 second->getTag(), false, false},
65 float(position.GetX()),
66 float(position.GetY()),
67 float(position.GetZ()),
68 normal.GetX(),
69 normal.GetY(),
70 normal.GetZ(),
71 speed,
72 pairTotal};
73 hit = hits.insert_or_assign(hitKey, value).first;
74 }
75 if (hit != hits.end()) hit->second.impulse = pairTotal;
76}
77
79 if (!isValid()) return rejected<BodyContacts3D>("World expired", eve::DiagnosticCode::StaleHandle);
80 if (state->faulted)
81 return rejected<BodyContacts3D>("No completed contact snapshot after solver failure",
83 if (maxPoints < 1 || maxPoints > 4096)
84 return rejected<BodyContacts3D>("maxPoints must be in [1,4096]", eve::DiagnosticCode::InvalidArgument);
85 if (!owner->findBody(body)) return rejected<BodyContacts3D>("Body expired", eve::DiagnosticCode::StaleHandle);
89 for (const auto& [key, points] : state->listener->solved)
90 for (const auto& point : points) {
91 if (!point.substep || point.substep != state->listener->substep || !owner->findShape(point.shapeA) ||
92 !owner->findShape(point.shapeB) || !owner->findShape(point.shapeA)->getBody()->isActive() ||
93 !owner->findShape(point.shapeB)->getBody()->isActive())
94 continue;
95 if (point.bodyA == body)
96 result.points.push_back(point.pointA);
97 else if (point.bodyB == body)
98 result.points.push_back(point.pointB);
99 }
100 std::sort(result.points.begin(), result.points.end(), [](const auto& a, const auto& b) {
101 return std::tie(a.shapeId, a.otherShapeId, a.featureId, a.x, a.y, a.z) <
102 std::tie(b.shapeId, b.otherShapeId, b.featureId, b.x, b.y, b.z);
103 });
104 if (result.points.size() > size_t(maxPoints)) {
105 result.points.resize(size_t(maxPoints));
107 }
109}
110} // namespace eve::physics::jolt
SQObjectPtr key
Definition ECS.cpp:155
std::int32_t second
std::int32_t first
std::array< float, 3 > position
Poco::JSON::Object::Ptr result
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
Texture * normal
std::shared_ptr< const std::vector< glm::vec2 > > points
bool hit
std::string body
Object::Ptr value
uint32_t index
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
bool isActive() const
True when active.
Definition Body3D.cpp:412
Body3D * getBody()
Returns the owning body, or null after invalidation.
Definition Shape3D.h:286
Shape3D * findShape(PhysicsShapeHandle handle) const
Resolves a live shape handle; returns null when stale or foreign.
Definition World3D.cpp:447
Body3D * findBody(PhysicsBodyHandle handle) const
Borrows a live body; null for stale/foreign handles. Never delete lookup results.
Definition World3D.cpp:431
std::shared_ptr< State > state
Definition JoltWorld.h:17
eve::Result< BodyContacts3D > queryBodyContactsOwned(PhysicsBodyHandle body, int maxPoints) const override
queryBodyContactsOwned; uses the class ownership, thread and failure contract.
bool isValid() const noexcept override
Native world liveness.
Definition JoltWorld.cpp:90
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...
Snapshot of current contacts; values survive later queries and world destruction.
std::unordered_map< JPH::SubShapeIDPair, std::vector< SolvedPoint > > solved
std::unordered_map< uint64_t, SolvedHit > hits
void OnContactSolved(const JPH::Body &, const JPH::SubShapeID &, const JPH::Body &, const JPH::SubShapeID &, const JPH::ContactSolveResult &) override
std::unordered_map< uint64_t, float > normalTotals
PhysicsShapeHandle shapeA