planck-deterministic/example/ContinuousTest.ts

111 lines
2.6 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, Body, stats, Circle, Edge, Box, 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
let bullet: Body;
let angularVelocity: number;
2024-12-24 15:07:16 +03:30
const ground = world.createBody({
type: "dynamic",
position: { x: 0.0, y: 0.0 },
});
2024-12-24 19:58:07 +03:30
ground.createFixture({
shape: new Edge({ x: -10.0, y: 0.0 }, { x: 10.0, y: 0.0 }),
density: 0.0,
});
ground.createFixture({
shape: new Box(0.2, 1.0, { x: 0.5, y: 1.0 }, 0.0),
density: 0.0,
});
2023-08-14 13:59:23 -07:00
if (true) {
// angle = 0.1;
2024-12-24 15:07:16 +03:30
bullet = world.createBody({
type: "dynamic",
position: { x: 0.0, y: 20.0 },
});
2024-12-24 19:58:07 +03:30
bullet.createFixture({
shape: new Box(2.0, 0.1),
density: 1.0,
});
2024-12-18 14:18:45 +03:30
angularVelocity = Math.random() * 100 - 50;
// angularVelocity = 46.661274;
2024-12-23 18:24:21 +03:30
bullet.setLinearVelocity({ x: 0.0, y: -100.0 });
bullet.setAngularVelocity(angularVelocity);
} else {
2024-12-18 14:18:45 +03:30
const shape = new Circle(0.5);
2024-12-24 15:07:16 +03:30
{
const body = world.createBody({
type: "dynamic",
position: { x: 0.0, y: 2.0 },
});
2024-12-24 19:58:07 +03:30
body.createFixture({
shape: shape,
density: 1.0,
});
2024-12-24 15:07:16 +03:30
}
{
const body = world.createBody({
type: "dynamic",
bullet: true,
position: { x: 0.0, y: 2.0 },
});
2024-12-24 19:58:07 +03:30
body.createFixture({
shape: shape,
density: 1.0,
});
2024-12-24 15:07:16 +03:30
body.setLinearVelocity({ x: 0.0, y: -100.0 });
}
}
2023-08-14 13:59:23 -07:00
function launch() {
stats.gjkCalls = 0;
stats.gjkIters = 0;
stats.gjkMaxIters = 0;
stats.toiCalls = 0;
stats.toiIters = 0;
stats.toiRootIters = 0;
stats.toiMaxRootIters = 0;
stats.toiTime = 0.0;
stats.toiMaxTime = 0.0;
2024-12-23 18:24:21 +03:30
bullet.setTransform({ x: 0.0, y: 20.0 }, 0.0);
2024-12-18 14:18:45 +03:30
angularVelocity = Math.random() * 100 - 50;
2024-12-23 18:24:21 +03:30
bullet.setLinearVelocity({ x: 0.0, y: -100.0 });
bullet.setAngularVelocity(angularVelocity);
}
2023-08-14 13:59:23 -07:00
launch();
2023-08-14 13:59:23 -07:00
let stepCount = 0;
testbed.step = function () {
testbed.status(stats);
if (stats.gjkCalls > 0) {
// "gjk calls = %d, ave gjk iters = %3.1, max gjk iters = %d", stats.gjkCalls, stats.gjkIters / float32(stats.gjkCalls), stats.gjkMaxIters
}
if (stats.toiCalls > 0) {
// "toi calls = %d, ave [max] toi iters = %3.1 [%d]", stats.toiCalls, stats.toiIters / float32(stats.toiCalls), stats.toiMaxRootIters
// "ave [max] toi root iters = %3.1 [%d]", stats.toiRootIters / float32(stats.toiCalls), stats.toiMaxRootIters
// "ave [max] toi time = %.1 [%.1] (microseconds)", 1000.0 * stats.toiTime / float32(stats.toiCalls), 1000.0 * stats.toiMaxTime
}
if (stepCount++ % 60 == 0) {
2023-08-14 13:59:23 -07:00
launch();
}
};