我试图在我的3D环境中进行一些简单的重力处理(我使用three.js)。我有一些代码,但它不起作用。我希望它在某个地方只是一个愚蠢的错误。 编辑:替换旧代码
function handleGravity() {
for(var j = 0; j < spheres.length; j++) {
for(var i = 0; i < spheres.length; i++) {
var r1 = new Array( spheres[j].position.x, spheres[j].position.y, spheres[j].position.z);
var r2 = new Array( spheres[i].position.x, spheres[i].position.y, spheres[i].position.z);
var r12 = new Array(r2[0]-r1[0], r2[1]-r1[1], r2[2]-r1[2]);
var r12UnitVector = new Array( r12[0]/Math.abs(r12[0]), r12[1]/Math.abs(r12[1]),r12[2]/Math.abs(r12[2]) );
var m1 = masses[j];
var m2 = masses[i];
var r12Squared = r12[0]*r12[0] + r12[1]*r12[1] + r12[2]*r12[2];
var a12 = new Array( -(gravConst*m2/r12Squared)*r12UnitVector[0], -(gravConst*m2/r12Squared)*r12UnitVector[1], -(gravConst*m2/r12Squared)*r12UnitVector[2]);
velocities[j][0] += a12[0]*timePassed;
velocities[j][1] += a12[1]*timePassed;
velocities[j][2] += a12[2]*timePassed;
}
}
}
如果您发现它有任何问题,或者给我任何关于使用JavaScript在3D环境中模拟许多粒子的提示,那么有效(我对物理+模拟非常新)那太棒了!
答案 0 :(得分:0)
这似乎有效:
function handleGravity() {
for(var j = 0; j < spheres.length; j++) {
for(var i = 0; i < spheres.length; i++) {
if(i != j) {
var r1 = new Array( spheres[j].position.x, spheres[j].position.y, spheres[j].position.z);
var r2 = new Array( spheres[i].position.x, spheres[i].position.y, spheres[i].position.z);
var r12 = new Array(r2[0]-r1[0], r2[1]-r1[1], r2[2]-r1[2]);
var r12UnitVector = new Array( r12[0]/Math.abs(r12[0] + r12[1] + r12[2])/3, r12[1]/Math.abs(r12[0] + r12[1] + r12[2])/3, r12[2]/Math.abs(r12[0] + r12[1] + r12[2])/3 );
var m1 = masses[j];
var m2 = masses[i];
var r12Squared = r12[0]*r12[0] + r12[1]*r12[1] + r12[2]*r12[2];
var a12 = new Array( -(gravConst*m2/r12Squared)*r12UnitVector[0], -(gravConst*m2/r12Squared)*r12UnitVector[1], -(gravConst*m2/r12Squared)*r12UnitVector[2]);
velocities[j][0] -= a12[0]*timePassed;
velocities[j][1] -= a12[1]*timePassed;
velocities[j][2] -= a12[2]*timePassed;
}
}
}
}