gpt4 book ai didi

java - 模拟恒星的引力?

转载 作者:搜寻专家 更新时间:2023-11-01 00:51:35 25 4
gpt4 key购买 nike

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

主类:

    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;

}
}

最佳答案

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

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

您可能还需要最大速度。这很容易计算,效果很好,并且会给你带来像 STAR CONTROL 游戏中有行星的效果,或者飞船在重力作用下相互拉近一点点。我对 10 个行星和一颗恒星进行了此操作,用户基本上可以对每个行星进行月球着陆器。这是一场爆炸,但我从未将其变成真正的游戏。这具有计算速度非常快的优势。有一些边缘条件,比如如果你把 map 做成环面,它们就会在 map 的边上弯曲,但基本上这只是简单的加减法。

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

关于java - 模拟恒星的引力?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13306596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com