Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <title>Rigid body - Physijs</title>
- <link rel="stylesheet" type="text/css" href="css/styles.css" />
- <script type="text/javascript" src="js/three.min.js"></script>
- <script type="text/javascript" src="js/stats.js"></script>
- <script type="text/javascript" src="../physi.js"></script>
- <script type="text/javascript">
- 'use strict';
- Physijs.scripts.worker = '../physijs_worker.js';
- Physijs.scripts.ammo = 'examples/js/ammo.js';
- var initScene, render, applyForce, setMousePosition, mouse_position,
- ground_material, box_material,
- projector, renderer, render_stats, physics_stats, scene, ground, light, camera, box, sphere, boxes = [];
- initScene = function() {
- projector = new THREE.Projector;
- renderer = new THREE.WebGLRenderer({ antialias: true });
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.shadowMapEnabled = true;
- renderer.shadowMapSoft = true;
- document.getElementById( 'viewport' ).appendChild( renderer.domElement );
- render_stats = new Stats();
- render_stats.domElement.style.position = 'absolute';
- render_stats.domElement.style.top = '1px';
- render_stats.domElement.style.zIndex = 100;
- document.getElementById( 'viewport' ).appendChild( render_stats.domElement );
- physics_stats = new Stats();
- physics_stats.domElement.style.position = 'absolute';
- physics_stats.domElement.style.top = '50px';
- physics_stats.domElement.style.zIndex = 100;
- document.getElementById( 'viewport' ).appendChild( physics_stats.domElement );
- scene = new Physijs.Scene;
- scene.setGravity(new THREE.Vector3( 0, -30, 0 ));
- scene.addEventListener(
- 'update',
- function() {
- //applyForce();
- scene.simulate( undefined, 1 );
- physics_stats.update();
- }
- );
- camera = new THREE.PerspectiveCamera(
- 35,
- window.innerWidth / window.innerHeight,
- 1,
- 1000
- );
- camera.position.set( 60, 50, 60 );
- camera.lookAt( scene.position );
- scene.add( camera );
- // Light
- light = new THREE.DirectionalLight( 0xFFFFFF );
- light.position.set( 20, 40, -15 );
- light.target.position.copy( scene.position );
- light.castShadow = true;
- light.shadowCameraLeft = -60;
- light.shadowCameraTop = -60;
- light.shadowCameraRight = 60;
- light.shadowCameraBottom = 60;
- light.shadowCameraNear = 20;
- light.shadowCameraFar = 200;
- light.shadowBias = -.0001
- light.shadowMapWidth = light.shadowMapHeight = 2048;
- light.shadowDarkness = .7;
- scene.add( light );
- // Materials
- ground_material = Physijs.createMaterial(
- new THREE.MeshLambertMaterial({ map: THREE.ImageUtils.loadTexture( 'images/rocks.jpg' ) }),
- .8, // high friction
- .4 // low restitution
- );
- ground_material.map.wrapS = ground_material.map.wrapT = THREE.RepeatWrapping;
- ground_material.map.repeat.set( 3, 3 );
- box_material = Physijs.createMaterial(
- new THREE.MeshLambertMaterial({ map: THREE.ImageUtils.loadTexture( 'images/plywood.jpg' ) }),
- .4, // low friction
- .6 // high restitution
- );
- box_material.map.wrapS = ground_material.map.wrapT = THREE.RepeatWrapping;
- box_material.map.repeat.set( .25, .25 );
- // Ground
- ground = new Physijs.BoxMesh(
- new THREE.CubeGeometry(100, 1, 100),
- ground_material,
- 0 // mass
- );
- ground.receiveShadow = true;
- scene.add( ground );
- box = new Physijs.BoxMesh(
- new THREE.CubeGeometry( 4, 4, 4 ),
- box_material
- );
- box.position.set(
- 0,
- 2,
- 0
- );
- box.castShadow = true;
- scene.add( box );
- boxes.push( box );
- sphere = new Physijs.SphereMesh(
- new THREE.SphereGeometry( 2, 8, 16 ),
- box_material
- );
- sphere.position.set(
- 0,
- 8,
- 0
- );
- sphere.addEventListener( 'collision', function ( other_object, relative_velocity, relative_rotation, contact_normal ) {
- // `this` 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('collision', new Date().getTime(), this.name, other_object.name);
- console.log('speed', relative_velocity.length(), new Date().getTime(), relative_velocity);
- } );
- sphere.castShadow = true;
- scene.add( sphere );
- //renderer.domElement.addEventListener( 'mousemove', setMousePosition );
- requestAnimationFrame( render );
- scene.simulate();
- };
- render = function() {
- requestAnimationFrame( render );
- renderer.render( scene, camera );
- render_stats.update();
- };
- window.onload = initScene;
- </script>
- </head>
- <body>
- <div id="heading">
- <h1>Rigid Body Double Collision</h1>
- </div>
- <div id="viewport"></div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement