// 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;const TEXTURE_FILE_0 ='/uploads/starter/smiley.png';const TEXTURE_FILE_1 ='/uploads/starter/ghost.3.png';// https://commons.wikimedia.org/wiki/Smileyconst TEXTURE_GROUND ='/uploads/starter/rocks.jpg';const SOUND_COLLISION ='/uploads/starter/wooden.mp3';// credit:// http://soundbible.com/715-Dropped-Wooden-Floor.htmlconst 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 )));}functionWorld(){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 );
light.castShadow =true;// how far away to draw shadows:
light.shadow.camera.left =-GROUNDSIZE;
light.shadow.camera.right = GROUNDSIZE;
light.shadow.camera.bottom =-GROUNDSIZE;
light.shadow.camera.top = GROUNDSIZE;
light.shadow.bias =-0.0001;// 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
light.shadow.mapSize.width =2048;
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 )}),
GROUND_FRICTION,
GROUND_RESTITUTION );
ground_material.map.wrapS = THREE.RepeatWrapping;
ground_material.map.wrapT = THREE.RepeatWrapping;
ground_material.map.repeat.set(3,3);
ground =newPhysijs.BoxMesh(new THREE.BoxGeometry( GROUNDSIZE,1, GROUNDSIZE ),
ground_material,0);// mass
ground.receiveShadow =true;
threeworld.scene.add( ground );// --- Box ------------------------------------------------------------------var loader0 =new THREE.TextureLoader();var loader1 =new THREE.TextureLoader();
box_texture_0 = loader0.load( TEXTURE_FILE_0 );
box_texture_1 = loader1.load( TEXTURE_FILE_1 );
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 =newPhysijs.BoxMesh( box_geometry, box_material_0 );
box.collisions =0;
box.castShadow =true;
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;}});
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 (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(){
threeworld.init3d ( startRadius, maxRadius, SKYCOLOR );// sets up renderer, scene, camera// can adjust renderer:
threeworld.renderer.shadowMap.enabled =true;// 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(){};}