简体   繁体   English

在JavaScript中使用递归的碰撞检测圈

[英]Collision detection circles using recursion in JavaScript

I want to generate circles on random locations with a specific distance from the main circle. 我想在距主圆特定距离的随机位置上生成圆。 The circles should not intersect with each other. 圆圈不应该相交。 I've created a method that checks for each circle if it's not intersecting all other circles. 我创建了一种方法来检查每个圆圈是否没有与所有其他圆圈相交。 The collision detection does its thing, but the recursion part in my code doesn't work: 冲突检测可以完成它的工作,但是我代码中的递归部分不起作用:

if (collision > 0) {
    console.log("GENERATE NEW RANDOM COORDINATE")
    generateRandomCoordinate(d, r)
}

Result 结果

As you can see in the output, my app generates new coordinates (158,290) when it detects collision (138,272). 从输出中可以看到,我的应用在检测到碰撞(138,272)时会生成新的坐标(158,290)。 However, it still draws circle at (138,272). 但是,它仍在(138,272)处绘制圆。

Any idea why this happens? 知道为什么会这样吗? Please let me know if you know more efficient way... 如果您知道更有效的方法,请告诉我...

generateRandomCoordinate function generateRandomCoordinate函数

function generateRandomCoordinate(d, r) {
    var phi = Math.random() * 2 * Math.PI
    var x = topicCoordinates[0].x + Math.round(d * Math.cos(phi))
    var y = topicCoordinates[0].y + Math.round(d * Math.sin(phi))
    var collision = 0
    for (var j = 0; j < topicCoordinates.length; j++) {
        var dx = x - topicCoordinates[j].x
        var dy = y - topicCoordinates[j].y
        var distance = Math.sqrt(dx * dx + dy * dy)

        if (distance < r + topicCoordinates[j].r) {
            collision++
            console.log("COLLISION DETECTED BETWEEN POINT(" + "x: " + x + ", y: " + y + ") AND  POINT(x1: " + topicCoordinates[j].x + ", y1: " + topicCoordinates[j].y + ")")
        }
    }
    if (collision > 0) {
        console.log("GENERATE NEW RANDOM COORDINATE")
        generateRandomCoordinate(d, r)
    }

    var topicCoordinate = {
        "x": x,
        "y": y,
        "r": r
    }
    return topicCoordinate
}

Calling the generateRandomCoordinate function 调用generateRandomCoordinate函数

var randomCoordinate = generateRandomCoordinate(d, r)
x = randomCoordinate.x
y = randomCoordinate.y
r = randomCoordinate.r
}

console.log("ADDED POINT(x: " + x + ", y: " + y + ", r: " + r + ")")
var topicCoordinate = {
    "x": x,
    "y": y,
    "r": r
}
topicCoordinates.push(topicCoordinate)
drawCircle(x, y, r)

Output 输出量

在此处输入图片说明

UPDATE Working code 更新工作代码

function generateRandomCoordinate(d, r) {
    var collision = true
    while (collision) {
        collision = false
        var phi = Math.random() * 2 * Math.PI
        var x = topicCoordinates[0].x + Math.round(d * Math.cos(phi))
        var y = topicCoordinates[0].y + Math.round(d * Math.sin(phi))
        for (var j = 0; j < topicCoordinates.length; j++) {
            var dx = x - topicCoordinates[j].x
            var dy = y - topicCoordinates[j].y
            var distance = Math.sqrt(dx * dx + dy * dy)

            if (distance < r + topicCoordinates[j].r) {
                collision = true
                console.log("COLLISION DETECTED BETWEEN POINT(" + "x: " + x + ", y: " + y + ") AND  POINT(x1: " + topicCoordinates[j].x + ", y1: " + topicCoordinates[j].y + ")")
            }
        }
    }

    var topicCoordinate = { "x" : x, "y" : y, "r" : r }
    return topicCoordinate
}

There is no need in recursive function call. 不需要递归函数调用。 Just organize cycle (pseudocode) 只是组织循环(伪代码)

 repeat
     iscollision = false 
     generate x,y
     for other circles check
         if collision occurs
             iscollision = true
             break  //no need to check later circles    
 until iscollision = false

Concerning your recursive confusion - for every recursive call level function instance contains own internal copies of variables, so outer function doesn't know about inner x,y - you don't return them in this code piece: 关于递归混乱-对于每个递归调用级别的函数实例都包含自己的变量内部副本,因此外部函数不知道内部x,y-您不会在此代码段中返回它们:

  if (collision > 0) {
    vvvvvvvvvvvvvvvvvv
    generateRandomCoordinate(d, r)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}

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

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