188 lines
3.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
*/
2024-12-24 15:07:16 +03:30
import { Vec2, World, Body, Fixture, Shape, Edge, Polygon, Box, Circle, Testbed } from "planck";
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
2023-08-14 13:59:23 -07:00
let pause = false;
2024-12-18 14:18:45 +03:30
const MAX_BODIES = 256;
2024-12-18 14:18:45 +03:30
const bodies: Body[] = [];
const shapes: Shape[] = [];
{
2024-12-24 15:07:16 +03:30
const ground = world.createBody({
type: "static",
});
2023-08-14 13:59:23 -07:00
let x1 = -20.0;
let y1 = 2.0 * Math.cos((x1 / 10.0) * Math.PI);
2023-08-14 13:59:23 -07:00
for (let i = 0; i < 80; ++i) {
2024-12-18 14:18:45 +03:30
const x2 = x1 + 0.5;
const y2 = 2.0 * Math.cos((x2 / 10.0) * Math.PI);
2024-12-24 19:58:07 +03:30
ground.createFixture({
shape: new Edge({ x: x1, y: y1 }, { x: x2, y: y2 }),
density: 0.0,
});
x1 = x2;
y1 = y2;
}
}
2024-12-23 18:24:21 +03:30
shapes[0] = new Polygon([
{ x: -0.5, y: 0.0 },
{ x: 0.5, y: 0.0 },
{ x: 0.0, y: 1.5 },
]);
2024-12-23 18:24:21 +03:30
shapes[1] = new Polygon([
{ x: -0.1, y: 0.0 },
{ x: 0.1, y: 0.0 },
{ x: 0.0, y: 1.5 },
]);
{
2024-12-18 14:18:45 +03:30
const w = 1.0;
const b = w / (2.0 + Math.sqrt(2.0));
const s = Math.sqrt(2.0) * b;
const vertices = [
2024-12-23 18:24:21 +03:30
{ x: 0.5 * s, y: 0.0 },
{ x: 0.5 * w, y: b },
{ x: 0.5 * w, y: b + s },
{ x: 0.5 * s, y: w },
{ x: -0.5 * s, y: w },
{ x: -0.5 * w, y: b + s },
{ x: -0.5 * w, y: b },
{ x: -0.5 * s, y: 0.0 },
2024-12-18 14:18:45 +03:30
];
shapes[2] = new Polygon(vertices);
}
shapes[3] = new Box(0.5, 0.5);
shapes[4] = new Circle(0.5);
2023-08-14 13:59:23 -07:00
let angle = 0.0;
2024-12-18 14:18:45 +03:30
function createItem(index: number) {
if (bodies.length > MAX_BODIES) {
2024-12-18 14:18:45 +03:30
world.destroyBody(bodies.shift()!);
}
2024-12-24 15:07:16 +03:30
const body = world.createBody({
type: "dynamic",
2024-12-23 18:24:21 +03:30
position: {
2024-12-24 15:07:16 +03:30
x: Math.random() * 20 - 10.0,
y: Math.random() * 10 + 10.0,
2024-12-23 18:24:21 +03:30
},
2024-12-24 15:07:16 +03:30
angle: Math.random() * 2 * Math.PI - Math.PI,
angularDamping: index === 4 ? 0.02 : 0,
});
2024-12-24 19:58:07 +03:30
body.createFixture({
shape: shapes[index],
2023-08-14 13:59:23 -07:00
density: 20.0,
friction: 0.3,
2023-08-14 13:59:23 -07:00
});
bodies.push(body);
}
function destroyBody() {
2024-12-18 14:18:45 +03:30
world.destroyBody(bodies.shift()!);
}
testbed.keydown = function (code, char) {
switch (char) {
case "1":
createItem(0);
break;
case "2":
createItem(1);
break;
case "3":
createItem(2);
break;
case "4":
createItem(3);
break;
case "5":
createItem(4);
break;
case "X":
destroyBody();
break;
case "Z":
pause = !pause;
break;
}
};
testbed.info("1-5: Drop new object, X: Destroy an object");
2023-08-14 13:59:23 -07:00
const rayCastResult = {
fixture: null,
point: null,
normal: null,
2024-12-18 14:18:45 +03:30
} as {
fixture: Fixture | null;
point: Vec2 | null;
normal: Vec2 | null;
2023-08-14 13:59:23 -07:00
};
2018-10-20 23:34:22 -07:00
2024-12-18 14:18:45 +03:30
function rayCastCallback(fixture: Fixture, point: Vec2, normal: Vec2, fraction: number) {
2023-08-14 13:59:23 -07:00
rayCastResult.fixture = fixture;
rayCastResult.point = point;
rayCastResult.normal = normal;
return fraction;
}
2018-10-20 23:34:22 -07:00
function rayCastReset() {
2023-08-14 13:59:23 -07:00
rayCastResult.fixture = null;
rayCastResult.point = null;
rayCastResult.normal = null;
}
testbed.step = function () {
2024-12-18 14:18:45 +03:30
const advanceRay = !pause; // settings.pause == 0 || settings.singleStep;
2024-12-18 14:18:45 +03:30
const L = 25.0;
2024-12-23 18:24:21 +03:30
const point1 = { x: 0.0, y: 10.0 };
const d = {
x: L * Math.cos(angle),
y: -L * Math.abs(Math.sin(angle)),
};
2024-12-18 14:18:45 +03:30
const point2 = Vec2.add(point1, d);
2023-08-14 13:59:23 -07:00
rayCastReset();
2023-08-14 13:59:23 -07:00
world.rayCast(point1, point2, rayCastCallback);
2018-10-20 23:34:22 -07:00
2024-12-18 14:18:45 +03:30
if (rayCastResult.point && rayCastResult.normal) {
2023-08-14 13:59:23 -07:00
testbed.drawPoint(rayCastResult.point, 5.0, testbed.color(0.4, 0.9, 0.4));
testbed.drawSegment(point1, rayCastResult.point, testbed.color(0.8, 0.8, 0.8));
2024-12-18 14:18:45 +03:30
const head = Vec2.combine(1, rayCastResult.point, 2, rayCastResult.normal);
2023-08-14 13:59:23 -07:00
testbed.drawSegment(rayCastResult.point, head, testbed.color(0.9, 0.9, 0.4));
} else {
testbed.drawSegment(point1, point2, testbed.color(0.8, 0.8, 0.8));
}
if (advanceRay) {
angle += (0.25 * Math.PI) / 180.0;
}
};