@pinkaphant/ecctrl 0.0.0-stage → 2.0.4-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (117) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/LICENSE +21 -0
  3. package/NOTICE +10 -0
  4. package/dist/Color-V9u0UyNg.cjs +81 -0
  5. package/dist/Color-V9u0UyNg.cjs.map +1 -0
  6. package/dist/Color-rCfz0rNn.js +28 -0
  7. package/dist/Color-rCfz0rNn.js.map +1 -0
  8. package/dist/CurveLUT-BfMhx3LX.js +88 -0
  9. package/dist/CurveLUT-BfMhx3LX.js.map +1 -0
  10. package/dist/CurveLUT-Ck18ZlkK.cjs +99 -0
  11. package/dist/CurveLUT-Ck18ZlkK.cjs.map +1 -0
  12. package/dist/EcctrlAnimationStateController-41Cm1Tt1.cjs +111 -0
  13. package/dist/EcctrlAnimationStateController-41Cm1Tt1.cjs.map +1 -0
  14. package/dist/EcctrlAnimationStateController-Bb2fARWF.js +94 -0
  15. package/dist/EcctrlAnimationStateController-Bb2fARWF.js.map +1 -0
  16. package/dist/Math-BkrN2TZK.js +49 -0
  17. package/dist/Math-BkrN2TZK.js.map +1 -0
  18. package/dist/Math-ByyUpUHB.cjs +68 -0
  19. package/dist/Math-ByyUpUHB.cjs.map +1 -0
  20. package/dist/all.cjs +52 -0
  21. package/dist/all.cjs.map +1 -0
  22. package/dist/all.d.ts +16 -0
  23. package/dist/all.js +32 -0
  24. package/dist/all.js.map +1 -0
  25. package/dist/animation.cjs +23 -0
  26. package/dist/animation.cjs.map +1 -0
  27. package/dist/animation.d.ts +8 -0
  28. package/dist/animation.js +20 -0
  29. package/dist/animation.js.map +1 -0
  30. package/dist/camera-B_UgIm14.js +71 -0
  31. package/dist/camera-B_UgIm14.js.map +1 -0
  32. package/dist/camera-CzMKt9dI.cjs +79 -0
  33. package/dist/camera-CzMKt9dI.cjs.map +1 -0
  34. package/dist/camera.cjs +10 -0
  35. package/dist/camera.d.ts +9 -0
  36. package/dist/camera.js +9 -0
  37. package/dist/character/Ecctrl.d.ts +105 -0
  38. package/dist/character/animation/EcctrlAnimationStateController.d.ts +17 -0
  39. package/dist/character/animation/index.d.ts +13 -0
  40. package/dist/character/animation/resolveAnimationState.d.ts +9 -0
  41. package/dist/character/animation/types.d.ts +19 -0
  42. package/dist/character/animation/useEcctrlAnimationStore.d.ts +21 -0
  43. package/dist/character/types.d.ts +27 -0
  44. package/dist/chunk-D6vf50IK.cjs +35 -0
  45. package/dist/curves/CurveLUT.d.ts +27 -0
  46. package/dist/curves.cjs +22 -0
  47. package/dist/curves.cjs.map +1 -0
  48. package/dist/curves.d.ts +9 -0
  49. package/dist/curves.js +20 -0
  50. package/dist/curves.js.map +1 -0
  51. package/dist/gravity/EcctrlCameraControls.d.ts +28 -0
  52. package/dist/gravity/useCustomGravity.d.ts +23 -0
  53. package/dist/gravity.cjs +21 -0
  54. package/dist/gravity.cjs.map +1 -0
  55. package/dist/gravity.d.ts +9 -0
  56. package/dist/gravity.js +20 -0
  57. package/dist/gravity.js.map +1 -0
  58. package/dist/index.cjs +14 -0
  59. package/dist/index.d.ts +12 -0
  60. package/dist/index.js +10 -0
  61. package/dist/input/Joystick.d.ts +18 -0
  62. package/dist/input/VirtualButton.d.ts +17 -0
  63. package/dist/input/stores/useButtonStore.d.ts +21 -0
  64. package/dist/input/stores/useJoystickStore.d.ts +26 -0
  65. package/dist/input-CrNBMRxm.js +315 -0
  66. package/dist/input-CrNBMRxm.js.map +1 -0
  67. package/dist/input-DBhf3WZe.cjs +340 -0
  68. package/dist/input-DBhf3WZe.cjs.map +1 -0
  69. package/dist/input.cjs +13 -0
  70. package/dist/input.d.ts +15 -0
  71. package/dist/input.js +9 -0
  72. package/dist/leva--ZV58vPr.cjs +771 -0
  73. package/dist/leva--ZV58vPr.cjs.map +1 -0
  74. package/dist/leva-BjLuypTs.js +764 -0
  75. package/dist/leva-BjLuypTs.js.map +1 -0
  76. package/dist/leva.cjs +10 -0
  77. package/dist/leva.d.ts +9 -0
  78. package/dist/leva.js +9 -0
  79. package/dist/plugins/leva/CurveEditorPlugin.d.ts +72 -0
  80. package/dist/shared/Math.d.ts +11 -0
  81. package/dist/shared/constants/Color.d.ts +16 -0
  82. package/dist/shared/types.d.ts +16 -0
  83. package/dist/src-Cz0JLHDZ.js +1164 -0
  84. package/dist/src-Cz0JLHDZ.js.map +1 -0
  85. package/dist/src-Qj3Pzy6X.cjs +1172 -0
  86. package/dist/src-Qj3Pzy6X.cjs.map +1 -0
  87. package/dist/time/TimeControl.d.ts +16 -0
  88. package/dist/time-CHRDN3n2.cjs +48 -0
  89. package/dist/time-CHRDN3n2.cjs.map +1 -0
  90. package/dist/time-D3dS_ohx.js +43 -0
  91. package/dist/time-D3dS_ohx.js.map +1 -0
  92. package/dist/time.cjs +10 -0
  93. package/dist/time.d.ts +9 -0
  94. package/dist/time.js +9 -0
  95. package/dist/useCustomGravity-B35U0d4V.cjs +45 -0
  96. package/dist/useCustomGravity-B35U0d4V.cjs.map +1 -0
  97. package/dist/useCustomGravity-elzlTR6Y.js +38 -0
  98. package/dist/useCustomGravity-elzlTR6Y.js.map +1 -0
  99. package/dist/utils.cjs +23 -0
  100. package/dist/utils.cjs.map +1 -0
  101. package/dist/utils.d.ts +8 -0
  102. package/dist/utils.js +20 -0
  103. package/dist/utils.js.map +1 -0
  104. package/dist/vehicle-B8WqQHee.js +2069 -0
  105. package/dist/vehicle-B8WqQHee.js.map +1 -0
  106. package/dist/vehicle-DYwDt5rB.cjs +2089 -0
  107. package/dist/vehicle-DYwDt5rB.cjs.map +1 -0
  108. package/dist/vehicle.cjs +12 -0
  109. package/dist/vehicle.d.ts +14 -0
  110. package/dist/vehicle.js +9 -0
  111. package/dist/vehicles/EcctrlVehicle.d.ts +109 -0
  112. package/dist/vehicles/components/ShapeCastWheel.d.ts +107 -0
  113. package/dist/vehicles/components/ThrustPropeller.d.ts +50 -0
  114. package/dist/vehicles/types.d.ts +39 -0
  115. package/package.json +155 -4
  116. package/readme.md +737 -0
  117. package/README.md +0 -3
