Skip to content
This repository
Browse code

Made demos run smoothly

  • Loading branch information...
commit b0895e40126fd11ab33f084e4a85afc9f494ce8e 1 parent 20ed011
Joseph Gentle authored

Showing 2 changed files with 33 additions and 6 deletions. Show diff stats Hide diff stats

  1. +9 0 demo/PyramidTopple.js
  2. +24 6 demo/demo.js
9 demo/PyramidTopple.js
@@ -48,6 +48,7 @@ var PyramidTopple = function()
48 48 space.collisionSlop = 0.5;
49 49
50 50 this.addFloor();
  51 + this.addWalls();
51 52
52 53 // Add the dominoes.
53 54 var n = 12;
@@ -68,6 +69,14 @@ var PyramidTopple = function()
68 69 }
69 70 }
70 71 }
  72 +
  73 + // Add a circle to knock the dominoes down
  74 + /*
  75 + var body = space.addBody(new cp.Body(2, cp.momentForCircle(2, 0, 5, v(0,0))));
  76 + body.setPos(v(65, 100));
  77 + var shape = space.addShape(new cp.CircleShape(body, 5, v(0,0)));
  78 + shape.setElasticity(0);
  79 + */
71 80 };
72 81
73 82 PyramidTopple.prototype = Object.create(Demo.prototype);
30 demo/demo.js
@@ -13,6 +13,7 @@ var Demo = function() {
13 13 this.remainder = 0;
14 14 this.fps = 0;
15 15 this.mouse = v(0,0);
  16 + this.timeTaken = 0;
16 17
17 18 var self = this;
18 19 var canvas2point = this.canvas2point = function(x, y) {
@@ -138,6 +139,7 @@ Demo.prototype.drawInfo = function() {
138 139 }
139 140 this.maxContacts = this.maxContacts ? Math.max(this.maxContacts, contacts) : contacts;
140 141 this.ctx.fillText("Contact points: " + contacts + " (Max: " + this.maxContacts + ")", 10, 140, maxWidth);
  142 + this.ctx.fillText("Time taken: " + this.timeTaken, 10, 170, maxWidth);
141 143
142 144 if (this.message) {
143 145 this.ctx.fillText(this.message, 10, this.height - 50, maxWidth);
@@ -216,6 +218,25 @@ Demo.prototype.run = function() {
216 218 step();
217 219 };
218 220
  221 +var soon = function(fn) { setTimeout(fn, 1); };
  222 +
  223 +Demo.prototype.benchmark = function() {
  224 + this.draw();
  225 +
  226 + var self = this;
  227 + soon(function() {
  228 + console.log("Benchmarking... waiting for the space to come to rest");
  229 + var start = Date.now();
  230 + while (self.space.activeShapes.count !== 0) {
  231 + self.update(1/60);
  232 + }
  233 + var end = Date.now();
  234 +
  235 + console.log('took ' + (end - start) + 'ms');
  236 + self.draw();
  237 + });
  238 +};
  239 +
219 240 Demo.prototype.stop = function() {
220 241 this.running = false;
221 242 };
@@ -241,13 +262,10 @@ Demo.prototype.step = function() {
241 262 // has a slow computer, we'll just slow the simulation down.
242 263 dt = Math.min(dt, 1/25);
243 264
244   - this.remainder += dt;
  265 + this.update(1/60);
245 266
246   - while(this.remainder > 1/60) {
247   - // Chipmunk works better with a constant framerate, because it can cache some results.
248   - this.remainder -= 1/60;
249   - this.update(1/60);
250   - }
  267 + var afterUpdate = Date.now();
  268 + this.timeTaken += afterUpdate - now;
251 269
252 270 // Only redraw if the simulation isn't asleep.
253 271 if (lastNumActiveShapes > 0 || Demo.resized) {

0 comments on commit b0895e4

Please sign in to comment.
Something went wrong with that request. Please try again.