我正在尝试在Libgdx中创建一个类似this的模拟器,我已经完成了使用这个公式计算位移x:
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)
我也做了这个程序。您有四个主要组件需要考虑:
如果你想让你的射弹在某个给定的高度开始,你需要将Y位置公式改为:
当你将速度Y设定为等于零时,投射时间(即飞行)的时间实际上是你得到的时间(因为当它到达它的峰值高度时,它是不动的)
峰高是VelocityY乘以达到峰值所需的时间
这是我前一段时间尝试做同样工作时的代码片段。我使用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;
}