package/readme.md ADDED
@@ -0,0 +1,737 @@
1
+ # Ecctrl
2
+
3
+ [![npm version](https://img.shields.io/npm/v/ecctrl.svg)](https://www.npmjs.com/package/ecctrl)
4
+ [![license](https://img.shields.io/npm/l/ecctrl.svg)](./LICENSE)
5
+ [![three](https://img.shields.io/badge/three.js-0.184+-111111)](https://threejs.org/)
6
+ [![r3f](https://img.shields.io/badge/react--three--fiber-9.4+-111111)](https://github.com/pmndrs/react-three-fiber)
7
+ [![rapier](https://img.shields.io/badge/react--three--rapier-2.2+-111111)](https://github.com/pmndrs/react-three-rapier)
8
+
9
+ Physics-driven character, vehicle, drone, and custom-gravity controllers for React Three Fiber and Rapier.
10
+
11
+ Ecctrl is a modular controller toolkit for custom-gravity worlds, ShapeCast characters, torque-driven cars, thrust-driven drones, touch controls, runtime animation states, and editable physics curves.
12
+
13
+ ![Ecctrl demo montage](https://raw.githubusercontent.com/pmndrs/ecctrl/main/docs/images/ecctrl-hero.gif)
14
+
15
+ ## Live Demo
16
+
17
+ **Explore Ecctrl at [ecctrl.app](https://ecctrl.app/).**
18
+
19
+ This is the best way to test the character controller, custom gravity, cars, drones, touch controls, time control, and tuning tools before adding Ecctrl to your project.
20
+
21
+ - Full docs: [API and configuration guide](https://github.com/pmndrs/ecctrl/blob/main/docs/api-reference.md)
22
+ - Changelog: [Release history and upcoming changes](./CHANGELOG.md)
23
+ - Discord: [Ecctrl discussion channel](https://discord.gg/xyUkPmaV6)
24
+ - Related project: [BVHEcctrl](https://github.com/pmndrs/BVHEcctrl), [BVHEcctrl demo](https://bvhecctrl.vercel.app/)
25
+ - Author: [GitHub](https://github.com/ErdongChen-Andrew), [X / Twitter](https://x.com/AndrewChenE), [Website](https://www.erdong-chen.com/)
26
+
27
+ ## Contents
28
+
29
+ - [Live Demo](#live-demo)
30
+ - [Install](#install)
31
+ - [Quick Start](#quick-start)
32
+ - [Detailed Docs](#detailed-docs)
33
+ - [Why Ecctrl](#why-ecctrl)
34
+ - [Highlights](#highlights)
35
+ - [Module Imports](#module-imports)
36
+ - [Custom Gravity](#custom-gravity)
37
+ - [ShapeCast Character Controller](#shapecast-character-controller)
38
+ - [Animation State](#animation-state)
39
+ - [Vehicle System](#vehicle-system)
40
+ - [ShapeCast Wheels](#shapecast-wheels)
41
+ - [Propeller Drones](#propeller-drones)
42
+ - [Curve LUTs](#curve-luts)
43
+ - [Leva Curve Editor](#leva-curve-editor)
44
+ - [Time Control](#time-control)
45
+ - [Touch Input](#touch-input)
46
+ - [Performance Notes](#performance-notes)
47
+ - [Support](#support)
48
+ - [Contributing](#contributing)
49
+ - [Roadmap](#roadmap)
50
+ - [Local Development](#local-development)
51
+ - [License](#license)
52
+
53
+ ## Install
54
+
55
+ ```bash
56
+ npm install ecctrl
57
+ ```
58
+
59
+ Peer dependencies:
60
+
61
+ ```bash
62
+ npm install three react react-dom @react-three/fiber @react-three/rapier
63
+ ```
64
+
65
+ Optional packages:
66
+
67
+ ```bash
68
+ npm install @react-three/drei leva
69
+ ```
70
+
71
+ `@react-three/drei` is only needed for helpers such as `KeyboardControls` and camera tooling. If you do not use Drei, you can still drive controller input through refs or stores and build your own camera follow logic. `leva` is only needed if you want the visual curve editor or debug panels.
72
+
73
+ ## Quick Start
74
+
75
+ This is the smallest useful ShapeCast character setup:
76
+
77
+ ```tsx
78
+ import { KeyboardControls } from "@react-three/drei";
79
+ import { Physics } from "@react-three/rapier";
80
+ import { Ecctrl } from "ecctrl";
81
+
82
+ const keyboardMap = [
83
+ { name: "forward", keys: ["ArrowUp", "KeyW"] },
84
+ { name: "backward", keys: ["ArrowDown", "KeyS"] },
85
+ { name: "leftward", keys: ["ArrowLeft", "KeyA"] },
86
+ { name: "rightward", keys: ["ArrowRight", "KeyD"] },
87
+ { name: "jump", keys: ["Space"] },
88
+ { name: "run", keys: ["Shift"] },
89
+ ];
90
+
91
+ export function CharacterScene() {
92
+ return (
93
+ <Physics>
94
+ <KeyboardControls map={keyboardMap}>
95
+ <Ecctrl>
96
+ <CharacterModel />
97
+ </Ecctrl>
98
+ </KeyboardControls>
99
+ </Physics>
100
+ );
101
+ }
102
+ ```
103
+
104
+ For custom gravity scenes, set Rapier gravity to `[0, 0, 0]` and let Ecctrl apply gravity per body:
105
+
106
+ ```tsx
107
+ <Physics gravity={[0, 0, 0]}>
108
+ <Ecctrl enableCustomGravity>
109
+ <CharacterModel />
110
+ </Ecctrl>
111
+ </Physics>
112
+ ```
113
+
114
+ ## Detailed Docs
115
+
116
+ The README gives the main concepts and common setup paths. Full prop tables, config defaults, handle output, input shapes, and tuning notes are available in the detailed docs:
117
+
118
+ - [API and configuration guide](https://github.com/pmndrs/ecctrl/blob/main/docs/api-reference.md)
119
+
120
+ ## Why Ecctrl
121
+
122
+ Ecctrl is built for games and simulations where controlled objects need to behave like part of the physics world.
123
+
124
+ The character floats with spring and damping, pushes back on the object below, follows moving platforms, aligns with custom gravity, and exposes runtime state for animation and gameplay logic. Ecctrl keeps that physics-driven direction and extends it into cars, drones, touch input, time control, and editable curve-driven behavior.
125
+
126
+ ## Highlights
127
+
128
+ | Feature | What it gives you |
129
+ | --- | --- |
130
+ | Custom gravity | Position-based gravity fields shared by characters, vehicles, drones, and custom rigid bodies |
131
+ | ShapeCast character | Stable floating support, accurate standing points, gap handling, slope checks, and optional RayCast mode |
132
+ | ShapeCast wheels | Torque-driven wheel simulation with editable longitudinal and lateral slip curves |
133
+ | Propeller drones | Thrust and reaction torque are mixed per propeller for stabilized velocity or position flight |
134
+ | Curve LUTs | Runtime-cheap curve sampling for grip, engine torque, steering, and mass falloff |
135
+ | Leva curve editor | Optional draggable curve editor for tuning points, tangents, and weights |
136
+ | Time control | Pause and slow down the physics world for bullet-time style control |
137
+ | Touch controls | DOM-based joystick and virtual buttons that can be replaced by your own UI |
138
+
139
+ ## Module Imports
140
+
141
+ Ecctrl is split into subpath exports so you can import only the parts you need.
142
+
143
+ ```ts
144
+ import { Ecctrl } from "ecctrl";
145
+ import { EcctrlVehicle, ShapeCastWheel, ThrustPropeller } from "ecctrl/vehicle";
146
+ import { Joystick, VirtualButton } from "ecctrl/input";
147
+ import { useCustomGravity } from "ecctrl/gravity";
148
+ import { EcctrlCameraControls } from "ecctrl/camera";
149
+ import { TimeControl } from "ecctrl/time";
150
+ import { EcctrlAnimationStateController } from "ecctrl/animation";
151
+ import { bakeCurveLUT, evaluateCurveLUT } from "ecctrl/curves";
152
+ import { CurveEditorPlugin } from "ecctrl/leva";
153
+ ```
154
+
155
+ For prototypes or examples, you can also import everything from `ecctrl/all`.
156
+
157
+ ## Custom Gravity
158
+
159
+ Custom gravity is one of the main systems in Ecctrl.
160
+
161
+ Instead of relying only on Rapier's global gravity vector, Ecctrl can read a gravity field function and apply gravity per body. This makes it possible to build spherical gravity, cylindrical gravity, wall-walking zones, gravity tunnels, rotating gravity fields, or gameplay-specific attraction areas.
162
+
163
+ ![Custom gravity demo](https://raw.githubusercontent.com/pmndrs/ecctrl/main/docs/images/custom-gravity.gif)
164
+
165
+ When Ecctrl applies custom gravity, set Rapier's global gravity to `[0, 0, 0]` so the global gravity force and custom gravity field do not stack.
166
+
167
+ ```tsx
168
+ import { useEffect } from "react";
169
+ import * as THREE from "three";
170
+ import { Physics } from "@react-three/rapier";
171
+ import { Ecctrl } from "ecctrl";
172
+ import { useCustomGravity } from "ecctrl/gravity";
173
+
174
+ const center = new THREE.Vector3(0, 20, 0);
175
+ const gravity = new THREE.Vector3();
176
+
177
+ function GravitySetup() {
178
+ const setGravityField = useCustomGravity((state) => state.setGravityField);
179
+
180
+ useEffect(() => {
181
+ setGravityField((bodyPos) => gravity.subVectors(center, bodyPos).normalize().multiplyScalar(9.81));
182
+ }, [setGravityField]);
183
+
184
+ return null;
185
+ }
186
+
187
+ export function Scene() {
188
+ return (
189
+ <Physics gravity={[0, 0, 0]}>
190
+ <GravitySetup />
191
+ <Ecctrl enableCustomGravity>
192
+ <CharacterModel />
193
+ </Ecctrl>
194
+ </Physics>
195
+ );
196
+ }
197
+ ```
198
+
199
+ For custom rigid bodies outside Ecctrl, call `applyGravityField` each frame:
200
+
201
+ ```tsx
202
+ import { useRef } from "react";
203
+ import { useFrame } from "@react-three/fiber";
204
+ import { RigidBody, useRapier, type RapierRigidBody } from "@react-three/rapier";
205
+ import { useCustomGravity } from "ecctrl/gravity";
206
+
207
+ function CustomGravityRigidBody() {
208
+ const body = useRef<RapierRigidBody>(null);
209
+ const { world } = useRapier();
210
+ const applyGravityField = useCustomGravity((state) => state.applyGravityField);
211
+
212
+ useFrame(() => {
213
+ if (body.current) applyGravityField(body.current, world.timestep);
214
+ });
215
+
216
+ return <RigidBody ref={body} />;
217
+ }
218
+ ```
219
+
220
+ For dynamic gravity, keep the field function stable and update refs that the function reads. This avoids React renders and Zustand updates on every frame.
221
+
222
+ ```tsx
223
+ const gravityStrength = useRef(9.81);
224
+ const gravityTarget = useRef(new THREE.Vector3(0, 20, 0));
225
+
226
+ useEffect(() => {
227
+ setGravityField((bodyPos) => gravity.subVectors(gravityTarget.current, bodyPos).normalize().multiplyScalar(gravityStrength.current));
228
+ }, [setGravityField]);
229
+
230
+ useFrame(({ clock }) => {
231
+ // Move the attractor without replacing the gravity field function.
232
+ const t = clock.elapsedTime;
233
+ gravityTarget.current.set(Math.sin(t) * 10, 20, Math.cos(t) * 10);
234
+ });
235
+ ```
236
+
237
+ ### Camera Controls For Custom Gravity
238
+
239
+ `EcctrlCameraControls` extends Drei `CameraControls` with `setUp(newUp)`. It does not automatically follow a character by itself; you still drive the follow target from your controller ref.
240
+
241
+ ```tsx
242
+ import { useRef } from "react";
243
+ import * as THREE from "three";
244
+ import { useFrame, useThree } from "@react-three/fiber";
245
+ import { Ecctrl, type EcctrlHandle } from "ecctrl";
246
+ import { EcctrlCameraControls } from "ecctrl/camera";
247
+ import type { EcctrlCameraControlsHandle } from "ecctrl/camera";
248
+
249
+ function SceneCamera() {
250
+ const ecctrl = useRef<EcctrlHandle>(null);
251
+ const cameraControls = useRef<EcctrlCameraControlsHandle>(null);
252
+ const cameraUp = useRef(new THREE.Vector3());
253
+ const { camera } = useThree();
254
+
255
+ useFrame(() => {
256
+ if (!ecctrl.current || !cameraControls.current) return;
257
+
258
+ const target = ecctrl.current.currPos;
259
+ cameraControls.current.moveTo(target.x, target.y, target.z, true);
260
+
261
+ cameraUp.current.copy(ecctrl.current.upAxis);
262
+ camera.up.lerp(cameraUp.current, 0.1);
263
+ cameraControls.current.setUp(camera.up);
264
+ });
265
+
266
+ return (
267
+ <>
268
+ <Ecctrl ref={ecctrl}>
269
+ <CharacterModel />
270
+ </Ecctrl>
271
+ <EcctrlCameraControls ref={cameraControls} makeDefault smoothTime={0.1} />
272
+ </>
273
+ );
274
+ }
275
+ ```
276
+
277
+ ## ShapeCast Character Controller
278
+
279
+ The character controller now uses ShapeCast ground detection by default. RayCast mode is still available when you want the lowest possible detection cost.
280
+
281
+ ![ShapeCast character demo](https://raw.githubusercontent.com/pmndrs/ecctrl/main/docs/images/shapecast-character.gif)
282
+
283
+ For the minimum setup, see [Quick Start](#quick-start).
284
+
285
+ ### Ground Detection
286
+
287
+ ShapeCast mode gives the character a stronger ground signal than a single ray:
288
+
289
+ - More natural movement over gaps and small seams
290
+ - More accurate standing point and ground normal data
291
+ - One cast can drive floating, slope, and support detection
292
+ - Fallback center ray improves steep-slope and sharp-surface handling
293
+ - Optional `groundDetection="rayCast"` mode for lower-cost scenes
294
+
295
+ ```tsx
296
+ <Ecctrl groundDetection="shapeCast" />
297
+ <Ecctrl groundDetection="rayCast" />
298
+ ```
299
+
300
+ ### Physics Interaction
301
+
302
+ The character can push back on the world instead of only moving itself:
303
+
304
+ - Standing applies counter mass to the supporting body
305
+ - Moving applies opposite impulse to the standing point
306
+ - Jumping applies downward impulse to the object below
307
+ - Moving and rotating platforms are followed smoothly
308
+
309
+ These behaviors are enabled by default. You can disable them when a scene needs simpler interaction:
310
+
311
+ ```tsx
312
+ <Ecctrl
313
+ followPlatform={false}
314
+ applyCounterMass={false}
315
+ applyCounterMoveImp={false}
316
+ applyCounterJumpImp={false}
317
+ />
318
+ ```
319
+
320
+ ### Runtime Character State
321
+
322
+ `EcctrlHandle` exposes readonly runtime state for animation, gameplay logic, debug UI, or user-defined systems.
323
+
324
+ ```tsx
325
+ import { useRef } from "react";
326
+ import { useFrame } from "@react-three/fiber";
327
+ import { Ecctrl, type EcctrlHandle } from "ecctrl";
328
+
329
+ const ecctrl = useRef<EcctrlHandle>(null);
330
+
331
+ useFrame(() => {
332
+ if (!ecctrl.current) return;
333
+
334
+ const grounded = ecctrl.current.isOnGround;
335
+ const speed = ecctrl.current.moveSpeed;
336
+ const falling = ecctrl.current.isFalling;
337
+ });
338
+
339
+ <Ecctrl ref={ecctrl}>
340
+ <CharacterModel />
341
+ </Ecctrl>
342
+ ```
343
+
344
+ `EcctrlHandle` exposes more runtime values than the three shown here, including position, velocity, gravity direction, input state, and body axes. See the [detailed docs](https://github.com/pmndrs/ecctrl/blob/main/docs/api-reference.md) for the full handle shape.
345
+
346
+ ## Animation State
347
+
348
+ Ecctrl includes a lightweight animation state resolver. The controller writes the current state into `useEcctrlAnimationStore`; your model decides how to play, blend, or hold each animation clip.
349
+
350
+ Default states:
351
+
352
+ ```ts
353
+ "IDLE" | "WALK" | "RUN" | "JUMP_START" | "JUMP_IDLE" | "JUMP_FALL" | "JUMP_LAND"
354
+ ```
355
+
356
+ Basic setup:
357
+
358
+ ```tsx
359
+ import { useEffect, useRef } from "react";
360
+ import { Ecctrl, type EcctrlHandle } from "ecctrl";
361
+ import { EcctrlAnimationStateController, useEcctrlAnimationStore } from "ecctrl/animation";
362
+ import { useAnimations, useGLTF } from "@react-three/drei";
363
+
364
+ const ANIMATION_MAP = {
365
+ IDLE: "Idle",
366
+ WALK: "Walk",
367
+ RUN: "Run",
368
+ JUMP_START: "Jump_Start",
369
+ JUMP_IDLE: "Jump_Idle",
370
+ JUMP_FALL: "Jump_Fall",
371
+ JUMP_LAND: "Jump_Land",
372
+ } as const;
373
+
374
+ function AnimatedCharacterModel() {
375
+ const group = useRef(null);
376
+ const { scene, animations } = useGLTF("/character.glb");
377
+ const { actions } = useAnimations(animations, group);
378
+ const animationState = useEcctrlAnimationStore((state) => state.animationState);
379
+
380
+ useEffect(() => {
381
+ const action = actions[ANIMATION_MAP[animationState]];
382
+ if (!action) return;
383
+
384
+ action.reset().fadeIn(0.15).play();
385
+ return () => action.fadeOut(0.15);
386
+ }, [actions, animationState]);
387
+
388
+ return (
389
+ <group ref={group}>
390
+ <primitive object={scene} />
391
+ </group>
392
+ );
393
+ }
394
+
395
+ function CharacterWithAnimation() {
396
+ const ecctrl = useRef<EcctrlHandle>(null);
397
+
398
+ return (
399
+ <>
400
+ <EcctrlAnimationStateController ecctrl={ecctrl} />
401
+ <Ecctrl ref={ecctrl}>
402
+ <AnimatedCharacterModel />
403
+ </Ecctrl>
404
+ </>
405
+ );
406
+ }
407
+ ```
408
+
409
+ For custom state mapping, pass a resolver:
410
+
411
+ ```tsx
412
+ import {
413
+ EcctrlAnimationStateController,
414
+ resolveEcctrlAnimationState,
415
+ type EcctrlAnimationStateResolver,
416
+ } from "ecctrl/animation";
417
+
418
+ const resolver: EcctrlAnimationStateResolver = (ctx) => {
419
+ if (ctx.handle.moveSpeed > 8) return "RUN";
420
+ return resolveEcctrlAnimationState(ctx);
421
+ };
422
+
423
+ <EcctrlAnimationStateController ecctrl={ecctrl} resolver={resolver} />;
424
+ ```
425
+
426
+ For fully custom animation graphs, skip the store and read `EcctrlHandle` directly in your animation system.
427
+
428
+ ## Vehicle System
429
+
430
+ Ecctrl includes a vehicle controller that can be built from wheels, propellers, or both.
431
+
432
+ ```tsx
433
+ import { EcctrlVehicle, ShapeCastWheel } from "ecctrl/vehicle";
434
+ import { CuboidCollider } from "@react-three/rapier";
435
+
436
+ <EcctrlVehicle>
437
+ <CuboidCollider args={[1, 0.5, 2]} />
438
+ <ShapeCastWheel position={[1, 0, 1.5]} driveWheel steerWheel brakeWheel />
439
+ <ShapeCastWheel position={[-1, 0, 1.5]} driveWheel steerWheel brakeWheel />
440
+ <ShapeCastWheel position={[1, 0, -1.5]} driveWheel brakeWheel />
441
+ <ShapeCastWheel position={[-1, 0, -1.5]} driveWheel brakeWheel />
442
+ </EcctrlVehicle>
443
+ ```
444
+
445
+ `EcctrlVehicle` owns the body state, input state, custom gravity, and high-level control logic. Wheels and propellers register as modules, compute local wheel or propeller data, and feed the vehicle with the values it needs to apply impulses.
446
+
447
+ Driving input can come from keyboard controls, touch controls, AI, or your own code:
448
+
449
+ ```tsx
450
+ import { useRef } from "react";
451
+ import { useFrame } from "@react-three/fiber";
452
+ import { EcctrlVehicle, type EcctrlVehicleHandle, type VehicleInput } from "ecctrl/vehicle";
453
+
454
+ function VehicleInputExample({ input }: { input: VehicleInput }) {
455
+ const vehicle = useRef<EcctrlVehicleHandle>(null);
456
+
457
+ useFrame(() => {
458
+ vehicle.current?.setMovement(input);
459
+ });
460
+
461
+ return (
462
+ <EcctrlVehicle ref={vehicle}>
463
+ {/* body collider and vehicle modules */}
464
+ </EcctrlVehicle>
465
+ );
466
+ }
467
+ ```
468
+
469
+ ## ShapeCast Wheels
470
+
471
+ `ShapeCastWheel` is designed for physics-driven vehicle control.
472
+
473
+ The wheel receives drive torque, updates wheel angular velocity, evaluates slip through baked curves, then applies longitudinal and lateral grip impulses to the vehicle body.
474
+
475
+ ![ShapeCast wheel demo](https://raw.githubusercontent.com/pmndrs/ecctrl/main/docs/images/shapecast-wheels.gif)
476
+
477
+ ```text
478
+ torque -> wheel speed -> slip -> grip -> impulse
479
+ ```
480
+
481
+ Key features:
482
+
483
+ - ShapeCast wheel detection for wheel-like support
484
+ - Optional `groundDetection="rayCast"` mode for maximum performance
485
+ - Stable support point handling for suspension force
486
+ - Accurate contact, normal, slip, torque, RPM, and impulse data
487
+ - Drive, steer, brake, rolling resistance, and tire relaxation logic
488
+ - AWD, RWD, FWD, and weighted torque distribution
489
+ - Longitudinal and lateral grip curves through LUTs
490
+ - Low-speed stability and tire relaxation behavior
491
+ - Counter impulses for objects under the wheel
492
+ - Moving platform support
493
+
494
+ ```tsx
495
+ <ShapeCastWheel
496
+ driveWheel
497
+ steerWheel
498
+ brakeWheel
499
+ driveTorqueWeight={1}
500
+ groundDetection="shapeCast"
501
+ tireGripFactor={1.5}
502
+ />
503
+ ```
504
+
505
+ ### Torque Distribution
506
+
507
+ Drive torque is distributed across registered drive wheels. Use `driveTorqueWeight` when one wheel should receive more or less drive torque.
508
+
509
+ ```tsx
510
+ <ShapeCastWheel driveWheel driveTorqueWeight={1.2} />
511
+ <ShapeCastWheel driveWheel driveTorqueWeight={0.8} />
512
+ ```
513
+
514
+ If no weight is provided, every drive wheel receives an equal share.
515
+
516
+ ### Engine And Transmission
517
+
518
+ The demo setup uses a simple motor-style fixed ratio. You can also provide a more combustion-engine-like setup with gear ratios, final drive, and automatic or manual transmission behavior.
519
+
520
+ ```tsx
521
+ <EcctrlVehicle
522
+ carConfig={{
523
+ engineHorsepower: 600,
524
+ engineMaxRPM: 6000,
525
+ gearRatios: [3.2, 2.1, 1.45, 1.0, 0.82],
526
+ finalDriveRatio: 18,
527
+ transmissionMode: "auto",
528
+ shiftUpRPM: 5200,
529
+ shiftDownRPM: 2200,
530
+ shiftCooldown: 0.35,
531
+ }}
532
+ />
533
+ ```
534
+
535
+ Manual gear selection can be driven through the vehicle ref:
536
+
537
+ ```tsx
538
+ vehicle.current?.setGear(2);
539
+ ```
540
+
541
+ The vehicle ref also exposes `gearIndex`, `driveRatio`, and `engineRPM` for UI or custom logic.
542
+
543
+ Longitudinal and lateral tire behavior use separate LUT curves, so acceleration/braking grip and side grip can be tuned independently. Low-speed stabilization and tire relaxation are included to keep the result stable and smooth instead of twitchy at rest or during transitions.
544
+
545
+ ## Propeller Drones
546
+
547
+ `ThrustPropeller` is the drone motor module. Each propeller registers its thrust and torque setup with `EcctrlVehicle`; the vehicle controller works as the flight brain, mixes throttle, and lets each propeller apply real thrust and reaction torque instead of directly setting body velocity or rotation.
548
+
549
+ ![Propeller drone demo](https://raw.githubusercontent.com/pmndrs/ecctrl/main/docs/images/propeller-drone.gif)
550
+
551
+ ```tsx
552
+ import { EcctrlVehicle, ThrustPropeller } from "ecctrl/vehicle";
553
+ import { CuboidCollider } from "@react-three/rapier";
554
+
555
+ <EcctrlVehicle
556
+ droneConfig={{
557
+ controlMode: "POSITION",
558
+ maxHorizSpeed: 20,
559
+ maxVertSpeed: 8,
560
+ maxTiltAngle: Math.PI / 4,
561
+ }}
562
+ >
563
+ <CuboidCollider args={[0.6, 0.15, 0.6]} />
564
+ <ThrustPropeller position={[1, 0, 1]} />
565
+ <ThrustPropeller position={[-1, 0, 1]} invertTorque />
566
+ <ThrustPropeller position={[1, 0, -1]} invertTorque />
567
+ <ThrustPropeller position={[-1, 0, -1]} />
568
+ </EcctrlVehicle>
569
+ ```
570
+
571
+ Key features:
572
+
573
+ - Per-propeller thrust impulse
574
+ - Per-propeller reaction torque impulse
575
+ - Throttle mixing handled by `EcctrlVehicle`
576
+ - `maxThrust`, `torqueRatio`, `invertThrust`, and `invertTorque` per propeller
577
+ - Custom gravity support through the same vehicle gravity system
578
+ - Optional propeller model spin update and debugger arrows
579
+
580
+ Drone control modes:
581
+
582
+ | Mode | Behavior |
583
+ | --- | --- |
584
+ | `VELOCITY` | Manual velocity-style flight. Input maps to target horizontal, vertical, yaw, pitch, and roll behavior while the controller stabilizes the body. |
585
+ | `POSITION` | Position-targeted flight. The controller computes output from target position and direction, then stabilizes tilt, yaw, and vertical movement. |
586
+
587
+ ## Curve LUTs
588
+
589
+ Ecctrl uses baked curve lookup tables for runtime performance. Curves can shape grip, torque, steering, mass-ratio falloff, and other controller responses.
590
+
591
+ ![Curve editor demo](https://raw.githubusercontent.com/pmndrs/ecctrl/main/docs/images/curve-editor.gif)
592
+
593
+ Bake a curve once, then sample the LUT with a normalized input value at runtime:
594
+
595
+ ```tsx
596
+ import { useMemo } from "react";
597
+ import { useFrame } from "@react-three/fiber";
598
+ import { bakeCurveLUT, evaluateCurveLUT } from "ecctrl/curves";
599
+
600
+ const gripCurve = useMemo(
601
+ () => bakeCurveLUT([
602
+ { x: 0, y: 0, r_out: 1.45, w_out: 1 },
603
+ { x: 0.25, y: 1, r_in: 0, r_out: 0, w_in: 1, w_out: 1 },
604
+ { x: 1, y: 0.7, r_in: 0, w_in: 1 },
605
+ ], 50),
606
+ []
607
+ );
608
+
609
+ useFrame(() => {
610
+ const grip = evaluateCurveLUT(slipRatio.current, gripCurve);
611
+ });
612
+ ```
613
+
614
+ Curve data supports position, incoming/outgoing tangent ratios, and tangent weights. Runtime sampling reads from the baked LUT, so per-frame curve evaluation stays cheap.
615
+
616
+ ## Leva Curve Editor
617
+
618
+ `CurveEditorPlugin` is optional. It gives Leva a draggable curve editor for tuning curve points and tangents visually.
619
+
620
+ ```tsx
621
+ import { useControls } from "leva";
622
+ import { CurveEditorPlugin } from "ecctrl/leva";
623
+
624
+ const { engineTorqueCurveData } = useControls("Car Control", {
625
+ engineTorqueCurveData: CurveEditorPlugin({
626
+ points: [
627
+ { x: 0, y: 1, r_out: 0, w_out: 1 },
628
+ { x: 1, y: 0, r_in: 0, w_in: 1 },
629
+ ],
630
+ samples: 50,
631
+ }),
632
+ });
633
+
634
+ <EcctrlVehicle carConfig={{ engineTorqueCurveData }} />;
635
+ ```
636
+
637
+ You can drag points, drag tangents, or type exact values. If `leva` is not installed, Ecctrl can still use plain curve data and baked LUTs.
638
+
639
+ ## Time Control
640
+
641
+ `TimeControl` manually steps Rapier. This is useful for pause, slow motion, bullet time, and deterministic example controls.
642
+
643
+ Set `<Physics paused>` and let `TimeControl` step the world:
644
+
645
+ ```tsx
646
+ import { Physics } from "@react-three/rapier";
647
+ import { TimeControl } from "ecctrl/time";
648
+
649
+ const timeScale = useRef(1);
650
+
651
+ <Physics paused gravity={[0, 0, 0]}>
652
+ <TimeControl timeScale={timeScale} maxDelta={1 / 30} />
653
+ </Physics>
654
+ ```
655
+
656
+ `timeScale` can be a number or a ref. Ref values are useful when the value changes often and you want to avoid React renders.
657
+
658
+ ## Touch Input
659
+
660
+ Ecctrl uses DOM-based touch controls. You can use the built-in joystick and virtual buttons, connect to the exported stores, or build your own UI.
661
+
662
+ ```tsx
663
+ import { Joystick, VirtualButton } from "ecctrl/input";
664
+
665
+ <>
666
+ <Joystick />
667
+ <VirtualButton id="jump" label="Jump" />
668
+ <VirtualButton id="enter" label="Enter" />
669
+ </>
670
+ ```
671
+
672
+ The built-in UI is just a DOM overlay. You can replace it completely and drive the same input stores yourself:
673
+
674
+ ```tsx
675
+ import { useButtonStore, useJoystickStore } from "ecctrl/input";
676
+
677
+ useJoystickStore.getState().setJoystick(x, y);
678
+ useButtonStore.getState().setButtonActive("jump", true);
679
+ ```
680
+
681
+ You can also bypass the stores and drive a controller directly through its ref:
682
+
683
+ ```tsx
684
+ ecctrl.current?.setMovement({ forward: true, jump: false });
685
+ vehicle.current?.setMovement({ forward: true, steerLeft: true });
686
+ ```
687
+
688
+ ## Performance Notes
689
+
690
+ Ecctrl is built around runtime-friendly patterns:
691
+
692
+ - Controller state is stored in refs where possible
693
+ - Curve data is baked into LUTs before per-frame sampling
694
+ - ShapeCast and RayCast modes can be selected per controller
695
+ - Leva and debug tools are optional
696
+ - Subpath exports avoid importing systems you do not use
697
+ - Dynamic gravity can read refs instead of updating React or Zustand every frame
698
+
699
+ For larger scenes, start with ShapeCast for behavior and switch selected characters or wheels to RayCast when you need the lowest detection cost.
700
+
701
+ ## Support
702
+
703
+ Thanks to everyone who has tested Ecctrl, reported issues, shared feedback, contributed code, or supported the project.
704
+
705
+ Ecctrl is MIT-licensed, so commercial use is allowed. If Ecctrl helps your project, [sponsorship](https://github.com/sponsors/ErdongChen-Andrew) is welcome and directly supports continued development.
706
+
707
+ For commercial projects, integration help, controller tuning, and custom physics-controller work are available by arrangement.
708
+
709
+ ## Contributing
710
+
711
+ Issues, bug reports, feature requests, tuning feedback, examples, docs improvements, and pull requests are welcome.
712
+
713
+ When reporting controller behavior, include the Ecctrl version, Rapier/R3F versions, a small reproduction if possible, and any relevant controller props. For vehicle tuning issues, screenshots or short clips are especially useful because wheel contact, suspension, and grip behavior are highly visual.
714
+
715
+ ## Roadmap
716
+
717
+ - Car position-based control for driving toward a target, with obstacle avoidance considered after the base controller is stable.
718
+ - Fully manual drone mode without self-balancing.
719
+ - Two-wheel vehicle mode with self-balancing and steering lean.
720
+ - More character abilities, such as double jump, air dash, swimming, wall climb, and wall kick jump.
721
+ - More vehicle types and tuning presets.
722
+ - Multiplayer demo examples.
723
+ - More complete documentation, recipes, and live examples.
724
+
725
+ ## Local Development
726
+
727
+ ```bash
728
+ npm install
729
+ npm run dev
730
+ npm run typecheck
731
+ ```
732
+
733
+ ## License
734
+
735
+ MIT License.
736
+
737
+ Created by [Erdong Chen](https://github.com/ErdongChen-Andrew).