Log inGet started
World

Starfall Garden

Branchmain
Commit554ebef952

README.md

touchTargetsKit

Make 3D objects tappable, double-tappable, holdable, swipeable (slice through them) and draggable on touch screens, with forgiving fingertip-sized hit circles, hit reactions, hold-progress rings and highlights, and one winner per target in multiplayer.

Version 1.0.1 (TT.VERSION). The version, this README and package.yaml move together.

Install

  1. Search ZeroMind for touchTargetsKit and install touchTargetsKit.package, or install it by its guid once published (zeromind_install guid=<touchTargetsKit guid>). It depends on juiceKit (hit particles), which the install brings along.
  2. local TT = require("touchTargetsKit.targets") from any component or module that runs in play.
  3. TT.start() once, then TT.add(entity, { gestures = "tap" }) for each object.

Or with no code: put the TouchTarget component on an entity and set its fields.

Quick start

local TT = require("touchTargetsKit.targets")

TT.start({ look = "neon" })                  -- once per client; safe to call again

-- a small star you tap
TT.add(star, { gestures = "tap", radius = 0.3, kind = "star", points = 10, reaction = "sparkle" })
-- a wisp you slice by swiping through it
TT.add(wisp, { gestures = "swipe", radius = 0.5, kind = "wisp", points = 25, reaction = "shatter" })
-- a seed pod you hold for a second
TT.add(pod, { gestures = "hold", holdSeconds = 1.0, radius = 0.6, kind = "pod", holdRing = "petals", reaction = "pop" })
-- a crate you drag, which stays (consume = false)
TT.add(crate, { gestures = "drag", radius = 0.5, consume = false })

TT.onHit(function(ev)                         -- every peer, once per hit
    if ev.isLocal then score += ev.points end
end)
TT.onMiss(function(ev)                        -- this client only
    if ev.reason == "wrongGesture" then showHint(ev.entity, ev.gesture) end
end)
TT.onDrag(function(ev)                        -- this client only
    if ev.phase == "move" then crate.position = { ev.position.x, ev.position.y, ev.position.z } end
end)

require("touchTargetsKit.gallery").spawn() in play spawns a row of every reaction (tap them) and every hold ring (hold them) in front of the camera.

How touches are recognised

  • Each client recognises its own fingers against every target's screen position (worldToScreen, scene viewport physical pixels, the space of Zin.touch.slots()). The mouse is one more finger while no touch is down.
  • A target's hit circle is its projected radius times 1.15, but never smaller than minRadius logical points (default 44, times ui.pixelRatio()), so a tiny, far or fast object is still easy to hit. Overlapping circles go to the one the finger is most central in.
  • tap: lift within the slop (22 pt). A long press on a tap target still counts (forgiving). doubleTap: two taps within 0.32 s; a target that takes both tap and doubleTap waits out the window before a single tap. hold: keep the finger on it for holdSeconds; the finger may wobble up to 1.4 hit radii; the hold ring shows the progress. swipe: a finger moving faster than 380 pt/s slices every swipe target whose circle the segment between two consecutive samples crosses, during the move, so a fast swipe never skips one. drag: the finger grabs it once it leaves the slop; onDrag reports begin/move/end with the finger's point on a plane (dragPlane = "view", facing the camera through the object, or "ground", horizontal) and position, where the object goes keeping the grab offset. any: tap, doubleTap (as a tap), hold and swipe all hit it.
  • A tap on a target that needs another gesture, or a swipe that crosses only non-swipe targets, is a miss with reason = "wrongGesture" and the entity; a gesture over nothing is reason = "empty".
  • Each target's circle is stated as a touch region the kit holds (input.setTouchClaims), so a finger that lands on a target belongs to the game even inside the on-screen look area. Contacts on an on-screen button are left alone. TT.config.claimTouches = false turns this off.

Styles

Every style is one setting per target (or per TT.add call); look is global.

SettingValues
reaction (on hit)pop, shatter, squash, spinAway, fade, sparkle, none
holdRing (hold progress)ring, fill, petals, segments, dots
highlight (finger over it)glow, outline, grow, none
look (TT.setLook)cartoon, minimal, neon, pixel, pastel

Rings, highlights and shatter shards are drawn by four pooled instanced draws (no entity per indicator, at most 256 instances each, 48 targets lit at once); reactions animate the entity's transform and play a juiceKit effect in the look's matching juice look. On a mid-range tablet the kit's frame costs a few hundred table writes and four buffer uploads.

API

TT.start(opts?)          -- idempotent; opts.look, opts.minRadius (logical points, default 44), opts.enabled
TT.stop()                -- stop recognising on this client; indicators clear, targets stay registered
TT.add(entity, opts) -> id
    -- opts: gestures = "tap" | { "tap", "swipe", ... }, radius = 1.0 (world units), holdSeconds = 0.7,
    --       kind = "target", points = 0, reaction = "pop", holdRing = "ring", highlight = "glow",
    --       consume = true, data = {}, dragPlane = "view" | "ground", despawn = true,
    --       positionOf = function() return {x,y,z} end  -- local: where the object is drawn
    -- calling add again on the same entity updates it (one target per entity);
    -- a target whose entity is despawned is dropped automatically
