简体   繁体   English

碰撞检测Javascript不起作用(而是Im不起作用)

[英]Collision Detection Javascript Not Working(rather Im not working)

I wrote a simple canvas with several bubbles that float around. 我写了一个简单的画布,上面漂浮着几个气泡。 They are supposed to be green when not colliding and red when any of them are. 当它们不碰撞时应该是绿色,而当它们中的任何一个都应该是红色。 For some reason they are all green but one. 由于某种原因,它们全都是绿色,但只有一个。 And when the bubbles collide the only ones that turn red are the ones touching that original bubble that wont start off green. 当气泡碰撞时,唯一变成红色的是那些接触到不会从绿色开始的原始气泡的气泡。 I know I am missing something really obvious to you guys, but I went over it a thousand times and just can't see it. 我知道我缺少对你们来说确实很明显的东西,但是我经过了一千​​遍却看不到。 If anyone can please let me know, I am ready to feel stupid... thanks. 如果有人可以让我知道,我准备感到愚蠢……谢谢。

Here is a link to the GitHub gist: 这是GitHub要点的链接:

https://gist.github.com/anonymous/e172bb18c078a2e9a797b8a30fdafcc3 https://gist.github.com/anonymous/e172bb18c078a2e9a797b8a30fdafcc3

Here is a snippet from the collision detection to drawing the canvas: 这是从碰撞检测到绘制画布的片段:

// Draw to Canvas
var draw = function() {
ctx.clearRect(0,0,600,400);
for(var i = 0 ; i < spawnArr.length; i++){ 

 // Collision Detect & Correct 
for(var j = 0; j < spawnArr.length; j++) {
  var dx = spawnArr[i].x - spawnArr[j].x;
  var dy = spawnArr[i].y - spawnArr[j].y;
  var distance = Math.sqrt(dx * dx + dy * dy);

  if (distance <  spawnArr[i].rad + spawnArr[j].rad) {

        ctx.strokeStyle = "#FF0000"; 
        ctx.beginPath();
        ctx.arc(spawnArr[i].x, spawnArr[i].y, spawnArr[i].rad, 0, 2*Math.PI);
        ctx.closePath();
        ctx.stroke();

        ctx.beginPath();
        ctx.arc(spawnArr[j].x, spawnArr[j].y, spawnArr[j].rad, 0, 2*Math.PI);
        ctx.closePath();
        ctx.stroke();
       // console.log('hit');
  }
  else {
    ctx.strokeStyle = "#00FF00"; 
    ctx.beginPath();
    ctx.arc(spawnArr[i].x, spawnArr[i].y, spawnArr[i].rad, 0, 2*Math.PI);
    ctx.closePath();
    ctx.stroke();

    ctx.beginPath();  
    ctx.arc(spawnArr[j].x, spawnArr[j].y, spawnArr[j].rad, 0, 2*Math.PI);
    ctx.closePath();
    ctx.stroke();
    //console.log('miss');
  }
}

The problem is you are drawing over the circles many times. 问题是您多次绘制圆圈。 When a circle is touching another you draw it red, then if it does not hit the next circle you draw it green. 当一个圆与另一个圆接触时,您将其绘制为红色,如果未碰到下一个圆,则将其绘制为绿色。 Also you are checking if the circle hit itself, so you draw all the circles red twice It can only be green or red, and it should only be drawn once. 另外,您还要检查圆是否被自身击中,因此您将所有圆绘制为红色两次。它只能是绿色或红色,并且只能绘制一次。

The following will solve you problems and make it run somewhat quicker. 以下内容将解决您的问题并使它运行更快。 I have added a style to each circle that defines its colour. 我为每个圆圈添加了一种定义其颜色的样式。 It test for a hit and if found set the colour to red. 它会测试是否有命中,如果找到则将颜色设置为红色。

The second for loop starts from the position of the first for loop plus one. 第二个for循环从第一个for循环的位置加一个开始。 There is no point in checking if B hit A when you already know A hit B 当您已经知道A命中B时,检查B是否命中A是没有意义的

// Draw to Canvas
var draw = function () {
    ctx.clearRect(0, 0, 600, 400);
    function drawThing(thing) {
        ctx.strokeStyle = thing.style;
        ctx.beginPath();
        ctx.arc(thing.x, thing.y, thing.rad, 0, 2 * Math.PI);
        ctx.stroke();
    }

    for (var i = 0; i < spawnArr.length; i++) {
        var t1 = spawnArr[i];

        var t1.style = "#00FF00";
        // Collision Detect & Correct
        for (var j = i + 1; j < spawnArr.length; j++) {
            var t2 = spawnArr[j];
            var dx = t1.x - t2.x;
            var dy = t1.y - t2.y;
            var distance = Math.sqrt(dx * dx + dy * dy);

            if (distance < t1.rad + t2.rad) {
                t1.style = "#FF0000";
            }
        }
        drawThing(t1);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM