From d71e4ef553baebaec00127e2e02445b242c5ae7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D0=B0=D0=BF?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=D0=BA=D0=BE?= Date: Thu, 27 May 2021 14:51:46 +0300 Subject: [PATCH] count sleeping --- 01.js | 22 +++++++++++++++++----- index.html | 2 +- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/01.js b/01.js index 03a2a3f..078c846 100644 --- a/01.js +++ b/01.js @@ -36,25 +36,37 @@ addObjects(); gameLoop(); function setupMatter() { - engine = Matter.Engine.create(); + engine = Matter.Engine.create({ enableSleeping: true }); } function addObjects() { boxes.push( - new Box(engine.world, 250, 20, 40, 40), - new Box(engine.world, 300, 350, 40, 40), - new Box(engine.world, 320, 70, 40, 40), - new Box(engine.world, 0, 400, 800, 60, { isStatic: true }) + new Box(engine.world, 250, 20, 40, 40, { restitution: 0.8 }), + new Box(engine.world, 300, 350, 40, 40, { restitution: 0.8 }), + new Box(engine.world, 320, 70, 40, 40, { restitution: 0.8 }), + new Box(engine.world, 0, 400, 1200, 60, { isStatic: true }), + new Box(engine.world, 260, 50, 40, 40), + new Box(engine.world, 330, 300, 40, 40), + new Box(engine.world, 350, 170, 40, 40), + new Box(engine.world, 160, 150, 40, 40), + new Box(engine.world, 190, 300, 40, 40), + new Box(engine.world, 250, 270, 40, 40), ); } function gameLoop() { // update the physics world Matter.Engine.update(engine, 1000 / 60); +var sleepingCount = 0; // update the DOM world for (let b of boxes) { b.update(); +if (b.physicsBox.isSleeping) +{ + sleepingCount += 1; +} } +console.debug("sleeping count:", sleepingCount); requestAnimationFrame(() => this.gameLoop()); } diff --git a/index.html b/index.html index 4e35225..481224e 100644 --- a/index.html +++ b/index.html @@ -22,7 +22,7 @@ box { box-sizing: border-box; position: absolute; display: block; - background-color: rgba(45, 188, 232, 0.687); + background-color: red; transform-origin: 20px 20px; }