# PixAnvil Particle System Gravity Demo

Er, JavaScript or WebGL doesn't seem to be running, so basically all you're going to see is the bare code...

 // Loop over our particles, and update each one. var holeCenter=new vec3(0.0,0.0,0.0); var G=6.67408e-11; var mBox=10.0e7; // mass of each box (Kg) var mHole=1e+11; // black hole in center (Kg) // Return the force on a box due to gravity with this mass, at this distance vector R var gravity=function(m,R) { var r2=R.lengthSquared(); // r^2 var softening2=0.001; // softening length, squared r2+=softening2; // soften gravity (also eliminates self-gravity) // See: http://www.ifa.hawaii.edu/~barnes/research/smoothing/soft.pdf // Scalar f = G m1 m2 / r^2 // Vector F = normalize(R) * f // Vector F = R * G m1 m2 / r^3 var F=R.t(G*m*mBox/(r2*Math.sqrt(r2))); //var Fmax=10000.0; // force limit (hard to scale with mass) //if (F.length()>Fmax) F=normalize(F).te(Fmax); return F; } for (var i = 0; i < SPS.nbParticles; i++) { var p=SPS.particles[i]; // Gravity from black hole in center var F=gravity(mHole,holeCenter.m(p.P)); // Gravity from mutual particles (naive N^2) for (var j = 0; j < SPS.nbParticles; j++) { var pj=SPS.particles[j]; F.pe(gravity(mBox,pj.P.m(p.P))); } p.V.pe(F.t(lib.dt/mBox)); // p.V.z-=9.8*lib.dt; // gravity p.P.pe(p.V.t(lib.dt)); if (lib.key['w']) p.rotation.x+=1.0*lib.dt; //if (p.V.z<1.0) // p.color.a=p.V.z; // alpha blend to zero //if (p.V.z<=0.0) // recycleParticle(p); } // Export particles to screen: SPS.setParticles(); // See: https://doc.babylonjs.com/overviews/solid_particle_system var SPS=new BABYLON.SolidParticleSystem("SPS", scene); var box = BABYLON.MeshBuilder.CreateBox("box", {size:0.1}, scene); // Add n copies of the box to the SPS: var n=100; SPS.addShape(box,n); box.dispose(); // don't draw single box var mesh=SPS.buildMesh(); // Make the sky black scene.clearColor = new BABYLON.Color3( 0.0, 0.1, 0.2); // Allow alpha blending SPS.mesh.hasVertexAlpha = true; /// (re) initialize a particle var recycleParticle = function(p) { // Generate position from a pancake var OK=false; do { p.P.rand(10.0); if (Math.abs(p.P.z)<0.3 // Z axis && p.P.length()>0.2 // hole && p.P.length()<3.0) OK=true; } while (!OK); // Orbit direction var Odir=normalize(cross(p.P,new vec3(0.0,0.0,-1.0))); var orbitalVelocity=Math.sqrt(7.0/p.P.length()); p.V=Odir.te(orbitalVelocity); p.rotation.set(0.0,0.0,0.0); var c=(new vec3).rand(1.0).pe(new vec3(0.5,0.5,0.5)); p.color.r=c.x; p.color.g=c.y; p.color.b=c.z; p.color.a=1.0; } // Initialize all particles: for (var i = 0; i < SPS.nbParticles; i++) { var p=SPS.particles[i]; p.i=i; // Link over to vector names: p.P=p.position; p.V=p.velocity; // Initial setup is treated like recycling. recycleParticle(p); } lib.createGround(0); // User Interface (UI) code: