带圆碰的矩形无法在p5.js中运行

时间:2018-08-12 20:00:19

标签: javascript math collision p5.js

我试图在p5.js中创建一个检查圆/矩形碰撞的函数。但这行不通。我不知道为什么我也在Java中尝试过。

这是代码

function BoxCircle(boxx, boxy, boxW, boxH, circleX, circleY, circleR){
    var closeX = circleX;
    var closeY = circleY;

    if(circleX < boxx) {
        closeX = boxx;
    }

    if(circleX > boxx + boxW) {
        closeX = boxx + boxW;
    }

    if(circleY < boxy) {
        closeY = boxy;
    }

    if(circleY > boxy + boxH) {
        closeY = boxy + boxH;
    }

    distance = sqrt(sq(closeX-circleX)+sq(closeY-circleY));
    fill(255, 0, 0);
    if(distance <= circleR){
        fill(255, 255, 255);
    }

    ellipse(closeX, closeY, 8, 8);

    return false;
}

0 个答案:

没有答案