-
Notifications
You must be signed in to change notification settings - Fork 2
/
worker.js
243 lines (214 loc) · 5.4 KB
/
worker.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
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
const physics = require('./build/Release/physics.node');
const FPS = 120;
const STEP_SECONDS = 1 / FPS;
const STEP_MILLISECONDS = 1000 / FPS;
const worlds = new Map();
const bodies = new Map();
class BodyRecord {
constructor(world, body) {
this.world = world;
this.body = body;
}
}
const _requestUpdate = () => {
const updates = [];
bodies.forEach((bodyRecord, bodyId) => {
const {body} = bodyRecord;
const position = body.getPosition();
const rotation = body.getRotation();
const linearVelocity = body.getLinearVelocity();
const angularVelocity = body.getAngularVelocity();
const update = {
id: bodyId,
position,
rotation,
linearVelocity,
angularVelocity,
};
updates.push(update);
});
send('update', updates);
};
let interval = null;
const _start = () => {
_stop();
interval = setInterval(() => {
worlds.forEach(world => {
world.stepSimulation(STEP_SECONDS, 1, STEP_SECONDS);
});
}, STEP_MILLISECONDS);
};
const _stop = () => {
if (interval) {
clearInterval(interval);
interval = null;
}
};
const _addWorld = ({id}) => {
const world = new physics.World();
worlds.set(id, world);
};
const _removeWorld = ({id}) => {
worlds.delete(id);
};
const _addBody = ({worldId, body: bodySpec}) => {
const world = worlds.get(worldId);
const body = _makeBody(bodySpec);
world.addRigidBody(body);
const {id: bodyId} = bodySpec;
const bodyRecord = new BodyRecord(world, body);
bodies.set(bodyId, bodyRecord);
};
const _removeBody = ({bodyId}) => {
const bodyRecord = bodies.get(bodyId);
const {world, body} = bodyRecord;
world.removeRigidBody(body);
bodies.delete(bodyId);
};
const _setPosition = ({bodyId, position}) => {
const bodyRecord = bodies.get(bodyId);
const {body} = bodyRecord;
const [x, y, z] = position;
body.setPosition(x, y, z);
};
const _setRotation = ({bodyId, rotation}) => {
const bodyRecord = bodies.get(bodyId);
const {body} = bodyRecord;
const [x, y, z, w] = rotation;
body.setRotation(x, y, z, w);
};
const _makeBody = bodySpec => {
const {type} = bodySpec;
switch (type) {
case 'plane': {
const {dimensions, mass, position, rotation} = bodySpec;
const plane = physics.RigidBody.make({
type: physics.RigidBody.PLANE,
dimensions,
mass,
});
if (position) {
plane.setPosition(position[0], position[1], position[2]);
}
if (rotation) {
plane.setRotation(rotation[0], rotation[1], rotation[2], rotation[3]);
}
return plane;
}
case 'box': {
const {dimensions, mass, position, rotation} = bodySpec;
const box = physics.RigidBody.make({
type: physics.RigidBody.BOX,
dimensions,
mass,
});
if (position) {
box.setPosition(position[0], position[1], position[2]);
}
if (rotation) {
box.setRotation(rotation[0], rotation[1], rotation[2], rotation[3]);
}
return box;
}
case 'sphere': {
const {size, mass, position, rotation} = bodySpec;
const sphere = physics.RigidBody.make({
type: physics.RigidBody.SPHERE,
size,
mass,
});
if (position) {
sphere.setPosition(position[0], position[1], position[2]);
}
if (rotation) {
sphere.setRotation(rotation[0], rotation[1], rotation[2], rotation[3]);
}
return sphere;
}
case 'convexHull': {
const {size, points, position, rotation} = bodySpec;
const convexHull = physics.RigidBody.make({
type: physics.RigidBody.CONVEX_HULL,
convexHull,
mass,
});
if (position) {
convexHull.setPosition(position[0], position[1], position[2]);
}
if (rotation) {
convexHull.setRotation(rotation[0], rotation[1], rotation[2], rotation[3]);
}
return convexHull;
}
case 'triangleMesh': {
const {size, points, position, rotation} = bodySpec;
const triangleMesh = physics.RigidBody.make({
type: physics.RigidBody.CONVEX_HULL,
points,
mass,
});
if (position) {
triangleMesh.setPosition(position[0], position[1], position[2]);
}
if (rotation) {
triangleMesh.setRotation(rotation[0], rotation[1], rotation[2], rotation[3]);
}
}
default:
return null;
}
};
process.on('message', m => {
const {type} = m;
switch (type) {
case 'requestUpdate':
_requestUpdate();
break;
case 'start':
_start();
break;
case 'stop':
_stop();
break;
case 'addWorld': {
const {args: {id}} = m;
_addWorld({id});
break;
}
case 'removeWorld': {
const {id} = m;
_removeWorld({id});
break;
}
case 'addBody': {
const {args: {worldId, body}} = m;
_addBody({worldId, body});
break;
}
case 'removeBody': {
const {args: {bodyId}} = m;
_removeBody({bodyId});
break;
}
case 'setPosition': {
const {args: {bodyId, position}} = m;
_setPosition({bodyId, position});
break;
}
case 'setRotation': {
const {args: {bodyId, rotation}} = m;
_setRotation({bodyId, rotation});
break;
}
default:
console.warn('unknown message type:', JSON.stringify(type));
break;
}
});
const send = (type, data) => {
process.send({
type,
data,
});
};
// console.log(physics);