-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
381 lines (351 loc) · 15.3 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>Ragdoll demo - p2.js physics engine</title>
<script src="https://cdn.rawgit.com/simondiep/p2.js/acbda08c/build/p2.min.js"></script>
<style>
html,
body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
overflow: hidden;
}
</style>
<meta name="description" content="Shows how to build a rag doll.">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
</head>
<body>
<script>
let canvas;
let ctx;
let w, h;
let wallMaterials = [];
let upperLeftLeg;
let lowerLeftLeg;
let upperRightLeg;
let lowerRightLeg;
let lowerLeftArm;
let lowerRightArm;
const shouldersDistance = 0.5,
upperArmLength = 0.4,
lowerArmLength = 0.4,
upperArmSize = 0.2,
lowerArmSize = 0.2,
neckLength = 0.1,
headRadius = 0.25,
upperBodyLength = 0.6,
pelvisLength = 0.4,
upperLegLength = 0.5,
upperLegSize = 0.2,
lowerLegSize = 0.2,
lowerLegLength = 0.5;
let OTHER = Math.pow(2, 1),
BODYPARTS = Math.pow(2, 2),
GROUND = Math.pow(2, 3),
bodyPartShapes = [],
world;
// Zoom is critical to syncing up the p2 values to the canvas
let zoom = 50;
const FORCE_MULTIPLIER = 5;
window.onload = () => {
// Init canvas
canvas = document.getElementById("myCanvas");
w = canvas.width;
h = canvas.height;
ctx = canvas.getContext("2d");
ctx.lineWidth = 1 / zoom;
window.addEventListener('keydown', keydownHandler);
createWorld();
createRagdoll('#FFBC42');
// for (let bodyPart of bodyPartShapes) {
// for (let wallMaterial of wallMaterials) {
// const frictionContactMaterial = new p2.ContactMaterial(bodyPart.material, wallMaterial, {
// friction: 1,
// });
// world.addContactMaterial(frictionContactMaterial);
// }
// }
// To animate the bodies, we must step the world forward in time, using a fixed time step size.
// The World will run substeps and interpolate automatically for us, to get smooth animation.
const fixedTimeStep = 1 / 60; // seconds
const maxSubSteps = 10; // Max sub steps to catch up with the wall clock
let lastTime;
// Animation loop
function animate(time) {
requestAnimationFrame(animate);
// Compute elapsed time since last render frame
const deltaTime = lastTime ? (time - lastTime) / 1000 : 0;
// Move bodies forward in time
world.step(fixedTimeStep, deltaTime, maxSubSteps);
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, w, h);
// Transform the canvas
// Note that we need to flip the y axis since Canvas pixel coordinates
// goes from top to bottom, while physics does the opposite.
ctx.save();
ctx.translate(w / 2, h / 2); // Translate to the center
ctx.scale(zoom, -zoom); // Zoom in and flip y axis
// for (let bodyPart of bodyPartShapes) {
// drawBody(bodyPart.body);
// }
// Draw all bodies
ctx.strokeStyle = 'none';
for (let i = 0; i < world.bodies.length; i++) {
const body = world.bodies[i];
drawBody(body);
}
lastTime = time;
// Restore transform
ctx.restore();
}
// Start the animation loop
requestAnimationFrame(animate);
}
function keydownHandler(evt) {
switch (evt.keyCode) {
case 81: //q
lowerLeftArm.applyImpulseLocal([-1 * FORCE_MULTIPLIER, 1 * FORCE_MULTIPLIER]);
break;
case 87: //w
lowerLeftLeg.applyImpulseLocal([-1 * FORCE_MULTIPLIER, 1 * FORCE_MULTIPLIER]);
break;
case 79: //o
lowerRightArm.applyImpulseLocal([1 * FORCE_MULTIPLIER, 1 * FORCE_MULTIPLIER]);
break;
case 80: //p
lowerRightLeg.applyImpulseLocal([1 * FORCE_MULTIPLIER, 1 * FORCE_MULTIPLIER]);
break;
}
}
function drawBody(body) {
const x = body.interpolatedPosition[0],
y = body.interpolatedPosition[1],
s = body.shapes[0];
ctx.save();
ctx.translate(x, y); // Translate to the center of the box
ctx.rotate(body.interpolatedAngle); // Rotate to the box body frame
if (s instanceof p2.Box) {
ctx.fillStyle = s.color;
ctx.fillRect(-s.width / 2, -s.height / 2, s.width, s.height);
} else if (s instanceof p2.Circle) {
ctx.fillStyle = s.color;
ctx.beginPath();
ctx.arc(0, 0, s.radius, 0, 2 * Math.PI);
ctx.fill();
ctx.closePath();
}
ctx.restore();
}
function createWorld() {
world = new p2.World({
gravity: [0, -10]
});
world.solver.iterations = 100;
world.solver.tolerance = 0.002;
// floor
addStaticBox(0,-4,0,12,1);
// ceiling
addStaticBox(0,4,0,12,1);
// left wall
addStaticBox(-6,0,0,1,12);
// right wall
addStaticBox(6,0,0,1,12);
}
function addStaticBox(x, y, angle, width, height){
var shape = new p2.Box({
collisionGroup: GROUND,
collisionMask: BODYPARTS | OTHER,
width: width,
height: height,
material: new p2.Material(),
});
shape.color = '#4a4c4f';
var body = new p2.Body({
position: [x, y],
angle: angle,
});
body.addShape(shape);
world.addBody(body);
wallMaterials.push[shape.material];
}
function createRagdoll(color) {
const headShape = new p2.Circle({ radius: headRadius, material: new p2.Material() }),
upperArmShapeLeft = new p2.Box({ width: upperArmLength, height: upperArmSize, material: new p2.Material() }),
upperArmShapeRight = new p2.Box({ width: upperArmLength, height: upperArmSize, material: new p2.Material() }),
lowerArmShapeLeft = new p2.Box({ width: lowerArmLength, height: lowerArmSize, material: new p2.Material() }),
lowerArmShapeRight = new p2.Box({ width: lowerArmLength, height: lowerArmSize, material: new p2.Material() }),
upperBodyShape = new p2.Box({ width: shouldersDistance, height: upperBodyLength, material: new p2.Material() }),
pelvisShape = new p2.Box({ width: shouldersDistance, height: pelvisLength, material: new p2.Material() }),
upperLegShapeLeft = new p2.Box({ width: upperLegSize, height: upperLegLength, material: new p2.Material() }),
upperLegShapeRight = new p2.Box({ width: upperLegSize, height: upperLegLength, material: new p2.Material() }),
lowerLegShapeLeft = new p2.Box({ width: lowerLegSize, height: lowerLegLength, material: new p2.Material() }),
lowerLegShapeRight = new p2.Box({ width: lowerLegSize, height: lowerLegLength, material: new p2.Material() });
bodyPartShapes.push(
headShape,
upperArmShapeRight,
upperArmShapeLeft,
lowerArmShapeRight,
lowerArmShapeLeft,
upperBodyShape,
pelvisShape,
upperLegShapeRight,
upperLegShapeLeft,
lowerLegShapeRight,
lowerLegShapeLeft
);
for (let i = 0; i < bodyPartShapes.length; i++) {
const s = bodyPartShapes[i];
s.collisionGroup = BODYPARTS;
s.collisionMask = GROUND | OTHER;
s.color = color;
}
// Lower legs
lowerLeftLeg = new p2.Body({
mass: 1,
position: [-shouldersDistance / 2, lowerLegLength / 2],
});
lowerRightLeg = new p2.Body({
mass: 1,
position: [shouldersDistance / 2, lowerLegLength / 2],
});
lowerLeftLeg.addShape(lowerLegShapeLeft);
lowerRightLeg.addShape(lowerLegShapeRight);
world.addBody(lowerLeftLeg);
world.addBody(lowerRightLeg);
// Upper legs
upperLeftLeg = new p2.Body({
mass: 1,
position: [-shouldersDistance / 2, lowerLeftLeg.position[1] + lowerLegLength / 2 + upperLegLength / 2],
});
upperRightLeg = new p2.Body({
mass: 1,
position: [shouldersDistance / 2, lowerRightLeg.position[1] + lowerLegLength / 2 + upperLegLength / 2],
});
upperLeftLeg.addShape(upperLegShapeLeft);
upperRightLeg.addShape(upperLegShapeRight);
world.addBody(upperLeftLeg);
world.addBody(upperRightLeg);
// Pelvis
const pelvis = new p2.Body({
mass: 1,
position: [0, upperLeftLeg.position[1] + upperLegLength / 2 + pelvisLength / 2],
});
pelvis.addShape(pelvisShape);
world.addBody(pelvis);
// Upper body
const upperBody = new p2.Body({
mass: 1,
position: [0, pelvis.position[1] + pelvisLength / 2 + upperBodyLength / 2],
});
upperBody.addShape(upperBodyShape);
world.addBody(upperBody);
// Head
const head = new p2.Body({
mass: 1,
position: [0, upperBody.position[1] + upperBodyLength / 2 + headRadius + neckLength],
});
head.addShape(headShape);
world.addBody(head);
// Upper arms
const upperLeftArm = new p2.Body({
mass: 1,
position: [-shouldersDistance / 2 - upperArmLength / 2, upperBody.position[1] + upperBodyLength / 2],
});
const upperRightArm = new p2.Body({
mass: 1,
position: [shouldersDistance / 2 + upperArmLength / 2, upperBody.position[1] + upperBodyLength / 2],
});
upperLeftArm.addShape(upperArmShapeLeft);
upperRightArm.addShape(upperArmShapeRight);
world.addBody(upperLeftArm);
world.addBody(upperRightArm);
// lower arms
lowerLeftArm = new p2.Body({
mass: 1,
position: [upperLeftArm.position[0] - lowerArmLength / 2 - upperArmLength / 2,
upperLeftArm.position[1]],
});
lowerRightArm = new p2.Body({
mass: 1,
position: [upperRightArm.position[0] + lowerArmLength / 2 + upperArmLength / 2,
upperRightArm.position[1]],
});
lowerLeftArm.addShape(lowerArmShapeLeft);
lowerRightArm.addShape(lowerArmShapeRight);
world.addBody(lowerLeftArm);
world.addBody(lowerRightArm);
// Neck joint
const neckJoint = new p2.RevoluteConstraint(head, upperBody, {
localPivotA: [0, -headRadius - neckLength / 2],
localPivotB: [0, upperBodyLength / 2],
});
neckJoint.setLimits(-Math.PI / 8, Math.PI / 8);
world.addConstraint(neckJoint);
// Knee joints
const leftKneeJoint = new p2.RevoluteConstraint(lowerLeftLeg, upperLeftLeg, {
localPivotA: [0, lowerLegLength / 2],
localPivotB: [0, -upperLegLength / 2],
});
const rightKneeJoint = new p2.RevoluteConstraint(lowerRightLeg, upperRightLeg, {
localPivotA: [0, lowerLegLength / 2],
localPivotB: [0, -upperLegLength / 2],
});
leftKneeJoint.setLimits(-Math.PI / 8, Math.PI / 8);
rightKneeJoint.setLimits(-Math.PI / 8, Math.PI / 8);
world.addConstraint(leftKneeJoint);
world.addConstraint(rightKneeJoint);
// Hip joints
const leftHipJoint = new p2.RevoluteConstraint(upperLeftLeg, pelvis, {
localPivotA: [0, upperLegLength / 2],
localPivotB: [-shouldersDistance / 2, -pelvisLength / 2],
});
const rightHipJoint = new p2.RevoluteConstraint(upperRightLeg, pelvis, {
localPivotA: [0, upperLegLength / 2],
localPivotB: [shouldersDistance / 2, -pelvisLength / 2],
});
leftHipJoint.setLimits(-Math.PI / 8, Math.PI / 8);
rightHipJoint.setLimits(-Math.PI / 8, Math.PI / 8);
world.addConstraint(leftHipJoint);
world.addConstraint(rightHipJoint);
// Spine
const spineJoint = new p2.RevoluteConstraint(pelvis, upperBody, {
localPivotA: [0, pelvisLength / 2],
localPivotB: [0, -upperBodyLength / 2],
});
spineJoint.setLimits(-Math.PI / 8, Math.PI / 8);
world.addConstraint(spineJoint);
// Shoulders
const leftShoulder = new p2.RevoluteConstraint(upperBody, upperLeftArm, {
localPivotA: [-shouldersDistance / 2, upperBodyLength / 2],
localPivotB: [upperArmLength / 2, 0],
});
const rightShoulder = new p2.RevoluteConstraint(upperBody, upperRightArm, {
localPivotA: [shouldersDistance / 2, upperBodyLength / 2],
localPivotB: [-upperArmLength / 2, 0],
});
leftShoulder.setLimits(-Math.PI / 3, Math.PI / 3);
rightShoulder.setLimits(-Math.PI / 3, Math.PI / 3);
world.addConstraint(leftShoulder);
world.addConstraint(rightShoulder);
// Elbow joint
const leftElbowJoint = new p2.RevoluteConstraint(lowerLeftArm, upperLeftArm, {
localPivotA: [lowerArmLength / 2, 0],
localPivotB: [-upperArmLength / 2, 0],
});
const rightElbowJoint = new p2.RevoluteConstraint(lowerRightArm, upperRightArm, {
localPivotA: [-lowerArmLength / 2, 0],
localPivotB: [upperArmLength / 2, 0],
});
leftElbowJoint.setLimits(-Math.PI / 8, Math.PI / 8);
rightElbowJoint.setLimits(-Math.PI / 8, Math.PI / 8);
world.addConstraint(leftElbowJoint);
world.addConstraint(rightElbowJoint);
}
</script>
<canvas width="600" height="400" id="myCanvas"></canvas>
<div>QWOP to move</div>
</body>
</html>