sbiermanlytle / iioEngine

iio Engine: A JavaScript game engine for HTML5 Canvas
http://iioengine.com
455 stars 81 forks source link

Add acceleration (setAcc and .acc) #5

Closed tyilo closed 11 years ago

tyilo commented 11 years ago

Added acceleration support for kinematics.

Example code:

var Example = function(io) {
    var dir = 1;

    var box = io.addObj(new iio.ioRect(500, 25, 10, 10)
        .setFillStyle('blue')
        .enableKinematics()
        .setAcc(0.1, 0)
        .setBound('right', 550, function(box) {
            if(dir == 1) {
                dir = -1;
                box.setAcc(-0.1, 0);
            }
            return true;
        })
        .setBound('left', 500, function(box) {
            if(dir == -1) {
                dir = 1;
                box.setAcc(0.1, 0);
            }
            return true;
        }));

    io.setFramerate(60);
};
iio.start(Example);
tyilo commented 11 years ago

Another example:

var Test = function(io) {
    var r = 25;
    var a = 0.1;
    var s = 50;
    var s0;
    //var v = 0;

    var ball;

    function onBottom(ball) {
        ball.vel.y *= -0.9;

        if(ball.pos.y > s0) {
            ball.pos.y = s0;
        }
        if(-ball.vel.y < ball.acc.y) {
            ball.acc.y = -ball.vel.y;
        }

        return true;
    }

    function init() {
        s0 = io.canvas.height - r;
        //var v0 = Math.sqrt(v * v - 2 * a * (s - s0));

        ball.setBound('bottom', s0 + r, onBottom);
        ball.acc.y = a;
    }

    this.onResize = init;

    ball = io.addObj(new iio.ioCircle(io.canvas.center.x, s, r)
        .setFillStyle('blue')
        .enableKinematics()
        .setAcc(0, a));

    init();

    io.setFramerate(60);
};

iio.start(Test);
sbiermanlytle commented 11 years ago

Very cool, thanks for the demos. I have merged this feature into the Git distribution, but I did it by hand because I wanted to modify some of the statements and I also just reorganized the structure of the git repo and didn't want to deal with all the deletions.

I'll put up some demos and docs about this feature soon. Thanks!