模拟恒星的引力作用?

5
我正在制作一款游戏,玩家将会(在鼠标松开后)以某个确定方向和速度射出“星星”,该速度由拖动鼠标的距离决定。我在画布上放置了一个“行星”(静止的圆形),想让它对运动中的星球施加引力。我相信我使用了正确的引力公式等,并且已经部分成功了——该行星会影响星球的轨迹,直到某个点为止,此时无论其角度如何,星星似乎会不停地加速并停止改变方向。有什么建议吗?(我知道星星不应该绕着行星转,而是反过来,但我已经编码完整个程序,所以请谅解)。

主类:

    import acm.graphics.GCompound;
    import acm.graphics.GImage;
    import acm.graphics.GLabel;
    import acm.graphics.GLine;
    import acm.graphics.GMath;
    import acm.graphics.GObject;
    import acm.graphics.GPen;
    import acm.graphics.GPoint;
    import acm.graphics.GRect;
    import acm.graphics.GOval;
    import acm.graphics.GRectangle;
    import acm.program.GraphicsProgram;
    import acm.util.RandomGenerator;
    import java.awt.Color;
    import java.awt.event.MouseEvent;
    import java.util.*;

    public class Space extends GraphicsProgram {
      public static int APPLICATION_WIDTH = 1000;
      public static int APPLICATION_HEIGHT = 1000;
      private int size = 15;
      public static double pMass = 1000;
      public static int sMass = 20;
      public static double G = 200;
      private RandomGenerator rand = new RandomGenerator();
      GOval planet, tempstar;
      shootingStar star;
      GLine line;
      double accel, xAccel, yAccel, xspeed, yspeed, angle;


      public void init(){
        planet = new GOval(APPLICATION_WIDTH/2, APPLICATION_HEIGHT/2, 30, 30);
        planet.setFilled(true);
        planet.setFillColor(rand.nextColor());
        add(planet);

      }


      public void mousePressed(GPoint point) {
        // draw a line
        tempstar = new GOval(point.getX() - size/2, point.getY() - size/2, size, size);
        tempstar.setFilled(true);
        tempstar.setColor(rand.nextColor());
        add(tempstar);
        line = new GLine(tempstar.getX() + size/2, tempstar.getY() + size/2, 
    point.getX(), point.getY());                             
        add(line);
        line.setVisible(true);
      }

      public void mouseDragged(GPoint point) {
        line.setEndPoint(point.getX(), point.getY());
      }

      public void mouseReleased(GPoint point){
        xspeed =            
    -.05*GMath.cosDegrees(getAngle(line))*GMath.distance(line.getStartPoint().getX(),         
    line.getStartPoint().getY(), line.getEndPoint().getX(), line.getEndPoint().getY());
        yspeed = 
    .05*GMath.sinDegrees(getAngle(line))*GMath.distance(line.getStartPoint().getX(), 
    line.getStartPoint().getY(), line.getEndPoint().getX(), line.getEndPoint().getY());
        System.out.println(xspeed + " " + yspeed);
        star = new shootingStar(xspeed, yspeed, this);
        if(xspeed != 0)
          add(star, tempstar.getX(), tempstar.getY());
        new Thread(star).start();
        remove(tempstar);
        remove(line);

      }

      private double getAngle(GLine line) {
        return GMath.angle(line.getStartPoint().getX(), line.getStartPoint().getY(), 
                           line.getEndPoint().getX(), line.getEndPoint().getY());
      }


      public void checkPlanet(){
        accel = .06*GMath.distance(star.getX(), star.getY(), planet.getX(), 
    planet.getY());
        angle = correctedAngle(GMath.angle(planet.getX(), planet.getY(), star.getX(), 
    star.getY()));       
        xAccel = accel*GMath.cosDegrees(GMath.angle(planet.getX(), planet.getY(), 
    star.getX(), star.getY()));
        yAccel = accel*GMath.sinDegrees(GMath.angle(planet.getX(), planet.getY(), 
    star.getX(), star.getY()));

        double newX = xspeed - xAccel*.01;
        double newY = yspeed + yAccel*.01;

        xspeed = newX + xAccel*Math.pow(.01, 2)/2;
        yspeed = newY + yAccel*Math.pow(.01, 2)/2;

        star.setSpeed(xspeed, yspeed);


      }

      public double correctedAngle(double x) {
        return (x%360.0+360.0+180.0)%360.0-180.0;
    }
    }

