球与球之间的弹性碰撞:球以不相等的比例弹跳

时间:2014-11-19 10:48:19

标签: javascript html5 html5-canvas collision-detection

我一直致力于一场需要控球与球碰撞的比赛,而我似乎无法找到解决这个问题的方法,我几乎得到了碰撞,因为球正朝着正确的方向前进弄乱了速度。

一旦球相互撞击,水平(x)反弹非常坚硬,而垂直(y)非常轻柔地反弹。我不知道是什么原因或原因,我试图通过除法来降低x速度,但这意味着它不会与y值成比例。

这是我的碰撞代码:

function resolveCollision(b1, b2){
    var dot = function(v1, v2) {
        return v1.x * v2.x + v1.y * v2.y;
    };
    v1x = b1.vx/2
    v2x = b2.vx/2
    v1y = b1.vy
    v2y = b2.vy
    var length = function(v) {
        return Math.sqrt(v.x * v.x + v.y * v.y);
    };

    var delta = {x: b1.x - b2.x, 
                 y: b1.y - b2.y};
    var d = Math.sqrt((delta.x * delta.x) + (delta.y * delta.y));
    var dn = {x: delta.x / d,
              y: delta.y / d}

    var dt = {x: dn.y,
              y: -dn.x};
    if (d === 0){
        b2.x += 0.01;    
    }
    var m1 = b1.m;
    var m2 = b2.m;
    var M = m1 + m2;

    var mt = {x: dn.x * (b1.r + b2.r - d),
              y: dn.y * (b1.r + b2.r - d)};

    b1.x = b1.x + mt.x * m2 / M
    b1.y = b1.y + mt.y * m2 / M
    b2.x = b2.x + mt.x * m1 / M
    b2.y = b2.y + mt.y * m1 / M

    var v1 = {x: v1x, y: v1y};
    var v2 = {x: v2x, y: v2y};

    var v1n = {
        x: dn.x * (v1.x * v1.x + dn.y * dn.y),
        y: dn.y * (v1.x * v1.x + dn.y * dn.y)
    };

    var v1t = {
        x: dt.x * (v1.x * v1.x + dt.y * dt.y),
        y: dt.y * (v1.x * v1.x + dt.y * dt.y)
    };

    var v2n = {
        x: dn.x * (v2.x * v2.x + dn.y * dn.y),
        y: dn.y * (v2.x * v2.x + dn.y * dn.y)
    };

    var v2t = {
        x: dt.x * (v2.x * v2.x + dt.y * dt.y),
        y: dt.y * (v2.x * v2.x + dt.y * dt.y)
    };

    b1.velocity = {
        x: v1t.x + dn.x * ((m1 - m2) / M * length(v1n) + 2 * m2 / M * length(v2n)),
        y: v1t.y + dn.y * ((m1 - m2) / M * length(v1n) + 2 * m2 / M * length(v2n))
    };
    b2.velocity = {
        x: v2t.x - dn.x * ((m2 - m1) / M * length(v2n) + 2 * m1 / M * length(v1n)),
        y: v2t.y - dn.y * ((m2 - m1) / M * length(v2n) + 2 * m1 / M * length(v1n))
    };
    b1.vx = b1.velocity.x;
    b1.vy = b1.velocity.y;
    b2.vx = b2.velocity.x;
    b2.vy = b2.velocity.y;
}    

此外,如果有人能告诉我如何让一个球员(圈子)进一步推球,而不会反弹,这将是伟大的。

这是文件,如果有人想看看,一切都在app.js: https://www.mediafire.com/?d6dpqn1cmwzp756

对于那些无法理解我想要实现的目标的人来说,这就是我想要复制的内容:http://www.haxball.com

1 个答案:

答案 0 :(得分:1)

1)你应该澄清你的代码,因为它有点复杂,这本身就是一个bug来源。
2)你正在用v1n / t v2n / t做一些奇怪的事情:投射+在X上调整速度...... ???我甚至没有尝试理解b1.velocity代码。哎哟! :-)

所以这里的代码有一些简化:它未经测试,但希望能帮助您解决问题:

function resolveCollision(b1, b2) {
    var v1x = b1.vx,
        v2x = b2.vx,
        v1y = b1.vy,
        v2y = b2.vy;

    // Collision vector
    var delta = {
        x: b2.x - b1.x,
        y: b2.y - b1.y
    };
    var d = length(delta);

    // Normalized collision vector
    var dn = {
        x: delta.x / d,
        y: delta.y / d
    }

    // Normalized tangent collision vector
    var dt = {
        x: dn.y,
        y: -dn.x
    };

    var m1 = b1.m;
    var m2 = b2.m;
    var M = m1 + m2;

    /*    
    // test those separation tricks in a second time
    if (d === 0) {
        b2.x += 0.01;
    }
    var mt = {
        x: dn.x * (b1.r + b2.r - d),
        y: dn.y * (b1.r + b2.r - d)
    };    
    b1.x = b1.x + mt.x * m2 / M
    b1.y = b1.y + mt.y * m2 / M
    b2.x = b2.x + mt.x * m1 / M
    b2.y = b2.y + mt.y * m1 / M
    */

    // projection of v1 on the collision vector
    var v1Proj = {
        n: dn.x * v1x + dn.y * v1y,
        t: dt.x * v1x + dt.y * v1y
    };

    // projection of v2 on the collision vector
    var v2Proj = {
        n: dn.x * v2x + dn.y * v1y,
        t: dt.y * v2x + dt.y * v2y
    };

    // solving collision on the normal
    var newV1ProjN = ((m1 - m2) * v1Proj.n + 2 * m2 * v2Proj.n) / M;
    var newV2ProjN = ((m2 - m1)* v2Proj.n + 2 * m1 * v1Proj.n) / M;

    // re-building speed vector out of projected vectors
    b1.vx = newV1ProjN * dn.x + v1Proj.t * dt.x;
    b1.vy = newV1ProjN * dn.y + v1Proj.t * dt.y;

    b2.vx = newV2ProjN * dn.x + v2Proj.t * dt.x;
    b2.vy = newV2ProjN * dn.y + v2Proj.t * dt.y;
}

function dot(v1, v2) {
    return v1.x * v2.x + v1.y * v2.y;
};

function length(v) {
    return Math.sqrt(v.x * v.x + v.y * v.y);
};