0
我想通過重力計算淨加速度,以便用(G *(m1 * m2)/ d * d)/ m1建立一個簡單的空間飛行模擬。船舶往往以階梯式走向半正確的方向。java 2d淨重計算
主類
public void update()
{
double[] accels = new double[bodies.size()];//acceleration of the planets
double[][] xyaccels = new double[bodies.size()][2];//storing the x and y
for(Body n: bodies){
int count = 0;
double dist = distance(ship.loc.x,ship.loc.y,n.loc.x,n.loc.y);
double acel = getAccel(n.mass, ship.mass, dist);
accels[count] = acel;
double alpha = getAngle(ship.loc.x,ship.loc.y,n.loc.x,n.loc.y);
//xyaccels[count][0] = Math.cos(alpha) * acel;
//xyaccels[count][1] = Math.sin(alpha) * acel;
//split the acceleration into the x and y
XA += Math.cos(alpha) * acel;
YA += Math.sin(alpha) * acel;
count++;
}
ship.update(XA, YA);
//XA = 0;
//YA = 0;
accels = null;
xyaccels = null;
}
更新功能,爲飛船
public void update(double XA, double YA){
xAccel += XA;
yAccel += YA;
//add the x-acceleration and the y-acceleration to the loc
loc.x += Math.round(xAccel);
loc.y += Math.round(yAccel);
}
感謝您的幫助,我不會真正理解這些方程式,但我想我可以弄明白。 – KoalaStorm 2015-02-08 03:29:22
你的問題不是編程;它是微積分和物理。你有沒有學過?如果沒有,你最好還是去圖書館幫忙。 – duffymo 2015-02-08 12:09:34