我已经用Java创建了一个带有用户界面的弹丸运动模拟。 该程序允许用户输入初始值来计算物体的抛射物。我目前没有任何东西可以将弹丸吸引到屏幕上。弹丸运动的实现
我有一个单独的弹簧工作线程在后台处理模拟代码。
我还添加了碰撞检测功能,以便当物体碰到地面时它会弹起并继续这样做,直到环路退出。
我所在的方程式对于我所要达到的目标并不正确。
用下面的初始条件,这里是输出的数据率的情节是什么:
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界限,以便y不会永远持续下去。
我也有一个NullPointerException。
NullPointerException提取不是堆栈跟踪,这可能很有用 - 相反,它显示的是NullPointerException类的字节码 - 它永远不会改变,因此不会提供对问题的深入了解。把你的后台任务包装在try/catch块中,该块捕获Throwable并执行printStackTrace。 –