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");

border

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() {
    //top
    var top = UB.scene.addLineSegment(0, {
            "ax": margin,
            "ay": margin,
            "bx": W - margin,
            "by": margin
        },
        props);
    //right 
    //bottom
    //left
}
addBorder();

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 = event.data.playerID;
    var ctr = UB.controller(id)
	
    // a player can draw shapes and push them around
    ctr.enableInput("draw");
    ctr.enableInput("edge");
	
    UB.player(id).set("edge.touch.fill",randomColor())
    UB.player(id).enableOutput("edge");
}

UB.game.on("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 event.data.type is inspected for known gestures.

function handlePlayerGesture(event) {
    console.log(event);
    var x = (event.data.left + event.data.right) / 2;
    var y = (event.data.top + event.data.bottom) / 2;
    var w = (event.data.right - event.data.left);
    var h = (event.data.bottom - event.data.top);
    if ("rectangle" == event.data.type) {
        addRectangle(event,x,y,w,h);
    } else if ("circle" == event.data.type) {
        addCircle(event,x,y,w,h)
    } else if ("triangle" == event.data.type) {
        addTriangle(event,x,y,w,h)
    } else if ("zig-zag" == event.data.type) {
        UB.scene.removeAllSprites(0);
        addBorder();
    }
}

UB.game.on("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);
}

main.js

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.

UB.utils.import("randomColor.js");
UB.utils.import("border.js");
UB.utils.import("join.js");
UB.utils.import("gestures.js");

Get the full source code

comments powered by Disqus