shootingStar类的相关部分:

     public void run() {
        // move the ball by a small interval
        while (alive) {
        oneTimeStep();
        }
      }

      // a helper method, move the ball in each time step
      private void oneTimeStep() {
        game1.checkPlanet();
        shootingStar.move(xSpeed, ySpeed);
        pause(20); 
      }

      public void setSpeed (double xspeed, double yspeed){
        xSpeed = xspeed;;
        ySpeed = yspeed;

      }
    }

编辑:

当前主类方法:

    public void checkPlanet(){
        double xDistance = star.getX() - planet.getX();
        double yDistance = star.getY() - planet.getY();
        double distance = Math.sqrt(Math.pow(xDistance, 2) + Math.pow(yDistance, 2));
        accel = G*pMass/Math.pow(distance, 2);

        xAccel = accel * xDistance/distance;
        yAccel = accel * yDistance/distance;

          xspeed += xAccel;

         yspeed += yAccel;

       star.setSpeed(xspeed, yspeed);

    }

当前的Star类方法:

    public void run() {
        while (alive) {
          oneTimeStep();
        }
      }

      private void oneTimeStep() {
        game1.checkPlanet();
        shootingStar.move(xSpeed, ySpeed);
        pause(20); 
      }

      public void setSpeed (double xspeed, double yspeed){
        xSpeed = xspeed;;
        ySpeed = yspeed;

      }
    }

在整整一天的阅读TDWTF之后,最终停留在这篇文章,我瞬间感到有责任问一下你是想模拟引力还是仅仅是背后的数学。 - Theodoros Chatzigiannakis
2个回答

1

哇,这比你“必须”做的要多得多。

如果物体在板上,请计算它与物体的距离。如果它比D远,则不执行任何操作。如果它距离D,则在其指向物体的方向上添加一小部分速度。假设它距离物体1000 X,500 z。只需执行简单的操作,例如除以100,并将其添加到物体速度中,使其向物体移动10 x和5 y。每次更新时都要再次添加速度。

您可能还需要一个最大速度。这样计算起来要容易得多,效果也很好,就像游戏STAR CONTROL中有一个行星或飞船会微微地相互引力。我用10个行星和一个恒星做到了这一点,用户基本上可以在每个行星上进行月球着陆。这是一次爆炸,但我从未将其变成真正的游戏。这具有快速计算的优势。如果您将地图制成环面,那么还有一些边缘条件,因此它们会穿过地图的侧面,但基本上所有操作都只是简单的加减法。

对于游戏来说,这已经足够好了。你不是在制作模拟器,而是在制作游戏。


0

我不确定,但是尝试将计算xAccel和yAccel值的部分更改为以下内容。

xDistance = XComponentObject1 - XComponentObject2; 

yDistance = YComponentObject1 - YComponentObject2;

(xDistance and yDistance can have negative values)

Distance = sqrt( xDistance^2 + yDistance^2 );

gConstant = constant Value for gravitational strenght in your world;

MassObject1 = some Mass;

MassObject2 = some other Mass;

Accel = gConstant*MassObject1*MassObject2 / (Distance^2 );

''NOW COMES THE IMPORTANT PART''

xAccel = Accel * xDistance/Distance;

yAccel = Accel * yDistance/Distance;

我认为你整个使用正弦和余弦的过程会产生很多难以追踪的错误。


谢谢 - 这肯定比我之前的方法更有效。然而,同样的问题似乎仍然存在 - 我该如何考虑到这样一个事实:当恒星被引入行星的引力场时,它应该(并且确实)加速。当它从另一侧“推出”(假设它不会与行星碰撞),它应该减速。我的恒星速度无限增加。 - user1811903
这听起来像是一个符号错误。之后你会如何处理加速度值?我不理解你代码的意义。试试这个:' NewXspeed = OldXspeed + xAcceltimefactor; ' NewYspeed = OldYspeed + yAcceltimefactor; ' - N_las
更新了当前代码的问题 - 仍然存在两个问题:1)引力场朝相反方向工作 - 所有物体都被推离中心行星,2)星星没有完成完整的轨道 - 它更像是一个部分轨道,它们只会越来越快,直到它们直线运动。有什么建议吗?非常感谢! - user1811903
关于你的第一个问题:只需使用负值作为你的重力常数即可。 - N_las

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接