Code viewer for World: cafe Collision World

// Cloned by Test Account  on 8 Feb 2018 from World "Collision World" by Starter user
// Please leave this clone trail here.

// Demo of Physijs based World

// From here, with many changes:
// http://chandlerprall.github.io/Physijs/examples/collisions.html

// These 3 have default values, so this section is optional:

AB.clockTick       = 100;

// Speed of run: Step every n milliseconds. Default 100.

AB.maxSteps        = 2000;

// Length of run: Maximum length of run in steps. Default 1000.

AB.screenshotStep  = 200;

// Take screenshot on this step. (All resources should have finished loading.) Default 50.

const BOXSIZE       = 5 ;

const BOXPOS        = BOXSIZE * 2 ; 	    	// range of box x,z start positions
const BOXHEIGHT     = BOXSIZE * 10 ; 			// y start position of boxes

const HEAVYIMPACT   = 30 ;   				 	// heavy impacts have y velocity greater than this

const GROUNDSIZE    = BOXSIZE * 40;

const startRadius   = BOXSIZE * 15 ;
const maxRadius     = BOXSIZE * 250 ;

// https://commons.wikimedia.org/wiki/Smiley

// credit:
// http://soundbible.com/715-Dropped-Wooden-Floor.html

const SKYCOLOR  = 0xffffcc;

const GROUND_FRICTION 		= 0.8;
const GROUND_RESTITUTION 	= 0.3;

const BOX_FRICTION 			= 0.6;
const BOX_RESTITUTION 		= 0.3;

function randomfloatAtoB ( A, B )
{
return ( A + ( Math.random() * (B-A) ) );
}

function randomintAtoB ( A, B )
{
return  ( Math.round ( randomfloatAtoB ( A, B ) ) );
}

function World() {

var  box_geometry, box_texture_0, box_texture_1 ;

var step = 0;

var currentbox ;

function initScene()
{

// --- Light ------------------------------------------------------------------

var  light  = new THREE.DirectionalLight( 0xFFFFFF, 1.3 );

light.position.set ( BOXHEIGHT, (BOXHEIGHT * 2), 0 );
light.target.position.copy( threeworld.scene.position );

// how far away to draw shadows:

// higher quality shadows at expense of computation time
// slower PCs can struggle if this is too high
// World author could make it an option at run-time - high or low graphics option - or just shadows on/off

// --- Ground ------------------------------------------------------------------

var  ground_material, ground;

ground_material = Physijs.createMaterial(
GROUND_FRICTION,
GROUND_RESTITUTION );

ground_material.map.wrapS = THREE.RepeatWrapping;
ground_material.map.wrapT = THREE.RepeatWrapping;
ground_material.map.repeat.set( 3, 3 );

ground = new Physijs.BoxMesh(
new THREE.BoxGeometry( GROUNDSIZE, 1, GROUNDSIZE ),
ground_material,
0 );					// mass

// --- Box ------------------------------------------------------------------

box_geometry = new THREE.BoxGeometry( BOXSIZE, BOXSIZE, BOXSIZE );

createBox();
}

function createBox()
// creates one random box
{
// think each box needs its own material

var box_material_0  = Physijs.createMaterial(
new THREE.MeshLambertMaterial({ map: box_texture_0 }),
BOX_FRICTION,
BOX_RESTITUTION	);

var box = new Physijs.BoxMesh (	box_geometry, box_material_0 );

box.collisions = 0;

box.position.set(  randomintAtoB(-BOXPOS,BOXPOS),		BOXHEIGHT,		randomintAtoB(-BOXPOS,BOXPOS)	);

box.rotation.set( 	Math.random() * Math.PI,	Math.random() * Math.PI,	Math.random() * Math.PI		);

box.addEventListener( 'collision', function( other_object, relative_velocity, relative_rotation, contact_normal )
// box has collided with `other_object` with an impact speed of `relative_velocity` and a rotational force of `relative_rotation` and at normal `contact_normal`
{
// console.log ( relative_velocity );
var mainImpact = relative_velocity.y;		// impact in direction of gravity
// console.log ( Math.abs(mainImpact) );
if (  Math.abs(mainImpact) > HEAVYIMPACT )			// main impact, not lesser ones as it settles
{
soundCollision();

var box_material_1 = Physijs.createMaterial(
new THREE.MeshLambertMaterial({ map: box_texture_1 }),
BOX_FRICTION,
BOX_RESTITUTION	);

this.material = box_material_1;
}
});

currentbox = box;

// Create another box in random (m to n) milliseconds time:
// Note: Put this in createBox, not nextStep - runs on independent timer.

setTimeout( createBox, randomintAtoB ( 200, 500 ) );
//	setTimeout( createBox, randomintAtoB ( 200, 1000 ) );

};

function soundCollision()
{
var x = "<audio    src=" + SOUND_COLLISION + "   autoplay  > </audio>";
\$("#user_span2").html( x );
}

// --- public interface ----------------------------------------------------------------------

this.endCondition = false;

this.newRun = function()
{

// sets up renderer, scene, camera

// scene is Physijs.Scene, not THREE.Scene
// can adjust scene - change gravity from default:

threeworld.scene.setGravity ( new THREE.Vector3( 40, -30, 0 ));
//		threeworld.scene.setGravity ( new THREE.Vector3( -30, -30, 0 ));

// multiply mouse scrolls by some factor to speed up/slow down movement:
//	  threehandler.SCROLLFACTOR 	= 0.1;

initScene();

threeworld.lookat.copy ( threeworld.scene.position );

threeworld.scene.simulate();		// Physics simulate - runs on independent timer to AB nextStep

};

this.nextStep = function()
{
// nothing to do each step
// physics simulate runs on independent timer
// create box runs on another independent timer

// for camera control, follow current falling box:
if ( currentbox )
threeworld.follow.copy ( currentbox.position );

step++;
\$("#user_span1").html( "Step: " + step );
};

this.endRun = function()
{
};

}