Hands and touch
Most Fabi Play games are played with the hands: catch, pop, touch, push. The engine gives you every child’s hands, already in stage units and already in the right place, and tests what they touch.
engine.hands()
Section titled “engine.hands()”for (const hand of engine.hands()) { // hand.player: 1, 2, …; hand.side: 'left' or 'right'; hand.x, hand.y: stage units; hand.color: the player's colour}const mine = engine.hands(2) // only player 2'sengine.hands(player?) gives this frame’s hands (everyone’s, or one player’s) as Hand objects:
| Field | What it is |
|---|---|
player |
The player, from 1 |
side |
'left' or 'right': the child’s own left or right |
x, y |
Where the hand is on the stage, in stage units |
color |
The player’s colour, as the TV draws them: player 1 orange, 2 blue, 3 green, 4 lilac (playerColor(player)) |
Where the hands are. A hand is the child’s wrist, mapped to the screen with the SDK’s toScreen (see
Mirror mode, backdrop and view):
- over your own background, from the smoothed body the avatar is drawn with, so it sits in the avatar’s hand;
- over the camera video, from the frame’s own body, so it sits on the real hand.
A game cannot get this wrong: it is what the children see. A wrist the camera doesn’t see well enough (visibility below 0.5) has no hand, and a child holding Fabi Play’s crossed-arms gesture has none either (see Reserved to Fabi Play).
The hands are computed once per frame, so calling engine.hands() many times is cheap. They come from the newest
frame: a player missing from it (out of the camera’s view) has no hands, and when no frame at all has come for a
second there are none.
engine.touched()
Section titled “engine.touched()”const hand = engine.touched(star, undefined, { padding: 12 })if (hand) catchStar(star, hand.player)engine.touched(node, player?, { padding }) is the first hand touching the node (anyone’s, or one player’s), or
null.
- The test uses the node’s rectangle (its
width×heightaround its anchor), with its rotation and scale and those of its groups. SethitRadiuson round things to test a circle instead. paddinggrows the area by that many stage units: be generous, children’s arms are not mice. 10 to 20 units is a good start; more for the youngest.- It tests the node whether it is visible or not: skip hidden nodes yourself.
const balloon = stage.add(engine.shape({ type: 'circle', r: 50, fill: '#EE6A43' }, { x: 400, y: 300, hitRadius: 50 }))engine.hits()
Section titled “engine.hits()”engine.hits(node, x, y, padding?) says whether a point, in stage units, is on the node: for your own tests, with a
point that is not a hand (a falling object against a basket, a projectile).
if (engine.hits(basket, apple.x, apple.y, 8)) score++Showing the hands
Section titled “Showing the hands”engine.handMarkers({ radius?, look? }) is a node that draws a ring in the player’s colour on every hand (radius 36
by default). Add it last, so it is on top:
stage.add(engine.handMarkers())Over your own background the avatars’ hands are already there, so rings are optional; they help when the game draws no avatars, or with small targets.
Over the video a plain ring is hard to see against a bright room. With look: 'auto' (the default) the rings
switch by themselves with the backdrop: over the video each gets a dark outline and a soft
dark halo under it, over your own background the plain look. look: 'video' or look: 'sky' keeps one of them.
More than hands
Section titled “More than hands”engine.players() gives the live player frames, with each child’s whole body (33 points, see
The body). To put something on another body point, map it like the hands are
mapped:
for (const p of engine.players()) { const nose = play.toScreen({ x: p.body[0 * 3]!, y: p.body[0 * 3 + 1]! }) hat.set({ x: nose.x * stage.width, y: nose.y * stage.height - 60 })}Over your own background, use the smoothed body (play.smoothedBody(p)) to stay on the avatar, as the hands do.
For movements (jumps, claps, lanes, shapes) listen to the SDK’s events: play.on('gesture', …) or
FabiPlay.on('jump', …) (see Events and bodies).