载入中...
搜索中...
未找到
JoltWorldMover.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 "physics/World3D.h"
9namespace eve::physics::jolt {
11 const JPH::Vec3 up(x, y, z);
12 if (!isFinite(x, y, z) || !std::isfinite(up.LengthSq()) || up.LengthSq() < 1.e-12f)
13 return rejected<void>("Invalid mover up", eve::DiagnosticCode::InvalidArgument);
14 const auto unit = up.Normalized();
15 moverUp = {unit.GetX(), unit.GetY(), unit.GetZ()};
16 return success();
17}
18eve::Result<CapsuleMove3D> WorldBackend::moveCapsuleOwned(float ax, float ay, float az, float bx, float by, float bz,
19 float radius, float dx, float dy, float dz,
21 if (!isValid()) return rejected<CapsuleMove3D>("World expired", eve::DiagnosticCode::StaleHandle);
22 auto cooked = capsuleVolume(ax, ay, az, bx, by, bz, radius);
23 if (!cooked) return eve::Result<CapsuleMove3D>::failure(cooked.status());
24 JPH::Vec3 up(policy.upX, policy.upY, policy.upZ), desired(dx, dy, dz);
25 if (!isFinite(dx, dy, dz) || !std::isfinite(desired.LengthSq()) || !isFinite(policy.upX, policy.upY, policy.upZ) ||
26 !std::isfinite(up.LengthSq()) || up.LengthSq() < 1.e-12f || !std::isfinite(policy.maxSlopeRadians) ||
27 policy.maxSlopeRadians < 0 || policy.maxSlopeRadians >= JPH::JPH_PI * .5f)
28 return rejected<CapsuleMove3D>("Invalid mover policy or displacement", eve::DiagnosticCode::InvalidArgument);
29 up = up.Normalized();
30 auto volume = std::move(cooked).takeValue();
31 const auto start = volume.transform.GetTranslation();
32 const auto target = start + desired;
35 JPH::Vec3 bestNormal = JPH::Vec3::sZero();
36 float bestDot = 0.f, groundDot = -1.f;
37 auto record = [&](JPH::Vec3Arg normal) {
38 ++result.planeCount;
39 result.constrained = true;
40 groundDot = std::max(groundDot, normal.Dot(up));
41 if (normal.Dot(desired) < bestDot) {
42 bestDot = normal.Dot(desired);
43 bestNormal = normal;
44 }
45 };
46 // Query-only controller: no body is created, no impulses or callbacks are dispatched.
47 // Each pass resolves initial penetration, clips against nearby planes, then sweeps continuously.
48 for (int pass = 0; pass < 8; ++pass) {
49 result.iterations = pass + 1;
50 auto overlaps = overlapVolume(*state, volume, .002f);
51 JPH::Vec3 correction = JPH::Vec3::sZero();
52 auto remaining = JPH::Vec3(target - volume.transform.GetTranslation());
53 for (const auto& hit : overlaps.mHits) {
54 if (!isAcceptedHit(*state, hit.mBodyID2, hit.mSubShapeID2, filter)) continue;
55 const auto normal = -hit.mPenetrationAxis.NormalizedOr(JPH::Vec3::sAxisY());
57 const float needed = hit.mPenetrationDepth + .0001f - correction.Dot(normal);
58 if (needed > 0) correction += needed * normal;
59 const float upDot = normal.Dot(up);
60 if (upDot > 0.f && upDot < std::cos(policy.maxSlopeRadians)) {
61 const auto horizontal = (normal - upDot * up).NormalizedOr(JPH::Vec3::sZero());
62 const float uphill = remaining.Dot(horizontal);
63 if (uphill < 0.f) remaining -= uphill * horizontal;
64 }
65 const float inward = remaining.Dot(normal);
66 if (inward < 0) remaining -= inward * normal;
67 }
68 volume.transform.SetTranslation(volume.transform.GetTranslation() + correction);
69 if (remaining.LengthSq() < 1.e-10f) break;
70 auto hits = sweepVolume(*state, volume, remaining);
71 float fraction = 1.f;
72 JPH::Vec3 normal = JPH::Vec3::sZero();
73 for (const auto& hit : hits.mHits) {
74 if (!isAcceptedHit(*state, hit.mBodyID2, hit.mSubShapeID2, filter)) continue;
75 auto n = -hit.mPenetrationAxis.NormalizedOr(JPH::Vec3::sAxisY());
76 if (n.Dot(remaining) >= -1.e-7f) continue;
77 fraction = std::max(0.f, hit.mFraction - .0001f / remaining.Length());
78 normal = n;
79 record(n);
80 break;
81 }
82 volume.transform.SetTranslation(volume.transform.GetTranslation() + remaining * fraction);
83 if (fraction >= 1.f) break;
84 // The next overlap includes the swept surface and clips the remaining displacement along it.
85 if (fraction == 0 && correction.LengthSq() < 1.e-12f &&
86 std::abs(normal.Dot(remaining)) >= remaining.Length() - .0001f)
87 break;
88 }
89 // Classify ground at the final position too, including a purely tangential movement.
90 for (const auto& hit : overlapVolume(*state, volume, .002f).mHits)
91 if (isAcceptedHit(*state, hit.mBodyID2, hit.mSubShapeID2, filter))
92 groundDot = std::max(groundDot, -hit.mPenetrationAxis.NormalizedOr(JPH::Vec3::sAxisY()).Dot(up));
93 auto delta = JPH::Vec3(volume.transform.GetTranslation() - start);
94 result.deltaX = delta.GetX();
95 result.deltaY = delta.GetY();
96 result.deltaZ = delta.GetZ();
97 result.normalX = bestNormal.GetX();
98 result.normalY = bestNormal.GetY();
99 result.normalZ = bestNormal.GetZ();
100 result.grounded = groundDot >= std::cos(policy.maxSlopeRadians);
101 owner->moverGroundDot_ = groundDot;
103}
104eve::Result<bool> WorldBackend::moveCapsule(float ax, float ay, float az, float bx, float by, float bz, float radius,
105 float dx, float dy, float dz) {
106 auto moved = moveCapsuleOwned(
107 ax, ay, az, bx, by, bz, radius, dx, dy, dz,
108 {owner->queryCategoryBits_, owner->queryMaskBits_, owner->queryIgnoredBodyId_, owner->queryIgnoredShapeId_},
109 {moverUp.x, moverUp.y, moverUp.z, std::acos(owner->moverSlopeCos_)});
110 if (!moved) return eve::Result<bool>::failure(moved.status());
111 const auto& r = moved.value();
112 owner->moverDeltaX_ = r.deltaX;
113 owner->moverDeltaY_ = r.deltaY;
114 owner->moverDeltaZ_ = r.deltaZ;
115 owner->moverNormalX_ = r.normalX;
116 owner->moverNormalY_ = r.normalY;
117 owner->moverNormalZ_ = r.normalZ;
118 owner->moverPlaneCount_ = r.planeCount;
119 owner->moverIterations_ = r.iterations;
120 owner->moverGrounded_ = r.grounded;
121 return eve::Result<bool>::success(r.constrained);
122}
123} // namespace eve::physics::jolt
double z
double y
double x
LogicalId target
Duration start
double volume
ScriptComponentRecord record
Definition ECS.cpp:228
glm::vec3 n
Definition Grass.cpp:63
double r
HexVec3 up
Poco::JSON::Object::Ptr result
double radius
Definition MeleeHit.cpp:42
Texture * normal
glm::dvec3 dx
glm::dvec3 dz
bool hit
std::string filter
float dy
Definition Shadow.cpp:151
TacticalUnit * unit
float fraction
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
PhysicsWorldHandle runtimeHandle() const noexcept
Process-local identity used by PhysicsLink; invalid after destruction.
Definition World3D.h:136
eve::Result< bool > moveCapsule(float ax, float ay, float az, float bx, float by, float bz, float radius, float dx, float dy, float dz) override
moveCapsule; uses the class ownership, thread and failure contract.
eve::Result< CapsuleMove3D > moveCapsuleOwned(float ax, float ay, float az, float bx, float by, float bz, float radius, float dx, float dy, float dz, QueryFilter3D filter, CapsuleMovePolicy3D policy) override
moveCapsuleOwned; uses the class ownership, thread and failure contract.
std::shared_ptr< State > state
Definition JoltWorld.h:17
eve::Result< void > setMoverUp(float x, float y, float z) override
setMoverUp; uses the class ownership, thread and failure contract.
bool isValid() const noexcept override
Native world liveness.
Definition JoltWorld.cpp:90
eve::Result< void > success()
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.
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.
Owning result of continuous capsule movement.
PhysicsWorldHandle world
Per-call capsule mover classification policy; never mutates persistent World3D settings.
Query filter applied atomically for one owning query operation.