Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何计算具有初始高度的弹丸的位移?_Java_Libgdx - Fatal编程技术网

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

Java 如何计算具有初始高度的弹丸的位移?,java,libgdx,Java,Libgdx,我试图制作一个类似于Libgdx的模拟器,我已经用这个公式计算了位移x: Java格式: theta = Parameter.angle * 2; range = ((float) Math.pow(Parameter.speed, 2) / Constant.GRAVITY) * (float) Math.sin(Math.toRadians(theta)); 但问题是,我的射弹总是从0.0米的高度开始,我希望能够设置射弹的初始高度,那么我需要什么公式呢?还有,如何计算位移y?我也制作了

我试图制作一个类似于Libgdx的模拟器,我已经用这个公式计算了位移x:

Java格式:

theta = Parameter.angle * 2;

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

但问题是,我的射弹总是从0.0米的高度开始,我希望能够设置射弹的初始高度,那么我需要什么公式呢?还有,如何计算位移y?

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

  • VelocityX-保持不变,除非将空气摩擦力考虑在内,它的计算方式为速度*cos(θ)
  • VelocityY-根据公式Vy=Velocitysin(θ)-gtime随时间变化
  • X位置-速度X*时间
  • Y位置-速度Y*时间-0.5*克*时间^2
如果你想让你的射弹从某个给定高度开始,你需要将Y位置公式更改为:

  • Y位置-启动高度+速度Y*时间-0.5*g*时间^2
弹丸在运动中所花费的时间(即飞行)实际上是你将速度Y设置为零时所得到的时间(因为弹丸在达到其峰值高度时是静止的)

  • 速度Y=0
  • (速度*sin(θ))/g 射弹达到峰值所需的时间与落在地面所需的时间相同(如果是从地面发射的,即起始高度为零)
峰值高度是速度y乘以达到该峰值所需的时间

  • 峰值高度=启动高度+(速度素(θ)-gtime)(速度素(θ))/g
这里有一段代码片段,来自不久前我尝试做同样的工作。我使用javafx多段线来绘制它,因为它具有亚像素精度(它将双精度作为参数) 编辑:

public void getYPoints(){
int计数器=0;
双倍时间=0.0;
双yCoord=y;
而(yCoord>=0){
yCoord=yCoord+(Y速度*时间+0.5*克*时间*时间);
yPoints.add(yCoord);
计数器++;
时间+=0.01;
//System.out.printf(“时间%f的Y坐标是%f,而在arraylist中是%d\n”,时间,yCoord,yPoints.get(计数器-1));
这个。时间=时间;
this.counter=计数器;
}
//返回1分;
}
public void getXPoints(){
双xCoord=x;
双牛顿时间=0.0;

虽然(新时间你能为我们添加一些代码让我们更容易理解吗?你是如何模拟时间的?因为我使用的是deltaTime,它非常不精确,θ足够小,应该可以,我的轨迹可以是0,01 delta。我想如果你想让它更有弹性,你也可以使用计时器。可以这样设置y位置:positionY+=速度Y*增量时间;然后速度Y-=重力*增量时间;
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;

    }