2016-12-24 65 views
1

我试图做一个模拟器像Libgdx this和我已经同排量的计算做X上使用这个公式:你如何计算初始高度的弹丸位移?

enter image description here

的Java格式:

theta = Parameter.angle * 2; 

range = ((float) Math.pow(Parameter.speed, 2)/Constant.GRAVITY) * (float) Math.sin(Math.toRadians(theta)); 

但问题是我的弹丸始终在0.0米高处开始,我希望能够设定弹丸的初始高度,那么我需要什么样的配方?你怎么计算位移y?

+0

你可以添加一些代码,我们,所以我们可以很容易地理解? –

回答

3

我也制作了这个程序。您有您需要考虑四个主要部分:除非你采取空气摩擦考虑

  • VelocityX-保持不变,它的速度计算* COS(THETA)
  • VelocityY-以下公式VY随时间变化= Velocity sinθ-g time
  • X position-VelocityX * time;
  • Ÿ位置 - VelocityY *时间0.5 * G * ^时间2

如果你希望你的炮弹在你需要改变Y位置公式像一些规定的高度开始:

  • ÿ位置 - StartingHeight + VelocityY *时间0.5 * G * ^时间2

时间抛射运动花(即飞行)实际上是在设置速度Y中的时候,你得到等于零(因为当它重新发射时,射弹是不动的酸痛它是峰值高度)

  • 速度Y = 0
  • (速度* SIN(THETA))/克 达到它的峰值为射弹所需的时间是需要为它上落下的同时地面(如果它是从地面发射的,即起始高度是零)

峰值高度VelocityY通过达到峰值所需要的时间乘以

  • PeakHeight = Starti ngHeight +(速度罪(THETA)-G时间)(速度罪(THETA))/ G

下面是前一阵子,当我做出尝试做同样的工作的代码片段。我用JavaFX的折线画它,因为它具有子像素精度(它需要兼作参数) 编辑:

public void getYPoints(){ 
    int counter=0; 
    double time=0.0; 
    double yCoord=y; 
    while(yCoord>=0){ 
     yCoord=yCoord+(ySpeed*time+0.5*g*time*time); 
     yPoints.add(yCoord); 
     counter++; 
     time+=0.01; 
     //System.out.printf("Y coord for time %f is %f and in arraylist is %d\n",time,yCoord,yPoints.get(counter-1)); 
     this.time=time; 
     this.counter=counter; 
    } 

    //return yPoints; 
} 
public void getXPoints(){ 
    double xCoord=x; 
    double newTime=0.0; 
    while(newTime<this.time){ 
     xCoord=xCoord+xSpeed*this.time; 
     xPoints.add(scale*xCoord); 
     newTime+=0.01; 

    } 
+0

你是如何模拟时间的?因为我正在使用deltaTime,而且它非常不准确 –

+0

如果足够小,theta应该没关系,我的轨迹很好,0,01 delta。我想你也可以使用计时器,如果你想让它更有弹性。 – Pavisa

+0

可以像这样设置y位置:positionY + = velocityY * deltaTime;那么velocityY - = gravity * deltaTime; –