-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy pathOneSidedPlatform.ts
More file actions
88 lines (73 loc) · 1.82 KB
/
OneSidedPlatform.ts
File metadata and controls
88 lines (73 loc) · 1.82 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
/*
* Copyright (c) Erin Catto
* Licensed under the MIT license
*/
import { World, Edge, Box, Circle, Testbed } from "planck";
const world = new World({
gravity: { x: 0, y: -10 },
});
const testbed = Testbed.mount();
testbed.start(world);
const radius = 0.5;
const top = 10.0 + 0.5;
const bottom = 10.0 - 0.5;
const UNKNOWN = 0;
const ABOVE = +1;
const BELOW = -1;
const state = UNKNOWN;
// Ground
const ground = world.createBody({
type: "static",
});
ground.createFixture({
shape: new Edge({ x: -20.0, y: 0.0 }, { x: 20.0, y: 0.0 }),
density: 0.0,
});
// Platform
const platform = world.createBody({
type: "static",
position: { x: 0.0, y: 10.0 },
});
const platformFix = platform.createFixture({
shape: new Box(3.0, 0.5),
density: 0.0,
});
// Actor
const character = world.createBody({
type: "dynamic",
position: { x: 0.0, y: 12.0 },
});
const characterFix = character.createFixture({
shape: new Circle(radius),
density: 20.0,
});
character.setLinearVelocity({ x: 0.0, y: -50.0 });
world.on("pre-solve", function (contact, oldManifold) {
const fixA = contact.getFixtureA();
const fixB = contact.getFixtureB();
const isCharPlatformContact =
(fixA === platformFix && fixB === characterFix) ||
(fixB === platformFix && fixA === characterFix);
if (!isCharPlatformContact) {
return;
}
if (false) {
// if character is below platform
// disable contact
const p = character.getPosition();
if (p.y < top + radius - 3.0 * /*linearSlop*/ 0.005) {
contact.setEnabled(false);
}
} else {
// if character is moving up
// disable contact
const v = character.getLinearVelocity();
if (v.y > 0.0) {
contact.setEnabled(false);
}
}
});
testbed.step = function () {
const v = character.getLinearVelocity();
testbed.status("Character Linear Velocity", v.y);
};