Skip to content

Commit e15ed5d

Browse files
authored
Deadzone (#19)
1 parent bbce598 commit e15ed5d

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

.changeset/purple-goats-joke.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
"@hmans/controlfreak": patch
3+
---
4+
5+
New processor: `deadzone`. Applies a deadzone to a `VectorControl`.
6+
7+
```ts
8+
controller
9+
.addControl("move", VectorControl)
10+
.addStep(gamepad.axisVector(0, 1))
11+
.addStep(processors.clampVector(1))
12+
.addStep(processors.deadzone(0.15))
13+
```

apps/demo/src/controller.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ controller
2323
.addStep(keyboard.compositeVector("KeyW", "KeyS", "KeyA", "KeyD"))
2424
.addStep(gamepad.axisVector(0, 1))
2525
.addStep(processors.clampVector(1))
26+
.addStep(processors.deadzone(0.15))
2627

2728
controller
2829
.addControl("fire", BooleanControl)
@@ -35,6 +36,7 @@ controller
3536
keyboard.compositeVector("ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight")
3637
)
3738
.addStep(gamepad.axisVector(2, 3))
39+
.addStep(processors.deadzone(0.15))
3840
.addStep(processors.normalizeVector)
3941

4042
controller.onDeviceChange.add((d) => console.log("new device:", d))

packages/controlfreak/src/processors.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,14 @@ export const clampVector = (maxLength = 1) => ({ value }: VectorControl) => {
1313
value.y *= factor
1414
}
1515
}
16+
17+
export const deadzone = (threshold = 0.1) => ({ value }: VectorControl) => {
18+
const length = vector.magnitude(value)
19+
20+
if (length < threshold) {
21+
value.x = 0
22+
value.y = 0
23+
} else {
24+
vector.multiply(value, (length - threshold) / (length - length * threshold))
25+
}
26+
}

0 commit comments

Comments
 (0)