javascript - 向量数学在其中一个函数中关闭
问题描述
我打算在 2D 平面上模拟重力加速度(简化,没有重力常数)。我的代码在控制台中被解释没有任何错误,但是两个圆圈没有慢慢地相互加速,而是相互远离。加速度似乎保持在 0。我无法理解这种行为。
// orbiters is gonna be the array containing the two celestial bodies
var orbiters;
var sun;
var earth;
function setup() {
frameRate(.3);
console.log("starting the program")
createCanvas(640, 480);
sun = new Orbiter(10, 0, 0, width/2, height/2);
earth = new Orbiter (1, 0, 0, width/5, height/4);
orbiters = [sun, earth];
}
function draw() {
console.log("starting the loop")
background(15, 40, 20);
// [missing: calculate gravity acceleration], then update position, display position.
orbiters.forEach(function(celBody) {
celBody.update();
})
orbiters.forEach(function(celBody) {
celBody.display();
})
// for each celestial body, accelerate it towards every other celestial body
orbiters.forEach(function(celBody){
orbiters.forEach(function (otherCelBody){
if(celBody != otherCelBody){
celBody.gravityAccelerate(otherCelBody.weight, otherCelBody.pos)
}
})
})
}
// No volume for now: describe a body with weight, velocity, position
function Orbiter(weight, vx, vy, posx, posy) {
this.weight = weight;
this.pos = createVector(posx, posy);
this.vel = createVector(vx, vy);
this.acc = createVector(0, 0);
//this finds an acceleration and an angle and gives out a vector to accelerate a body by
this.gravityAccelerate = function(otherBodyWeight, otherBodyPosition) {
// calculate magnitude
gravDistance = sqrt(sq(otherBodyPosition.x - this.pos.x)+sq(otherBodyPosition.y - this.pos.y));
// calculate angle
gravHeading = createVector(otherBodyPosition.sub(this.pos)).heading();
// calculate force and divide by own mass(acceleration amount)
gravForce = (this.weight * otherBodyWeight) / gravDistance;
// calculate gravitational acceleration's magnitude as a vector
gravAccMag = gravForce / this.weight;
// apply amount to angle and give a vector
console.log("gravity acceleration = ")
gravAccVec = createVector(Math.cos(gravHeading), Math.sin(gravHeading));
// display gravitational acceleration vector
console.log(gravAccVec.x);
console.log(gravAccVec.y);
console.log("this body's acceleration = ");
console.log(this.acc.x);
console.log(this.acc.y);
this.acc = this.acc.add(gravAccVec);
console.log("this body's position")
console.log(this.pos.x)
console.log(this.pos.x)
}
this.update = function() {
this.vel.add(this.acc);
this.pos.add(this.vel);
}
this.display = function() {
this.size = sqrt(this.weight * 10);
fill (60, 180, 70);
ellipse(this.pos.x, this.pos.y, this.size, this.size);
}
}
我尝试降低帧率以更好地分析执行,两个圆圈的初始位置似乎正确。它们似乎被排斥而不是被吸引,更奇怪的是,我看到了位置的变化,但总加速度 (this.acc) 和重力加速度变化 (gravAccVec) 的值为 0 并且不改变,并且航向的重力加速度矢量是null
,所以我不明白是什么导致了排斥运动。改变权重值不会影响结果,甚至切换正弦和余弦来评估重力矢量的角度也不会改变任何东西:
body1(x,y) body2(x,y)
320,240 -192,-120
512,360 -704,-480
1216,840 -1920,-1320
3136,2160 -5056,-3480
要执行此代码,您需要导入 p5.js 库。
解决方案
推荐阅读
- java - 这个 lambda 表达式中的 imageProxy 参数是如何定义的?
- flutter - 如何使用 Radius CustomPainter 像这个样机一样创建弧
- javascript - Onclick 按钮只能工作一次。div 滑块 scrollIntoView
- mysql - 用于更改工作台中存在的所有模式的 SQL 查询?
- javascript - 分页在php网页中的page2上不起作用
- haskell - 字符串加倍?
- python - 用于命名字符串常量的 Python 构造
- python - 通过python合并两个外部边缘
- python-3.x - 如何自动将图像传输到文件夹
- c++ - list::iterator 对移至列表无效?