Я пытаюсь смоделировать проблему N-Body, используя Jpanels.Мои векторы силы работают так, как будто два тела взаимодействуют так, как они должны быть, когда они находятся близко друг к другу.Однако, когда два тела находятся далеко друг от друга, их силы невелики.JPanel допускает только приращения x и y позиций, по крайней мере, 1, что означает, что скорость должна быть некоторым натуральным числом.Это означает, что ускорение также должно быть некоторым натуральным числом.
Это приводит к двум очень плохим решениям:
Приведите ускорение от двойного к целому.Это приводит к ускорению, равному 0, когда два тела находятся далеко друг от друга.
Math.Ceil () или Math.Floor () ускорение в соответствии с относительными положениями x и y.Это означает, что ускорение всегда будет по крайней мере -1 или 1, даже когда два тела находятся далеко друг от друга.
Оба решения отстой.
public class Body {
int mass;
int x;
int y;
int velx = 0;
int vely = 0;
int accx = 0;
int accy = 0;
int WIDTH ;
int HEIGHT ;
boolean fixed;
final int GFIELD = 20;
Space world;
public Body(int x, int y, int mass,boolean fixed, Space world) {
this.x = x;
this.y = y;
this.WIDTH=mass;
this.HEIGHT=mass;
this.mass = mass;
this.fixed=fixed;
this.world = world;
}
public double distanceFrom(Body other) {
return (Math.sqrt(Math.pow(x-other.x, 2) +Math.pow(y-other.y, 2)));
}
public void update() {
accx = 0;
accy = 0;
if(!fixed){
for (int i = 0; i < world.bodies.size(); i++) {
if((world.bodies.get(i).x!=this.x)&&(world.bodies.get(i).y!=this.y)){
Body otherBody = world.bodies.get(i);
double GMm = (GFIELD * mass * otherBody.mass);
double force = GMm/Math.pow(this.distanceFrom(otherBody), 2);
double forcex = force * ((otherBody.x-this.x)/distanceFrom(otherBody));
double forcey = force * ((otherBody.y-this.y)/distanceFrom(otherBody));
//my ceil and floor method, it sucks
if(this.x<otherBody.x){
accx+=(int)Math.ceil(forcex/mass);
}else if(this.x>otherBody.x){
accx+=(int)Math.floor(forcex/mass);
}
if(this.y<otherBody.y){
accy+=(int)Math.ceil(forcey/mass);
}else if(this.x>otherBody.x){
accy+=(int)Math.floor(forcey/mass);
}
}
}
velx += accx;
vely += accy;
}
public void draw() {
world.g.drawOval(x, y, WIDTH, HEIGHT);
}
}