planck-deterministic/example/OneSidedPlatform.ts

89 lines
1.8 KiB
TypeScript
Raw Permalink Normal View History

/*
2024-11-23 16:32:23 +03:30
* Copyright (c) Erin Catto
* Licensed under the MIT license
*/
2024-12-23 18:24:21 +03:30
import { World, Edge, Box, Circle, Testbed } from "planck";
2024-12-24 15:07:16 +03:30
const world = new World({
gravity: { x: 0, y: -10 },
});
2023-08-20 03:24:33 -07:00
const testbed = Testbed.mount();
testbed.start(world);
2018-10-20 23:34:22 -07:00
2024-12-18 14:18:45 +03:30
const radius = 0.5;
const top = 10.0 + 0.5;
const bottom = 10.0 - 0.5;
2024-12-18 14:18:45 +03:30
const UNKNOWN = 0;
const ABOVE = +1;
const BELOW = -1;
2024-12-18 14:18:45 +03:30
const state = UNKNOWN;
// Ground
2024-12-24 15:07:16 +03:30
const ground = world.createBody({
type: "static",
});
2024-12-24 19:58:07 +03:30
ground.createFixture({
shape: new Edge({ x: -20.0, y: 0.0 }, { x: 20.0, y: 0.0 }),
density: 0.0,
});
// Platform
2024-12-24 15:07:16 +03:30
const platform = world.createBody({
type: "static",
position: { x: 0.0, y: 10.0 },
});
2024-12-24 19:58:07 +03:30
const platformFix = platform.createFixture({
shape: new Box(3.0, 0.5),
density: 0.0,
});
// Actor
2024-12-24 15:07:16 +03:30
const character = world.createBody({
type: "dynamic",
position: { x: 0.0, y: 12.0 },
});
2024-12-24 19:58:07 +03:30
const characterFix = character.createFixture({
shape: new Circle(radius),
density: 20.0,
});
2024-12-23 18:24:21 +03:30
character.setLinearVelocity({ x: 0.0, y: -50.0 });
2018-10-20 23:34:22 -07:00
world.on("pre-solve", function (contact, oldManifold) {
2024-12-18 14:18:45 +03:30
const fixA = contact.getFixtureA();
const fixB = contact.getFixtureB();
2024-12-18 14:18:45 +03:30
const isCharPlatformContact =
(fixA === platformFix && fixB === characterFix) ||
(fixB === platformFix && fixA === characterFix);
if (!isCharPlatformContact) {
return;
}
2023-08-14 13:59:23 -07:00
if (false) {
// if character is below platform
// disable contact
2024-12-18 14:18:45 +03:30
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
2024-12-18 14:18:45 +03:30
const v = character.getLinearVelocity();
if (v.y > 0.0) {
contact.setEnabled(false);
}
}
});
testbed.step = function () {
2024-12-18 14:18:45 +03:30
const v = character.getLinearVelocity();
testbed.status("Character Linear Velocity", v.y);
};