2 回答

TA贡献1876条经验 获得超6个赞
你在那里犯了几个缺陷:
当球上升时,您的碰撞检测器不起作用(无需检查
if (goingDown)
),因为球在任何方向行进都可能发生碰撞。第二个缺陷是您正在测量从球中心到矩形中心的距离。当球与矩形的远端碰撞时,您将不会检测到碰撞。像这样:
dist <= r
为 FALSE,因此未检测到碰撞
您需要的是计算到矩形上最近点的圆心距离,如下所示:
当球到达矩形时, dist <= r 将为 TRUE。
在修复这些缺陷的同时,我们得到了这样的碰撞检测功能:
checkPlatformCollision(platforms) {
for(let j=0; j<platforms.length; j++) {
let NearestX = Math.max(platforms[j].x, Math.min(this.x, platforms[j].x + platformWidth));
let NearestY = Math.max(platforms[j].y, Math.min(this.y, platforms[j].y + platformHeight));
let dx = Math.abs(this.x - NearestX);
let dy = Math.abs(this.y - NearestY);
if (dx*dx + dy*dy <= (this.r*this.r)) {
return true;
}
}
return false;
}

TA贡献1836条经验 获得超3个赞
似乎进行以下更改解决了问题。现在碰撞检测工作得很好。
checkPlatformCollision(platforms) {
for(let j=0; j<platforms.length; j++) {
if (
(goingDown) &&
(this.x < platforms[j].x + platformWidth) &&
(this.x + this.r > platforms[j].x) &&
(this.y + this.r > platforms[j].y) &&
(this.y + this.r < platforms[j].y + platformHeight)
) {
return true
}
}
return false
}
添加回答
举报