Skip to content

Commit d334c29

Browse files
committed
feat: add deadzone threshold for joysticks
1 parent 8b35a75 commit d334c29

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/Ecctrl.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ const Ecctrl: ForwardRefRenderFunction<CustomEcctrlRigidBody, EcctrlProps> = ({
6969
camTargetPos = { x: 0, y: 0, z: 0 },
7070
camMoveSpeed = 1,
7171
camZoomSpeed = 1,
72+
leftJoystickDeadZoneThreshold = 0,
73+
rightJoystickDeadZoneThreshold = 0,
7274
camCollision = true,
7375
camCollisionOffset = 0.7,
7476
camCollisionSpeedMult = 4,
@@ -511,10 +513,16 @@ const Ecctrl: ForwardRefRenderFunction<CustomEcctrlRigidBody, EcctrlProps> = ({
511513
}
512514
}
513515

516+
const applyDeadZone = (value: number, threshold: number) => {
517+
return Math.abs(value) > threshold ? value : 0;
518+
}
519+
514520
const handleSticks = (axes: readonly number[]) => {
521+
const adjustedLeftStickX = applyDeadZone(axes[0], leftJoystickDeadZoneThreshold);
522+
const adjustedLeftStickY = applyDeadZone(axes[1], leftJoystickDeadZoneThreshold);
515523
// Gamepad first joystick trigger the EcctrlJoystick event to move the character
516-
if (Math.abs(axes[0]) > 0 || Math.abs(axes[1]) > 0) {
517-
gamepadJoystickVec2.set(axes[0], -axes[1])
524+
if (Math.abs(adjustedLeftStickX) > 0 || Math.abs(adjustedLeftStickY) > 0) {
525+
gamepadJoystickVec2.set(adjustedLeftStickX, -adjustedLeftStickY)
518526
gamepadJoystickDis = Math.min(Math.sqrt(Math.pow(gamepadJoystickVec2.x, 2) + Math.pow(gamepadJoystickVec2.y, 2)), 1)
519527
gamepadJoystickAng = gamepadJoystickVec2.angle()
520528
const runState = gamepadJoystickDis > 0.7
@@ -524,9 +532,11 @@ const Ecctrl: ForwardRefRenderFunction<CustomEcctrlRigidBody, EcctrlProps> = ({
524532
gamepadJoystickAng = 0
525533
resetJoystick()
526534
}
535+
const adjustedRightStickX = applyDeadZone(axes[2], rightJoystickDeadZoneThreshold);
536+
const adjustedRightStickY = applyDeadZone(axes[3], rightJoystickDeadZoneThreshold);
527537
// Gamepad second joystick trigger the useFollowCam event to move the camera
528-
if (Math.abs(axes[2]) > 0 || Math.abs(axes[3]) > 0) {
529-
joystickCamMove(axes[2], axes[3])
538+
if (Math.abs(adjustedRightStickX) > 0 || Math.abs(adjustedRightStickY) > 0) {
539+
joystickCamMove(adjustedRightStickX, adjustedRightStickY);
530540
}
531541
}
532542

@@ -1566,6 +1576,8 @@ export interface EcctrlProps extends RigidBodyProps {
15661576
camTargetPos?: { x: number, y: number, z: number };
15671577
camMoveSpeed?: number;
15681578
camZoomSpeed?: number;
1579+
leftJoystickDeadZoneThreshold?: number;
1580+
rightJoystickDeadZoneThreshold?: number;
15691581
camCollision?: boolean;
15701582
camCollisionOffset?: number;
15711583
camCollisionSpeedMult?: number;

0 commit comments

Comments
 (0)