Challenge 8 - Physics

Original Example by Eric Mixon and "fiddlization" and various cleanups and enhancements by strelz and Jeremy Jones

HTML

<script src="http://bigcat.cs.usm.edu/~emixon/sandbox/physics/js/ammo.js"></script>
<script src="https://github.com/mrdoob/three.js/raw/master/build/Three.js"></script>
<script src="https://github.com/mrdoob/three.js/raw/master/examples/js/RequestAnimationFrame.js"></script>
<script src="https://github.com/mrdoob/three.js/raw/master/examples/js/Stats.js"></script>
<script src="https://github.com/mrdoob/three.js/raw/master/examples/js/Detector.js"></script>

<div style="position:absolute;left:10px;top:10px;z-index:2000;font-size:70%"><b>Challenge 8 - Physics<br>(1) Build some other pile-of-shapes target (tower, pyramid, ..)<br> (2) Change gravity and appearnce to simulate some other heavenly body (moon, jupiter, mars,...) <br>point and dbl-click to fire block at wall<br>mouse keys to move view</div>

JavaScript

/*
*
*
*  I wish this code was commented more so I really knew what it was doing...
*  ...because I don't, and I really like physics.
*
*
*/




//PHYSICS SECTION
//ammo.js
// try changing physical properties
var mygravity = -1;
var mytargetmass = 100;
var myprojectilemass = 10;
var myprojectiletype = "sphere"; //"box" //"cone"  
var myprojectilespeed = 150;

var NUM = 15;
var NUMRANGE = [];
for (var i = 0; i <= NUM; i++)
NUMRANGE[i] = i + 1;
// Bullet-interfacing code
var collisionConfiguration = new Ammo.btDefaultCollisionConfiguration();
var dispatcher = new Ammo.btCollisionDispatcher(collisionConfiguration);
var overlappingPairCache = new Ammo.btDbvtBroadphase();
var solver = new Ammo.btSequentialImpulseConstraintSolver();
var dynamicsWorld = new Ammo.btDiscreteDynamicsWorld(dispatcher, overlappingPairCache, solver, collisionConfiguration);
dynamicsWorld.setGravity(new Ammo.btVector3(0, mygravity, 0));

var groundShape = new Ammo.btBoxShape(new Ammo.btVector3(1000, 50, 1000));

var bodies = [];

var groundTransform = new Ammo.btTransform();
groundTransform.setIdentity();
groundTransform.setRotation(new Ammo.btQuaternion(0, 0, -.5));
groundTransform.setOrigin(new Ammo.btVector3(0, -55, 0));

function addStaticBody(shape, trans) {
    var mass = 0;
    var localInertia = new Ammo.btVector3(0, 0, 0);
    var myMotionState = new Ammo.btDefaultMotionState(trans);
    var rbInfo = new Ammo.btRigidBodyConstructionInfo(0, myMotionState, shape, localInertia);
    var body = new Ammo.btRigidBody(rbInfo);

    dynamicsWorld.addRigidBody(body);
    bodies.push(body);
}

addStaticBody(groundShape, groundTransform);        

NUMRANGE.forEach(function(i) {
    if((myprojectiletype == "sphere") &&(i==1)){
       var boxShape = new Ammo.btSphereShape(2);
    }
    else if((myprojectiletype == "cone") &&(i==1)){
       var boxShape = new Ammo.btConeShape(1,2);
    }
    else{        
       var boxShape = new Ammo.btBoxShape(new Ammo.btVector3(1, 1, 1));
    }
    var...