Skip to content

Commit

Permalink
fix: update slope ray position in fps mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ErdongChen-Andrew committed Apr 19, 2024
1 parent 5eeaa71 commit 464cb75
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ecctrl",
"version": "1.0.75",
"version": "1.0.76",
"author": "Erdong Chen",
"license": "MIT",
"description": "A floating rigibody character controller for R3F",
Expand Down
20 changes: 13 additions & 7 deletions src/Ecctrl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -611,14 +611,18 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
/**
* Setup moving direction
*/
// Only apply slope extra force when slope angle is between 0.2 and slopeMaxAngle, actualSlopeAngle < slopeMaxAngle
// Only apply slope angle to moving direction
// when slope angle is between 0.2rad and slopeMaxAngle,
// and actualSlopeAngle < slopeMaxAngle
if (
actualSlopeAngle < slopeMaxAngle &&
Math.abs(slopeAngle) > 0.2 &&
Math.abs(slopeAngle) < slopeMaxAngle
) {
movingDirection.set(0, Math.sin(slopeAngle), Math.cos(slopeAngle));
} else if (actualSlopeAngle >= slopeMaxAngle) {
}
// If on a slopeMaxAngle slope, only apply small a mount of forward direction
else if (actualSlopeAngle >= slopeMaxAngle) {
movingDirection.set(
0,
Math.sin(slopeAngle) > 0 ? 0 : Math.sin(slopeAngle),
Expand Down Expand Up @@ -753,6 +757,11 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
if (getCameraBased().isCameraBased) {
modelEuler.y = pivot.rotation.y
pivot.getWorldDirection(modelFacingVec)
// Update slopeRayOrigin to new positon
const slopeRayOriginNewPosition = new THREE.Vector3(movingDirection.x, 0, movingDirection.z)
const crossVecOnY = slopeRayOriginNewPosition.clone().cross(modelFacingVec)
slopeRayOriginRef.current.position.x = slopeRayOriginOffest * Math.sin(slopeRayOriginNewPosition.angleTo(modelFacingVec) * (crossVecOnY.y < 0 ? 1 : -1))
slopeRayOriginRef.current.position.z = slopeRayOriginOffest * Math.cos(slopeRayOriginNewPosition.angleTo(modelFacingVec) * (crossVecOnY.y < 0 ? 1 : -1))
} else {
characterModelIndicator.getWorldDirection(modelFacingVec)
}
Expand Down Expand Up @@ -969,9 +978,10 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
useFrame((state, delta) => {
if (delta > 1) delta %= 1;

// Character current position
// Character current position/velocity
if (characterRef.current) {
currentPos.copy(characterRef.current.translation() as THREE.Vector3);
currentVel.copy(characterRef.current.linvel() as THREE.Vector3);
// Assign userDate properties
(characterRef.current.userData as userDataType).canJump = canJump;
(characterRef.current.userData as userDataType).slopeAngle = slopeAngle;
Expand Down Expand Up @@ -1031,10 +1041,6 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
if (forward || backward || leftward || rightward || gamepadKeys.forward || gamepadKeys.backward || gamepadKeys.leftward || gamepadKeys.rightward)
moveCharacter(delta, run, slopeAngle, movingObjectVelocity);

// Character current velocity
if (characterRef.current)
currentVel.copy(characterRef.current.linvel() as THREE.Vector3);

// Jump impulse
if ((jump || button1Pressed) && canJump) {
// characterRef.current.applyImpulse(jumpDirection.set(0, 0.5, 0), true);
Expand Down

0 comments on commit 464cb75

Please sign in to comment.