Skip to content

Instantly share code, notes, and snippets.

@treeform
Created February 9, 2013 17:36
Show Gist options
  • Save treeform/4746218 to your computer and use it in GitHub Desktop.
Save treeform/4746218 to your computer and use it in GitHub Desktop.
Cannon.js space ship sample thingy.
<DOCTYPE html>
<html>
<head>
<title>cannon.js - compound 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:180,
});
// list of spheres
var sphers = [];
demo.addScene("Spheres",function(){
var world = setupWorld(demo);
for(i=0; i < 10; i++) {
// Compound shape
var compoundShape = new CANNON.Compound();
// create a cone/snowman body, many of may space ships have this shape
compoundShape.addChild(new CANNON.Sphere(.45), new CANNON.Vec3( 0, 0,-.2));
compoundShape.addChild(new CANNON.Sphere(.4), new CANNON.Vec3( 0, 0, .2));
compoundShape.addChild(new CANNON.Sphere(.2), new CANNON.Vec3( 0, 0, .6));
var mass = 10;
var body = new CANNON.RigidBody(mass,compoundShape);
body.position.set(-Math.random()*5,Math.random()*5,1+Math.random()*5);
body.quaternion.set(0,1,0,0.1);
body.angularDamping = .1
// which direction you are going to
body.dir = -1;
world.add(body);
demo.addVisual(body);
sphers.push(body);
console.log(body);
}
move()
});
function move(){
for(var i in sphers){
s = sphers[i]
// there seem to be no quat.traslate vec3
// so i had to write one myself
// i might have made errors, copied form here:
// http://www.flipcode.com/documents/matrfaq.html#Q47
var X = s.quaternion.x,
Y = s.quaternion.y,
Z = s.quaternion.z,
W = s.quaternion.w;
xx = X * X;
xy = X * Y;
xz = X * Z;
xw = X * W;
yy = Y * Y;
yz = Y * Z;
yw = Y * W;
zz = Z * Z;
zw = Z * W;
m3 = new CANNON.Mat3();
mat = m3.elements;
mat[0] = 1 - 2 * ( yy + zz );
mat[1] = 2 * ( xy - zw );
mat[2] = 2 * ( xz + yw );
mat[3] = 2 * ( xy + zw );
mat[4] = 1 - 2 * ( xx + zz );
mat[5] = 2 * ( yz - xw );
mat[6] = 2 * ( xz - yw );
mat[7] = 2 * ( yz + xw );
mat[8] = 1 - 2 * ( xx + yy );
var fov = new CANNON.Vec3(0,0,1);
// quaternion.forward() would be nice
fov = m3.vmult(fov);
if ((s.dir > 0 && s.position.x > s.dir*10) || (s.dir < 0 && s.position.x < s.dir*10)) {
console.log("Go the other way!", s.position.x, s.dir)
s.dir = - s.dir;
}
// when go exaclty back distabalize and force a turn
fov.y += Math.random()*.01
// s.applyLocalImpulse would be nice
s.applyImpulse(
//s.position, // zero rotation
s.position.vadd(fov.mult(7)), // apply some rotation to turn twards the direction
new CANNON.Vec3(s.dir*4,0,0)
)
}
}
function setupWorld(demo){
var world = demo.getWorld();
world.gravity.set(0,0,0);
world.broadphase = new CANNON.NaiveBroadphase();
world.solver.iterations = 5;
world.solver.k = 10000;
world.solver.d = 10;
// ground plane
var groundShape = new CANNON.Plane();
var groundBody = new CANNON.RigidBody(0,groundShape);
world.add(groundBody);
demo.addVisual(groundBody);
setInterval(move, 1);
return world;
};
demo.start();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment