|
| 1 | +const Matter = require("matter-js"); |
| 2 | + |
| 3 | + |
| 4 | +const Engine = Matter.Engine, |
| 5 | + Render = Matter.Render, |
| 6 | + Runner = Matter.Runner, |
| 7 | + World = Matter.World, |
| 8 | + Bodies = Matter.Bodies; |
| 9 | + |
| 10 | + |
| 11 | +const engine = Engine.create(); |
| 12 | +engine.constraintIterations = 1; |
| 13 | + |
| 14 | +const world = engine.world; |
| 15 | +world.gravity.x = 0; |
| 16 | +world.gravity.y = 0; |
| 17 | + |
| 18 | +// create runner |
| 19 | +const runner = Runner.create(); |
| 20 | +runner.delta = 100; |
| 21 | +runner.isFixed = true; |
| 22 | + |
| 23 | +const CANVAS_WIDTH = 800; |
| 24 | +const CANVAS_HEIGHT = 600; |
| 25 | + |
| 26 | +const RECT_WIDTH = 20; |
| 27 | +const RECT_HEIGHT = 5; |
| 28 | + |
| 29 | +const position_log = {}; |
| 30 | + |
| 31 | +function addTile(x_start, y_start, angle) { |
| 32 | + const body = Bodies.rectangle(x_start, y_start, RECT_WIDTH, RECT_HEIGHT); |
| 33 | + Matter.Body.setAngle(body, angle); |
| 34 | + Matter.Body.setAngularVelocity(body, 0.001); |
| 35 | + console.log(angle) |
| 36 | + body.friction = 0; |
| 37 | + //body.velocity = 0.000000000000001; |
| 38 | + body.frictionAir = 0; |
| 39 | + body.frictionStatic = 0; |
| 40 | + body.slop = 0.005; |
| 41 | + //body.velocity = 0.00000001; |
| 42 | + World.add(world, [ |
| 43 | + body |
| 44 | + ]); |
| 45 | +} |
| 46 | + |
| 47 | +function addTileEmitter(x_start, y_start, angle, timeout = 300) { |
| 48 | + //addTile(x_start, y_start, angle) |
| 49 | + setInterval(() => addTile(x_start, y_start, angle), timeout); |
| 50 | +} |
| 51 | + |
| 52 | +function removeOutOfBoundsBodies(bodies) { |
| 53 | + let count = 0; |
| 54 | + |
| 55 | + for (let i = 0; i < bodies.length; i++) { |
| 56 | + let body = bodies[i]; |
| 57 | + /* |
| 58 | + if (body.collisionFilter.mask != 0) { |
| 59 | + if (!position_log[body.id]) { |
| 60 | + position_log[body.id] = [] |
| 61 | + } |
| 62 | + position_log[body.id].push([body.position.x, body.position.y]); |
| 63 | +
|
| 64 | + if (position_log[body.id].length > 1) { |
| 65 | + const positions = position_log[body.id]; |
| 66 | + const diff_x = positions[0][0] - positions[1][0]; |
| 67 | + const diff_y = positions[0][1] - positions[1][1]; |
| 68 | + //console.log(body) |
| 69 | + //body.force = {x: diff_x / 10.0, y: diff_y / 10.0}; |
| 70 | + //exit(1) |
| 71 | +
|
| 72 | + //body.collisionFilter.mask = 0; |
| 73 | + } |
| 74 | +
|
| 75 | + } |
| 76 | + */ |
| 77 | + |
| 78 | + if (body.position.x > CANVAS_WIDTH || body.position.x < 0 || body.position.y > CANVAS_HEIGHT || body.position.y < 0) { |
| 79 | + World.remove(world, body); |
| 80 | + count++; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + console.log(`Removed ${count} entities`); |
| 85 | +} |
| 86 | + |
| 87 | +document.addEventListener("DOMContentLoaded", function () { |
| 88 | + |
| 89 | + // create renderer |
| 90 | + const render = Render.create({ |
| 91 | + element: document.body, |
| 92 | + engine: engine, |
| 93 | + options: { |
| 94 | + width: CANVAS_WIDTH, |
| 95 | + height: CANVAS_HEIGHT, |
| 96 | + // showVelocity: true |
| 97 | + } |
| 98 | + }); |
| 99 | + |
| 100 | + // run the engine |
| 101 | + Engine.run(engine); |
| 102 | + |
| 103 | + Render.run(render); |
| 104 | + |
| 105 | + Runner.run(runner, engine); |
| 106 | + |
| 107 | + const spawn_x = CANVAS_WIDTH / 2; |
| 108 | + const spawn_y = CANVAS_HEIGHT / 2; |
| 109 | + const radius = 100; |
| 110 | + for (let i = 0; i < 180; i += 30) { |
| 111 | + const angle_r = i * (Math.PI / 180); |
| 112 | + const x = spawn_x + radius * Math.cos(angle_r); |
| 113 | + const y = spawn_y + radius * Math.sin(angle_r); |
| 114 | + addTileEmitter(x, y, i, 1000); |
| 115 | + } |
| 116 | + |
| 117 | + // Remove out of bound elements every 1s |
| 118 | + setInterval(() => removeOutOfBoundsBodies(Matter.Composite.allBodies(world)), 300); |
| 119 | + |
| 120 | + Render.lookAt(render, { |
| 121 | + min: {x: 0, y: 0}, |
| 122 | + max: {x: 800, y: 600} |
| 123 | + }); |
| 124 | + |
| 125 | + console.log("wut") |
| 126 | + |
| 127 | +}); |
0 commit comments