我一直在用Java进行粒子模拟,对于跨越式的集成我有点困惑。
我读到它可以节约能源"并且这意味着当他们的分离距离接近零时,我的粒子将不再以荒谬的速度射出。但它仍然会发生。我是否误解了跨越式整合的观点,还是我错误地实现了它?
以下是:http://screencast.com/t/UcMNxucKVwn
我不会发现仅基于当前位置的力量与平均值更新速度之间的结果有任何差异。
除非我需要,否则我不会发布我的整个项目,因为它很长。使用Java swing计时器运行模拟,该计时器只运行updatePosition,然后在每个主体上运行updateVelocity,然后全部绘制它们。
这是身体类:
public class Body {
private final double G = 100; // Gravity strength
private double xPos, yPos;
private double xVel, yVel;
private double radius;
private double mass;
private double oldFx;
private double oldFy;
public Body(double xPos, double yPos, double xVel, double yVel, double radius) {
this.xPos = xPos;
this.yPos = yPos;
this.xVel = xVel;
this.yVel = yVel;
this.radius = radius;
this.mass = Math.PI * radius * radius;
this.oldFx = 0;
this.oldFy = 0;
}
void updatePosition() {
xPos += xVel;
yPos += yVel;
}
void updateVelocity(Body b) {
double DT = BHMain.DT/1000.0; // Time step
// Force on this body
double dx = xPos - b.xPos;
double dy = yPos - b.yPos;
double r = Math.sqrt((dx*dx + dy*dy));
double newF = - G * mass * b.mass / (r*r);
double angle = Math.atan2(dy, dx);
double newFx = newF*Math.cos(angle);
double newFy = newF*Math.sin(angle);
double Fx = (oldFx + newFx) / 2;
double Fy = (oldFy + newFy) / 2;
// Update velocity. a = F / m. v = v0 + a*t
xVel += DT * Fx / mass;
yVel += DT * Fy / mass;
oldFx = newFx;
oldFy = newFy;
}
void drawBody(Graphics g) {
int diameter = (int) (2 * radius);
int x = (int) (xPos - radius);
int y = (int) (yPos - radius);
g.fillOval(x, y, diameter, diameter);
}
}