feat: gamepad haptics engine, roller-mode ghost rigs, sound re-enable
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -21,7 +21,7 @@ import { signed } from "./signed.js";
|
||||
// point early-returns, master gain pinned to 0, context never resumed).
|
||||
// Node-returning helpers (busNode, createEmitter) keep building their
|
||||
// graph either way so callers stay untouched.
|
||||
const SOUND_DISABLED = true;
|
||||
const SOUND_DISABLED = false;
|
||||
|
||||
// ── Context, master, buses ────────────────────────────────────────────
|
||||
const MASTER_LEVEL = 0.9;
|
||||
|
||||
@@ -46,6 +46,15 @@ const BTN_R3 = 11, BTN_DPAD_UP = 12, BTN_DPAD_DOWN = 13;
|
||||
|
||||
const DPAD_UP_HOLD_MS = 1000; // hold-to-switch-loco duration
|
||||
|
||||
// The one pad-picking rule, shared by poll() and the haptics engine
|
||||
// (haptics.js): prefer a standard-mapping pad - the button indices above
|
||||
// are only guaranteed by the "standard" layout - and fall back to
|
||||
// whatever is connected.
|
||||
export function pickPad() {
|
||||
const pads = [...(navigator.getGamepads?.() ?? [])].filter((p) => p && p.connected);
|
||||
return pads.find((p) => p.mapping === "standard") ?? pads[0];
|
||||
}
|
||||
|
||||
export class GamepadSource {
|
||||
id = "gamepad";
|
||||
connected = false;
|
||||
@@ -82,12 +91,10 @@ export class GamepadSource {
|
||||
|
||||
poll() {
|
||||
const prev = this.pressed;
|
||||
// Prefer a standard-mapping pad: the button indices above are only
|
||||
// guaranteed by the "standard" layout. A non-standard pad (mapping "")
|
||||
// is a last-resort fallback - its indices are firmware-defined and
|
||||
// some mirror one combined trigger channel onto both trigger slots.
|
||||
const pads = [...(navigator.getGamepads?.() ?? [])].filter((p) => p && p.connected);
|
||||
const gp = pads.find((p) => p.mapping === "standard") ?? pads[0];
|
||||
// A non-standard pad (mapping "") is a last-resort fallback in
|
||||
// pickPad - its indices are firmware-defined and some mirror one
|
||||
// combined trigger channel onto both trigger slots.
|
||||
const gp = pickPad();
|
||||
this.connected = !!gp;
|
||||
if (!gp) {
|
||||
if (this.#active) {
|
||||
|
||||
+55
-9
@@ -45,6 +45,7 @@ import {
|
||||
import { Controller } from "./controls/controller.js";
|
||||
import { KeyboardSource } from "./controls/keyboard.js";
|
||||
import { GamepadSource } from "./controls/gamepad.js";
|
||||
import { haptics } from "./haptics.js";
|
||||
import { TouchSource } from "./controls/touch.js";
|
||||
import * as fx from "./fx/fx-wireframe.js";
|
||||
import { createCeremony, CAM_RESET_S } from "./ceremony.js";
|
||||
@@ -769,12 +770,12 @@ async function boot({ scene, camera, renderer }) {
|
||||
}
|
||||
}
|
||||
}
|
||||
// Roller rumble: gain and slight pitch follow ground speed.
|
||||
// Roller rumble: gain and slight pitch follow ground speed - and the
|
||||
// pad's haptic bed mirrors it (texture on the weak motor).
|
||||
const speed = Math.hypot(data.qvel[0], data.qvel[1]);
|
||||
setRumble(
|
||||
loco === "rollers" && !inputLocked ? Math.min(1, speed / 0.5) : 0,
|
||||
duckEmitter,
|
||||
);
|
||||
const rollLevel = loco === "rollers" && !inputLocked ? Math.min(1, speed / 0.5) : 0;
|
||||
setRumble(rollLevel, duckEmitter);
|
||||
haptics.setBed(rollLevel);
|
||||
// Body bumps: trunk |dv| against a nearby wall/prop. The proximity
|
||||
// gate keeps gait/kick jerks (which rival slow wall hits) silent.
|
||||
if (!inputLocked) {
|
||||
@@ -790,6 +791,7 @@ async function boot({ scene, camera, renderer }) {
|
||||
rate: 0.55 + 0.15 * u + Math.random() * 0.06, // way below the ball's range
|
||||
out: duckEmitter.node,
|
||||
});
|
||||
haptics.pulse("bump", 0.5 + 0.5 * u); // shove felt in the hands
|
||||
}
|
||||
}
|
||||
bumpPrevV[0] = v[0]; bumpPrevV[1] = v[1]; bumpPrevV[2] = v[2];
|
||||
@@ -813,6 +815,12 @@ async function boot({ scene, camera, renderer }) {
|
||||
rate: 1.25 - 0.45 * u + Math.random() * 0.08,
|
||||
out: ballEmitter.node,
|
||||
});
|
||||
// Haptics only when the duck plausibly caused/received the hit:
|
||||
// a far bounce off a wall shouldn't shake the hands.
|
||||
const bq = ballQposAdr;
|
||||
const dBall = Math.hypot(
|
||||
data.qpos[bq] - data.qpos[0], data.qpos[bq + 1] - data.qpos[1]);
|
||||
if (dBall < 0.6) haptics.pulse("ballHit", 0.4 + 0.6 * u);
|
||||
}
|
||||
}
|
||||
ballPrevV[0] = v[b]; ballPrevV[1] = v[b + 1]; ballPrevV[2] = v[b + 2];
|
||||
@@ -820,6 +828,9 @@ async function boot({ scene, camera, renderer }) {
|
||||
} else {
|
||||
ballPrevValid = false;
|
||||
}
|
||||
// Haptic channel scheduler: keeps the roller bed alive between pulses
|
||||
// and cuts the motors when it falls silent. 50 Hz, like everything here.
|
||||
haptics.tick();
|
||||
}
|
||||
|
||||
async function controlStep() {
|
||||
@@ -842,6 +853,7 @@ async function boot({ scene, camera, renderer }) {
|
||||
|
||||
const death = poseIsDead();
|
||||
if (death === "exploded") {
|
||||
haptics.pulse("explode");
|
||||
resetSim();
|
||||
} else if (recovery) {
|
||||
// Recovery state machine owns the duck: settle -> stand policy ->
|
||||
@@ -859,6 +871,7 @@ async function boot({ scene, camera, renderer }) {
|
||||
recovery = null;
|
||||
mode = "walk";
|
||||
lastAction.fill(0);
|
||||
haptics.pulse("recover"); // back on its feet: light double tap
|
||||
syncButtons();
|
||||
} else if (recovery.steps >= RECOVER_GIVEUP_STEPS) {
|
||||
resetSim();
|
||||
@@ -873,12 +886,18 @@ async function boot({ scene, camera, renderer }) {
|
||||
fallDebounce = 0;
|
||||
exitHeadMode();
|
||||
recovery = { state: "fallen", steps: 0 };
|
||||
// Haptic thud on the confirmed fall (one-shot: this transition
|
||||
// fires once per fall, the recovery machine owns the duck after).
|
||||
haptics.pulse("fall");
|
||||
syncButtons();
|
||||
}
|
||||
} else {
|
||||
fallDebounce = 0;
|
||||
const now = performance.now();
|
||||
const graceMs = mode === "roll" ? 5000 : 1000;
|
||||
// First frame of a non-recoverable fall (rollers, sit, one-shots):
|
||||
// same haptic thud, once - fallenSince latches until reset/upright.
|
||||
if (fallenSince == null) haptics.pulse("fall");
|
||||
fallenSince ??= now;
|
||||
if (now - fallenSince > graceMs) resetSim();
|
||||
}
|
||||
@@ -949,6 +968,7 @@ async function boot({ scene, camera, renderer }) {
|
||||
// Timed out mid-roll: don't hand a tipped duck to the walking
|
||||
// policy (it has no get-up skill).
|
||||
if (!upright) resetSim();
|
||||
else haptics.pulse("land"); // rolled through and stuck the landing
|
||||
syncButtons();
|
||||
}
|
||||
}
|
||||
@@ -1065,6 +1085,27 @@ async function boot({ scene, camera, renderer }) {
|
||||
return rollersLoading;
|
||||
}
|
||||
|
||||
// Ghost-only roller rig: kinematics + THREE meshes, no physics model and
|
||||
// no ONNX sessions - just enough to render roller-mode PEERS correctly
|
||||
// for a visitor who never leaves legs mode (ensureRollers' full stack
|
||||
// stays lazy). Kept resident once built; if the player later switches
|
||||
// for real, getRigFor prefers the live locos.rollers rig and this one
|
||||
// quietly remains as a clone source.
|
||||
let ghostRollerRig = null;
|
||||
let ghostRollerRigLoading = null;
|
||||
function ensureGhostRollerRig() {
|
||||
if (locos.rollers || ghostRollerRig || rollersLoading) return;
|
||||
ghostRollerRigLoading ??= (async () => {
|
||||
const rk = await loadKinematics(`${MODEL_DIR}/kinematics_rollers.json`);
|
||||
ghostRollerRig = await buildRig(rk, {
|
||||
materialForMesh: materialHookFor(VARIANTS[currentVariant]),
|
||||
});
|
||||
})().catch((e) => {
|
||||
ghostRollerRigLoading = null; // next roller peer retries the load
|
||||
console.warn("[ghosts] roller ghost rig load failed", e);
|
||||
});
|
||||
}
|
||||
|
||||
function activateLoco(name) {
|
||||
const L = locos[name];
|
||||
loco = name;
|
||||
@@ -1878,6 +1919,7 @@ async function boot({ scene, camera, renderer }) {
|
||||
mode = foot === "left" ? "kickL" : "kickR";
|
||||
sitFlag = 0;
|
||||
kickRun = { steps: 0 };
|
||||
haptics.pulse("kick"); // swing launch; ball contact adds ballHit
|
||||
syncButtons();
|
||||
stickers?.pop("kick");
|
||||
return true;
|
||||
@@ -1999,10 +2041,14 @@ async function boot({ scene, camera, renderer }) {
|
||||
// default instead of letting applyVariant throw on a bad key.
|
||||
variantNames: Object.keys(VARIANTS),
|
||||
defaultVariant: DEFAULT_VARIANT,
|
||||
// Ghost rig per locomotion flag: peers in roller mode clone the
|
||||
// roller rig once this tab has built it, and fall back to the leg
|
||||
// rig until then. Known v1 limitation, documented in the README.
|
||||
getRigFor: (l) => (l && locos.rollers ? locos.rollers.rig : locos.legs.rig),
|
||||
// Ghost rig per locomotion flag: roller peers clone the live roller
|
||||
// rig when this tab has it, else the lightweight ghost-only roller
|
||||
// rig. hasRigFor/prepareRigFor let ghosts.js render legs as a
|
||||
// stopgap while lazily loading the real thing, then rebuild.
|
||||
getRigFor: (l) =>
|
||||
(l ? (locos.rollers?.rig ?? ghostRollerRig ?? locos.legs.rig) : locos.legs.rig),
|
||||
hasRigFor: (l) => !l || !!(locos.rollers || ghostRollerRig),
|
||||
prepareRigFor: (l) => { if (l) ensureGhostRollerRig(); },
|
||||
getLocalState: () => {
|
||||
const qpos = data.qpos;
|
||||
const j = new Array(NUM_JOINTS);
|
||||
|
||||
+32
-15
@@ -103,9 +103,15 @@ export async function initGhosts(env) {
|
||||
const knownVariants = env.variantNames ? new Set(env.variantNames) : null;
|
||||
const defaultVariant = env.defaultVariant ?? env.variantNames?.[0];
|
||||
// Locomotion-variant rig source: state.l 1 = rollers, 0/absent = legs
|
||||
// (old clients never send it). Falls back to the leg rig when the local
|
||||
// tab hasn't built the roller rig yet - known v1 limitation.
|
||||
// (old clients never send it). The host may not have the wanted rig yet
|
||||
// (the roller stack is lazy): servedLoco says which flavor a ghost
|
||||
// actually gets right now, and prepareRigFor warms the missing one in
|
||||
// the background so a later packet can rebuild onto the real thing.
|
||||
const rigFor = (l) => (env.getRigFor ? env.getRigFor(l ?? 0) : env.rig);
|
||||
const servedLoco = (l) => {
|
||||
const want = l ?? 0;
|
||||
return env.hasRigFor && !env.hasRigFor(want) ? 0 : want;
|
||||
};
|
||||
// trystero 0.25 (backed by @trystero-p2p): makeAction returns
|
||||
// { send, onMessage, onReceiveProgress } where onMessage is a SETTER -
|
||||
// the receive handler is registered by assignment, not by calling it.
|
||||
@@ -189,7 +195,12 @@ export async function initGhosts(env) {
|
||||
at: Math.max(performance.now(), prevAt + RESPACE_MIN_MS),
|
||||
});
|
||||
const makeGhost = (state) => {
|
||||
const rig = cloneRig(rigFor(state.l));
|
||||
// g.loco records the rig the ghost actually GOT (the served flavor),
|
||||
// not the peer's flag: while the roller rig is still loading the ghost
|
||||
// walks on legs, and the mismatch on later packets drives the rebuild.
|
||||
const served = servedLoco(state.l);
|
||||
if (served !== (state.l ?? 0)) env.prepareRigFor?.(state.l);
|
||||
const rig = cloneRig(rigFor(served));
|
||||
applyVariant(rig, state.v);
|
||||
ghostify(rig);
|
||||
scene.add(rig.placer);
|
||||
@@ -199,7 +210,7 @@ export async function initGhosts(env) {
|
||||
trunk.quaternion.set(state.p[4], state.p[5], state.p[6], state.p[3]);
|
||||
return {
|
||||
rig, trunk, buf: [snapOf(state)],
|
||||
variant: state.v, loco: state.l ?? 0,
|
||||
variant: state.v, loco: served,
|
||||
gapAvg: 1000 / SEND_HZ, // measured arrival cadence, ms
|
||||
delay: INTERP_DELAY_MS, // current playback delay, ms
|
||||
};
|
||||
@@ -278,17 +289,23 @@ export async function initGhosts(env) {
|
||||
g.revealed = true; // latched for the rest of this peer session
|
||||
g.rig.placer.visible = true;
|
||||
}
|
||||
// Peer switched legs <-> rollers: rebuild its ghost on the other rig
|
||||
// (cheap - cloneRig shares geometry). The snapshot buffer carries over
|
||||
// so the interpolated motion stays continuous across the swap.
|
||||
// Peer switched legs <-> rollers, or the rig its mode needs just
|
||||
// finished lazy-loading: rebuild the ghost on the right rig (cheap -
|
||||
// cloneRig shares geometry). The snapshot buffer carries over so the
|
||||
// interpolated motion stays continuous across the swap. While the
|
||||
// wanted rig is still loading, keep warming it and stay on legs.
|
||||
if (state.l !== g.loco) {
|
||||
const { lastSeen, spawn, revealed, buf, gapAvg, delay } = g;
|
||||
removeGhost(peerId);
|
||||
g = makeGhost(state);
|
||||
Object.assign(g, { lastSeen, spawn, revealed, buf, gapAvg, delay });
|
||||
g.rig.placer.visible = revealed;
|
||||
ghosts.set(peerId, g);
|
||||
return;
|
||||
if (servedLoco(state.l) !== state.l) {
|
||||
env.prepareRigFor?.(state.l);
|
||||
} else {
|
||||
const { lastSeen, spawn, revealed, buf, gapAvg, delay } = g;
|
||||
removeGhost(peerId);
|
||||
g = makeGhost(state);
|
||||
Object.assign(g, { lastSeen, spawn, revealed, buf, gapAvg, delay });
|
||||
g.rig.placer.visible = revealed;
|
||||
ghosts.set(peerId, g);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (state.v !== g.variant) {
|
||||
g.variant = state.v;
|
||||
@@ -392,7 +409,7 @@ export async function initGhosts(env) {
|
||||
meshes++; if (o.visible) visible++; op ??= o.material.opacity;
|
||||
});
|
||||
const w = g.trunk.getWorldPosition(g.trunk.position.clone());
|
||||
return { p: g.trunk.position.toArray(), world: w.toArray(), inScene: !!g.rig.placer.parent, revealed: g.revealed, meshes, visible, twins, op, v: g.variant, gapAvg: g.gapAvg, delay: g.delay, bufLen: g.buf.length };
|
||||
return { p: g.trunk.position.toArray(), world: w.toArray(), inScene: !!g.rig.placer.parent, revealed: g.revealed, meshes, visible, twins, op, v: g.variant, l: g.loco, gapAvg: g.gapAvg, delay: g.delay, bufLen: g.buf.length };
|
||||
}),
|
||||
update() {
|
||||
const now = performance.now();
|
||||
|
||||
@@ -0,0 +1,139 @@
|
||||
// Gamepad haptics engine: named one-shot pulses plus a continuous rumble
|
||||
// bed, multiplexed onto the single dual-rumble channel the Gamepad API
|
||||
// exposes per pad (playEffect always REPLACES the running effect - there
|
||||
// is no hardware mixing, so the mixing policy lives here).
|
||||
//
|
||||
// Model (the usual game-feel split):
|
||||
// strongMagnitude = heavy low-frequency motor -> mass, impacts, thuds
|
||||
// weakMagnitude = light high-frequency motor -> texture, ticks
|
||||
//
|
||||
// Pulses are named presets with a priority: while one is playing, a
|
||||
// lower-priority pulse is dropped (a wall bump can never eat the fall
|
||||
// thud) and an equal/higher one takes the channel. `scale` multiplies
|
||||
// both magnitudes so impact-style cues can ride the physics (ball |dv|,
|
||||
// bump strength). Presets may be sequences (multi-tap patterns) - taps
|
||||
// are scheduled with timeouts and cancelled wholesale if another pulse
|
||||
// takes over mid-pattern.
|
||||
//
|
||||
// The bed is a 0..1 level written every control step (roller ground
|
||||
// speed); it keeps the motors alive between pulses by re-issuing a short
|
||||
// overlapping effect, yields the instant a pulse fires, and resumes when
|
||||
// the pulse expires. Everything is fire-and-forget and safe without a
|
||||
// pad or actuator (Safari, some Firefox/driver stacks): silent no-op.
|
||||
|
||||
import { pickPad } from "./controls/gamepad.js";
|
||||
|
||||
// Kill switch, mirroring SOUND_DISABLED in audio.js: flip to true and
|
||||
// every entry point below early-returns.
|
||||
const HAPTICS_DISABLED = false;
|
||||
|
||||
// One-shot presets. d/gap in ms, s/w = strong/weak magnitudes (0..1
|
||||
// before scale), prio decides who wins the single channel.
|
||||
const EFFECTS = {
|
||||
// Confirmed fall (recovery entry or non-recoverable tip-over).
|
||||
fall: { prio: 3, seq: [{ d: 450, s: 1.0, w: 0.7 }] },
|
||||
// Solver explosion -> instant reset: the hardest hit in the game.
|
||||
explode: { prio: 4, seq: [{ d: 550, s: 1.0, w: 1.0 }] },
|
||||
// Barrel roll finished upright: landing thump.
|
||||
land: { prio: 2, seq: [{ d: 170, s: 0.7, w: 0.35 }] },
|
||||
// Kick swing launch (the ball contact rides ballHit on top).
|
||||
kick: { prio: 1, seq: [{ d: 90, s: 0.5, w: 0.2 }] },
|
||||
// Ball impact felt through the body - scaled by the hit's |dv|.
|
||||
ballHit: { prio: 2, seq: [{ d: 120, s: 0.85, w: 0.4 }] },
|
||||
// Trunk bump against a wall or prop - scaled by |dv|.
|
||||
bump: { prio: 1, seq: [{ d: 130, s: 0.55, w: 0.25 }] },
|
||||
// Recovery success (the duck got back up): light double tap.
|
||||
recover: { prio: 2, seq: [{ d: 70, s: 0.3, w: 0.45 }, { gap: 90 }, { d: 100, s: 0.5, w: 0.65 }] },
|
||||
};
|
||||
|
||||
// Bed tuning: texture-forward (weak motor), a whisper of mass underneath.
|
||||
const BED_REFRESH_MS = 240; // re-issue cadence; duration overlaps 2x
|
||||
const BED_STRONG = 0.12;
|
||||
const BED_WEAK = 0.4;
|
||||
const BED_FLOOR = 0.03; // below this the bed is considered off
|
||||
|
||||
let current = { until: 0, prio: 0 }; // the pulse owning the channel
|
||||
let seqGen = 0; // generation token: bumping it cancels pending seq taps
|
||||
let bedLevel = 0;
|
||||
let bedIssuedAt = 0;
|
||||
let bedActive = false;
|
||||
|
||||
const actuator = () => pickPad()?.vibrationActuator ?? null;
|
||||
|
||||
const play = (act, step, scale) => {
|
||||
act
|
||||
.playEffect("dual-rumble", {
|
||||
duration: step.d,
|
||||
strongMagnitude: Math.min(1, step.s * scale),
|
||||
weakMagnitude: Math.min(1, step.w * scale),
|
||||
})
|
||||
.catch(() => {});
|
||||
};
|
||||
|
||||
function pulse(name, scale = 1) {
|
||||
if (HAPTICS_DISABLED) return;
|
||||
const fx = EFFECTS[name];
|
||||
if (!fx) return;
|
||||
const act = actuator();
|
||||
if (!act?.playEffect) return;
|
||||
const now = performance.now();
|
||||
if (now < current.until && fx.prio < current.prio) return;
|
||||
seqGen++; // cancel any in-flight sequence from the previous pulse
|
||||
const gen = seqGen;
|
||||
let at = 0;
|
||||
let total = 0;
|
||||
for (const step of fx.seq) total += (step.gap ?? 0) + (step.d ?? 0);
|
||||
current = { until: now + total, prio: fx.prio };
|
||||
bedActive = false; // the pulse owns the channel; tick() resumes the bed
|
||||
for (const step of fx.seq) {
|
||||
at += step.gap ?? 0;
|
||||
if (!step.d) continue;
|
||||
if (at === 0) {
|
||||
play(act, step, scale);
|
||||
} else {
|
||||
setTimeout(() => {
|
||||
if (gen !== seqGen) return; // a newer pulse took the channel
|
||||
const a = actuator(); // pad may have been yanked meanwhile
|
||||
if (a?.playEffect) play(a, step, scale);
|
||||
}, at);
|
||||
}
|
||||
at += step.d;
|
||||
}
|
||||
}
|
||||
|
||||
// Continuous rumble level (0..1), meant to be written every control step
|
||||
// (it only becomes motion through tick()).
|
||||
function setBed(level) {
|
||||
bedLevel = HAPTICS_DISABLED ? 0 : Math.max(0, Math.min(1, level));
|
||||
}
|
||||
|
||||
// Channel scheduler, called once per control step: re-issues the bed
|
||||
// while it's on and no pulse owns the channel, and cuts the motors when
|
||||
// the bed falls silent (playEffect would otherwise finish the last
|
||||
// overlap on its own, a ~0.5 s tail).
|
||||
function tick() {
|
||||
if (HAPTICS_DISABLED) return;
|
||||
const now = performance.now();
|
||||
if (now < current.until) return;
|
||||
const act = actuator();
|
||||
if (!act) return;
|
||||
if (bedLevel > BED_FLOOR) {
|
||||
if (now - bedIssuedAt >= BED_REFRESH_MS) {
|
||||
bedIssuedAt = now;
|
||||
bedActive = true;
|
||||
act
|
||||
.playEffect?.("dual-rumble", {
|
||||
duration: BED_REFRESH_MS * 2,
|
||||
strongMagnitude: Math.min(1, BED_STRONG * bedLevel),
|
||||
weakMagnitude: Math.min(1, BED_WEAK * bedLevel),
|
||||
})
|
||||
.catch(() => {});
|
||||
}
|
||||
} else if (bedActive) {
|
||||
bedActive = false;
|
||||
bedIssuedAt = 0;
|
||||
try { act.reset?.()?.catch?.(() => {}); } catch { /* older impls throw sync */ }
|
||||
}
|
||||
}
|
||||
|
||||
export const haptics = { pulse, setBed, tick };
|
||||
+1
-3
@@ -403,9 +403,7 @@ function Quickbar() {
|
||||
})}
|
||||
</Box>
|
||||
</HudPlate>
|
||||
{/* Sound is fully disabled for now (SOUND_DISABLED in audio.js);
|
||||
re-enable this toggle together with that flag. */}
|
||||
{/* <SoundToggle /> */}
|
||||
<SoundToggle />
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user