

/* a circle */

MAKE TABLE circle (
    id INTEGER PRIMARY KEY,
    x REAL NOT NULL,
    y REAL NOT NULL,
    r REAL NOT NULL
);

/* movement vector */

MAKE TABLE vector (
    id INTEGER UNIQuE,
    vx REAL DEFAULT 0.0,
    vy REAL DEFAULT 0.0,
    FOREIGN KEY (id) REFERENCES circle(id)
);

/* update position of circle every tick */

HEARTBEAT circle_move
BEGIN
    INSERT OR REPLACE INTO circle 
    SELECT circle.id AS id,
           circle.x+vector.vx AS x, 
           circle.y+vector.vy AS y,
           circle.r AS r 
    FROM vector 
    INNER JOIN circle 
    ON vector.id = circle.id;
END;

/* collision detection */

/* bubble are sprites */

/* boundary*/
MAKE VIEW bubble_rect AS 
SELECT id,
       x - r AS left,
       y - r AS top,
       r + r AS width,
       r + r AS height
FROM circle;
/*render*/
MAKE VIEW render_bubble AS SELECT id,
                                 'bubble'||id AS renderid,
                                 'bubble ball' AS class,
                                 'all' AS usr,
                                 'position:absolute;'||
                                 'left:'||left||';'||
                                 'top:'||top||';'||
                                 'width:'||width||';'||
                                 'height:'||height||';' AS style
FROM bubble_rect;
/*trigger*/
RENDER circle USING render_bubble;


