模拟恒星的引力?

时间:2012-11-09 10:46:35

标签: java space gravity motion

我正在制作一个游戏,其中玩家将(释放鼠标点击)以某个方向拍摄一个“星星”,其初始速度取决于他在释放之前拖动鼠标的距离。我在画布上有一个“行星”(静止圆),我想在移动的行星上施加一个引力。我相信我正在使用正确的引力等公式,而且它部分工作 - 行星影响行星的轨迹直到某一点,当恒星似乎无休止地加速并停止根据它的角度改变方向到了明星。 任何建议? (我知道恒星不应该绕行星运行,反之亦然。我用互换的名称对整个事物进行编码,原谅)。

主要课程:

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

射击星级的相关部分:

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

    }

当前星级方法:

    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;

      }
    }

2 个答案:

答案 0 :(得分:1)

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

如果物品在板上,则计算它与物体的距离。如果它比D做得更远。如果它离开D那么它就在物体的引力范围内。只需在指向物体的位置添加少量速度即可。假设它距离1000 X,距离500 z。只做一些简单的事情,比如除以100,然后将其添加到物体速度,使其朝向物体移动10 x和5 y。每次更新时都会再次添加速度。

您可能也想要最大速度。这很容易计算得很好,并且会给你带来像游戏STAR CONTROL那样的效果,那里有一颗行星,或者船只重力相互拉近一点点。我用10个行星和一颗星做了这个,用户基本上可以用每个行星做月球着陆器。这是一场爆炸,但我从未把它变成真正的游戏。这具有快速计算的优点。有一些边缘条件,比如如果你让地图成为圆环,那么它们会在地图的两侧扭曲,但基本上它只是简单的加法和减法。

这对游戏来说足够了。你不是在做模拟器。你正在制作游戏。

答案 1 :(得分: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;

我认为你的整个yadayada与正弦和余弦创造了一大堆难以追踪的错误。