Fast bodies and thin walls
Open the live demo · Read the source · View on GitHub
A body is stepped from where it is to where its speed takes it in one step. At three hundred metres a second and sixty steps a second, that is five metres, and a wall a centimetre thick is never anywhere the ball is. Without something more, the ball goes through it.
The physics core has two answers. Soft continuous collision is on for every new world: a contact reaches as far as its two bodies can close in a step, so the solver sees the wall before the ball gets there. Hard continuous collision is for a body you mark as a bullet: after the solve it is swept along the path it took and put back where it first met something. This page fires three balls at one thin wall, one with each answer and one with neither.
Step 1: Two worlds, because speculative contacts are a world's #
speculative is a setting of a NativeWorld, not of a body, and it is on
when the world is made. The green ball's lane lives in a world left that way.
The other two share a second world with it turned off, so the only thing
stopping the bullet is the sweep. Gravity is off in both, so the balls fly
level. If the core will not start, the reason is kept and the page draws the
wall with nothing to fire at it.
/// Two worlds of the core. Speculative contacts are a setting of the
/// world, on for a new one, so the lane that shows them on lives in a
/// world of its own, and the two that show them off share the other.
void _makeWorlds() {
try {
_soft = NativeWorld()..gravity = Vector3.zero();
_hard = NativeWorld()
..gravity = Vector3.zero()
..speculative = false;
} on Object catch (e) {
_soft?.dispose();
_soft = null;
_hard = null;
fallback = physicsCoreLoaded
? '$e'
: 'the core is not loaded in this browser yet';
}
}
Step 2: A wall and a ball #
Each wall is a fixed box a centimetre thick. Each ball is a dynamic sphere
ten centimetres across, three metres in front of it. setBullet is the whole
difference between the amber ball and the red one, which sit in the same
world. A bullet costs a sweep against every body after each step, so it is
meant for what is small and fast, not for everything.
/// A fixed wall a centimetre thick across [world], and a small ball in
/// front of it at [z], made a bullet when [bullet] is set.
static NativeBody _wallAndBall(
NativeWorld world,
double z, {
required bool bullet,
}) {
final NativeBody wall = world.addBody(
position: Vector3(0.0, _height, 0.0),
type: NativeBodyType.fixed,
mass: 0.0,
);
world.setShape(wall, NativeShape.box(Vector3(_wallHalf, 0.6, 1.2)));
final NativeBody ball = world.addBody(
position: Vector3(_launchX, _height, z),
mass: 0.03,
);
world.setShape(ball, const NativeShape.sphere(_radius));
// Hard continuous collision: swept from where the step began to where it
// ended, and put back at the first thing it met.
if (bullet) world.setBullet(ball);
return ball;
}
Step 3: Fire, and step #
Every second and a half all three balls go back to the line and leave at the speed on the slider. Each frame steps both worlds by a sixtieth of a second. A streak is drawn from the line to each ball, because at these speeds a ball is in a frame for one step and then stopped or gone. Turn the speed down and the red ball starts to stop at some speeds and pass at others: whether it does depends on where its last step before the wall happens to end, which is exactly the luck the other two do not rely on.
/// Every ball back at the line and off again at [speed].
void _fire() {
for (final _Lane lane in _lanes) {
lane.world
..setPosition(lane.ball, Vector3(_launchX, _height, lane.z))
..setAngularVelocity(lane.ball, Vector3.zero())
..setVelocity(lane.ball, Vector3(speed, 0.0, 0.0))
..wake(lane.ball);
}
_sinceShot = 0.0;
}
_soft!.step(_step);
_hard!.step(_step);
Step 4: What has to happen #
From a fresh shot at three hundred metres a second, thirty steps later, the green ball and the bullet have to be on the near side of the wall and the red ball on the far side. On macOS the first two stop five and a half centimetres short of the wall's middle, touching its face, and the red one is about thirty metres past it, slowed by the air on the way.
// At three hundred metres a second a ball goes five metres a step, five
// hundred times the wall's thickness. The soft ball and the bullet have
// to end on the near side of it, the ball with neither on the far side.
speed = 300.0;
final [double soft, double bullet, double plain] = shoot();
if (soft >= 0.0) {
throw StateError('the soft ball went through, to x = $soft');
}
if (bullet >= 0.0) {
throw StateError('the bullet went through, to x = $bullet');
}
if (plain <= 0.0) {
throw StateError('the unprotected ball stopped at x = $plain');
}
Note. The soft kind stops what is coming straight at a wall, but it works from the speeds at the start of the step. A body that turns sharply, or is struck mid-step by something else fast, can still pass a contact it did not expect. The sweep follows a bullet's path through the step, turns included, but it is not run between two bullets, and a turn of more than half a revolution in one substep is past what it follows.