Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm trying to do some simple gravity handling in my 3D environment (I'm using three.js). I've got some code, but it doesn't work. I'm hoping it's just a silly bug somewhere. Note: This is a cross-question from stack overflow since I've got no responses

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;
        }

    }
}

If you can see anything wrong with it, or give me any tips on using JavaScript to simulate many particles in a 3D environment most efficiently (I'm very new to physics + simulation), that would be awesome!

share|improve this question

closed as too localized by Sean Middleditch, bummzack, Josh Petrie, Byte56, John McDonald Apr 5 '13 at 17:39

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

    
What do you mean by "simulating gravity" ? Do you want to make animation of falling stuff, or just compute positions of objects after some time? Do you want objets to collide with each other? –  Ivan Kuckir Apr 1 '13 at 11:04
    
Gravity between the particles. So they act like planets in space. I think I've solved it though - thanks –  JoeRocc Apr 1 '13 at 12:49
add comment

1 Answer 1

This seems to work. It's a little clunky though - it makes me think there's some sort of rogue rounding going on:

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;
            }
        }

    }
}
share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.