2014-12-27 95 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并正确模拟场景。那么你根本不需要参与数学。我在某处读到这就是“愤怒的小鸟”使用的地方,并且是模拟这类物理游戏的绝佳选择。

我希望你的游戏顺利。