forked from piqnt/planck.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShuffle.js
101 lines (82 loc) · 2.55 KB
/
Shuffle.js
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
planck.testbed('Shuffle', function(testbed) {
var pl = planck, Vec2 = pl.Vec2, Math = pl.Math;
var SPI4 = Math.sin(Math.PI / 4), SPI3 = Math.sin(Math.PI / 3);
var width = 10.00, height = 10.00;
var BALL_R = 0.3;
var BALL_D = 1;
testbed.x = 0;
testbed.y = 0;
testbed.width = width * 1.5;
testbed.height = height * 1.5;
testbed.ratio = 50;
testbed.mouseForce = -100;
pl.internal.Settings.velocityThreshold = 0;
var world = pl.World({});
var walls = [
Vec2(-width * .5, -height * .5),
Vec2(-width * .5, +height * .5),
Vec2(+width * .5, +height * .5),
Vec2(+width * .5, -height * .5)
];
var wallFixDef = {
userData : 'wall'
};
var ballFixDef = {
friction: 0.1,
restitution: 0.98,
density: 0.8,
userData : 'ball'
};
var ballBodyDef = {
bullet: true,
linearDamping : 1.6,
angularDamping : 1.6
};
world.createBody().createFixture(pl.Chain(walls, true), wallFixDef);
row(1, 8, BALL_R, BALL_D).map(translate(height * 0.4, 0)).forEach(function(p) {
var ball = world.createDynamicBody(ballBodyDef);
ball.setPosition(p);
ball.setAngle(Math.PI);
ball.createFixture(pl.Circle(BALL_R), ballFixDef);
ball.render = {fill : '#ff411a', stroke: 'black'};
});
row(1, 8, BALL_R, BALL_D).map(translate(-height * 0.4, 0)).forEach(function(p) {
var ball = world.createDynamicBody(ballBodyDef);
ball.setPosition(p);
ball.createFixture(pl.Circle(BALL_R), ballFixDef);
ball.render = {fill : '#0077ff', stroke: 'black'};
});
world.on('post-solve', function(contact) {
var fA = contact.getFixtureA(), bA = fA.getBody();
var fB = contact.getFixtureB(), bB = fB.getBody();
var wall = fA.getUserData() == wallFixDef.userData && bA || fB.getUserData() == wallFixDef.userData && bB;
var ball = fA.getUserData() == ballFixDef.userData && bA || fB.getUserData() == ballFixDef.userData && bB;
// do not change world immediately
setTimeout(function() {
if (ball && wall) {
world.destroyBody(ball);
}
}, 1);
});
return world;
function row(n, m, r, l) {
var d = r * 2;
var balls = [];
for (var i = 0; i < n; i++) {
for (var j = 0; j < m; j++) {
balls.push(Vec2(i * l - (n - 1) * .5 * l + Math.random(r * 0.02), j * l - (m - 1) * .5 * l + Math.random(r * 0.02)));
}
}
return balls;
}
function scale(x, y) {
return function (v) {
return pl.Vec2(v.x * x, v.y * y);
};
}
function translate(x, y) {
return function (v) {
return pl.Vec2(v.x + x, v.y + y);
};
}
});