95 lines
1.9 KiB
TypeScript
Raw Permalink Normal View History

2016-03-23 22:59:54 -04:00
/*
2024-11-23 16:32:23 +03:30
* Copyright (c) Erin Catto
* Licensed under the MIT license
2016-03-23 22:59:54 -04:00
*/
2024-12-23 18:24:21 +03:30
import { World, Transform, Box, Distance, Testbed } from "planck";
2017-04-06 11:47:38 -04:00
2024-12-18 14:18:45 +03:30
const DistanceInput = Distance.Input;
const DistanceOutput = Distance.Output;
const SimplexCache = Distance.Cache;
2016-03-23 22:59:54 -04:00
2024-12-18 14:18:45 +03:30
const world = new World();
2016-03-23 22:59:54 -04:00
2023-08-20 03:24:33 -07:00
const testbed = Testbed.mount();
testbed.start(world);
2016-03-23 22:59:54 -04:00
2024-12-23 18:24:21 +03:30
const transformA = new Transform({ x: 0.0, y: -0.2 });
2016-03-23 22:59:54 -04:00
2024-12-18 14:18:45 +03:30
const polygonA = new Box(10.0, 0.2);
2018-10-20 23:34:22 -07:00
2024-12-23 18:24:21 +03:30
const positionB = { x: 12.017401, y: 0.13678508 };
2023-08-14 13:59:23 -07:00
let angleB = -0.0109265;
2024-12-18 14:18:45 +03:30
const transformB = new Transform(positionB, angleB);
2016-03-23 22:59:54 -04:00
2024-12-18 14:18:45 +03:30
const polygonB = new Box(2.0, 0.1);
2016-03-23 22:59:54 -04:00
2024-12-24 15:07:16 +03:30
const bodyA = world.createBody({
type: "static",
});
2024-12-24 19:58:07 +03:30
const fixA = bodyA.createFixture({
shape: polygonA,
});
2016-03-23 22:59:54 -04:00
2024-12-24 15:07:16 +03:30
const bodyB = world.createBody({
type: "static",
});
2024-12-24 19:58:07 +03:30
const fixB = bodyB.createFixture({
shape: polygonB,
});
2016-03-23 22:59:54 -04:00
testbed.step = function () {
2024-12-18 14:18:45 +03:30
const input = new DistanceInput();
input.proxyA.set(polygonA, 0);
input.proxyB.set(polygonB, 0);
2023-08-11 08:09:56 -07:00
input.transformA.set(transformA);
input.transformB.set(transformB);
input.useRadii = true;
2016-03-23 22:59:54 -04:00
2024-12-18 14:18:45 +03:30
const cache = new SimplexCache();
2016-03-23 22:59:54 -04:00
2024-12-18 14:18:45 +03:30
const output = new DistanceOutput();
2017-01-22 14:36:51 -05:00
Distance(output, cache, input);
2017-01-22 14:36:51 -05:00
testbed.status("Distance", output.distance);
testbed.status("Iterations", output.iterations);
2017-01-22 14:36:51 -05:00
bodyA.setTransform(transformA);
bodyB.setTransform(transformB);
2017-01-22 14:36:51 -05:00
2024-12-18 14:18:45 +03:30
const x1 = output.pointA;
const x2 = output.pointB;
2017-01-22 14:36:51 -05:00
testbed.drawPoint(x1, 4.0, testbed.color(1.0, 0.0, 0.0));
testbed.drawPoint(x2, 4.0, testbed.color(1.0, 1.0, 0.0));
};
2017-01-22 14:36:51 -05:00
testbed.keydown = function () {
if (testbed.activeKeys["left"]) {
positionB.x -= 0.1;
}
2017-01-22 14:36:51 -05:00
if (testbed.activeKeys["right"]) {
positionB.x += 0.1;
}
2017-01-22 14:36:51 -05:00
if (testbed.activeKeys["down"]) {
positionB.y -= 0.1;
}
2017-01-22 14:36:51 -05:00
if (testbed.activeKeys["up"]) {
positionB.y += 0.1;
}
2017-01-22 14:36:51 -05:00
if (testbed.activeKeys["Z"]) {
angleB += 0.1;
}
2016-03-23 22:59:54 -04:00
if (testbed.activeKeys["X"]) {
angleB -= 0.1;
}
2016-03-23 22:59:54 -04:00
transformB.set(positionB, angleB);
};