Javascript碰撞检测无法正常工作

3
由于某种原因,我的代码无法正常工作。它应该是一款碰撞模拟游戏,但球只是粘在一起,我似乎找不到原因。我一直在使用来自https://en.wikipedia.org/wiki/Elastic_collision#Two-dimensional_collision_with_two_moving_objects的方程式,据我所见,我已经完美地复制了它,甚至添加了括号以确保操作顺序正确,但仍然没有运气。
这是我的代码:

var canvas = document.getElementById("canvas"),
    ctx = canvas.getContext("2d");

function Ball(){
    this.pos = {
        "x": Math.random() * canvas.width,
        "y": Math.random() * canvas.height
    };
    this.vel = {
        "x": Math.random() - 0.5,
        "y": Math.random() - 0.5
    };
    this.r = 16;
    this.colliding = false;
    this.m = 1;
}

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

function dir(v){
    return Math.atan2(v.y, v.x);
}

function dist(a, b){
    var dx = b.x - a.x,
        dy = b.y - a.y;
    return Math.sqrt(dx * dx + dy * dy);
}

var balls = [];
for(var i = 0; i < 10; i++){
    balls.push(new Ball());
}

setInterval(function(){
    for(var i = 0; i < balls.length; i++){
        balls[i].pos.x += balls[i].vel.x;
        balls[i].pos.y += balls[i].vel.y;

        if(balls[i].pos.x + balls[i].r > canvas.width){
            balls[i].pos.x = canvas.width - balls[i].r;
            balls[i].vel.x *= -1;
        }
        if(balls[i].pos.x < balls[i].r){
            balls[i].pos.x = balls[i].r;
            balls[i].vel.x *= -1;
        }
        if(balls[i].pos.y + balls[i].r > canvas.height){
            balls[i].pos.y = canvas.height - balls[i].r;
            balls[i].vel.y *= -1;
        }
        if(balls[i].pos.y < balls[i].r){
            balls[i].pos.y = balls[i].r;
            balls[i].vel.y *= -1;
        }

        balls[i].colliding = false;
    }

    for(var i = 0; i < balls.length; i++){
        for(var j = i + 1; j < balls.length; j++){
            if(mag(balls[i].vel) < 0){
                break;
            }

            if(dist(balls[i].pos, balls[j].pos) < balls[i].r + balls[j].r){
                balls[i].colliding = true;

                var contact = Math.atan2(balls[j].pos.y - balls[i].pos.y, balls[j].pos.x - balls[i].pos.x);

                balls[i].vel.x = ((((mag(balls[i].vel) * Math.cos(dir(balls[i].vel) - contact) * (balls[i].m - balls[j].m)) + (2 * balls[j].m * mag(balls[j].vel) * Math.cos(dir(balls[j].vel) - contact))) / (balls[i].m + balls[j].m)) * Math.cos(contact)) + (mag(balls[i].vel) * Math.sin(dir(balls[i].vel) - contact) * Math.cos(contact + (Math.PI / 2)));
                balls[i].vel.y = ((((mag(balls[i].vel) * Math.cos(dir(balls[i].vel) - contact) * (balls[i].m - balls[j].m)) + (2 * balls[j].m * mag(balls[j].vel) * Math.cos(dir(balls[j].vel) - contact))) / (balls[i].m + balls[j].m)) * Math.sin(contact)) + (mag(balls[i].vel) * Math.sin(dir(balls[i].vel) - contact) * Math.sin(contact + (Math.PI / 2)));

                balls[j].vel.x = ((((mag(balls[j].vel) * Math.cos(dir(balls[j].vel) - contact) * (balls[j].m - balls[i].m)) + (2 * balls[i].m * mag(balls[i].vel) * Math.cos(dir(balls[i].vel) - contact))) / (balls[j].m + balls[i].m)) * Math.cos(contact)) + (mag(balls[j].vel) * Math.sin(dir(balls[j].vel) - contact) * Math.cos(contact + (Math.PI / 2)));
                balls[j].vel.y = ((((mag(balls[j].vel) * Math.cos(dir(balls[j].vel) - contact) * (balls[j].m - balls[i].m)) + (2 * balls[i].m * mag(balls[i].vel) * Math.cos(dir(balls[i].vel) - contact))) / (balls[j].m + balls[i].m)) * Math.sin(contact)) + (mag(balls[j].vel) * Math.sin(dir(balls[j].vel) - contact) * Math.sin(contact + (Math.PI / 2)));
            }
        }
    }

    ctx.clearRect(0, 0, canvas.width, canvas.height);
    for(var i = 0; i < balls.length; i++){
        ctx.beginPath();

        if(balls[i].colliding){
            ctx.fillStyle = "#f00";
        }else{
            ctx.fillStyle = "#0f0";
        }


        ctx.arc(balls[i].pos.x, balls[i].pos.y, balls[i].r, 0, 2 * Math.PI);
        ctx.fill();
    }
}, 16);
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <canvas id="canvas" width="640" height="480"></canvas>
        <script src="main.js"></script>
    </body>
</html>


@T.J.Crowder 我已经尝试使用 Math.sqrt,但没有任何区别。 - dangee1705
2
@T.J.Crowder添加了代码片段(顺便说一句,谢谢,我不知道我可以这样做:)) - dangee1705
1个回答

