Skip to content

Commit 0a453a2

Browse files
committed
added Demo.manipulation
1 parent 7ed28f7 commit 0a453a2

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

demo/dev.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ <h1>Matter.js Demo (Dev. Build)</h1>
3737
<option value="rounded">Rounded Corners (Chamfering)</option>
3838
<option value="views">Views</option>
3939
<option value="timescale">Time Scaling</option>
40+
<option value="manipulation">Body Manipulation</option>
4041
<option value="sprites">Sprites</option>
4142
<option value="pyramid">Pyramid</option>
4243
<option value="raycasting">Raycasting</option>

demo/js/Demo.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,57 @@
234234
renderOptions.showCollisions = true;
235235
};
236236

237+
Demo.manipulation = function() {
238+
var _world = _engine.world;
239+
240+
Demo.reset();
241+
242+
var bodyA = Bodies.rectangle(100, 200, 50, 50),
243+
bodyB = Bodies.rectangle(200, 200, 50, 50),
244+
bodyC = Bodies.rectangle(300, 200, 50, 50),
245+
bodyD = Bodies.rectangle(400, 200, 50, 50),
246+
bodyE = Bodies.rectangle(550, 200, 50, 50),
247+
bodyF = Bodies.rectangle(700, 200, 50, 50);
248+
249+
World.add(_world, [bodyA, bodyB, bodyC, bodyD, bodyE, bodyF]);
250+
251+
var counter = 0,
252+
scaleFactor = 1.01;
253+
254+
_sceneEvents.push(
255+
Events.on(_engine, 'tick', function(event) {
256+
counter += 1;
257+
258+
if (scaleFactor > 1) {
259+
Body.scale(bodyF, scaleFactor, scaleFactor);
260+
261+
// modify body vertices
262+
bodyE.vertices[0].x -= 0.2;
263+
bodyE.vertices[0].y -= 0.2;
264+
bodyE.vertices[1].x += 0.2;
265+
bodyE.vertices[1].y -= 0.2;
266+
Body.setVertices(bodyE, bodyE.vertices);
267+
}
268+
269+
// every 1.5 sec
270+
if (counter >= 60 * 1.5) {
271+
Body.setPosition(bodyA, { x: 100, y: 200 });
272+
Body.setVelocity(bodyB, { x: 0, y: -10 });
273+
Body.setAngle(bodyC, -Math.PI * 0.26);
274+
Body.setAngularVelocity(bodyD, 0.2);
275+
276+
// reset counter
277+
counter = 0;
278+
scaleFactor = 1;
279+
}
280+
})
281+
);
282+
283+
var renderOptions = _engine.render.options;
284+
renderOptions.showAxes = true;
285+
renderOptions.showCollisions = true;
286+
};
287+
237288
Demo.views = function() {
238289
var _world = _engine.world;
239290

0 commit comments

Comments
 (0)