我創建一個矩形爲我的「線」,它是一個旋轉的「激光瞄準器」我該如何改變旋轉線的碰撞檢測?
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