Skip to main content
Returns refs tracking a joint’s position and velocity values. Updated every physics frame without re-renders.

Signature

useJointState(name: string): {
  position: React.RefObject<number | Float64Array>;
  velocity: React.RefObject<number | Float64Array>;
}

Usage

import { useJointState } from "mujoco-react";

function JointDisplay() {
  const { position, velocity } = useJointState("joint1");
  const textRef = useRef<any>(null);

  useFrame(() => {
    const angle = position.current as number; // Scalar for hinge joints
    if (textRef.current) {
      textRef.current.text = `${(angle * 180 / Math.PI).toFixed(1)}°`;
    }
  });

  return <Text ref={textRef} position={[0, 0.3, 0]} />;
}

Return Value

FieldTypeDescription
positionRefObject<number | Float64Array>Joint position(s) from data.qpos. Scalar for hinge/slide, Float64Array for ball/free.
velocityRefObject<number | Float64Array>Joint velocity/velocities from data.qvel. Scalar for hinge/slide, Float64Array for ball/free.

Array Size by Joint Type

Joint TypePosition SizeVelocity Size
hinge1 (scalar)1 (scalar)
slide1 (scalar)1 (scalar)
ball4 (quaternion)3 (angular vel)
free7 (pos + quat)6 (lin vel + ang vel)

Notes

  • Returns refs for zero-overhead per-frame reads
  • For hinge/slide joints, values are scalars (not arrays)
  • For ball/free joints, typed arrays are preallocated once and reused via .set() each physics frame — no per-frame allocation
  • For free joints, position is [x, y, z, qw, qx, qy, qz]