0
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.

Produces a double from 0 to 2, 0 being when ship.locY < planets.get(i).locY && ship.locX == (planets.get(i).locX - planets.get(i).radi). (when relative X = 0 and relative 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;}                          
        }
    }

This uses the data to modify the X and Y velocities of the spacecraft.

This equation works for the majority of an orbit, but under certain circumstances has an issue in which the spacecraft undergoes a retrograde force, slowing it. Shortly afterward it begins to be repelled by the planetary body, which after a short period begins attracting it again. When the spacecraft reaches the original position at which this occurred, it begins to move in the opposite direction of its original orbit.

This continues to occur until the spacecraft begins a wavelike motion.

Is there a way to solve this, or am I simply using the wrong equation? I've been attempting to fix this for about two weeks now. I have no education in physics nor calculus at this point in time, so my understanding is limited.

Edit: The comments had questions about my math, so I'll attempt to answer them here. From what I know about atan2, it produces a number from -pi to pi. I divide by pi to produce a number from -1 to 1, then add 1 to produce 0 to 2. I then use this number as a radian measurement. My knowledge of radians (unit circle) is that a circle's radian measure is 0 to 2pi.

Edit 2: The following code has very different math but produces the desired results, save for issues of repelling rather than attracting when approaching the North and South 'poles' of the planet.

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;}
            }   
        }
    }

I wrote it up very quickly to see if using the slope of the line rather than that strange atan2 equation would help. Apparently it did. I also made the code a bit more readable in this section.

4

1 回答 1

1

以下代码解决了我的问题。我像往常一样使我的数学方程式过于复杂。我花了三周的时间在谷歌上搜索,询问拥有物理和数学学位的人,并阅读 Javadocs,然后才弄明白。事实证明,atan2 的工作方式与我认为它的工作方式完全不同,而且我使用不当。解决方案是事先简化 atan2 方程并删除不必要的添加。

        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 = Math.atan2((y2 - y1),(x2 - x1)); //Converts the difference to polar coordinates and returns theta.

            xm -= Math.cos(angle) * planets.get(i).gravrate; //Converts theta to X/Y
            ym -= Math.sin(angle) * planets.get(i).gravrate; //velocity values.
        }
于 2015-05-12T18:12:19.660 回答