我创建一个矩形为我的“线”,它是一个旋转的“激光瞄准器”我该如何改变旋转线的碰撞检测?
public Rectangle getLaserBox() {
float lOriginX = (leon.getPosition().x + 0.27f);
float lOriginY = (leon.getPosition().y + 0.7f);
float lEndX = lOriginX + (float)Math.cos((leonAimLaserSprite.getRotation())/57) * 5f;
float lEndY = lOriginY + (float)Math.sin((leonAimLaserSprite.getRotation())/57) * 5f;
Rectangle laserBox = new Rectangle(lOriginX, lOriginY, lEndX, lEndY);
return laserBox;
}
然后我有检查矩形的重叠,应该缩短“激光瞄准器”精灵如果一个方法重叠被检测到。我现在在我的渲染方法中调用了laserCol()方法(我知道我打破了MVC,只是试图让它工作),我的laserWidth被应用为激光精灵的宽度。
private float laserWidth;
public void laserCol() {
Vector2 laserOrigin = new Vector2(leon.getPosition().x + 0.55f, leon.getPosition().y + 0.7f);
boolean laserIsCol = false;
for (Tile t : world.getTiles()) { //pulling in tiles as t, from world method getTiles()
laserWidth = laserOrigin.dst(t.getPosition().x + 0.1f, t.getPosition().y + 0.7f);
if (Intersector.overlapRectangles(getLaserBox(), t.getBounds())) {
laserIsCol = true;
}
}
if (laserIsCol) {
for (Tile t : world.getTiles()) { //pulling in tiles as t, from world method getTiles()
laserWidth = laserOrigin.dst(t.getPosition().x, t.getPosition().y + t.getBounds().y);
}
}
if (!laserIsCol) {
laserWidth = 8f;
}
}
但是正如你在截图中看到的那样,激光不会缩短。我看了其他例子,但似乎无法理解更好的方式来做到这一点。
所以在我的代码中添加了我称之为事物的单个对象之后,我决定检查我创建的sprite boundingbox,它看起来像这样。
在那之后,我改变了一些代码,并正尝试使用光线,我已经得到了它在一定程度上工作,但它不是亲如我想,有什么建议?
public Ray getLaserRay() {
lOriginX = (leon.getPosition().x + 0.27f);
lOriginY = (leon.getPosition().y + 0.7f);
lEndX = lOriginX + (float)Math.cos((leonAimLaserSprite.getRotation())/57) * 10f;
lEndY = lOriginY + (float)Math.sin((leonAimLaserSprite.getRotation())/57) * 10f;
laserO = new Vector3(lOriginX, lOriginY, 0);
laserD = new Vector3(lEndX, lEndY, 0);
Ray laserRay = new Ray(laserO, laserD);
return laserRay;
}
private float laserWidth;
public void laserCol() {
Vector2 laserOrigin = new Vector2(leon.getPosition().x + 0.55f, leon.getPosition().y + 0.7f);
boolean laserIsCol = false;
if (Intersector.intersectRayBoundsFast(getLaserRay(), thing.getBB())) {
laserIsCol = true;
}
if (laserIsCol) {
laserWidth = laserOrigin.dst(thing.getPosition().x, thing.getPosition().y);
}
if (!laserIsCol) {
laserWidth = 10f;
}
}
我结束了使用2条线段得到碰撞检测工作。我制造了一个用于激光瞄准器,另一个用于我的物体(敌人),从低x点延伸到顶x点。除了我使用libgdx的段交叉器之外,基本上是相同的代码。
任何人都有想法如何使子弹或损伤发生在激光视点?
如果没有更多的代码就很难诊断问题。你用调试器完成了吗? – Thomas 2013-02-14 16:04:22