-
Notifications
You must be signed in to change notification settings - Fork 1
/
billiards_physics.em
67 lines (56 loc) · 1.66 KB
/
billiards_physics.em
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
var table;
var cue;
var balls = [];
var ballMesh = 'meerkat:///emily2e/models/earth.dae/optimized/0/earth.dae';
var tableMesh = 'meerkat:///wmonroe4/billiard_table.dae/optimized/0/billiard_table.dae';
var NUM_ROWS = 4;
var NUM_BILLIARD_BALLS = 10;
var TABLE_SCALE = 8.4;
var BALL_SCALE = 0.16;
var TABLE_OFFSET = TABLE_SCALE * 0.2;
var CUE_OFFSET = 3;
var CUE_VELOCITY = 3;
function onTableCreated(pres) {
table = pres;
table.scale = TABLE_SCALE;
table.physics = {
treatment: 'static',
bounds: 'triangles'
};
var DELTA = 3 * BALL_SCALE;
for(var row = 0; row < NUM_ROWS; row++) {
for(var ball = 0; ball < row + 1; ball++) {
var position = table.position + <-DELTA * row, TABLE_OFFSET,
DELTA * (ball - row / 2)>;
system.createPresence(ballMesh, onBallCreated(position));
}
}
}
function onBallCreated(position) {
return function(pres) {
balls.push(pres);
pres.scale = BALL_SCALE;
pres.position = position;
if(table && balls.length == NUM_BILLIARD_BALLS)
system.createPresence(ballMesh, onCueCreated);
}
}
function onCueCreated(pres) {
cue = pres;
balls.push(cue);
cue.scale = BALL_SCALE;
cue.position = table.position + <CUE_OFFSET, TABLE_OFFSET, 0>;
cue.physics = {
treatment: 'dynamic',
bounds: 'sphere',
mass: 1
};
for(var i in balls)
balls[i].physics = cue.physics;
}
function hitCue() {
cue.physics = {treatment: 'ignore'};
cue.velocity = <-CUE_VELOCITY, 0, 0>;
cue.physics = {treatment: 'dynamic'};
}
system.createPresence(tableMesh, onTableCreated);