-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathragdoll.js
executable file
·145 lines (134 loc) · 4.73 KB
/
ragdoll.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
import { Circle, Spring, LineSegment } from './shapes.js'
import MultiSetter from './multiSetter.js';
import { distanceBetweenPoints as distPoints } from './distance.js';
class Boundary extends LineSegment {
constructor(pointA, pointB, normal) {
super(pointA, pointB);
}
hit() {}
draw(ctx) {
ctx.beginPath();
ctx.moveTo(this.pointA.x, this.pointA.y);
ctx.lineTo(this.pointB.x, this.pointB.y)
ctx.stroke();
}
}
class Head extends Circle {
constructor(radius, mass, x, y) {
super(radius, mass, x, y);
}
draw(ctx) {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI)
ctx.stroke();
}
}
class HandFoot extends Circle {
constructor(radius, mass, x, y) {
super(radius, mass, x, y);
}
draw(ctx) {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI)
ctx.stroke();
}
}
class Limb extends Spring {
constructor(k, relaxedLength, pointA, pointB) {
super(k, relaxedLength, pointA, pointB);
}
draw(ctx) {
ctx.beginPath();
ctx.moveTo(this.pointA.x, this.pointA.y);
ctx.lineTo(this.pointB.x, this.pointB.y)
ctx.stroke();
}
}
class Chest extends Spring {
constructor(k, relaxedLength, pointA, pointB) {
super(k, relaxedLength, pointA, pointB);
}
draw(ctx) {
ctx.beginPath();
ctx.moveTo(this.pointA.x, this.pointA.y);
ctx.lineTo(this.pointB.x, this.pointB.y)
ctx.stroke();
}
}
export class Ragdoll {
constructor(posX, posY, scale) {
this.scale = scale;
const handFootSize = 6;
this.nodes = [
new Head(scale * handFootSize * 2, scale * 20, scale * (posX), scale * (posY - 60)), // Head
new HandFoot(scale * handFootSize, scale * 10, scale * (posX + 35), scale * (posY + 5)), // Left hand
new HandFoot(scale * handFootSize, scale * 10, scale * (posX - 35), scale * (posY + 5)), // Right hand
new HandFoot(scale * handFootSize, scale * 10, scale * (posX + 20), scale * (posY + 60)), // Left foot
new HandFoot(scale * handFootSize, scale * 10, scale * (posX - 20), scale * (posY + 60)), // Right foot
new Circle(scale * handFootSize, scale * 10, scale * (posX + 10), scale * (posY - 40)), // Left shoulder
new Circle(scale * handFootSize, scale * 10, scale * (posX - 10), scale * (posY - 40)), // Right shoulder
new Circle(scale * handFootSize, scale * 10, scale * (posX), scale * (posY)), // Center/
];
this.links = [
new Limb(50, distPoints(this.nodes[1], this.nodes[5]), this.nodes[1], this.nodes[5]), // Left arm
new Limb(50, distPoints(this.nodes[2], this.nodes[6]), this.nodes[2], this.nodes[6]), // Right arm
new Limb(50, distPoints(this.nodes[7], this.nodes[3]), this.nodes[7], this.nodes[3]), // Left leg
new Limb(50, distPoints(this.nodes[7], this.nodes[4]), this.nodes[7], this.nodes[4]), // Right leg
new Chest(50, distPoints(this.nodes[5], this.nodes[7]), this.nodes[5], this.nodes[7]), // Left chest
new Chest(50, distPoints(this.nodes[6], this.nodes[7]), this.nodes[6], this.nodes[7]), // Right chest
new Spring(50, distPoints(this.nodes[5], this.nodes[0]), this.nodes[5], this.nodes[0]), // Left shoulder to head
new Spring(50, distPoints(this.nodes[6], this.nodes[0]), this.nodes[6], this.nodes[0]), // Right shoulder to head
new Spring(50, distPoints(this.nodes[5], this.nodes[6]), this.nodes[5], this.nodes[6]), // Between shoulders
new Spring(10, distPoints(this.nodes[1], this.nodes[2]), this.nodes[1], this.nodes[2]), // Between hands
new Spring(10, distPoints(this.nodes[3], this.nodes[4]), this.nodes[3], this.nodes[4]), // Between feet
new Spring(50, distPoints(this.nodes[0], this.nodes[7]), this.nodes[0], this.nodes[7]), // Between head center
];
}
draw(ctx) {
for(let node of this.nodes) {
node.draw(ctx);
}
for(let link of this.links) {
link.draw(ctx);
}
}
computePhysics(timeDelta, accelerometer) {
const resistance = 0.99;
const minForce = this.scale * 100;
const maxForce = this.scale * 1000;
const ms = new MultiSetter();
for(let node of this.nodes) {
if(node.forced || node.pinned) {
continue;
}
let force = {
x: node.mass * accelerometer.x,
y: node.mass * accelerometer.y,
}
for(let link of this.links) {
if(link.pointA === node || link.pointB === node) {
const linkForce = Math.abs(link.force);
if(minForce > linkForce) {
continue;
}
const forceVector = link.getForceVector(node);
force = {
x: force.x + forceVector.x,
y: force.y + forceVector.y,
};
}
}
let velocity = {
x: node.velocity.x * resistance + timeDelta * force.x / node.mass,
y: node.velocity.y * resistance + timeDelta * force.y / node.mass,
};
let pos = {
x: node.x + velocity.x * timeDelta,
y: node.y + velocity.y * timeDelta,
};
ms.add(node, { velocity, x: pos.x, y: pos.y });
}
ms.apply();
// console.log(1/timeDelta)
}
}