简体   繁体   English

弹跳球碰撞时错过了一些平台

[英]Bouncing ball misses some platforms when colliding

I am trying to create a demo game just like Doodle Jump and i am stuck in the most silly case.我正在尝试创建一个像 Doodle Jump 一样的演示游戏,但我陷入了最愚蠢的情况。 My bouncing ball just misses some platforms (falls through) when checking for collision.在检查碰撞时,我的弹跳球只是错过了一些平台(落下)。

Any ideas on that?对此有何想法? Codepen follows for help. Codepen 跟随寻求帮助。

I've tried sorting the platforms in the array (thinking that this was the error), to no avail of course.我试过对数组中的平台进行排序(认为这是错误),当然无济于事。

Here is my codepen example for showing the case.这是我用于展示案例的 codepen 示例。 https://codepen.io/v4vaios/pen/ZEzpozg https://codepen.io/v4vaios/pen/ZEzpozg

    checkPlatformCollision(platforms) {
    if (goingDown) {

      for(let j=0; j<platforms.length; j++) {
        let distX = Math.abs(this.x - platforms[j].x-platformWidth/2);
        let distY = Math.abs(this.y - platforms[j].y-platformHeight/2);
        let dx=distX-platformWidth/2;
        let dy=distY-platformHeight;

        if (dx*dx + dy*dy <= (this.r*this.r)) {
          return true
        }


      }
    }

    return false
  }

You made several flaws there :你在那里犯了几个缺陷:

  1. Your collision detector don't works when ball goes UP (no need for check if (goingDown) ), because collision may occur on ball traveling ANY direction.当球上升时,您的碰撞检测器不起作用(无需检查if (goingDown) ),因为球在任何方向行进都可能发生碰撞。

  2. Second flaw is that you are measuring distance from ball center to rectangle center.第二个缺陷是您正在测量从球中心到矩形中心的距离。 When ball will collide with far side of rectangle you will not detect collision.当球与矩形的远端碰撞时,您将不会检测到碰撞。 Like this:像这样: 在此处输入图片说明

dist <= r is FALSE, so no collision detected dist <= r为 FALSE,因此未检测到碰撞

What you need is to calculate circle centers distance to NEAREST point on rectangle, like this:您需要的是计算到矩形上最近点的圆心距离,如下所示:

在此处输入图片说明

When ball will reach rectangle, dist <= r will be TRUE.当球到达矩形时, dist <= r 将为 TRUE。

While fixing these flaws, we get such collision detection function:在修复这些缺陷的同时,我们得到了这样的碰撞检测功能:

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;

  }

Seems that making the following changes fixed the problem.似乎进行以下更改解决了问题。 Now the collision detection works just fine.现在碰撞检测工作得很好。

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 }

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

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