Skip to content

Commit c1247d8

Browse files
committed
init
0 parents  commit c1247d8

File tree

7 files changed

+4058
-0
lines changed

7 files changed

+4058
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
box2d_try/
2+
.idea/
3+
node_modules

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2.7.15

index.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Testing</title>
6+
<script src="bundle.js"></script>
7+
</head>
8+
<body>
9+
10+
</body>
11+
</html>

package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "dancehack_rectangle_physics",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"dependencies": {
7+
"matter-js": "^0.14.2",
8+
"webpack": "^4.41.5",
9+
"webpack-dev-server": "^3.10.1"
10+
},
11+
"devDependencies": {
12+
"webpack-cli": "^3.3.10"
13+
}
14+
}

src/index.js

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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+
});

webpack.config.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const path = require('path');
2+
3+
module.exports = {
4+
entry: './src/index.js',
5+
mode: 'development',
6+
output: {
7+
path: path.resolve(__dirname, 'dist'),
8+
filename: 'bundle.js'
9+
}
10+
};

0 commit comments

Comments
 (0)