【问题标题】:Java Arc2D Collision detection (With Rotation)Java Arc2D 碰撞检测(带旋转)
【发布时间】:2022-08-19 03:52:20
【问题描述】:

我试图通过使用视锥来创建可以“看到”玩家的 NPC 角色。 NPC会一直来回旋转。

我的问题是弧有一个通用且不变的位置,但是当它被绘制到屏幕上时看起来正确的。 [行动中的碰撞截图][1] [Java 文件的 GitHub 链接][2]

我正在使用 Arc2D 在我的 NPC 类中绘制这样的形状

// Update the shapes used in the npc
    rect.setRect(x, y, w, h);
    ellipse.setFrame(rect);
    visionArc.setArcByCenter(cx, cy, visionDistance, visionAngle, visionAngle * 2, Arc2D.PIE); 

/ CenterX, CenterY (of the npc), / the distance from the arc to the npc / a constant value around 45 degrees and a constant value around 90 degress (to make a pie shape)

我尝试将位置和角度乘以 NPC 当前角度的正弦和余弦

  • 像这样的东西

  • visionArc.setArcByCenter(cx * (Math.cos(Math.toRadians(angle))), cy (Math.sin(Math.toRadians(angle)), visionDistance, visionAngle, visionAngle * 2, Arc2D.PIE);

  • 或者

  • visionArc.setArcByCenter(cx, cy, visionDistance, visionAngle - 角度, (visionAngle + angle) * 2, Arc2D.PIE);

  • 或者

  • visionArc.setArcByCenter(cx, cy, visionDistance, visionAngle * (Math.cos(Math.toRadians(angle))), visionAngle * 2, Arc2D.PIE);

  • 我尝试了很多,但似乎找不到有效的方法。制作视角不是常数使弧线膨胀和收缩,将位置乘以角度的正弦或余弦将使弧线在屏幕周围飞行,这也不起作用。

  • 这是绘制给定 NPC 的函数 public void drawNPC(NPC npc, Graphics2D g2, AffineTransform old) {

      // translate to the position of the npc and rotate
      AffineTransform npcTransform = AffineTransform.getRotateInstance(Math.toRadians(npc.angle), npc.x, npc.y);
      // Translate back a few units to keep the npc rotating about its own center
      // point
      npcTransform.translate(-npc.halfWidth, -npc.halfHeight);
      g2.setTransform(npcTransform);
    
      // g2.draw(npc.rect); //<-- show bounding box if you want 
      g2.setColor(npc.outlineColor);
      g2.draw(npc.visionArc);
      g2.setColor(Color.BLACK);
      g2.draw(npc.ellipse);
    
      g2.setTransform(old);
    

    }

  • 这是我的碰撞检测算法 - NPC 是忍者的超类(更短的范围,更高的外围设备)

    public void checkNinjas(Level level) { for (int i = 0; i < level.ninjas.size(); i++) { 忍者 ninja = level.ninjas.get(i); playerRect = level.player.rect;

          // Check collision
          if (playerRect.getBounds2D().intersects(ninja.visionArc.getBounds2D())) {
              // Create an area of the object for greater precision
              Area area = new Area(playerRect);
              area.intersect(new Area(ninja.visionArc));
              // After checking if the area intersects a second time make the NPC \"See\" the player
              if (!area.isEmpty()) {
                  ninja.seesPlayer = true;
              }
              else {
                  ninja.seesPlayer = false;
              }
          }
      }
    

    }

你能帮我纠正碰撞检测的弧线的实际位置吗?我已经尝试过创建新的形状,这样我就可以让一个来做数学运算,一个来绘制到屏幕上,但我放弃了它,并从这里重新开始。 [1]:https://i.stack.imgur.com/rUvTM.png [2]:https://github.com/ShadowDraco/ArcCollisionDetection

    标签: java rotation collision-detection automatic-ref-counting


    【解决方案1】:

    经过几天的编码、学习和测试新想法后,我回到了这个程序,并使用我最初的想法(光线投射)实现了碰撞检测,并用光线创建了等价物! Screenshot of the new product Github link to the project that taught me the solution

    • 这是新的数学
    public void setRays() {
        for (int i = 0; i < rays.length; i++) {
            double rayStartAngleX = Math.sin(Math.toRadians((startAngle - angle) + i));
            double rayStartAngleY = Math.cos(Math.toRadians((startAngle - angle) + i));
            rays[i].setLine(cx, cy, cx + visionDistance * rayStartAngleX, cy + visionDistance * rayStartAngleY);
        }
    }
    
    • 这是我在提出这个问题并继续了解更多信息后启动的程序的链接,以及新产品外观的图片 (原来的 github 页面已经更新了一个新的分支 :) 我现在也在学习 git hub

    • 我不相信以我想要的方式使用 Arc2D 是可能的,但是有 .setArcByTangent 方法,它可能可以使用,但我不打算进入。光线更凉爽。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-19
      相关资源
      最近更新 更多