129 lines
2.7 KiB
TypeScript
Raw Permalink Normal View History

/*
2024-11-23 16:32:23 +03:30
* Copyright (c) Erin Catto
* Licensed under the MIT license
*/
// This is used to test sensor shapes.
2024-12-18 14:18:45 +03:30
import { World, Body, Fixture, Vec2, CircleShape, Box, Edge, 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);
2024-12-18 14:18:45 +03:30
const COUNT = 7;
2024-12-18 14:18:45 +03:30
interface UserDate {
touching: boolean;
}
let sensor: Fixture;
const bodies: Body[] = [];
const touching: UserDate[] = [];
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: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }),
density: 0.0,
});
if (0) {
sensor = ground.createFixture({
2024-12-23 18:24:21 +03:30
shape: new Box(10.0, 2.0, { x: 0.0, y: 20.0 }, 0.0),
isSensor: true,
});
} else {
sensor = ground.createFixture({
2024-12-23 18:24:21 +03:30
shape: new CircleShape({ x: 0.0, y: 10.0 }, 5.0),
isSensor: true,
});
}
2024-12-18 14:18:45 +03:30
const circle = new CircleShape(1.0);
2023-08-14 13:59:23 -07:00
for (let i = 0; i < COUNT; ++i) {
touching[i] = { touching: false };
2024-12-24 15:07:16 +03:30
bodies[i] = world.createBody({
type: "dynamic",
position: { x: -10.0 + 3.0 * i, y: 20.0 },
2024-12-24 19:58:07 +03:30
userData: touching[i],
});
bodies[i].createFixture({
shape: circle,
density: 1.0,
2024-12-24 15:07:16 +03:30
});
}
// Implement contact listener.
world.on("begin-contact", function (contact) {
2024-12-18 14:18:45 +03:30
const fixtureA = contact.getFixtureA();
const fixtureB = contact.getFixtureB();
if (fixtureA === sensor) {
2024-12-18 14:18:45 +03:30
const userData = fixtureB.getBody().getUserData() as UserDate;
if (userData) {
userData.touching = true;
}
}
if (fixtureB === sensor) {
2024-12-18 14:18:45 +03:30
const userData = fixtureA.getBody().getUserData() as UserDate;
if (userData) {
userData.touching = true;
}
}
});
// Implement contact listener.
world.on("end-contact", function (contact) {
2024-12-18 14:18:45 +03:30
const fixtureA = contact.getFixtureA();
const fixtureB = contact.getFixtureB();
if (fixtureA === sensor) {
2024-12-18 14:18:45 +03:30
const userData = fixtureB.getBody().getUserData() as UserDate;
if (userData) {
userData.touching = false;
}
}
if (fixtureB === sensor) {
2024-12-18 14:18:45 +03:30
const userData = fixtureA.getBody().getUserData() as UserDate;
if (userData) {
userData.touching = false;
}
}
});
testbed.step = function () {
// Traverse the contact results. Apply a force on shapes
// that overlap the sensor.
2023-08-14 13:59:23 -07:00
for (let i = 0; i < COUNT; ++i) {
if (!touching[i].touching) {
continue;
}
2024-12-18 14:18:45 +03:30
const body = bodies[i];
const ground = sensor.getBody();
2024-12-18 14:18:45 +03:30
const circle = sensor.getShape() as CircleShape;
const center = ground.getWorldPoint(circle.getCenter());
2024-12-18 14:18:45 +03:30
const position = body.getPosition();
2024-12-18 14:18:45 +03:30
const d = Vec2.sub(center, position);
2023-08-10 20:55:02 -07:00
if (d.lengthSquared() < 1e-18) {
continue;
}
d.normalize();
2024-12-18 14:18:45 +03:30
const F = Vec2.mul(d, 100.0);
body.applyForce(F, position, false);
}
};