// Cloned by test2 on 7 Apr 2018 from World "Collision World" by Starter user
// Please leave this clone trail here.
// ==== Starter World ===============================================================================================
// (c) Ancient Brain Ltd. All rights reserved.
// This code is only for use on the Ancient Brain site.
// This code may be freely copied and edited by anyone on the Ancient Brain site.
// This code may not be copied, re-published or used on any other website.
// To include a run of this code on another website, see the "Embed code" links provided on the Ancient Brain site.
// ==================================================================================================================
// Demo of Physijs based World
// From here, with many changes:
// http://chandlerprall.github.io/Physijs/examples/collisions.html
// ===================================================================================================================
// === Start of tweaker's box ========================================================================================
// ===================================================================================================================
// The easiest things to modify are in this box.
// You should be able to change things in this box without being a JavaScript programmer.
// Go ahead and change some of these. What's the worst that could happen?
// 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.
// add comment
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/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 GROUND_FRICTION = 0.8;
const GROUND_RESTITUTION = 0.3;
const BOX_FRICTION = 1.6;
const BOX_RESTITUTION = 0.3;
// define gravity along x,y,z dimensions:
var gravity = new THREE.Vector3 ( 0, 100, 0 );
// var gravity = new THREE.Vector3 ( -30, -30, 0 );
function nextboxin()
// Speed of box creation
// Create next box in some random (m to n) milliseconds time.
{
// return ( randomintAtoB ( 2000, 4000 ) );
return ( randomintAtoB ( 1, 1 ) );
}
// ===================================================================================================================
// === End of tweaker's box ==========================================================================================
// ===================================================================================================================
// You will need to be some sort of JavaScript programmer to change things below the tweaker's box.
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 );
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 = new Physijs.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 = new Physijs.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;
setTimeout ( createBox, nextboxin() );
// Create next box in some random time.
// N.B. Put this in createBox, not nextStep - runs on independent timer.
};
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 ( gravity );
// 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()
{
};
}