使用 JavaScript 创建引力
Create gravity using JavaScript
我正在开发一个物理沙箱。每个 body 都有自己的属性,如质量、速度和加速度。默认情况下,所有物体都会落到地面上,但其中一些物体具有可以吸引其他物体的引力场。我试图计算 body 的移动向量并对它们求和,但它不起作用。如何正确实施这个景点?
class Body {
// Physical properties
position = { x: 0, y: 0 }
velocity = { x: 0, y: 0 }
acceleration = { x: 0, y: 0 }
mass = 1.0;
gravity = 0.0;
// ... class constructord and etc.
move = () => {
if(this.checkCollision == true) return;
this.setVelocityVector();
this.position.y += this.velocity.y;
this.position.x += this.velocity.x;
}
setVelocityVector = () => {
// By default body falls down
this.acceleration.y = this.mass * GRAVITY_ACCELERATION;
// Check gravity body's attraction
activeParticles.forEach(body => {
if(body.gravity > 0) {
// Calculate gravity power with Newton's formula:
// F = G * m1 * m2 / r^2
var rr = (this.position.x - body.position.x) * (this.position.x - body.position.x)
+ (this.position.y - body.position.y) * (this.position.y - body.position.y);
var a = body.gravity * this.mass * body.mass / rr;
this.acceleration.x += a;
this.acceleration.y += a;
}
});
this.velocity.y += this.acceleration.y;
this.velocity.x += this.acceleration.x;
}
}
正如评论已经指出的,您应该先计算所有粒子的加速度,然后再移动它们。
您正在为加速度增加力。
你计算的只是力的大小,而不是方向。这是正确的(我认为)版本:
var rr = (this.position.x - body.position.x) * (this.position.x - body.position.x)
+ (this.position.y - body.position.y) * (this.position.y - body.position.y);
var a = body.gravity * body.mass / rr;
this.acceleration.x += (body.position.x - this.position.x) * a / sqrt(rr);
this.acceleration.y += (body.position.y - this.position.y) * a / sqrt(rr);
我正在开发一个物理沙箱。每个 body 都有自己的属性,如质量、速度和加速度。默认情况下,所有物体都会落到地面上,但其中一些物体具有可以吸引其他物体的引力场。我试图计算 body 的移动向量并对它们求和,但它不起作用。如何正确实施这个景点?
class Body {
// Physical properties
position = { x: 0, y: 0 }
velocity = { x: 0, y: 0 }
acceleration = { x: 0, y: 0 }
mass = 1.0;
gravity = 0.0;
// ... class constructord and etc.
move = () => {
if(this.checkCollision == true) return;
this.setVelocityVector();
this.position.y += this.velocity.y;
this.position.x += this.velocity.x;
}
setVelocityVector = () => {
// By default body falls down
this.acceleration.y = this.mass * GRAVITY_ACCELERATION;
// Check gravity body's attraction
activeParticles.forEach(body => {
if(body.gravity > 0) {
// Calculate gravity power with Newton's formula:
// F = G * m1 * m2 / r^2
var rr = (this.position.x - body.position.x) * (this.position.x - body.position.x)
+ (this.position.y - body.position.y) * (this.position.y - body.position.y);
var a = body.gravity * this.mass * body.mass / rr;
this.acceleration.x += a;
this.acceleration.y += a;
}
});
this.velocity.y += this.acceleration.y;
this.velocity.x += this.acceleration.x;
}
}
正如评论已经指出的,您应该先计算所有粒子的加速度,然后再移动它们。
您正在为加速度增加力。
你计算的只是力的大小,而不是方向。这是正确的(我认为)版本:
var rr = (this.position.x - body.position.x) * (this.position.x - body.position.x) + (this.position.y - body.position.y) * (this.position.y - body.position.y); var a = body.gravity * body.mass / rr; this.acceleration.x += (body.position.x - this.position.x) * a / sqrt(rr); this.acceleration.y += (body.position.y - this.position.y) * a / sqrt(rr);