Cannon.js demo
For fiddling with a cannon.js demo scene
HTML
<script src="https://raw.github.com/schteppe/cannon.js/master/build/cannon.js"></script>
<script src="https://raw.github.com/schteppe/cannon.js/master/libs/Three.js"></script>
<script src="https://raw.github.com/schteppe/cannon.js/master/libs/Stats.js"></script>
<script src="https://raw.github.com/schteppe/cannon.js/master/libs/Detector.js"></script>
<script src="https://raw.github.com/schteppe/cannon.js/master/libs/dat.gui.js"></script>
<script src="https://raw.github.com/schteppe/cannon.js/master/build/cannon.demo.js"></script>
<DOCTYPE html>
<html>
<head>
<title>cannon.js demo</title>
<meta charset="utf-8">
</head>
<body>
<div id="info"></div>
<div id="blocker">
<div id="instructions">
<span style="font-size:40px">Click to play</span>
<br />
(W,A,S,D = Move, SPACE = Jump, MOUSE = Look, CLICK = Shoot)
</div>
</div>
</body>
</html>
CSS
* {margin:0;padding:0}
html, body {
width: 100%;
height: 100%;
}
body {
background-color: #ffffff;
margin: 0;
overflow: hidden;
font-family: arial;
}
#blocker {
position: absolute;
width: 100%;
height: 100%;
background-color: rgba(0,0,0,0.5);
}
#info {
text-align: center;
padding: 10px;
z-index: 10;
width: 100%;
position: absolute;
color:white;
}
#instructions {
width: 100%;
height: 100%;
display: -webkit-box;
display: -moz-box;
display: box;
-webkit-box-orient: horizontal;
-moz-box-orient: horizontal;
box-orient: horizontal;
-webkit-box-pack: center;
-moz-box-pack: center;
box-pack: center;
-webkit-box-align: center;
-moz-box-align: center;
box-align: center;
color: #ffffff;
text-align: center;
cursor: pointer;
}
JavaScript
/**
* @author mrdoob / http://mrdoob.com/
* @author schteppe / https://github.com/schteppe
*/
var PointerLockControls = function ( camera, cannonBody ) {
var eyeYPos = 2; // eyes are 2 meters above the ground
var velocityFactor = 0.2;
var jumpVelocity = 20;
var scope = this;
var pitchObject = new THREE.Object3D();
pitchObject.add( camera );
var yawObject = new THREE.Object3D();
yawObject.position.y = 2;
yawObject.add( pitchObject );
var quat = new THREE.Quaternion();
var moveForward = false;
var moveBackward = false;
var moveLeft = false;
var moveRight = false;
var canJump = false;
var contactNormal = new CANNON.Vec3(); // Normal in the contact, pointing *out* of whatever the player touched
var upAxis = new CANNON.Vec3(0,1,0);
cannonBody.addEventListener("collide",function(e){
var contact = e.contact;
// contact.bi and contact.bj are the colliding bodies, and contact.ni is the collision normal.
// We do not yet know which one is which! Let's check.
if(contact.bi.id == cannonBody.id) // bi is the player body, flip the contact normal
contact.ni.negate(contactNormal);
else
contactNormal.copy(contact.ni); // bi is something else. Keep the normal as it is
// If contactNormal.dot(upAxis) is between 0 and 1, we know that the contact normal is somewhat in the up direction.
if(contactNormal.dot(upAxis) > 0.5) // Use a "good" threshold value between 0 and 1 here!
canJump = true;
});
var velocity = cannonBody.velocity;
var PI_2 = Math.PI / 2;
var onMouseMove = function ( event ) {
if ( scope.enabled === false ) return;
var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
var movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
yawObject.rotation.y -= movementX * 0.002;
...