【发布时间】: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