Java 模拟恒星的引力?
我正在制作一个游戏,玩家将(在释放鼠标点击时)以初始速度朝某个方向射出一颗“星星”,这个初始速度取决于他在释放鼠标之前拖动鼠标的距离。画布上有一个“行星”(静止的圆圈),我想对移动的行星施加引力。我相信我使用的是正确的引力公式,我认为它部分起作用了——行星影响着行星的轨迹,直到某一点,当恒星似乎无休止地加速,并停止根据它与恒星的角度改变方向时有什么建议吗?(我知道恒星不应该绕行星运行,相反。我用互换的名称对整个事情进行了编码,所以请原谅) 主要类别:Java 模拟恒星的引力?,java,space,gravity,motion,Java,Space,Gravity,Motion,我正在制作一个游戏,玩家将(在释放鼠标点击时)以初始速度朝某个方向射出一颗“星星”,这个初始速度取决于他在释放鼠标之前拖动鼠标的距离。画布上有一个“行星”(静止的圆圈),我想对移动的行星施加引力。我相信我使用的是正确的引力公式,我认为它部分起作用了——行星影响着行星的轨迹,直到某一点,当恒星似乎无休止地加速,并停止根据它与恒星的角度改变方向时有什么建议吗?(我知道恒星不应该绕行星运行,相反。我用互换的名称对整个事情进行了编码,所以请原谅) 主要类别: import acm.graphic
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;
}
}
当前的星级方法:
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;
}
}
我不确定,但是试着把计算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加上正弦和余弦会产生一大堆难以追踪的错误。哇,这比你“不得不”做的要多得多 如果物体在板上,计算它与物体的距离。如果它比我更远,我什么也不做。如果它离开了,那么它就在物体的引力范围内。只需向它添加少量指向对象的速度。假设距离为1000 X和500 z。只需要做一些简单的事情,比如除以100,然后把它加到物体的速度上,这样它就可以朝着物体移动10 x和5 y。每次更新时,请再次添加速度 你可能还需要一个最大速度。这是一个更容易计算的好方法,并且会给你一些效果,比如在游戏中有一颗行星的星际控制中,或者飞船在重力作用下相互吸引一点点。我用10颗行星和一颗恒星做了这个,用户基本上可以用每颗行星做月球着陆器。这是一个爆炸,但我从来没有把它变成一个真正的游戏。这具有计算速度极快的优点。有一些边缘条件,比如,如果你把地图做成一个圆环,那么它们会在地图的两边扭曲,但基本上都是简单的加法和减法
这对一场比赛来说已经足够好了。你不是在做模拟器。你在做游戏。谢谢-这肯定比我之前做的更有效率。同样的问题似乎仍然存在——我如何解释当恒星被吸引到行星的引力场中时,它应该(并且确实)加速的事实。当它被“推”到另一边时(假设它没有与行星相撞),它应该会减速。我的星体速度无休止地增加。这听起来像是一个符号错误。之后,您将如何处理Acceleratien值?我不明白你的代码的意思。试试看:“NewXspeed=OldXspeed+xAcceltimefactor;”新速度=旧速度+加速系数;'更新了当前代码的问题-仍然有两个问题:重力场在相反方向工作-所有物体都被推离中心行星,2)恒星没有完成完整的轨道-更像是一个部分轨道,它们越来越快,直到沿着直线运行。有什么建议吗?非常感谢你!关于你的第一个问题:只用一个负值来表示你的引力常数。在读了一整天的TDWTF之后,最终停在了,我立刻觉得有义务问你是想模拟引力还是仅仅是它背后的数学。