如何计算初始高度的弹丸位移?

时间:2016-12-24 09:26:57

标签: java libgdx

我正在尝试在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?

1 个答案:

答案 0 :(得分:3)

我也做了这个程序。您有四个主要组件需要考虑:

  • VelocityX-保持不变,除非考虑到空气摩擦,它计算为Velocity * cos(theta)
  • VelocityY-随公式Vy = Velocity sin(theta)-g 时间而随时间变化
  • X位置 - VelocityX *时间;
  • Y位置 - VelocityY *时间-0.5 * g *时间^ 2

如果你想让你的射弹在某个给定的高度开始,你需要将Y位置公式改为:

  • Y位置 - StartingHeight + VelocityY * time-0.5 * g * time ^ 2

当你将速度Y设定为等于零时,投射时间(即飞行)的时间实际上是你得到的时间(因为当它到达它的峰值高度时,它是不动的)

  • 速度Y = 0
  • (速度* SIN(THETA))/克 射弹达到顶峰所需的时间与坠落在地面上所需的时间相同(如果从地面发射,则起始高度为零)

峰高是VelocityY乘以达到峰值所需的时间

  • PeakHeight = StartingHeight +(速度 SIN(THETA)-g 时间)(速度 SIN(THETA))/克

这是我前一段时间尝试做同样工作时的代码片段。我使用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;

    }