-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbilliards_motion.em
275 lines (234 loc) · 9.08 KB
/
billiards_motion.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
runBilliards = function() {
system.require('std/movement/motion.em');
system.require('std/movement/collision.em');
var table;
var cue;
var balls = [];
var ballIDs = {};
var stopped = [];
var pockets = [];
var pocketIDs = {};
var NUM_ROWS = 3;
var NUM_BILLIARD_BALLS = 6;
var ballMeshes = [];
for(var i = 1; i <= NUM_BILLIARD_BALLS + 1; i++) {
if(i == 6)
continue;
ballMeshes.push('meerkat:///wmonroe4/' + i + 'ball.dae/optimized/0/' +
i + 'ball.dae');
}
var cueBallMesh = 'meerkat:///wmonroe4/cueball.dae/optimized/0/cueball.dae';
var tableMesh = 'meerkat:///wmonroe4/billiard_table.dae/optimized/0/billiard_table.dae';
var TABLE_SCALE = 8.4;
var BALL_SCALE = 0.16;
var TABLE_OFFSET = TABLE_SCALE * 0.2;
var TABLE_LENGTH = 0.74 * TABLE_SCALE;
var TABLE_WIDTH = 0.45 * TABLE_SCALE;
var TABLE_BOUNDS = {
max: <TABLE_LENGTH, TABLE_OFFSET + 0.5, TABLE_WIDTH>,
min: <-TABLE_LENGTH, TABLE_OFFSET - 0.5, -TABLE_WIDTH>
};
var DELTA = 2.5 * BALL_SCALE;
var POCKET_ROW_DELTA = <TABLE_LENGTH, 0, 0>;
var POCKET_COL_DELTA = <0, 0, TABLE_WIDTH>;
var POCKET_SCALE = 0.03 * TABLE_SCALE;
var SIDE_POCKET_DELTA = <0, 0, BALL_SCALE>;
var NUM_POCKETS = 6;
var CUE_OFFSET = 0.3 * TABLE_SCALE;
var CUE_VELOCITY_FACTOR = 0.5;
var ELASTICITY = 0.8;
var FRICTION = 0.4;
var ROLLING_FRICTION = 0.000;
var DEFAULT_QUERY_ANGLE = 0.001;
frictionController = motion.ForceTorque.extend({
init: function(presence, coefficient, period) {
var radial = <0, -presence.scale, 0>;
var posFn = function(p) {
return p.position + radial;
};
var forceFn = function(p) {
var angVel = p.orientationVel.axis().scale(p.orientationVel.length());
// transform to world coordinates
angVel = p.orientation.mul(angVel);
var tangVel = p.velocity - radial.cross(angVel);
if(tangVel.length() < 1e-08)
return <0, 0, 0>;
return tangVel.scale(-coefficient * motion.util.mass(p) *
motion.defaultGravity / Math.max(tangVel.length(), 1));
};
this._super(presence, forceFn, posFn, period);
}
});
function debugPrintStopped() {
var status = '';
for(var i in stopped) {
if(stopped[i] == 'pocketed')
status += 'O';
else if(stopped[i])
status += '.';
else
status += '>';
}
//system.__debugPrint(status + '\n');
}
function onMovementStopped() {
system.__debugPrint('all motion stopped\n');
}
function allBallsStopped() {
for(var i in stopped)
if(!stopped[i])
return false;
return true;
}
function getBallNumber(pres) {
var num = parseInt(pres.mesh.slice(pres.mesh.lastIndexOf('/') + 1,
pres.mesh.lastIndexOf('ball')));
if(isNaN(num)) {
system.__debugPrint('couldn\'t recognize ball: \n' +
pres.mesh);
return 0;
}
return num;
}
function stopBall(pres) {
if(!stopped[ballIDs[pres.toString()]]) {
stopped[ballIDs[pres.toString()]] = true;
debugPrintStopped();
if(allBallsStopped())
onMovementStopped();
}
pres.orientationVel = <0, 0, 0, 0>;
pres.velocity = <0, 0, 0>;
}
function rollingVelFn(pres) {
if(pres.velocity.lengthSquared() <
ROLLING_FRICTION * ROLLING_FRICTION) {
stopBall(pres);
return <0, 0, 0>;
}
return pres.velocity - pres.velocity.normal() * ROLLING_FRICTION;
}
function onBallPocketed(num) {
system.__debugPrint('ball ' + num + ' pocketed\n');
}
function pocketEndResponse(pres, evt) {
if(!('plane' in evt.other) || evt.other.plane.normal.y < 0.5)
return;
ballNumber = ballIDs[pres.toString()];
pres.mesh = 'meerkat:///elliotconte/models/DSTARIN.dae/optimized/DSTARIN.dae';
pres.gravity.suspend();
pres.coll.suspend();
pres.pockets.suspend();
pres.velocity = <0, 0, 0>;
pres.position = table.position;
onBallPocketed(ballNumber);
stopBall(pres);
stopped[ballNumber] = 'pocketed';
}
function pocketStartResponse(pres, evt) {
if(pres.bounds.isSuspended() ||
!(evt.other.id in pocketIDs))
return;
pres.friction.suspend();
pres.bounds.suspend();
pres.pockets.suspend();
pres.gravity = new motion.Gravity(pres);
pres.pockets = new motion.Collision(pres, coll.TestSphereToPlanes([{
anchor: table.position + TABLE_BOUNDS.min,
normal: <0, 1, 0>
}]), pocketEndResponse);
}
function billiardsBounce(pres, evt) {
if(!(evt.other.id in ballIDs))
return;
system.__debugPrint('bounce! ' + ballIDs[pres.toString()] + '->' +
ballIDs[evt.other.id] + '\n');
coll.Bounce(ELASTICITY)(pres, evt);
stopped[ballIDs[evt.other.id]] = false;
stopped[ballIDs[pres.toString()]] = false;
debugPrintStopped();
}
function onTableCreated(pres) {
table = pres;
table.orientation = new util.Quaternion();
table.scale = TABLE_SCALE;
var DELTA = 2.1 * BALL_SCALE;
for(i = -1; i <= 1; i++) {
for(var j = -1; j <= 1; j += 2) {
var position = table.position + <0, TABLE_OFFSET, 0> +
POCKET_ROW_DELTA.scale(i) + POCKET_COL_DELTA.scale(j);
if(i == 0)
position = position + SIDE_POCKET_DELTA.scale(j);
system.createPresence('', onPocketCreated(position));
}
}
}
function onPocketCreated(pocketPos) {
return function(pres) {
pres.position = pocketPos;
pres.scale = POCKET_SCALE;
pockets.push(pres);
pocketIDs[pres.toString()] = true;
if(pockets.length == NUM_POCKETS) {
var i = 0;
for(var row = 0; row < NUM_ROWS; row++) {
for(var ball = 0; ball < row + 1; ball++) {
var ballPos = table.position + <-DELTA * row, TABLE_OFFSET,
DELTA * (ball - row / 2)>;
system.createPresence(ballMeshes[i], onBallCreated(ballPos));
i++;
}
}
}
};
}
function onBallCreated(position) {
return function(pres) {
balls.push(pres);
ballIDs[pres.toString()] = getBallNumber(pres);
stopped.push(true);
pres.scale = BALL_SCALE;
pres.position = position;
pres.queryAngle = DEFAULT_QUERY_ANGLE;
if(balls.length == NUM_BILLIARD_BALLS &&
pockets.length == NUM_POCKETS)
system.createPresence(cueBallMesh, onCueCreated);
};
}
function onCueCreated(pres) {
cue = pres;
balls.push(cue);
ballIDs[cue.toString()] = 0;
stopped.push(true);
cue.scale = BALL_SCALE;
cue.position = table.position + <CUE_OFFSET, TABLE_OFFSET, 0>;
cue.queryAngle = DEFAULT_QUERY_ANGLE;
for(var i in balls)
{
balls[i].coll = new motion.Collision(balls[i],
coll.TestSpheres(balls), billiardsBounce,
motion.defaultPeriod * (Math.random() + 0.5));
balls[i].bounds = new motion.Collision(balls[i],
coll.TestBounds(table.position + TABLE_BOUNDS.max,
table.position + TABLE_BOUNDS.min),
coll.Bounce(ELASTICITY),
motion.defaultPeriod * (Math.random() + 0.5));
balls[i].friction = new frictionController(balls[i], FRICTION,
motion.defaultPeriod * (Math.random() + 0.5));
balls[i].pockets = new motion.Collision(balls[i],
coll.TestSpheres(pockets), pocketStartResponse,
motion.defaultPeriod * (Math.random() + 0.5));
balls[i].rolling = new motion.Velocity(balls[i], rollingVelFn,
motion.defaultPeriod * (Math.random() + 0.5));
}
}
function hitCue(msg, sender) {
disp = cue.position - sender.getPosition();
disp.y = 0;
cue.velocity = disp * CUE_VELOCITY_FACTOR;
stopped[0] = false;
debugPrintStopped();
}
hitCue << [{'action':'touch':}];
system.createPresence(tableMesh, onTableCreated);
};