128 if (!std::isfinite(
x) || !std::isfinite(
y) || !std::isfinite(
z))
129 throw eve::Exception(
"DistanceField3D.sample: coordinates must be finite");
130 const float gx = (
x - originX_) / cellSize_;
131 const float gy = (
y - originY_) / cellSize_;
132 const float gz = (
z - originZ_) / cellSize_;
133 if (gx < 0.f || gy < 0.f || gz < 0.f || gx > width_ - 1.f || gy > height_ - 1.f ||
135 return outsideDistance_;
137 const int x0 = std::min(
static_cast<int>(std::floor(gx)), width_ - 2);
138 const int y0 = std::min(
static_cast<int>(std::floor(gy)), height_ - 2);
139 const int z0 = std::min(
static_cast<int>(std::floor(gz)), depth_ - 2);
140 const float tx = std::clamp(gx - x0, 0.f, 1.f);
141 const float ty = std::clamp(gy - y0, 0.f, 1.f);
142 const float tz = std::clamp(gz - z0, 0.f, 1.f);
143 auto mix = [](
float a,
float b,
float t) {
return a + (
b -
a) *
t; };
147 const float c11 = mix(
getDistance(x0, y0 + 1, z0 + 1),
149 return mix(mix(c00, c10, ty), mix(c01, c11, ty), tz);
315 float radius,
float deltaX,
float deltaY,
float deltaZ) {
316 if (!std::isfinite(ax) || !std::isfinite(ay) || !std::isfinite(az) ||
317 !std::isfinite(bx) || !std::isfinite(by) || !std::isfinite(bz) ||
318 !(
radius >= 0.f) || !std::isfinite(
radius) || !std::isfinite(deltaX) ||
319 !std::isfinite(deltaY) || !std::isfinite(deltaZ))
321 "DistanceField3D.castCapsule: coordinates, radius, and delta must be finite; radius >= 0");
322 const float travel = std::hypot(deltaX, deltaY, deltaZ);
323 float hitX, hitY, hitZ;
324 const float initial =
325 capsuleClearance(ax, ay, az, bx, by, bz,
radius, hitX, hitY, hitZ);
326 if (initial <= 0.f) {
327 setCastResult(0.f, travel, hitX, hitY, hitZ, initial,
radius,
true);
331 castDistance_ = travel;
332 castStartedInside_ =
false;
333 if (travel <= 1e-7f)
return false;
335 const int steps = queryIntervals(travel, cellSize_ * 0.25,
"DistanceField3D.castCapsule");
337 for (
int i = 1; i <=
steps; ++i) {
338 const float fraction =
static_cast<float>(i) /
static_cast<float>(
steps);
339 float closestX, closestY, closestZ;
340 const float clearance = capsuleClearance(
343 closestX, closestY, closestZ);
344 if (clearance > 0.f) {
349 for (
int iteration = 0; iteration < 14; ++iteration) {
350 const float middle = 0.5f * (low + high);
351 float ignoredX, ignoredY, ignoredZ;
352 if (capsuleClearance(
353 ax + deltaX * middle, ay + deltaY * middle, az + deltaZ * middle,
354 bx + deltaX * middle, by + deltaY * middle, bz + deltaZ * middle,
radius,
355 ignoredX, ignoredY, ignoredZ) <= 0.f)
360 const float finalClearance = capsuleClearance(
361 ax + deltaX * high, ay + deltaY * high, az + deltaZ * high,
362 bx + deltaX * high, by + deltaY * high, bz + deltaZ * high,
radius, hitX, hitY,
364 setCastResult(high, travel, hitX, hitY, hitZ, finalClearance,
radius,
false);
409 float radius,
float deltaX,
float deltaY,
float deltaZ) {
412 if (!std::isfinite(deltaX) || !std::isfinite(deltaY) || !std::isfinite(deltaZ))
413 throw eve::Exception(
"DistanceField3D.moveCapsule: delta must be finite");
418 auto dot = [](Vec3
a, Vec3
b) {
return a.
x *
b.x +
a.y *
b.y +
a.z *
b.z; };
421 constexpr int maxIterations = 6;
422 constexpr int maxPlanes = 4;
425 const float skin = std::max(moverSkinWidth_, cellSize_ * 1e-6f);
427 Vec3 remaining{deltaX, deltaY, deltaZ};
430 bool constrained =
false;
431 moverIterations_ = 0;
435 moverGroundDot_ = -1.f;
436 moverGrounded_ =
false;
437 moverHitWall_ =
false;
438 auto recordNormal = [&](Vec3
normal) {
444 moverGroundDot_ = std::max(moverGroundDot_, upDot);
445 if (upDot < moverSlopeCos_) moverHitWall_ =
true;
451 auto contactNormal = [&](Vec3
offset, Vec3 travel) {
452 const Vec3
previous{normalX_, normalY_, normalZ_};
455 const float closest = collisionDistance_;
456 const float axisLength = std::hypot(bx - ax, by - ay, bz - az);
457 const int intervals = queryIntervals(axisLength, cellSize_ * 0.5,
"DistanceField3D.moveCapsule");
458 for (
int i = 0; i <= intervals; ++i) {
459 const float t =
static_cast<float>(i) /
static_cast<float>(intervals);
461 az +
offset.z + (bz - az) *
t};
464 const Vec3 candidate{normalX_, normalY_, normalZ_};
465 const float candidateDot =
dot(candidate, travel);
466 if (candidateDot < opposed) {
467 opposed = candidateDot;
477 for (
int iteration = 0; iteration < maxIterations; ++iteration) {
478 moverIterations_ = iteration + 1;
484 if (
checkCapsule(currentAx, currentAy, currentAz, currentBx, currentBy, currentBz,
486 const float recovery = -collisionDistance_ + skin;
493 if (planeCount < maxPlanes)
planes[planeCount++] =
normal;
497 const float travel =
length(remaining);
498 if (travel <= 1e-6f)
break;
499 if (!
castCapsule(currentAx, currentAy, currentAz, currentBx, currentBy, currentBz,
radius,
500 remaining.x, remaining.y, remaining.z)) {
509 const Vec3 impactOffset{
position.
x + remaining.x * castFraction_,
position.y + remaining.y * castFraction_,
510 position.z + remaining.z * castFraction_};
511 const Vec3
normal = contactNormal(impactOffset, remaining);
513 if (planeCount < maxPlanes)
planes[planeCount++] =
normal;
515 const float safeFraction = std::max(0.f, castFraction_ - skin / travel);
516 position.x += remaining.x * safeFraction;
517 position.y += remaining.y * safeFraction;
518 position.z += remaining.z * safeFraction;
519 remaining.x *= 1.f - safeFraction;
520 remaining.y *= 1.f - safeFraction;
521 remaining.z *= 1.f - safeFraction;
525 for (
int pass = 0; pass < planeCount; ++pass) {
526 for (
int plane = 0; plane < planeCount; ++plane) {
527 const float into =
dot(remaining,
planes[plane]);
529 remaining.x -=
planes[plane].x * into;
530 remaining.y -=
planes[plane].y * into;
531 remaining.z -=
planes[plane].z * into;
537 moverGrounded_ = moverGroundDot_ >= moverSlopeCos_;
538 if (!moverGrounded_ && moverGroundSnap_ > 0.f) {
539 const Vec3 snap{-moverUpX_ * moverGroundSnap_, -moverUpY_ * moverGroundSnap_,
540 -moverUpZ_ * moverGroundSnap_};
543 !castStartedInside_) {
544 const Vec3 snapNormal{normalX_, normalY_, normalZ_};
545 const float groundDot = snapNormal.x * moverUpX_ + snapNormal.y * moverUpY_ +
546 snapNormal.z * moverUpZ_;
547 if (groundDot >= moverSlopeCos_) {
548 const float safeFraction =
549 std::max(0.f, castFraction_ - skin / moverGroundSnap_);
550 position.x += snap.x * safeFraction;
551 position.y += snap.y * safeFraction;
552 position.z += snap.z * safeFraction;
553 recordNormal(snapNormal);
554 moverGrounded_ =
true;
564 const Vec3 requested{deltaX, deltaY, deltaZ};
565 const float requestedUp =
566 requested.
x * moverUpX_ + requested.y * moverUpY_ + requested.z * moverUpZ_;
567 const Vec3
lateral{requested.
x - moverUpX_ * requestedUp,
568 requested.y - moverUpY_ * requestedUp,
569 requested.z - moverUpZ_ * requestedUp};
571 if (!moverStepping_ && moverStepHeight_ > 0.f && moverHitWall_ && lateralLength > 1e-6f) {
575 bool grounded, hitWall;
577 const MoverState direct{moverDeltaX_, moverDeltaY_, moverDeltaZ_,
578 moverNormalX_, moverNormalY_, moverNormalZ_,
579 moverGroundDot_, moverIterations_, moverGrounded_,
581 auto restore = [&](
const MoverState &
state) {
582 moverDeltaX_ =
state.dx;
583 moverDeltaY_ =
state.dy;
584 moverDeltaZ_ =
state.dz;
585 moverNormalX_ =
state.nx;
586 moverNormalY_ =
state.ny;
587 moverNormalZ_ =
state.nz;
588 moverGroundDot_ =
state.groundDot;
589 moverIterations_ =
state.iterations;
590 moverGrounded_ =
state.grounded;
591 moverHitWall_ =
state.hitWall;
593 const Vec3
up{moverUpX_ * moverStepHeight_, moverUpY_ * moverStepHeight_,
594 moverUpZ_ * moverStepHeight_};
598 moverStepping_ =
true;
601 bz +
up.z,
radius, deltaX, deltaY, deltaZ);
603 moverStepping_ =
false;
606 moverStepping_ =
false;
608 Vec3 candidate{
up.
x + moverDeltaX_,
up.y + moverDeltaY_,
up.z + moverDeltaZ_};
609 const int candidateIterations = moverIterations_;
610 const bool candidateHitWall = moverHitWall_;
611 const float downDistance = moverStepHeight_ + moverGroundSnap_ + skin;
612 const Vec3 down{-moverUpX_ * downDistance, -moverUpY_ * downDistance,
613 -moverUpZ_ * downDistance};
614 if (
castCapsule(ax + candidate.x, ay + candidate.y, az + candidate.z,
615 bx + candidate.x, by + candidate.y, bz + candidate.z,
radius, down.x,
617 !castStartedInside_) {
618 const Vec3 landingNormal{normalX_, normalY_, normalZ_};
619 const float landingDot = landingNormal.x * moverUpX_ +
620 landingNormal.y * moverUpY_ +
621 landingNormal.z * moverUpZ_;
622 if (landingDot >= moverSlopeCos_) {
623 const float safeFraction =
624 std::max(0.f, castFraction_ - skin / downDistance);
625 candidate.x += down.x * safeFraction;
626 candidate.y += down.y * safeFraction;
627 candidate.z += down.z * safeFraction;
628 const float directProgress =
631 const float candidateProgress =
635 if (candidateProgress > directProgress + std::max(1e-4f, skin)) {
636 moverDeltaX_ = candidate.x;
637 moverDeltaY_ = candidate.y;
638 moverDeltaZ_ = candidate.z;
639 moverNormalX_ = landingNormal.x;
640 moverNormalY_ = landingNormal.y;
641 moverNormalZ_ = landingNormal.z;
642 moverGroundDot_ = landingDot;
643 moverGrounded_ =
true;
644 moverHitWall_ = candidateHitWall;
645 moverIterations_ = candidateIterations + 2;