我试图在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;
}