physics - Simple 3D particle gravity in javascript? -


i'm trying simple gravity handling in 3d environment (i'm using three.js). i've got code, doesn't work. i'm hoping it's silly bug somewhere. edit: replaced old code

function handlegravity() {     for(var j = 0; j < spheres.length; j++) {          for(var = 0; < 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;         }      } } 

if can see wrong it, or give me tips on using javascript simulate many particles in 3d environment efficiently (i'm new physics + simulation), awesome!

this seems work:

function handlegravity() {     for(var j = 0; j < spheres.length; j++) {          for(var = 0; < 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;             }         }      } } 

Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -