Skip to content

Commit 7fd37b6

Browse files
noah-wardlowclaude
andcommitted
docs: clean up controller examples in README
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 53c208c commit 7fd37b6

1 file changed

Lines changed: 15 additions & 36 deletions

File tree

README.md

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -87,57 +87,36 @@ function MyComponent() {
8787

8888
## Writing a Controller
8989

90-
A controller is a React component that uses handle-based hooks for type-safe actuator and sensor access:
90+
A controller is a hook that uses handle-based access for type-safe actuator and sensor control:
9191

9292
```tsx
93-
import { useCtrl, useSensor, useBeforePhysicsStep } from "mujoco-react";
93+
import { createControllerHook, useCtrl, useSensor, useBeforePhysicsStep } from "mujoco-react";
9494

95-
function MyController() {
96-
const shoulder = useCtrl("shoulder");
97-
const elbow = useCtrl("elbow");
98-
const force = useSensor("force_sensor");
99-
100-
useBeforePhysicsStep(() => {
101-
shoulder.write(Math.sin(Date.now() / 1000));
102-
elbow.write(force.read()[0] * -0.5);
103-
});
104-
return null;
105-
}
106-
```
107-
108-
Drop it into the tree:
109-
110-
```tsx
111-
<MujocoCanvas config={config}>
112-
<MyController />
113-
</MujocoCanvas>
114-
```
115-
116-
The `createControllerHook` factory produces a typed hook with config stabilization and default merging. Pass `null` to disable:
117-
118-
```tsx
119-
import { createControllerHook, useBeforePhysicsStep } from "mujoco-react";
120-
121-
export const useMyController = createControllerHook<{ gain: number }, { value: number }>(
95+
export const useMyController = createControllerHook<{ gain: number }, { amplitude: number }>(
12296
{ name: "useMyController", defaultConfig: { gain: 1.0 } },
12397
(config) => {
124-
useBeforePhysicsStep((_model, data) => {
98+
const shoulder = useCtrl("shoulder");
99+
const elbow = useCtrl("elbow");
100+
const force = useSensor("force_sensor");
101+
102+
useBeforePhysicsStep(() => {
125103
if (!config) return;
126-
data.ctrl[0] = config.gain * Math.sin(data.time);
104+
shoulder.write(config.gain * Math.sin(Date.now() / 1000));
105+
elbow.write(force.read()[0] * -0.5);
127106
});
128-
if (!config) return null;
129-
return { value: config.gain };
107+
108+
return config ? { amplitude: shoulder.read() } : null;
130109
},
131110
);
132111

133112
// const result = useMyController({ gain: 2.0 });
134-
// const disabled = useMyController(null); // returns null
113+
// const disabled = useMyController(null); // returns null, no-ops
135114
```
136115

137-
The `createController` factory is the component equivalent — same config stabilization, but returns a component that can render children:
116+
For controllers that render children (debug overlays, context providers, etc.), use the component factory:
138117

139118
```tsx
140-
import { createController, useBeforePhysicsStep, Debug } from "mujoco-react";
119+
import { createController, useBeforePhysicsStep } from "mujoco-react";
141120

142121
export const MyController = createController<{ gain: number }>(
143122
{ name: "MyController", defaultConfig: { gain: 1.0 } },

0 commit comments

Comments
 (0)