TT.remove(entityOrId)
TT.onHit(fn, opts?)      -- every peer: { entity, id, kind, gesture, player, peer, isLocal, points, data, screenX, screenY, world = {x,y,z} }
                         -- player = the winner's user id, peer = the winner's peer id (two devices on one account differ by peer)
                         -- opts.key: registering again under the same key replaces the earlier listener (also onMiss, onHoldProgress, onDrag)
TT.onMiss(fn)            -- local: { gesture, screenX, screenY, reason = "empty" | "wrongGesture", entity?, id?, kind?, early? }
TT.onHoldProgress(fn)    -- local: { entity, id, progress } (0 when the finger lets go early)
TT.onDrag(fn)            -- local: { entity, id, phase = "begin" | "move" | "end", world, position, screenX, screenY }
TT.setLook(name)  TT.getLook()  TT.looks()  TT.reactions()  TT.holdRings()  TT.highlights()  TT.gestures()
TT.setEnabled(bool)  TT.isEnabled()  TT.isStarted()
TT.get(entityOrId)       -- the target's settings and live screen circle
TT.list()  TT.stats()  TT.config  TT.VERSION

Every on* returns a function that unsubscribes. id is the entity's id. screenX/screenY in a hit are where the winning player's finger was, on their screen. player is the winner's user id. Drag never produces a hit; the game moves the object (from the peer that owns it).

Multiplayer rules

  • Claims: the client that saw the gesture sends a claim on multiplayer.send("touchTargetsKit.claim", ...). The authority for a target is its owner for a synced entity (entity:owner(), isLocal()), else the host (multiplayer.isHost()), which also covers scene entities every peer builds its own copy of.
  • One winner: the authority accepts the first claim; a consumed target is dead from then on and later claims are refused; a target that stays (consume = false) refuses claims for 0.12 s after a hit. It broadcasts the hit on touchTargetsKit.hit with a unique hit id and applies it locally, so onHit fires once on every peer, the winner's included (isLocal = true there only).
  • Reactions play on every peer; the transform animation runs on the peer that may write the entity (its owner, or every peer for an unsynced copy) and replicates from there. With consume = true that peer despawns the entity when the reaction ends (despawn = false hides it instead), and every other peer hides its view.
  • Registration: TT.add on a synced entity this peer owns tells every other peer (touchTargetsKit.reg), and a client that starts later asks the owners (touchTargetsKit.ask), so the host can spawn synced targets and register them once. Calling TT.add on every peer (the TouchTarget component does) works the same.
  • A synced entity that stands still while a local child moves: call TT.add(syncedEntity, { positionOf = function() return bodyPos end, ... }) on every peer. Projection, highlights, hold rings, the hit's world and the reaction effects use the callback; claims and ids stay keyed by the synced entity, and a consumed one is despawned by its owner. A registration arriving from another peer never clears a callback this peer set.
  • Single player needs nothing: with no relay the local client is the authority.
  • Indicators, highlights, misses, hold progress and drags are local to the player whose finger it is.

Parts

  • targets.module: the API above, recognition, claims and drawing of indicators.
  • TouchTarget.component: the no-code way; its fields are the TT.add settings.
  • gallery.module: spawn(opts) / clear(), every reaction and hold ring side by side.
  • reactions.module, overlay.module, styles.module, state.module: the reactions, the pooled instanced draws, the style tables, and the state that survives a hot reload.
  • Guides: How to make 3D objects tappable, swipeable and holdable on touch screens (touchTargetsKit.package/how-to-make-3d-objects-tappable-swipeable-and-holdable-on-touch-screens) and Example: build a tap-and-slice touch arcade game (touchTargetsKit.package/example-build-a-tap-and-slice-touch-arcade-game).

Listeners and play sessions

Listeners live in state that outlasts a hot reload and a trip out of play and back. Each listener is stamped with the play session it was registered in, and one left over from an earlier session is dropped the next time an event fires, so a listener a game forgot to unsubscribe can never count a hit twice. A game that registers its listeners again every round should pass { key = "..." } so the new one replaces the old.

Mouse

The left mouse button is one more finger (ignored for mouseAfterTouch seconds after a touch). A mouse sweeps slower than a flicking finger, so a mouse drag slices at TT.config.mouseSwipeSpeed (200 points per second) instead of swipeSpeed (380).

Changelog

  • 1.0.1: hits carry peer (the winner's peer id); onHit / onMiss / onHoldProgress / onDrag take { key } to replace an earlier listener; listeners from an earlier play session are dropped instead of firing again; a mouse drag slices at its own, gentler speed (mouseSwipeSpeed).
  • 1.0.0: first release.

Known limits

  • A hit arrives after the round trip to the authority (no optimistic reaction on the claimer); the target stops taking claims from that finger while it waits.
  • A contact an on-screen control already holds (a button, the movement stick) is not a target finger; a swipe that starts inside the look area and never crosses a target's circle on its first frame can be taken by the look area.
  • grow scales the entity only on a peer that may write it; elsewhere it shows as a soft halo.