1
首先,在球 j 的碰撞中,我已经反转了新的 velX 和 velY,你的碰撞系统似乎运行良好。但是如果你仔细观察,你会发现它们会像你所提到的那样被卡住,这是因为球可以在每个tick内移动超过一个像素,导致球彼此穿过,并且碰撞将一直尝试,直到它们不再重叠。为了防止这种情况发生,如果距离小于两个半径的组合,则需要调整velX和velY。

var canvas = document.getElementById("canvas"),
    ctx = canvas.getContext("2d");

function Ball(){
    this.pos = {
        "x": Math.random() * canvas.width,
        "y": Math.random() * canvas.height
    };
    this.vel = {
        "x": Math.random() - 0.5,
        "y": Math.random() - 0.5
    };
    this.r = 16;
    this.colliding = false;
    this.m = 1;
}

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

function dir(v){
    return Math.atan2(v.y, v.x);
}

function dist(a, b){
    var dx = b.x - a.x,
        dy = b.y - a.y;
    return Math.sqrt(dx * dx + dy * dy);
}

var balls = [];
for(var i = 0; i < 10; i++){
    balls.push(new Ball());
}

setInterval(function(){
    for(var i = 0; i < balls.length; i++){
        balls[i].pos.x += balls[i].vel.x;
        balls[i].pos.y += balls[i].vel.y;

        if(balls[i].pos.x + balls[i].r > canvas.width){
            balls[i].pos.x = canvas.width - balls[i].r;
            balls[i].vel.x *= -1;
        }
        if(balls[i].pos.x < balls[i].r){
            balls[i].pos.x = balls[i].r;
            balls[i].vel.x *= -1;
        }
        if(balls[i].pos.y + balls[i].r > canvas.height){
            balls[i].pos.y = canvas.height - balls[i].r;
            balls[i].vel.y *= -1;
        }
        if(balls[i].pos.y < balls[i].r){
            balls[i].pos.y = balls[i].r;
            balls[i].vel.y *= -1;
        }

        balls[i].colliding = false;
    }

    for(var i = 0; i < balls.length; i++){
        for(var j = i + 1; j < balls.length; j++){
            if(mag(balls[i].vel) < 0){
                break;
            }

            if(dist(balls[i].pos, balls[j].pos) < balls[i].r + balls[j].r){
                balls[i].colliding = true;

                var contact = Math.atan2(balls[j].pos.y - balls[i].pos.y, balls[j].pos.x - balls[i].pos.x);

                balls[i].vel.x = ((((mag(balls[i].vel) * Math.cos(dir(balls[i].vel) - contact) * (balls[i].m - balls[j].m)) + (2 * balls[j].m * mag(balls[j].vel) * Math.cos(dir(balls[j].vel) - contact))) / (balls[i].m + balls[j].m)) * Math.cos(contact)) + (mag(balls[i].vel) * Math.sin(dir(balls[i].vel) - contact) * Math.cos(contact + (Math.PI / 2)));
                balls[i].vel.y = ((((mag(balls[i].vel) * Math.cos(dir(balls[i].vel) - contact) * (balls[i].m - balls[j].m)) + (2 * balls[j].m * mag(balls[j].vel) * Math.cos(dir(balls[j].vel) - contact))) / (balls[i].m + balls[j].m)) * Math.sin(contact)) + (mag(balls[i].vel) * Math.sin(dir(balls[i].vel) - contact) * Math.sin(contact + (Math.PI / 2)));

                balls[j].vel.x = -((((mag(balls[j].vel) * Math.cos(dir(balls[j].vel) - contact) * (balls[j].m - balls[i].m)) + (2 * balls[i].m * mag(balls[i].vel) * Math.cos(dir(balls[i].vel) - contact))) / (balls[j].m + balls[i].m)) * Math.cos(contact)) + (mag(balls[j].vel) * Math.sin(dir(balls[j].vel) - contact) * Math.cos(contact + (Math.PI / 2)));
                balls[j].vel.y = -((((mag(balls[j].vel) * Math.cos(dir(balls[j].vel) - contact) * (balls[j].m - balls[i].m)) + (2 * balls[i].m * mag(balls[i].vel) * Math.cos(dir(balls[i].vel) - contact))) / (balls[j].m + balls[i].m)) * Math.sin(contact)) + (mag(balls[j].vel) * Math.sin(dir(balls[j].vel) - contact) * Math.sin(contact + (Math.PI / 2)));
            }
        }
    }

    ctx.clearRect(0, 0, canvas.width, canvas.height);
    for(var i = 0; i < balls.length; i++){
        ctx.beginPath();

        if(balls[i].colliding){
            ctx.fillStyle = "#f00";
        }else{
            ctx.fillStyle = "#0f0";
        }


        ctx.arc(balls[i].pos.x, balls[i].pos.y, balls[i].r, 0, 2 * Math.PI);
        ctx.fill();
    }
}, 16);
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <canvas id="canvas" width="640" height="480"></canvas>
        <script src="main.js"></script>
    </body>
</html>


另外,为了提高性能,我建议你研究一下原型。如果你想看一些实际使用的例子,可以查看此游戏的源代码,地址是 asteroidio.bitballoon.com。 - Jared Bledsoe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接