-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
437 lines (393 loc) · 12.2 KB
/
index.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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
import { m4, vector } from "math";
import {
ArrayDataFromGltf,
PrimitiveRenderInfoFromArrayData,
EntityDataFromGltf,
getGlContext,
resizeCanvasToDisplaySize,
ProgramInfo,
MeshRenderer,
Drawer,
createBox,
PrimitiveRenderer,
Texture,
makeImgFromSvgXml,
makeStripeImg,
Entity,
GLTF,
GLcontextWrapper,
createCone,
createCircle,
defaultShaders,
pointLightShaders,
createSphere,
createTruncatedCone,
} from "graphics";
import MouseInput from "./src/misc/mouseInput";
import KeyInput from "./src/misc/keyInput";
const mouseInput = new MouseInput();
mouseInput.listen();
const keyInput = new KeyInput();
keyInput.listen();
const context = new GLcontextWrapper("canvas");
const gl = context.getContext();
context.resizeCanvasToDisplaySize();
const drawer = new Drawer();
drawer.setContext(context).update3DProjectionMatrix();
const defaultProgramInfo = new ProgramInfo(
defaultShaders.vert,
defaultShaders.frag
);
const pointLightProgramInfo = new ProgramInfo(
pointLightShaders.vert,
pointLightShaders.frag
);
defaultProgramInfo.setContext(context).compileShaders().createUniformSetters();
pointLightProgramInfo
.setContext(context)
.compileShaders()
.createUniformSetters();
import prog from "./shader";
prog.setContext(context).compileShaders().createUniformSetters();
const box = new PrimitiveRenderer(createBox(1, 1, 1));
const sphere = new PrimitiveRenderer(createSphere(1, 15, 15));
const circle = new PrimitiveRenderer(createCircle(8, 4));
const cylinder = new PrimitiveRenderer(createTruncatedCone(1, 1, 1, 8, 1));
const points = new PrimitiveRenderer({
mode: gl.POINTS,
numElements: 2,
offset: 0,
});
const line = new PrimitiveRenderer({
mode: gl.LINES,
numElements: 2,
offset: 0,
});
box
.setContext(context)
.createVAO()
.setDrawer(drawer)
.setProgramInfo(pointLightProgramInfo)
.createGeometryBuffers()
.setAttributes()
.setMode(2);
cylinder
.setContext(context)
.createVAO()
.setDrawer(drawer)
.setProgramInfo(pointLightProgramInfo)
.createGeometryBuffers()
.setAttributes()
.setMode(2);
sphere
.setContext(context)
.createVAO()
.setDrawer(drawer)
.setProgramInfo(pointLightProgramInfo)
.createGeometryBuffers()
.setAttributes()
.setMode(2);
circle
.setContext(context)
.createVAO()
.setDrawer(drawer)
.setProgramInfo(defaultProgramInfo)
.createGeometryBuffers()
.setAttributes()
.setMode(3);
line
.setContext(context)
.createVAO()
.setDrawer(drawer)
.setProgramInfo(defaultProgramInfo)
.createBufferAttribData("a_position", "vec3", { location: 0 })
.setOwnAttribute("a_position")
.bufferData("a_position", new Float32Array([0, 0, 0, 0, 1, 0]));
points
.setContext(context)
.createVAO()
.setDrawer(drawer)
.setProgramInfo(defaultProgramInfo)
.createBufferAttribData("a_position", "vec3", { location: 0 })
.setOwnAttribute("a_position")
.bufferData("a_position", new Float32Array([0, 0, 0]));
const uniforms = {
u_lightWorldPosition: [1000, 11, 1000],
u_ambientLight: [1, 1, 0.3, 0.11],
u_reverseLightDirection: [1, 0, 0],
u_shininess: 300,
};
import Simulation from "./src/physics/simulation";
import { Player, RigidBody } from "./src/physics/RigidBody";
import { Box, Cylinder, Sphere } from "./src/physics/collider";
import { Controllable, Noclip } from "./src/misc/controllable";
import { Joint, JointPositionConstraint } from "./src/physics/constraints";
const g = 9.8;
const sim = new Simulation();
const floor = { physics: new RigidBody(new Box(1000, 6, 1000)), sprite: box };
const cube2 = { physics: new RigidBody(new Box(2, 2, 2)), sprite: box };
const cube3 = { physics: new RigidBody(new Box(2, 2, 2)), sprite: box };
const cube4 = {
physics: new RigidBody(new Box(1, 1, 2)),
sprite: box,
};
cube2.physics.translate([0, 4.7, 0]);
cube4.physics.translate([0, 10, -5]);
cube3.physics.translate([0, 3, 0]);
//cube4.physics.rotate([Math.PI*0.6,Math.PI*0.3,Math.PI*0.3])
cube2.physics.addAcceleration([0, -g, 0]);
cube3.physics.addAcceleration([0, -g, 0]);
cube4.physics.addAcceleration([0, -g * 2, 0]);
cube4.physics.friction = 0;
cube2.physics.setMass(20);
cube3.physics.setMass(20);
sim.addObject(floor.physics);
sim.addObject(cube2.physics);
sim.addObject(cube3.physics);
sim.addObject(cube4.physics);
const wheel1 = {
physics: new RigidBody(new Cylinder(1, 1, 0.5)),
sprite: cylinder,
};
const wheel2 = {
physics: new RigidBody(new Cylinder(1, 1, 0.5)),
sprite: cylinder,
};
const wheel3 = {
physics: new RigidBody(new Cylinder(1, 1, 0.5)),
sprite: cylinder,
};
const wheel4 = {
physics: new RigidBody(new Cylinder(1, 1, 0.5)),
sprite: cylinder,
};
wheel1.physics.addAcceleration([0, -2*g, 0]);
wheel2.physics.addAcceleration([0, -2*g, 0]);
wheel3.physics.addAcceleration([0, -2*g, 0]);
wheel4.physics.addAcceleration([0, -2*g, 0]);
sim.addObject(wheel1.physics);
sim.addObject(wheel2.physics);
sim.addObject(wheel3.physics);
sim.addObject(wheel4.physics);
sim.addConstraints([
new Joint(cube4.physics, wheel1.physics, [2, -0.5, 2], [0, -0.5, 0], 0.2),
new Joint(cube4.physics, wheel1.physics, [1, -0.5, 2], [0, 0.5, 0], 0.2),
new Joint(cube4.physics, wheel2.physics, [-2, -0.5, 2], [0, 0.5, 0], 0.2),
new Joint(cube4.physics, wheel2.physics, [-1, -0.5, 2], [0, -0.5, 0], 0.2),
new Joint(cube4.physics, wheel3.physics, [2, -0.5, -2], [0, -0.5, 0], 0.2),
new Joint(cube4.physics, wheel3.physics, [1, -0.5, -2], [0, 0.5, 0], 0.2),
new Joint(cube4.physics, wheel4.physics, [-2, -0.5, -2], [0, 0.5, 0], 0.2),
new Joint(cube4.physics, wheel4.physics, [-1, -0.5, -2], [0, -0.5, 0], 0.2),
], 'ww');
sim.addPositionConstraints([
new JointPositionConstraint(cube4.physics, wheel1.physics, [2, -0.5, 2], [0, -0.5, 0], 0.2),
new JointPositionConstraint(cube4.physics, wheel1.physics, [1, -0.5, 2], [0, 0.5, 0], 0.2),
new JointPositionConstraint(cube4.physics, wheel2.physics, [-2, -0.5, 2], [0, 0.5, 0], 0.2),
new JointPositionConstraint(cube4.physics, wheel2.physics, [-1, -0.5, 2], [0, -0.5, 0], 0.2),
new JointPositionConstraint(cube4.physics, wheel3.physics, [2, -0.5, -2], [0, -0.5, 0], 0.2),
new JointPositionConstraint(cube4.physics, wheel3.physics, [1, -0.5, -2], [0, 0.5, 0], 0.2),
new JointPositionConstraint(cube4.physics, wheel4.physics, [-2, -0.5, -2], [0, 0.5, 0], 0.2),
new JointPositionConstraint(cube4.physics, wheel4.physics, [-1, -0.5, -2], [0, -0.5, 0], 0.2),
], 'ww');
const objects = [floor, cube2, cube3, cube4, wheel1, wheel2, wheel3, wheel4];
for (let i = 0; i < 3; i++) {
const cube = { physics: new RigidBody(new Box(6, 2, 2)), sprite: box };
cube.physics.translate([10, 5 * i + 2.5, i * 0.1]);
cube.physics.setMass(20);
cube.physics.addAcceleration([0, -g, 0]);
sim.addObject(cube.physics);
objects.push(cube);
}
const chain = [];
for (let i = 0; i < 5; i++) {
const cube = { physics: new RigidBody(new Box(2, 5, 2)), sprite: box };
cube.physics.translate([20, 5, i * 5 - 25]);
cube.physics.setMass(5);
cube.physics.addAcceleration([0, -g, 0]);
cube.physics.friction = 0;
//cube.physics.group = 1
if (i > 0) {
const c = new Joint(
cube.physics,
objects.at(-1).physics,
[0, -3, 0],
[0, 3, 0],
0.1
);
chain.push(c);
// cube.physics.static = true
}
sim.addObject(cube.physics);
objects.push(cube);
}
sim.addConstraints(chain, "chain");
function createRagdoll(pos = [0, 0, 0]) {
const body = new RigidBody(new Box(1, 2, 0.5));
const leftHand = new RigidBody(new Cylinder(0.5, 2, 0.5));
const rightHand = new RigidBody(new Cylinder(0.5, 2, 0.5));
const leftLeg = new RigidBody(new Cylinder(0.5, 2, 0.5));
const rightLeg = new RigidBody(new Cylinder(0.5, 2, 0.5));
const head = new RigidBody(new Sphere(1, 1, 1));
const constraints = [
new Joint(body, head, [0, 1.5, 0], [0, -1, 0], 0),
new Joint(body, leftHand, [1, 1, 0], [0, 1, 0], 0),
new Joint(body, rightHand, [-1, 1, 0], [0, 1, 0], 0),
new Joint(body, rightLeg, [-0.5, -1.2, 0], [0, 1.2, 0], 0),
new Joint(body, leftLeg, [0.5, -1.2, 0], [0, 1.2, 0], 0),
];
const positionConstraints = [
new JointPositionConstraint(body, head, [0, 1.5, 0], [0, -1, 0], 0.2),
new JointPositionConstraint(body, leftHand, [1, 1, 0], [0, 1, 0], 0.2),
new JointPositionConstraint(body, rightHand, [-1, 1, 0], [0, 1, 0], 0.2),
new JointPositionConstraint(
body,
rightLeg,
[-0.5, -1.2, 0],
[0, 1.2, 0],
0.2
),
new JointPositionConstraint(
body,
leftLeg,
[0.5, -1.2, 0],
[0, 1.2, 0],
0.2
),
];
//positionConstraints.forEach(c =>c.treshold = 0.1)
body.translate(pos);
head.translate(vector.sum(pos, [0, 2, 0]));
leftHand.translate(vector.sum(pos, [-3, 2, 0]));
rightHand.translate(vector.sum(pos, [3, 2, 0]));
rightLeg.translate(vector.sum(pos, [2, -2.5, 0]));
leftLeg.translate(vector.sum(pos, [-2, -2.5, 0]));
const bodies = [body, leftHand, rightHand, rightLeg, leftLeg, head];
positionConstraints.forEach((c) => (c.treshold = 0.05));
bodies.forEach((b) => {
//b.group = 'ragdoll'
//b.setMass(10)
});
return [bodies, constraints, positionConstraints];
}
for (let i = 0; i < 3; i++) {
const [bodies, constraints, positionConstraints] = createRagdoll([
6 * i,
10,
-10,
]);
bodies.forEach((b) => {
sim.addObject(b);
b.addAcceleration([0, -g, 0]);
// b.group = 'ragdoll' + i
objects.push({ physics: b, sprite: box });
});
sim.addConstraints(constraints, "ragdoll" + i);
sim.addPositionConstraints(positionConstraints, "ragdol" + i);
}
const platform = new RigidBody(new Box());
platform.DOF = [0, 0, 0, 0, 0, 0];
platform.static = true;
sim.addConstraints(
[new Joint(platform, objects.at(-1).physics, [0, -1.5, 0], [0, 2, 0], 0.1)],
"platform"
);
platform.translate([20, 10, -10]);
floor.physics.setMass(100000000);
floor.physics.translate([0, -3, 0]);
floor.static = true;
floor.DOF = [0, 0, 0, 0, 0, 0];
const player = new Controllable(cube4.physics);
player.listenKeyboard(keyInput);
player.listenMouse(mouseInput);
let lastCall = Date.now();
const fps = document.querySelector("#fps");
const loop = () => {
sim.tick(0.01666);
player.tick();
const curentTime = Date.now();
const delta = curentTime - lastCall;
fps.textContent = (1 / delta) * 1000;
lastCall = curentTime;
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.enable(gl.CULL_FACE);
gl.enable(gl.DEPTH_TEST);
const cameraMatrix = player.camMatrix;
const { translation } = m4.decompose(cameraMatrix);
objects.forEach((obj) => {
const u_matrix = obj.physics.collider.getM4();
const u_viewWorldPosition = translation;
obj.sprite.draw(
{ ...uniforms, u_matrix, u_viewWorldPosition, u_color: [1, 0.5, 0.1, 1] },
cameraMatrix
);
});
circle
.draw(
{
u_matrix: m4.rotation(Math.PI / 2, 0, 0),
u_color: [1, 0.5, 0.1, 1],
u_worldViewPosition: cameraMatrix,
},
cameraMatrix
)
.draw(
{
u_matrix: m4.identity(),
u_color: [1, 0.5, 0.1, 1],
u_worldViewPosition: cameraMatrix,
},
cameraMatrix
);
points.draw(
{
u_matrix: m4.identity(),
u_color: [0, 0.5, 0.1, 1],
u_worldViewPosition: cameraMatrix,
},
cameraMatrix
);
for (const [name, constraints] of sim.constraints) {
constraints.forEach((c) => {
points.draw(
{
u_matrix: m4.translation(...c.PA),
u_color: [1.0, 0.0, 0.1, 1],
u_worldViewPosition: cameraMatrix,
},
cameraMatrix
);
points.draw(
{
u_matrix: m4.translation(...c.PB),
u_color: [1.0, 0.0, 0.1, 1],
u_worldViewPosition: cameraMatrix,
},
cameraMatrix
);
});
}
const manifolds = sim.collisionManifolds.values();
/*
for (const manifold of manifolds) {
manifold.contacts.forEach((contact) => {
points
.draw(
{
u_matrix: m4.translation(...contact.PA),
u_color: [0.6, 0.6, 0.0, 1],
},
cameraMatrix
)
.draw(
{
u_matrix: m4.translation(...contact.PB),
u_color: [0.5, 0.1, 0.2, 1],
},
cameraMatrix
);
});
}*/
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
requestAnimationFrame(loop);
};
loop();