有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java模拟恒星的引力?

我正在制作一个游戏,玩家将(释放鼠标点击)以初始速度朝某个方向射出一颗“星星”,这个初始速度取决于他在释放鼠标之前拖动鼠标的距离。我在画布上有一个“行星”(静止圆),我想对移动的行星施加引力。我相信我使用的是正确的引力公式,我认为它部分起作用了——行星影响着行星的轨迹,直到某一点,当恒星似乎无休止地加速,并停止根据它与恒星的角度改变方向时有什么建议吗?(我知道恒星不应该绕行星运行,相反。我用互换的名称对整个事情进行了编码,所以请原谅

主要类别:

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

    }

当前的星级方法:

    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) 个答案

  1. # 1 楼答案

    哇,这比你“必须”去做的要努力得多

    如果物体在板上,计算它与物体的距离。如果它比我更远,我什么也不做。如果它消失了,那么它就在物体的引力范围内。只需增加一点速度,指向物体即可。假设距离为1000 X和500 z。只需要做一些简单的事情,比如除以100,然后把它加到物体的速度上,这样它就会朝着物体移动10 x和5 y。每次更新时,再次添加速度

    你可能还需要一个最大速度。这是一个更容易计算的好方法,它会给你带来一些效果,比如在游戏中有一颗行星的星际控制中,或者飞船在引力作用下相互吸引一点点。我用10颗行星和一颗恒星做了这个,用户基本上可以用每颗行星做月球着陆器。这是一个爆炸,但我从来没有把它变成一个真正的游戏。它的优点是计算速度非常快。这里有一些边缘条件,比如如果你把地图做成一个圆环,这样它们就会在地图的两边扭曲,但基本上这都只是简单的加法和减法

    这对一场比赛来说已经足够好了。你不是在做模拟器。你在做游戏

  2. # 2 楼答案

    我不确定,但是试着把计算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加上正弦和余弦会产生一大堆难以追踪的错误