1
我想在我的3D環境中做一些簡單的重力處理(我使用three.js)。我有一些代碼,但它不起作用。我希望這只是一個愚蠢的錯誤。 編輯:替換舊代碼在JavaScript中簡單的三維粒子引力?
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環境模擬許多顆粒最有效任何提示(我很新的物理+模擬),那會很棒!