2014-12-27 96 views
3

我正在開發一款用LibGdx Engine和Java編寫的新遊戲。 我在這個遊戲中遇到了一些物理問題。LibGdx弓箭遊戲物理

我想射擊彈道彈道(憤怒的小鳥風格) 中的箭頭,並找不到要這樣做的等式。

我使用這些速度方程:

float velx = (float) (Math.cos(rotation) * spd); 
    float vely = (float) (Math.sin(rotation) * spd); 

我這個添加到當前位置,並在一個方向的箭頭芽 - 直。

我想也許改變輪換將幫助我實現我想要的(彈道)。

它確實有幫助,但我也想要有軌跡。

我看到這個 ProjectileEquation類,有人已經發布,但不知道如何使用它:

public class ProjectileEquation 
{ 

    public float gravity; 
    public Vector2 startVelocity = new Vector2(); 
    public Vector2 startPoint = new Vector2(); 
    public Vector2 gravityVec = new Vector2(0,-10f); 

    public float getX(float n) { 
     return startVelocity.x * (n) + startPoint.x; 
    } 

    public float getY(float n) { 
     float t = n; 
     return 0.5f * gravity * t * t + startVelocity.y * t + startPoint.y; 
    } 

}

我正在尋找一些幫助,幫助我使用這個類用於彈道軌跡。

這是我嘗試使用它:

for(int i =0;i<30;i++) 
      { 
       Texture f = ResData.Square_1; 
       ProjectileEquation e= new ProjectileEquation(); 
       e.gravity = 1; 
       e.startPoint = new Vector2(bow.getX(),bow.getY());//new Vector2(-bow.getX(),-bow.getY()); //My bow is opposite so it suppose to work fine 
       e.startVelocity = getVelocityOf(bow.getRotation()); 
       Vector3 touchpos = new Vector3(); 

       s.draw(f,e.getX(i) ,e.getX(i),5,5); 

      } 
+0

你需要什麼?我不明白夠好。你需要渲染你的軌跡嗎? – nikoliazekter 2014-12-27 16:16:54

+0

是的,不能設法使用這個課程 – 2014-12-27 16:44:03

+0

如果你沒有空氣阻力,那麼你的軌跡是簡單的拋物線。公式http://upload.wikimedia.org/math/6/0/5/605184454f022e69b228699e8983c9c3.png – nikoliazekter 2014-12-27 17:48:12

回答

0

你張貼看起來像它會計算你傳遞應該是時間的X和給定的時間三角洲Y位置,所以浮動的ProjectileEquation類三角洲,因爲你開始箭頭移動(以秒爲單位)。

該代碼不會給你箭頭的角度。爲了找到這個問題,我建議你保持先前的X和Y,然後你可以使用Math.atan2()來計算基於前一個XY和當前XY的角度。 Google atan2獲取關於如何使用它的信息。

然而,最好的方法是使用Box2d並正確模擬場景。那麼你根本不需要參與數學。我在某處讀到這就是「憤怒的小鳥」使用的地方,並且是模擬這類物理遊戲的絕佳選擇。

我希望你的遊戲順利。