115 lines
2.5 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 stress tests the dynamic tree broad-phase. This also shows that tile
// based collision is _not_ smooth due to Box2D not knowing about adjacency.
2024-12-23 18:24:21 +03:30
import { World, 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
const COUNT = 20;
2023-08-14 13:59:23 -07:00
let fixtureCount = 0;
2023-08-14 13:59:23 -07:00
{
2024-12-18 14:18:45 +03:30
const a = 0.5;
2024-12-24 15:07:16 +03:30
const ground = world.createBody({
type: "static",
position: { x: 0, y: -a },
});
2023-08-14 13:59:23 -07:00
if (true) {
2024-12-18 14:18:45 +03:30
const N = 200;
const M = 10;
2024-12-23 18:24:21 +03:30
const position = { x: 0, y: 0 };
position.y = 0.0;
2023-08-14 13:59:23 -07:00
for (let j = 0; j < M; ++j) {
position.x = -N * a;
for (let i = 0; i < N; ++i) {
2024-12-24 19:58:07 +03:30
ground.createFixture({
shape: new Box(a, a, position, 0.0),
density: 0.0,
});
2023-08-14 13:59:23 -07:00
++fixtureCount;
position.x += 2.0 * a;
}
position.y -= 2.0 * a;
}
2023-08-14 13:59:23 -07:00
} else {
2024-12-18 14:18:45 +03:30
const N = 200;
const M = 10;
2024-12-23 18:24:21 +03:30
const position = { x: 0, y: 0 };
2023-08-14 13:59:23 -07:00
position.x = -N * a;
for (let i = 0; i < N; ++i) {
position.y = 0.0;
for (let j = 0; j < M; ++j) {
2024-12-24 19:58:07 +03:30
ground.createFixture({
shape: new Box(a, a, position, 0.0),
density: 0.0,
});
2023-08-14 13:59:23 -07:00
position.y -= 2.0 * a;
}
position.x += 2.0 * a;
}
}
}
2023-08-14 13:59:23 -07:00
{
2024-12-18 14:18:45 +03:30
const a = 0.5;
const shape = new Box(a, a);
2024-12-23 18:24:21 +03:30
const x = { x: -7.0, y: 0.75 };
const y = { x: 0, y: 0 };
const deltaX = { x: 0.5625, y: 1.25 };
const deltaY = { x: 1.125, y: 0.0 };
2023-08-14 13:59:23 -07:00
for (let i = 0; i < COUNT; ++i) {
2024-12-23 18:24:21 +03:30
y.x = x.x;
y.y = x.y;
2023-08-14 13:59:23 -07:00
for (let j = i; j < COUNT; ++j) {
// bd.allowSleep = !(i == 0 && j == 0)
2024-12-24 15:07:16 +03:30
const body = world.createBody({
type: "dynamic",
position: y,
});
2024-12-24 19:58:07 +03:30
body.createFixture({
shape: shape,
density: 5.0,
});
2023-08-14 13:59:23 -07:00
++fixtureCount;
2024-12-23 18:24:21 +03:30
y.x += deltaY.x;
y.y += deltaY.y;
2023-08-14 13:59:23 -07:00
}
2024-12-23 18:24:21 +03:30
x.x += deltaX.x;
x.y += deltaX.y;
}
}
2024-12-18 14:18:45 +03:30
const createTime = Date.now();
testbed.step = function () {
2024-12-18 14:18:45 +03:30
const height = world.getTreeHeight();
const leafCount = world.getProxyCount();
const minimumNodeCount = 2 * leafCount - 1;
const minimumHeight = Math.ceil(Math.log(minimumNodeCount) / Math.log(2.0));
testbed.status("dynamic tree height", height);
testbed.status("min", minimumHeight);
testbed.status("create time", createTime + "ms");
testbed.status("fixture count", fixtureCount);
2023-08-14 13:59:23 -07:00
// let tree = world.m_broadPhase.m_tree;
// if (stepCount++ == 400) {
// tree.rebuildBottomUp();
// }
};