我用 Java 创建了一个带有用户界面的抛射运动模拟。该程序允许用户输入初始值来计算物体的射弹。我当前没有设置任何内容来将射弹绘制到屏幕上。
我有一个单独的 spring 工作线程在后台处理模拟代码。
我还添加了碰撞检测,以便当物体撞击地面时它会反弹并继续这样做,直到循环退出。
我所使用的方程对于我想要实现的目标来说并不正确。
在以下初始条件下,输出数据的绘图结果如下:
Initial Conditions:
Angle: 30 degrees;
Initial Speed 8.66 m/s;
Height: 50 m;
Elasticity of object: .5 coefficient of restitution in the y direction;
Acceleration: -9.8 m/s^2;
No acceleration in the x direction
看来,一旦模拟开始,y就会变得越来越大,因此循环永远不会自行退出。
这是代码:
//This class will handle all time consuming activities
class Simulation extends SwingWorker<Void, Void>
{
//Execute time consuming task
protected Void doInBackground() throws Exception
{
FileWriter fstream = new FileWriter("output.txt");
BufferedWriter out = new BufferedWriter(fstream);
double angle = Double.valueOf(angleText.getText());
double radians = angle * (Math.PI/180);
double vel = Double.valueOf(speedText.getText());
double mass = Double.valueOf(massText.getText());
double y = Double.valueOf(heightText.getText());
double x = 0;
double epX = Double.valueOf(epxText.getText());
double epY = Double.valueOf(epyText.getText());
double ax = Double.valueOf(accxText.getText());
double ay = Double.valueOf(accyText.getText());
int numBounces = 0;
double deltaTime = .00000001;
double total_velocity = 0.0;
double time = 0.0;
String fs;
angle = angle * Math.PI / 180;
while(numBounces < 10)
{
//Increment Time
time = time + deltaTime;
//Calculate new values for velocity[x] and velocity[y]
double vx = (vel*Math.cos(angle)) + ax*time;;
double vy = (vel*Math.sin(angle)) + ay*time;
//Calculate new values for x and y
x = x + vx*time;
y = y + vy*time + .5*ay*(time*time);
System.out.format("%.3f\n", y);
fs = String.format("%f\t %f\t %f\t %f\t %f\t %f\t %f\t\n", ax, ay, x, y, vx, vy, time);
out.write(fs);
//If ball hits ground: y < 0
if(y < 0)
{
numBounces++;
System.out.println("Number of Bounces: " + numBounces);
//Why is this statement needed if the velocity in the y direction is already being reversed?
vy = -vy - ay*time; // vy = -vy - ay*time;
//Calculate angle
angle = Math.atan(vy/vx);
angle = angle * Math.PI / 180;
//Calculate total velocity
total_velocity = Math.sqrt((vy*vy) + (vx*vx));
//Velocity with elasticity factored in
total_velocity = Math.sqrt((epY) * total_velocity);
//New velocities for when ball makes next trip
vy = total_velocity*Math.sin(angle);
vx = total_velocity*Math.cos(angle);
out.write(fs);
}
//Draw projectile
//Thread.sleep(.00001); //Sleep for deltaTime - 10 nanoseconds or draw after n number of points
}
out.close();
return null;
}
//SwingWorker lets you execute code on the event dispatching thread. Also allows you to update the GUI
public void done()
{
try
{
/*
rangeText.setText(" " + x);
heightTText.setText(" " + y);
timeText.setText(" " + time);
*/
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
可能的问题是什么?我的猜测是,这可能与角度有关。在以前版本的代码中,我没有考虑角度,它工作得很好。另外,我不确定是否必须设置 GUI 上的界限,以免永远继续下去。
我也有一个 NullPointerException。
我看到的第一个问题在这里:
//Calculate angle
angle = Math.atan(vy/vx);
angle = angle * Math.PI / 180;
Math.atan
已返回一个以弧度为单位的值:
Returns the arc tangent of a value; the returned angle is in the range -pi/2 through pi/2.
因此,* Math.PI/180
不会给您带来任何好处。
第二个问题在这里:
//Calculate new values for velocity[x] and velocity[y]
double vx = (vel*Math.cos(angle)) + ax*time;;
double vy = (vel*Math.sin(angle)) + ay*time;
每次通过循环,这些值都会重新初始化。由于 angle
、ax
、ay
和 time
在循环期间无法更改,这意味着您始终会得到相同的 vx
和(正)vy
。 vy 应该随着每次循环而变小,更像是:
//Calculate initial values for velocity[x] and velocity[y]
double vx = (vel*Math.cos(angle)) + ax*time;
double vy = (vel*Math.sin(angle)) + ay*time;
while(numBounces < 10) {
//Increment Time
time = time + deltaTime;
//Calculate new values for x and y
x = x + vx*time;
y = y + vy*time + .5*ay*(time*time);
//Calculate new values for velocity[x] and velocity[y]
vx += ax * time;
vy += ay * time;
我是一名优秀的程序员,十分优秀!