Code viewer for World: Cloned Collision World

// Demo of Physijs based World

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



 
const	 	CLOCKTICK 	= 100;				// speed of run - move things every n milliseconds
const		MAXSTEPS 	= 2000;				// length of a run 
 
const 	SCREENSHOT_STEP = 200;			// take screenshot on this step (all resources should have finished loading) 




const BOXSIZE = 40 ;
const BOXPOS = 10; 		// range of box start positions 

				
const LIGHTRADIUS = BOXSIZE * 5 ;

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



	
	const TEXTURE_FILE_0   	= '/uploads/starter/smiley.png' ;
	const TEXTURE_FILE_1   	= '/uploads/starter/ghost.3.png' ;
	
	
// https://commons.wikimedia.org/wiki/Smiley
	
	
	const TEXTURE_GROUND 	= '/uploads/starter/rocks.jpg' ;

	const SOUND_COLLISION 	= '/uploads/starter/wooden.mp3' ;
 
 // credit:
 // http://soundbible.com/715-Dropped-Wooden-Floor.html
 
 
const SKYCOLOR  = 0xffffcc;    

const HIGH_FRICTION 	= 0.8; 		
const MEDIUM_FRICTION 	= 0.6; 		
	 
const LOW_RESTITUTION 	= 0.3; 		 

const MUSIC_BACK  = '/uploads/kian/8-bit-Dancer2.mp3' ;



function initMusic()
{
        // put music element in one of the spans
        var x = "<audio  id=theaudio  src=" + MUSIC_BACK + "   autoplay loop> </audio>" ;
        $("#user_span1").html( x );
} 


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_material_0, box_material_1 ;

	var step = 0;

	var currentbox ;
	

function initScene()
{

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

	var      light;

	light = new THREE.DirectionalLight( 0xFFFFFF, 1.3 );
	light.position.set( LIGHTRADIUS, startRadius, -LIGHTRADIUS );
	light.target.position.copy( threeworld.scene.position );

	light.castShadow = true;
	
	light.shadow.camera.left = -60;
	light.shadow.camera.top = -60;
	light.shadow.camera.right = 60;
	light.shadow.camera.bottom = 60;
	light.shadow.camera.near = 20;
	light.shadow.camera.far = 200;
	light.shadow.bias = -0.0001
	light.shadow.mapSize.width = light.shadow.mapSize.height = 2048;

	threeworld.scene.add( light );

	

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

	var  ground_material, ground;

	var loader = new THREE.TextureLoader();
	
	ground_material = Physijs.createMaterial(
		new THREE.MeshLambertMaterial({ map: loader.load( TEXTURE_GROUND ) }),
		HIGH_FRICTION,
		LOW_RESTITUTION );

//	ground_material.map.wrapS = ground_material.map.wrapT = THREE.RepeatWrapping;
//	ground_material.map.repeat.set( 3, 3 );
	
	ground = new Physijs.BoxMesh(
		new THREE.BoxGeometry( BOXSIZE*25, 1, BOXSIZE*25 ),
		ground_material,
		0 );					// mass
		
	ground.receiveShadow = true;
	threeworld.scene.add( ground );
	
	
	
	// --- Box ------------------------------------------------------------------

	var loader2 = new THREE.TextureLoader();
	var loader3 = new THREE.TextureLoader();

	box_geometry = new THREE.BoxGeometry( BOXSIZE, BOXSIZE, BOXSIZE );
		
	box_material_0  = Physijs.createMaterial(
					new THREE.MeshLambertMaterial({ map: loader2.load( TEXTURE_FILE_0 ) }), 
					MEDIUM_FRICTION,
					LOW_RESTITUTION	);
						
	box_material_1 = Physijs.createMaterial(
					new THREE.MeshLambertMaterial({ map: loader3.load( TEXTURE_FILE_1 ) }),
					MEDIUM_FRICTION,
					LOW_RESTITUTION	);
	
	createBox();
}



 
function createBox() 		
// creates one random box
{
	var box = new Physijs.BoxMesh (	box_geometry, box_material_0 );
	
	box.collisions = 0;
	box.castShadow = true;
				
	box.position.set(  randomintAtoB(-BOXPOS,BOXPOS),		(BOXPOS * 3),		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 
 		if (  Math.abs(mainImpact) > 4 )			// main impact, not lesser ones as it settles  
		{
			soundCollision();
			this.material = box_material_1;
		}
	});
	
	
	threeworld.scene.add( box );
	
	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 ( 300, 500 ) );		 

};



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





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


	this.endCondition;	
	

	
this.newRun = function() 
{
  this.endCondition = false;

  threeworld.init3d ( startRadius, maxRadius, SKYCOLOR  ); 

 // 	threeworld.renderer = new THREE.WebGLRenderer({ antialias: true });
 
 // can adjust renderer:
 
	threeworld.renderer.shadowMap.enabled = true;
	threeworld.renderer.shadowMapSoft = true;


 // can adjust scene - change gravity from default:
 
		threeworld.scene.setGravity ( new THREE.Vector3( 50, -50, 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 WWM nextStep

   initMusic();
};



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_span2").html( "Step: " + step );
};


this.endRun = function()
{
};

 
}