examples\control\Box2D\worlds\tumbler.ck
if(me.args() == 0)
{
    <<<"This script is intended for use by Box2D. Bailing...", me.id()>>>;
    1::second => now;
    me.exit();
}

fun void addShape(DbBox2D b, float x, float y, float sz)
{
    int id;
    Math.random2f(.1, .8) *=> sz;
    if(Math.randomf() > .5)
    {
        b.newRectangle(#(x, y), #(sz, sz),
            Math.random2f(0, pi), 1., b.dynamicType) => id;
    }
    else
    {
        .5 *=> sz;
        b.newCircle(#(x, y), sz, 1, b.dynamicType) => id;
    }
    b.setRestitution(id, Math.random2f(.5, 1.0));
}

fun void buildWorld(DbBox2D b)
{
    #(0, 50) => complex center;
    #(50, 50) => complex size;
    #(0, 0) => complex org;
    b.worldBegin(#(0, -10)); // gravity is down
    b.newPoint(org) => int groundId;
    b.newRoom(center, size, 5.0, b.dynamicType) => int roomId; // xxx: allowSleep(false)

    b.newRevoluteJoint(groundId, roomId, center, org,
        0, /*refAngle*/
        .2 * pi, /*motorSpeed*/
        10000000. /*maxTorque*/) => int motorId;

    .5*size.re - 10 => float xmax;
    .5*size.im - 10 => float ymax;
    3 => float dd;
    -ymax => float y;
    while(y < ymax)
    {
        -xmax => float x;
        while(x < xmax)
        {
            addShape(b, center.re + x, center.im + y, dd);
            dd +=> x;
        }
        dd +=> y;
    }
    b.worldEnd();
}


// we use the first argument to find our Shredder
me.arg(0).toInt() => int boxId;
Box2D.Boxes[boxId] @=> Box2D boxO;
boxO.dbBox2D @=> DbBox2D box2D; // handle onto chugin

buildWorld(box2D);

<<<"tumbler created",  box2D.getNumBodies(), "bodies">>>;
home .. topics .. interface .. reference .. examples .. tipjar