Skip to content

Commit

Permalink
update webgl demo
Browse files Browse the repository at this point in the history
  • Loading branch information
kripken committed Mar 16, 2013
1 parent e674d4d commit c6c8339
Showing 1 changed file with 39 additions and 22 deletions.
61 changes: 39 additions & 22 deletions examples/webgl_demo/ammo.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
<head>
<title>Bullet/WebGL Demo</title>
<script src="CubicVR.min.js" type="text/javascript"></script>
<script src="../../builds/ammo.js" type="text/javascript"></script>
<script src="../../builds/ammo.asm.js" type="text/javascript"></script>
<style type="text/css">
body { background-color: #ccc; }
</style>

<script type="text/javascript">
var NUM = 35;
var NUM = 216;
var NUMRANGE = [];
while (NUMRANGE.length < NUM) NUMRANGE.push(NUMRANGE.length+1);

Expand Down Expand Up @@ -58,19 +58,32 @@
});

function resetPositions() {
NUMRANGE.forEach(function(i) {
var body = bodies[i];
var origin = body.getWorldTransform().getOrigin();
origin.setX((Math.random()-0.5)*2);
origin.setY(4+i*2.5);
origin.setZ((Math.random()-0.5)*1);
body.activate();
});
var side = Math.ceil(Math.pow(NUM, 1/3));
var i = 1;
for (var x = 0; x < side; x++) {
for (var y = 0; y < side; y++) {
for (var z = 0; z < side; z++) {
if (i == bodies.length) break;
var body = bodies[i++];
var origin = body.getWorldTransform().getOrigin();
origin.setX(x * (2.2 + Math.random()) - 1.7 - side/2);
origin.setY(y * (4.2 + Math.random()));
origin.setZ(z * (2.2 + Math.random()) - 1.7 - side - 5);
body.activate();
var rotation = body.getWorldTransform().getRotation();
rotation.setX(1);
rotation.setY(0);
rotation.setZ(0);
rotation.setW(1);
}
}
}
}

resetPositions();

var transform = new Ammo.btTransform(); // taking this out of readBulletObject reduces the leaking

function readBulletObject(i, pos, quat) {
var body = bodies[i];
body.getMotionState().getWorldTransform(transform);
Expand All @@ -85,16 +98,21 @@
quat.w = rotation.w();
}

var lastInactivity = Date.now();
function noneActive() {
var num = 0;
NUMRANGE.forEach(function(i) {
var nextTimeToRestart = 0;
function timeToRestart() { // restart if at least one is inactive - the scene is starting to get boring
if (nextTimeToRestart) {
if (Date.now() >= nextTimeToRestart) {
nextTimeToRestart = 0;
return true;
}
return false;
}
for (var i = 1; i <= NUM; i++) {
var body = bodies[i];
num += body.isActive();
});
if ((Date.now() - lastInactivity) >= 1000*num) { // consider none active also if enough time passed
lastInactivity = Date.now();
return true;
if (!body.isActive()) {
nextTimeToRestart = Date.now() + 1000; // add another second after first is inactive
break;
}
}
return false;
}
Expand All @@ -119,7 +137,7 @@
renderObject.rotation = quaternion.toEuler();
}

if (noneActive()) restart();
if (timeToRestart()) restart();
}

var fpsInfo = {
Expand All @@ -141,7 +159,6 @@
}

function restart() {
totalTime = 0;
resetPositions();
}

Expand Down Expand Up @@ -182,7 +199,7 @@
var boxMaterials = NUMRANGE.map(function(i) {
return new CubicVR.Material({
textures: {
color: new CubicVR.Texture("cube" + ((i % 5)+1) + ".jpg")
color: new CubicVR.Texture("cube" + (Math.floor(Math.random()*5)+1) + ".jpg")
}
});
});
Expand Down

0 comments on commit c6c8339

Please sign in to comment.