-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbasic.js
More file actions
126 lines (108 loc) · 2.58 KB
/
basic.js
File metadata and controls
126 lines (108 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// install plugin
Matter.use(
'matter-wrap' // PLUGIN_NAME
);
var Example = Example || {};
Example.basic = function() {
// module aliases
var Engine = Matter.Engine,
Runner = Matter.Runner,
Render = Matter.Render,
World = Matter.World,
Body = Matter.Body,
Mouse = Matter.Mouse,
Common = Matter.Common,
Bodies = Matter.Bodies;
// create engine
var engine = Engine.create();
// create renderer
var render = Render.create({
element: document.body,
engine: engine,
options: {
width: Math.min(document.body.clientWidth, 1024),
height: Math.min(document.body.clientHeight, 1024),
wireframes: false
}
});
Render.run(render);
// create runner
var runner = Runner.create();
Runner.run(runner, engine);
// create demo scene
var world = engine.world;
world.gravity.scale = 0;
// add some random bodies
for (var i = 0; i < 150; i += 1) {
var body = Bodies.polygon(
Common.random(0, render.options.width),
Common.random(0, render.options.height),
Common.random(1, 5),
Common.random() > 0.9 ? Common.random(15, 25) : Common.random(5, 10),
{
friction: 0,
frictionAir: 0,
// set the body's wrapping bounds
plugin: {
wrap: {
min: {
x: 0,
y: 0
},
max: {
x: render.canvas.width,
y: render.canvas.height
}
}
}
}
);
Body.setVelocity(body, {
x: Common.random(-3, 3) + 3,
y: Common.random(-3, 3) + 3
});
World.add(world, body);
}
// add a composite
var car = Matter.Composites.car(150, 100, 100, 30, 20);
// set the composites's wrapping bounds
car.plugin.wrap = {
min: {
x: 0,
y: 0
},
max: {
x: render.canvas.width,
y: render.canvas.height
}
};
for (i = 0; i < car.bodies.length; i += 1) {
Body.setVelocity(car.bodies[i], {
x: Common.random(-3, 3) + 3,
y: Common.random(-3, 3) + 3
});
}
World.add(world, car);
// add mouse control
var mouseConstraint = Matter.MouseConstraint.create(engine, {
mouse: Mouse.create(render.canvas),
constraint: {
stiffness: 0.2,
render: {
visible: false
}
}
});
World.add(world, mouseConstraint);
// context for MatterTools.Demo
return {
engine: engine,
runner: runner,
render: render,
canvas: render.canvas,
stop: function() {
Matter.Render.stop(render);
Matter.Runner.stop(runner);
}
};
}