Refactor collision resolution logic in RapierCollisionWorld

This commit is contained in:
2026-04-12 01:39:50 +02:00
parent 42fe430944
commit d2eff2f3a8

View File

@@ -473,6 +473,21 @@ export class RapierCollisionWorld {
this.characterController.disableAutostep(); this.characterController.disableAutostep();
} }
const computeResolvedMovement = (allowAutostep: boolean) => {
this.playerCollider.setTranslation(currentCenter);
if (autostepWasEnabled) {
if (allowAutostep) {
this.characterController.enableAutostep(
this.autostepHeight,
this.autostepMinWidth,
false
);
} else {
this.characterController.disableAutostep();
}
}
this.characterController.computeColliderMovement(this.playerCollider, motion); this.characterController.computeColliderMovement(this.playerCollider, motion);
if (snapToGroundWasEnabled) { if (snapToGroundWasEnabled) {
@@ -519,20 +534,42 @@ export class RapierCollisionWorld {
groundCollisionNormal = toVec3(collision.normal1); groundCollisionNormal = toVec3(collision.normal1);
} }
this.playerCollider.setTranslation(nextCenter); return {
correctedMovement,
collidedAxes,
nextCenter,
nextFeetPosition,
collisionCount,
groundCollisionNormal
};
};
const allowAutostep = autostepWasEnabled && supportGrounded;
let resolved = computeResolvedMovement(allowAutostep);
if (
allowAutostep &&
motion.y <= COLLISION_EPSILON &&
resolved.correctedMovement.y > COLLISION_EPSILON &&
(resolved.collidedAxes.x || resolved.collidedAxes.z)
) {
resolved = computeResolvedMovement(false);
}
this.playerCollider.setTranslation(resolved.nextCenter);
const groundedProbe = this.probePlayerGround( const groundedProbe = this.probePlayerGround(
nextFeetPosition, resolved.nextFeetPosition,
shape, shape,
this.snapToGroundDistance this.snapToGroundDistance
); );
return { return {
feetPosition: nextFeetPosition, feetPosition: resolved.nextFeetPosition,
grounded: groundedProbe.grounded, grounded: groundedProbe.grounded,
collisionCount, collisionCount: resolved.collisionCount,
groundCollisionNormal, groundCollisionNormal: resolved.groundCollisionNormal,
collidedAxes collidedAxes: resolved.collidedAxes
}; };
} }