-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy pathWeb.ts
More file actions
167 lines (142 loc) · 3.27 KB
/
Web.ts
File metadata and controls
167 lines (142 loc) · 3.27 KB
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
/*
* Copyright (c) Erin Catto
* Licensed under the MIT license
*/
// This tests distance joints, body destruction, and joint destruction.
import { World, Body, Joint, Box, DistanceJoint, Testbed } from "planck";
const world = new World({
gravity: { x: 0, y: -10 },
});
const testbed = Testbed.mount();
testbed.start(world);
const ground = world.createBody({
type: "static",
});
const bodies: Body[] = [];
const joints: Joint[] = [];
const box = new Box(0.5, 0.5);
bodies[0] = world.createBody({
type: "dynamic",
position: { x: -5.0, y: 5.0 },
});
bodies[0].createFixture({
shape: box,
density: 5.0,
});
bodies[1] = world.createBody({
type: "dynamic",
position: { x: 5.0, y: 5.0 },
});
bodies[1].createFixture({
shape: box,
density: 5.0,
});
bodies[2] = world.createBody({
type: "dynamic",
position: { x: 5.0, y: 15.0 },
});
bodies[2].createFixture({
shape: box,
density: 5.0,
});
bodies[3] = world.createBody({
type: "dynamic",
position: { x: -5.0, y: 15.0 },
});
bodies[3].createFixture({
shape: box,
density: 5.0,
});
const jd = {
frequencyHz: 2.0,
dampingRatio: 0.0,
};
joints[0] = new DistanceJoint({
...jd,
bodyA: ground,
localAnchorA: { x: -10.0, y: 0.0 },
bodyB: bodies[0],
localAnchorB: { x: -0.5, y: -0.5 },
});
world.createJoint(joints[0]);
joints[1] = new DistanceJoint({
...jd,
bodyA: ground,
localAnchorA: { x: 10.0, y: 0.0 },
bodyB: bodies[1],
localAnchorB: { x: 0.5, y: -0.5 },
});
world.createJoint(joints[1]);
joints[2] = new DistanceJoint({
...jd,
bodyA: ground,
localAnchorA: { x: 10.0, y: 20.0 },
bodyB: bodies[2],
localAnchorB: { x: 0.5, y: 0.5 },
});
world.createJoint(joints[2]);
joints[3] = new DistanceJoint({
...jd,
bodyA: ground,
localAnchorA: { x: -10.0, y: 20.0 },
bodyB: bodies[3],
localAnchorB: { x: -0.5, y: 0.5 },
});
world.createJoint(joints[3]);
joints[4] = new DistanceJoint({
...jd,
bodyA: bodies[0],
localAnchorA: { x: 0.5, y: 0.0 },
bodyB: bodies[1],
localAnchorB: { x: -0.5, y: 0.0 },
});
world.createJoint(joints[4]);
joints[5] = new DistanceJoint({
...jd,
bodyA: bodies[1],
localAnchorA: { x: 0.0, y: 0.5 },
bodyB: bodies[2],
localAnchorB: { x: 0.0, y: -0.5 },
});
world.createJoint(joints[5]);
joints[6] = new DistanceJoint({
...jd,
bodyA: bodies[2],
localAnchorA: { x: -0.5, y: 0.0 },
bodyB: bodies[3],
localAnchorB: { x: 0.5, y: 0.0 },
});
world.createJoint(joints[6]);
joints[7] = new DistanceJoint({
...jd,
bodyA: bodies[3],
localAnchorA: { x: 0.0, y: -0.5 },
bodyB: bodies[0],
localAnchorB: { x: 0.0, y: 0.5 },
});
world.createJoint(joints[7]);
testbed.keydown = function (code, char) {
switch (char) {
case "X":
if (bodies.length) {
const body = bodies.pop();
if (body) world.destroyBody(body);
}
break;
case "Z":
if (joints.length) {
const joint = joints.pop();
if (joint) world.destroyJoint(joint);
}
break;
}
};
testbed.info("This demonstrates a soft distance joint.\nX: Delete a body, Z: Delete a joint");
world.on("remove-joint", function (joint) {
const index = joints.indexOf(joint);
if (index > -1) joints.splice(index, 1);
});
world.on("remove-body", function (body) {
const index = bodies.indexOf(body);
if (index > -1) bodies.splice(index, 1);
});