examples\control\Box2D\worlds\mobile.ck
if(me.args() == 0)
{
    <<<"This script is intended for use by Box2D. Bailing...", me.id()>>>;
    1::second => now;
    me.exit();
}
// we use the first argument to find our Box2D context.
me.arg(0).toInt() => int boxId;
Box2D.Boxes[boxId] @=> Box2D boxO;
boxO.dbBox2D @=> DbBox2D box2D; // handle onto chugin

3 => int maxDepth; // at 4, we get chaos, so we reduce gravity
2 => float a;
#(0,0) => complex zero;
1 => int doJoint; // 0: debugging
box2D.dynamicType => int bodyType; // staticType: debugging
(maxDepth <= 3) ? 6 : 10 => int offset;

box2D.worldBegin(#(0, -10));  // gravity points down
box2D.newPoint(#(0, 20.)) => int ground;
newNode(ground, zero, 0, offset, a) => int root; // recursively contruct mobile

if(doJoint)
{
    box2D.newRevoluteJoint(ground, root, 
        zero,  // local anchor point relative to ground
        #(0, a), // local anchor point relative to root
        0, /*refAngle*/
        0, /*motorSpeed*/
        0. /*maxTorque*/);
}

fun int newNode(int parent, complex localAnchor, 
                int depth, float offset, float a)
{
    10 => float density;
    #(0.0, a) => complex h;

    box2D.getPosition(parent) => complex ppos;
    ppos + localAnchor => complex mypos;
    box2D.newRectangle(mypos, #(a, .25*a), 0, density, bodyType) => int body;
    // <<<pos>>>;
    if(depth == maxDepth)
    {
        return body;
    }

    #(offset, -a) => complex a1; // right
    #(-offset, -a) => complex a2; // left

    newNode(body, a1, depth + 1, 0.5 * offset, a) => int body1;
    newNode(body, a2, depth + 1, 0.5 * offset, a) => int body2;

    if(doJoint)
    {
        box2D.newRevoluteJoint(body, body1, a1, h,
            0, /*refAngle*/
            0, /*motorSpeed*/
            0. /*maxTorque*/);

        box2D.newRevoluteJoint(body, body2, a2, h,
            0, /*refAngle*/
            0, /*motorSpeed*/
            0. /*maxTorque*/);
    }

    return body;
}
home .. topics .. interface .. reference .. examples .. tipjar