public void move(){
double angle;
for(int i = 0; i < planets.size(); i++){
if(Math.abs(Math.sqrt(Math.pow(ship.locX - (planets.get(i).locX + planets.get(i).radi), 2) + Math.pow(ship.locY - (planets.get(i).locY + planets.get(i).radi), 2))) < planets.get(i).gravrange){
//Distance formula between spaceship and planets to determine whether the ship is within the influence of the planet.
angle = ((Math.atan2((planets.get(i).locX + planets.get(i).radi) - ship.locX, (planets.get(i).locY + planets.get(i).radi) - ship.locY)) / Math.PI) + 1;
//The problematic math equation.
0 から 2 までの double を生成します。0 は、ship.locY < Planets.get(i).locY && ship.locX == (planets.get(i).locX - Planets.get(i).radi) の場合です。(相対 X = 0、相対 Y < 0 の場合)
if(ship.locX > (planets.get(i).locX + planets.get(i).radi)){xm += Math.cos(angle) * planets.get(i).gravrate;}
else{xm -= Math.cos(angle) * planets.get(i).gravrate;}
if(ship.locY > (planets.get(i).locY + planets.get(i).radi)){ym += Math.sin(angle) * planets.get(i).gravrate;}
else{ym -= Math.sin(angle) * planets.get(i).gravrate;}
}
}
これはデータを使用して、宇宙船の X 速度と Y 速度を変更します。
この方程式は軌道の大部分で機能しますが、特定の状況下では、宇宙船が逆行する力を受けて減速するという問題があります。その後まもなく、惑星体によって反発され始め、しばらくすると再び引き寄せられ始めます。これが発生した元の位置に探査機が到達すると、元の軌道とは逆方向に動き始めます。
これは、宇宙船が波のような動きを始めるまで続きます。
これを解決する方法はありますか、それとも単に間違った方程式を使用していますか? 私はこれを約2週間修正しようとしています。私は現時点で物理学や微積分学の教育を受けていないため、理解は限られています。
編集:コメントには私の数学に関する質問があったので、ここで答えようとします. atan2 について私が知っていることから、-pi から pi までの数値を生成します。pi で割って -1 から 1 までの数値を生成し、次に 1 を加算して 0 から 2 を生成します。次に、この数値をラジアン単位として使用します。ラジアン (単位円) に関する私の知識は、円のラジアン測定値は 0 ~ 2pi であるということです。
編集 2: 次のコードは数学が大きく異なりますが、惑星の北極と南極に近づくときに引き寄せるのではなく反発する問題を除けば、望ましい結果が得られます。
public void move(){
double angle;
double x1, x2, y1, y2;
for(int i = 0; i < planets.size(); i++){
x1 = ship.locX;
y1 = ship.locY;
x2 = planets.get(i).locX + planets.get(i).radi;
y2 = planets.get(i).locY + planets.get(i).radi;
if(Math.abs(Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2))) < planets.get(i).gravrange){
//Distance formula between spaceship and planets
angle = (y2 - y1)/(x2 - x1); //Gets slope of line between points.
if(angle > 0){
if(y1 > y2){
xm += Math.cos(angle) * planets.get(i).gravrate;
ym += Math.sin(angle) * planets.get(i).gravrate;
}else{
xm -= Math.cos(angle) * planets.get(i).gravrate;
ym -= Math.sin(angle) * planets.get(i).gravrate;
}
}
else{
if(y1 > y2){
xm -= Math.cos(angle) * planets.get(i).gravrate;
ym -= Math.sin(angle) * planets.get(i).gravrate;
}else{
xm += Math.cos(angle) * planets.get(i).gravrate;
ym += Math.sin(angle) * planets.get(i).gravrate;}
}
}
}
その奇妙なatan2方程式ではなく、直線の傾きを使用することが役立つかどうかを確認するために、私はそれを非常に迅速に書き上げました. どうやらそうでした。また、このセクションでは、コードをもう少し読みやすくしました。