Commit 5696a7b
authored
feat: Let the length unit be set through initializeForge2D (#120)
# Description
Box2D has a handful of tolerances that are absolute lengths rather than
fractions of the shapes
they apply to. The most visible one is the speculative distance:
`manifold.c` stops generating
contact points past `B2_SPECULATIVE_DISTANCE` (`4 * B2_LINEAR_SLOP`, so
0.02 m), and
`contact.c` sets `touching = pointCount > 0`, which means `beginContact`
fires while there is
still a gap of up to 2 cm. A world laid out at a much smaller scale than
a meter is dominated by
this: shapes that are only a couple of centimeters across are
permanently in contact with their
neighbors.
This came up while migrating flame_forge2d (flame-engine/flame#3952),
where a reporter's ball had
a radius of exactly 0.02, and it took a week to track down because the
0.02 is not discoverable
from Dart.
Box2D's answer is `b2SetLengthUnitsPerMeter`, which scales all of them.
Its contract is
`@warning This must be modified before any calls to Box2D`, which a
free-standing setter cannot
enforce, so it is exposed through the `initializeForge2D` gate that
already has to run first:
```dart
await initializeForge2D(lengthUnitsPerMeter: 100);
```
- Passing the value already in effect is a no-op, so several games that
agree on a scale can each
ask for it. The comparison round-trips through float32, since that is
how Box2D stores it and
values like `0.04` are not representable in either float width.
- A value that conflicts with the one in effect throws a `StateError`
once a `World` exists,
rather than silently corrupting live simulations and the defaults Box2D
hands out.
- Non-positive and non-finite values throw an `ArgumentError`.
`Tolerances` exposes the derived constants (`lengthUnitsPerMeter`,
`linearSlop`,
`speculativeDistance`, `aabbMargin`), so the 0.02 becomes a documented
number that callers can
reason about and assert against instead of a mystery.
The web backend needs a keepalive wrapper: `b2SetLengthUnitsPerMeter`
and
`b2GetLengthUnitsPerMeter` are plain `B2_API` functions, so emcc drops
them without one.
The README gains a "Units" section covering the scale to lay a world out
at, the absolute
tolerances that bite when you do not, and how the other quantities scale
when you rescale a world
(lengths, velocities and gravity by `S`, masses by `S²`, forces and
impulses by `S³`, torques by
`S⁴`, with densities, friction, restitution and damping unchanged, which
leaves the timing of the
simulation unchanged).
## Testing
`dart test` runs suites as isolates that share one process, and
therefore one copy of the native
library, so a suite that changes the length unit would be visible to
whichever suites run
alongside it. `dart_test.yaml` therefore sets `concurrency: 1`, so
suites run one at a time as
part of the normal test run, and the mutating suite puts the length unit
back when it is done.
The whole suite takes a couple of seconds either way.
`melos test` passes and `melos analyze` is clean.
## Checklist
- [x] The title of my PR starts with a [Conventional Commit] prefix
(`fix:`, `feat:`, `docs:` etc).
- [x] I have read the [Contributor Guide] and followed the process
outlined for submitting PRs.
- [x] I have updated/added tests for ALL new/updated/fixed
functionality.
- [x] I have updated/added relevant documentation in `docs` and added
dartdoc comments with `///`.
- [-] I have updated/added relevant examples in `examples`.
## Breaking Change
- [ ] Yes, this is a breaking change.
- [x] No, this is *not* a breaking change.
Everything is additive: the new parameter is optional and defaults to
leaving the length unit
alone, and `Tolerances` is a new class.
## Related Issues
Needed by flame-engine/flame#3952, which uses it for worlds that cannot
be laid out at a
realistic scale, and reports the underlying problem as a debug-mode
warning.
<!-- Links -->
[issue database]: https://github.com/flame-engine/flame/issues
[Contributor Guide]:
https://github.com/flame-engine/flame/blob/main/CONTRIBUTING.md
[Flame Style Guide]:
https://github.com/flame-engine/flame/blob/main/STYLEGUIDE.md
[Conventional Commit]: https://conventionalcommits.org1 parent 49dd404 commit 5696a7b
15 files changed
Lines changed: 355 additions & 9 deletions
File tree
- .github/workflows
- packages/forge2d
- lib
- src
- api
- backend
- wasm
- native/wasm
- test/api
- tool
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
24 | 30 | | |
25 | 31 | | |
26 | 32 | | |
27 | 33 | | |
28 | | - | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
29 | 40 | | |
30 | 41 | | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
84 | 129 | | |
85 | 130 | | |
86 | 131 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| 24 | + | |
24 | 25 | | |
25 | | - | |
| 26 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
49 | | - | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
50 | 53 | | |
51 | 54 | | |
52 | 55 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
11 | 22 | | |
12 | 23 | | |
13 | 24 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
64 | 73 | | |
65 | 74 | | |
66 | 75 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
110 | 110 | | |
111 | 111 | | |
112 | 112 | | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
113 | 123 | | |
114 | 124 | | |
115 | 125 | | |
| |||
Binary file not shown.
0 commit comments