微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

碰撞检测不起作用计算后圆形和方形未检测到碰撞

如何解决碰撞检测不起作用计算后圆形和方形未检测到碰撞

这是检测碰撞的方法。卫星是圆形,船是矩形。

boolean hitSatellite(Ship ship,Satellite satellite) {
  float left = ship.getXPos();
  float right = ship.getXPos() + ship.getWidth();
  float top = ship.getYPos();
  float bottom = ship.getYPos() + ship.getHeight();
  float satelliteLeft = abs(satellite.getXPos() - satellite.getDiameter()/2);
  float satelliteRight = abs(satellite.getXPos() + satellite.getDiameter()/2);
  float satellitetop = abs(satellite.getYPos() + satellite.getDiameter()/2);
  float satelliteBottom = abs(satellite.getYPos() - satellite.getDiameter()/2);
   if (!(left > satelliteRight || right < satelliteLeft || top < satelliteBottom || bottom > satellitetop)) {
      return true;
    }
  return false;
}

这是在draw方法调用方法

boolean collection = hitSatellite(ship,satellite);
  if (collection) {
    println("You have collected" + count + " Satellite");
    satellite.Start();
  }
  score++;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。