Kimtato, a simple physics playground

Kimtato is the name of a simple interactive demo that runs on the Ubanita platform. It is a playground of physical objects which are created by players using touch gestures on the controller. Once created, the objects can be pushed around using the two-finger gesture, we call edge input mode.

The following will discuss the making of this demo using the Ubanita Script API.

get dimensions

To position objects on the 2D scene, it can be useful to known the actual dimensions of that scene. The scene has two properties for the width (pixels) and height respectively. Using this snippet, we assign those property values to global variables W and H.

var W = UB.scene.get("viewport.width");
var H = UB.scene.get("viewport.height");


To prevent the physical moving objects from leaving the scene, we create a fence around the border of the scene. The border is created using 4 LineSegment sprites. Each LineSegment will share the same physical properties (props). A small margin is added such that the border is a bit smaller. Notice that the LineSegment does not have visual properties; it will be present but not visible.

var margin = 2;
var props = {
    "thickness": 1,
    "elasticity": 1,
    "friction": 1

function addBorder() {
    var top = UB.scene.addLineSegment(0, {
            "ax": margin,
            "ay": margin,
            "bx": W - margin,
            "by": margin

let the players join

When players join the game, their controller must be setup with the correct input mode. For the Kimtato demo, the controller will accept draw and edge input. The draw input will recognize gestures using a single touch movement. The edge input is activated by two touches at the same time; an edge will be drawn.

function handlePlayerJoined(event) {
    var id =;
    var ctr = UB.controller(id)
    // a player can draw shapes and push them around
}"playerJoined", handlePlayerJoined);

translate the gestures

By touching on the controller, the player can produces different gestures. As soon as a gesture is recognized, an event will be triggered. The code below registers a function to handle such events. Inside the function, the is inspected for known gestures.

function handlePlayerGesture(event) {
    var x = ( + / 2;
    var y = ( + / 2;
    var w = ( -;
    var h = ( -;
    if ("rectangle" == {
    } else if ("circle" == {
    } else if ("triangle" == {
    } else if ("zig-zag" == {
}"gesture", handlePlayerGesture);

triangle gesture

One of the recognized gestures is the triangle. Use the x and y location of the gesture to add a Triangle sprite. The physical properties are defined by the variable physics. See the full source for its definition.

function addTriangle(event,x,y,w,h) {
    UB.scene.addTriangle(0, {
        "fill": randomColor(),
        "stroke": randomColor(),
        "lineWidth": 2,
        "x": x,
        "y": y,
        "radius": (Math.min(w, h) / 2) * scaleFactor
    }, physics);


It is a good practice to keep different functionality in separate files. This will help you organize the logic of bigger games and allow for better collaboration with other team members. Using the import function, all scripts are loaded upon startup.


Get the full source code

comments powered by Disqus