topical media & game development
mobile-query-three-plugins-cannonjs-vendor-cannon.js-demos-container.htm / htm
<DOCTYPE html>
<html>
<head>
<title>cannon.js - container demo</title>
<meta charset="utf-8">
<style>* {margin:0;padding:0}</style>
</head>
<body>
<script src="../build/cannon.js"></script>
<script src="../build/cannon.demo.js"></script>
<script src="../libs/dat.gui.js"></script>
<script src="../libs/Three.js"></script>
<script src="../libs/Detector.js"></script>
<script src="../libs/Stats.js"></script>
<script>
var demo = new CANNON.Demo({
stepFrequency:120
});
var nx=4, ny=4;
demo.addScene((nx*ny*4)+ " spheres", function(){ createContainer(demo,nx,ny,4); });
demo.addScene((nx*ny*8)+ " spheres", function(){ createContainer(demo,nx,ny,8); });
demo.addScene((nx*ny*15)+" spheres", function(){ createContainer(demo,nx,ny,15); });
demo.addScene((nx*ny*20)+" spheres", function(){ createContainer(demo,nx,ny,20); });
demo.addScene((nx*ny*25)+" spheres", function(){ createContainer(demo,nx,ny,25); });
demo.addScene((nx*ny*30)+" spheres", function(){ createContainer(demo,nx,ny,30); });
demo.start();
function createContainer(demo,nx,ny,nz){
// Create world
var world = demo.getWorld();
world.solver.setSpookParams(10000,7);
world.solver.iterations = 25;
world.gravity.set(0,0,-40);
// Materials
var stone = new CANNON.Material('stone');
var stone_stone = new CANNON.ContactMaterial(stone,
stone,
0.3, // friction
0.3 // Restitution
);
world.addContactMaterial(stone_stone);
// ground plane
var groundShape = new CANNON.Plane();
var groundBody = new CANNON.RigidBody(0,groundShape,stone);
world.add(groundBody);
demo.addVisual(groundBody);
// plane -x
var planeShapeXmin = new CANNON.Plane();
var planeXmin = new CANNON.RigidBody(0, planeShapeXmin,stone);
planeXmin.quaternion.setFromAxisAngle(new CANNON.Vec3(0,1,0),Math.PI/2);
planeXmin.position.set(-5,0,0);
world.add(planeXmin);
// Plane +x
var planeShapeXmax = new CANNON.Plane();
var planeXmax = new CANNON.RigidBody(0, planeShapeXmax,stone);
planeXmax.quaternion.setFromAxisAngle(new CANNON.Vec3(0,1,0),-Math.PI/2);
planeXmax.position.set(5,0,0);
world.add(planeXmax);
// Plane -y
var planeShapeYmin = new CANNON.Plane();
var planeYmin = new CANNON.RigidBody(0, planeShapeYmin,stone);
planeYmin.quaternion.setFromAxisAngle(new CANNON.Vec3(1,0,0),-Math.PI/2);
planeYmin.position.set(0,-5,0);
world.add(planeYmin);
// Plane +y
var planeShapeYmax = new CANNON.Plane();
var planeYmax = new CANNON.RigidBody(0, planeShapeYmax, stone);
planeYmax.quaternion.setFromAxisAngle(new CANNON.Vec3(1,0,0),Math.PI/2);
planeYmax.position.set(0,5,0);
world.add(planeYmax);
// Sphere on plane
var rand = 0.1;
var h = 0;
var sphereShape = new CANNON.Sphere(1); // Sharing shape saves memory
for(var i=0; i<nx; i++){
for(var j=0; j<ny; j++){
for(var k=0; k<nz; k++){
var sphereBody = new CANNON.RigidBody(5,sphereShape,stone);
sphereBody.position.set(i*2-nx*0.5 + (Math.random()-0.5)*rand,
j*2-ny*0.5 + (Math.random()-0.5)*rand,
1+k*2.1+h+(i+j)*0.0);
world.add(sphereBody);
demo.addVisual(sphereBody);
}
}
}
}
</script>
</body>
</html>
(C) Æliens
04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.