2013-02-14 54 views
0

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

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; 
    } 
} 

但是正如你在截圖中看到的那樣,激光不會縮短。我看了其他例子,但似乎無法理解更好的方式來做到這一點。 enter image description here

所以在我的代碼中添加了我稱之爲事物的單個對象之後,我決定檢查我創建的sprite boundingbox,它看起來像這樣。

enter image description here

在那之後,我改變了一些代碼,並正嘗試使用光線,我已經得到了它在一定程度上工作,但它不是親如我想,有什麼建議?

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; 
    } 
} 

enter image description here enter image description here

我結束了使用2條線段得到碰撞檢測工作。我製造了一個用於激光瞄準器,另一個用於我的物體(敵人),從低x點延伸到頂x點。除了我使用libgdx的段交叉器之外,基本上是相同的代碼。

任何人都有想法如何使子彈或損傷發生在激光視點?

enter image description here

+0

如果沒有更多的代碼就很難診斷問題。你用調試器完成了嗎? – Thomas 2013-02-14 16:04:22

回答

2

沒有有更多的信息,我只是能猜到,但我認爲問題可能是你使用計算所有的瓷磚列表/數組中的寬度。相反,您應該使用激光碰撞的平鋪來計算寬度。

事情是這樣的:

laserWidth = 8f; //default if no collision is found 
for (Tile t : world.getTiles()) {  //pulling in tiles as t, from world method getTiles() 
    if (Intersector.overlapRectangles(getLaserBox(), t.getBounds())) { 
     laserWidth = laserOrigin.dst(t.getPosition().x, t.getPosition().y + t.getBounds().y); 
     break; 
    } 
} 

請注意,我只是重用你的代碼,我不知道如果這是正確的,如果有它的一些計算錯誤。正如我在評論中所說的那樣,您應該調試計算並查看他們開始出錯的位置。

+0

我已經使用了一些調試器,但通常只是尋找空對象,不確定如何正確地查看它的計算。我會創建一個像瓦片的東西,但只是一個對象,並用它來檢查它。另外,你用這個代碼改變了什麼?休息是什麼;做? – AspiretoCode 2013-02-14 16:22:02

+0

'break'結束循環。這可能不正確,因爲我不知道你的其他代碼是幹什麼的。檢查所有瓷磚並使用最短寬度可能會更好,這應該是最近擊中的結果。至於調試計算:您可以遍歷調試器並檢查每個步驟的值是否有意義。儘管如此,您可能需要進行一些粗略的計算,以便了解它們是否合理。 – Thomas 2013-02-14 17:51:54