wallhammer-web 0.0.0-stage → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +92 -2
- package/dist/base/data.d.ts +67 -0
- package/dist/base/data.js +10 -0
- package/dist/base/destructible.d.ts +167 -0
- package/dist/base/destructible.js +47 -0
- package/dist/base/env-slot.d.ts +101 -0
- package/dist/base/env-slot.js +8 -0
- package/dist/base/frames.d.ts +138 -0
- package/dist/base/frames.js +459 -0
- package/dist/base/index.d.ts +21 -0
- package/dist/base/index.js +11 -0
- package/dist/base/mat34.d.ts +46 -0
- package/dist/base/mat34.js +173 -0
- package/dist/base/material.d.ts +87 -0
- package/dist/base/material.js +6 -0
- package/dist/base/math.d.ts +27 -0
- package/dist/base/math.js +60 -0
- package/dist/base/progress.d.ts +38 -0
- package/dist/base/progress.js +81 -0
- package/dist/base/rig.d.ts +177 -0
- package/dist/base/rig.js +97 -0
- package/dist/base/trig.d.ts +29 -0
- package/dist/base/trig.js +169 -0
- package/dist/base/up.d.ts +26 -0
- package/dist/base/up.js +40 -0
- package/dist/base/view.d.ts +128 -0
- package/dist/base/view.js +136 -0
- package/dist/camera/camera.d.ts +99 -0
- package/dist/camera/camera.js +228 -0
- package/dist/camera/follow.d.ts +120 -0
- package/dist/camera/follow.js +320 -0
- package/dist/camera/free.d.ts +153 -0
- package/dist/camera/free.js +359 -0
- package/dist/camera/index.d.ts +10 -0
- package/dist/camera/index.js +6 -0
- package/dist/camera/orbit.d.ts +93 -0
- package/dist/camera/orbit.js +261 -0
- package/dist/camera/path.d.ts +48 -0
- package/dist/camera/path.js +179 -0
- package/dist/core/ballistic.d.ts +66 -0
- package/dist/core/ballistic.js +104 -0
- package/dist/core/bodies.d.ts +77 -0
- package/dist/core/bodies.js +125 -0
- package/dist/core/clock.d.ts +37 -0
- package/dist/core/clock.js +87 -0
- package/dist/core/cover-fade.d.ts +14 -0
- package/dist/core/cover-fade.js +38 -0
- package/dist/core/index.d.ts +13 -0
- package/dist/core/index.js +8 -0
- package/dist/core/kepler.d.ts +74 -0
- package/dist/core/kepler.js +108 -0
- package/dist/core/level-fade.d.ts +66 -0
- package/dist/core/level-fade.js +123 -0
- package/dist/core/palette.d.ts +15 -0
- package/dist/core/palette.js +47 -0
- package/dist/env/atmosphere.d.ts +129 -0
- package/dist/env/atmosphere.js +707 -0
- package/dist/env/cloud-field.d.ts +44 -0
- package/dist/env/cloud-field.js +111 -0
- package/dist/env/environment.d.ts +217 -0
- package/dist/env/environment.js +441 -0
- package/dist/env/frame-block.d.ts +29 -0
- package/dist/env/frame-block.js +152 -0
- package/dist/env/glsl.d.ts +35 -0
- package/dist/env/glsl.js +246 -0
- package/dist/env/index.d.ts +13 -0
- package/dist/env/index.js +9 -0
- package/dist/env/sky.d.ts +48 -0
- package/dist/env/sky.js +227 -0
- package/dist/env/sun.d.ts +31 -0
- package/dist/env/sun.js +42 -0
- package/dist/gen-arch/build.d.ts +17 -0
- package/dist/gen-arch/build.js +414 -0
- package/dist/gen-arch/damage/damaged.d.ts +44 -0
- package/dist/gen-arch/damage/damaged.js +1119 -0
- package/dist/gen-arch/damage/fall-material.d.ts +10 -0
- package/dist/gen-arch/damage/fall-material.js +47 -0
- package/dist/gen-arch/damage/fall.d.ts +78 -0
- package/dist/gen-arch/damage/fall.js +797 -0
- package/dist/gen-arch/damage/rubble.d.ts +22 -0
- package/dist/gen-arch/damage/rubble.js +188 -0
- package/dist/gen-arch/index.d.ts +25 -0
- package/dist/gen-arch/index.js +16 -0
- package/dist/gen-arch/interior/interior-plan.d.ts +114 -0
- package/dist/gen-arch/interior/interior-plan.js +615 -0
- package/dist/gen-arch/interior/interior.d.ts +97 -0
- package/dist/gen-arch/interior/interior.js +966 -0
- package/dist/gen-arch/structure/building-shader.d.ts +25 -0
- package/dist/gen-arch/structure/building-shader.js +946 -0
- package/dist/gen-arch/structure/buildings.d.ts +248 -0
- package/dist/gen-arch/structure/buildings.js +294 -0
- package/dist/gen-arch/structure/dims.d.ts +77 -0
- package/dist/gen-arch/structure/dims.js +39 -0
- package/dist/gen-arch/structure/distant.d.ts +62 -0
- package/dist/gen-arch/structure/distant.js +316 -0
- package/dist/gen-arch/structure/env-slots.d.ts +18 -0
- package/dist/gen-arch/structure/env-slots.js +47 -0
- package/dist/gen-arch/structure/facade.d.ts +193 -0
- package/dist/gen-arch/structure/facade.js +159 -0
- package/dist/gen-arch/structure/parts.d.ts +31 -0
- package/dist/gen-arch/structure/parts.js +69 -0
- package/dist/gen-arch/structure/roof.d.ts +76 -0
- package/dist/gen-arch/structure/roof.js +568 -0
- package/dist/gen-arch/structure/structure-material.d.ts +23 -0
- package/dist/gen-arch/structure/structure-material.js +55 -0
- package/dist/gen-arch/structure/writer.d.ts +94 -0
- package/dist/gen-arch/structure/writer.js +195 -0
- package/dist/gen-core/cell.d.ts +138 -0
- package/dist/gen-core/cell.js +150 -0
- package/dist/gen-core/cube-sphere.d.ts +57 -0
- package/dist/gen-core/cube-sphere.js +159 -0
- package/dist/gen-core/field.d.ts +55 -0
- package/dist/gen-core/field.js +154 -0
- package/dist/gen-core/geometry.d.ts +31 -0
- package/dist/gen-core/geometry.js +231 -0
- package/dist/gen-core/hash.d.ts +18 -0
- package/dist/gen-core/hash.js +58 -0
- package/dist/gen-core/height-field.d.ts +33 -0
- package/dist/gen-core/height-field.js +137 -0
- package/dist/gen-core/index.d.ts +22 -0
- package/dist/gen-core/index.js +17 -0
- package/dist/gen-core/instance-cells.d.ts +37 -0
- package/dist/gen-core/instance-cells.js +72 -0
- package/dist/gen-core/near-scatter.d.ts +56 -0
- package/dist/gen-core/near-scatter.js +120 -0
- package/dist/gen-core/noise.d.ts +23 -0
- package/dist/gen-core/noise.js +72 -0
- package/dist/gen-core/occupancy.d.ts +21 -0
- package/dist/gen-core/occupancy.js +104 -0
- package/dist/gen-core/sphere-patch.d.ts +138 -0
- package/dist/gen-core/sphere-patch.js +544 -0
- package/dist/gen-core/strip-surface.d.ts +34 -0
- package/dist/gen-core/strip-surface.js +246 -0
- package/dist/gen-core/strip.d.ts +102 -0
- package/dist/gen-core/strip.js +817 -0
- package/dist/gen-ground/env-slots.d.ts +48 -0
- package/dist/gen-ground/env-slots.js +78 -0
- package/dist/gen-ground/ground.d.ts +78 -0
- package/dist/gen-ground/ground.js +208 -0
- package/dist/gen-ground/index.d.ts +9 -0
- package/dist/gen-ground/index.js +7 -0
- package/dist/gen-ground/markings.d.ts +46 -0
- package/dist/gen-ground/markings.js +124 -0
- package/dist/gen-ground/materials.d.ts +18 -0
- package/dist/gen-ground/materials.js +22 -0
- package/dist/gen-ground/terrain-shader.d.ts +5 -0
- package/dist/gen-ground/terrain-shader.js +151 -0
- package/dist/gen-mesh/bare-shapes.d.ts +194 -0
- package/dist/gen-mesh/bare-shapes.js +596 -0
- package/dist/gen-mesh/bisect-surface.d.ts +47 -0
- package/dist/gen-mesh/bisect-surface.js +261 -0
- package/dist/gen-mesh/box.d.ts +18 -0
- package/dist/gen-mesh/box.js +2 -0
- package/dist/gen-mesh/cell-morph-mesh.d.ts +52 -0
- package/dist/gen-mesh/cell-morph-mesh.js +258 -0
- package/dist/gen-mesh/cell-surface-mesh.d.ts +32 -0
- package/dist/gen-mesh/cell-surface-mesh.js +140 -0
- package/dist/gen-mesh/chunk-mesh.d.ts +25 -0
- package/dist/gen-mesh/chunk-mesh.js +112 -0
- package/dist/gen-mesh/drape-mesh.d.ts +36 -0
- package/dist/gen-mesh/drape-mesh.js +98 -0
- package/dist/gen-mesh/height-field-mesh.d.ts +14 -0
- package/dist/gen-mesh/height-field-mesh.js +98 -0
- package/dist/gen-mesh/index.d.ts +26 -0
- package/dist/gen-mesh/index.js +15 -0
- package/dist/gen-mesh/line-mesh.d.ts +25 -0
- package/dist/gen-mesh/line-mesh.js +62 -0
- package/dist/gen-mesh/mesh-builder.d.ts +115 -0
- package/dist/gen-mesh/mesh-builder.js +315 -0
- package/dist/gen-mesh/packed-mesh.d.ts +53 -0
- package/dist/gen-mesh/packed-mesh.js +205 -0
- package/dist/gen-mesh/prism-mesh.d.ts +33 -0
- package/dist/gen-mesh/prism-mesh.js +176 -0
- package/dist/gen-mesh/rig-mesh.d.ts +9 -0
- package/dist/gen-mesh/rig-mesh.js +135 -0
- package/dist/gen-mesh/strip-mesh.d.ts +45 -0
- package/dist/gen-mesh/strip-mesh.js +258 -0
- package/dist/gen-plan/drain.d.ts +58 -0
- package/dist/gen-plan/drain.js +270 -0
- package/dist/gen-plan/grid.d.ts +37 -0
- package/dist/gen-plan/grid.js +182 -0
- package/dist/gen-plan/horizon.d.ts +50 -0
- package/dist/gen-plan/horizon.js +185 -0
- package/dist/gen-plan/index.d.ts +15 -0
- package/dist/gen-plan/index.js +13 -0
- package/dist/gen-plan/line-tiles.d.ts +35 -0
- package/dist/gen-plan/line-tiles.js +190 -0
- package/dist/gen-plan/lines.d.ts +31 -0
- package/dist/gen-plan/lines.js +183 -0
- package/dist/gen-plan/plots.d.ts +40 -0
- package/dist/gen-plan/plots.js +282 -0
- package/dist/gen-plan/route.d.ts +55 -0
- package/dist/gen-plan/route.js +297 -0
- package/dist/gen-plan/sites.d.ts +32 -0
- package/dist/gen-plan/sites.js +209 -0
- package/dist/gen-tex/code-stamp.d.ts +31 -0
- package/dist/gen-tex/code-stamp.js +137 -0
- package/dist/gen-tex/index.d.ts +7 -0
- package/dist/gen-tex/index.js +5 -0
- package/dist/gen-tex/light-horizon.d.ts +47 -0
- package/dist/gen-tex/light-horizon.js +180 -0
- package/dist/gen-tex/textures.d.ts +8 -0
- package/dist/gen-tex/textures.js +49 -0
- package/dist/gen-tex/value-textures.d.ts +46 -0
- package/dist/gen-tex/value-textures.js +102 -0
- package/dist/gen-vegetation/env-slots.d.ts +20 -0
- package/dist/gen-vegetation/env-slots.js +29 -0
- package/dist/gen-vegetation/index.d.ts +8 -0
- package/dist/gen-vegetation/index.js +7 -0
- package/dist/gen-vegetation/materials.d.ts +7 -0
- package/dist/gen-vegetation/materials.js +17 -0
- package/dist/gen-vegetation/planting.d.ts +170 -0
- package/dist/gen-vegetation/planting.js +363 -0
- package/dist/gen-vegetation/tree-shader.d.ts +23 -0
- package/dist/gen-vegetation/tree-shader.js +229 -0
- package/dist/gen-vegetation/trees.d.ts +59 -0
- package/dist/gen-vegetation/trees.js +329 -0
- package/dist/gpu/device.d.ts +286 -0
- package/dist/gpu/device.js +1157 -0
- package/dist/gpu/gl.d.ts +134 -0
- package/dist/gpu/gl.js +276 -0
- package/dist/gpu/index.d.ts +4 -0
- package/dist/gpu/index.js +3 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +13 -0
- package/dist/motion/graph.d.ts +244 -0
- package/dist/motion/graph.js +739 -0
- package/dist/motion/index.d.ts +8 -0
- package/dist/motion/index.js +8 -0
- package/dist/motion/pose-ops.d.ts +30 -0
- package/dist/motion/pose-ops.js +232 -0
- package/dist/motion/reach.d.ts +45 -0
- package/dist/motion/reach.js +264 -0
- package/dist/motion/skeleton.d.ts +49 -0
- package/dist/motion/skeleton.js +99 -0
- package/dist/platform/byte-store.d.ts +21 -0
- package/dist/platform/byte-store.js +137 -0
- package/dist/platform/canvas.d.ts +29 -0
- package/dist/platform/canvas.js +76 -0
- package/dist/platform/context-loss.d.ts +8 -0
- package/dist/platform/context-loss.js +17 -0
- package/dist/platform/free-input.d.ts +51 -0
- package/dist/platform/free-input.js +100 -0
- package/dist/platform/index.d.ts +18 -0
- package/dist/platform/index.js +10 -0
- package/dist/platform/input.d.ts +48 -0
- package/dist/platform/input.js +117 -0
- package/dist/platform/keys.d.ts +12 -0
- package/dist/platform/keys.js +29 -0
- package/dist/platform/loop.d.ts +41 -0
- package/dist/platform/loop.js +84 -0
- package/dist/platform/walk-input.d.ts +74 -0
- package/dist/platform/walk-input.js +226 -0
- package/dist/platform/worker.d.ts +51 -0
- package/dist/platform/worker.js +170 -0
- package/dist/render/bone-instances.d.ts +24 -0
- package/dist/render/bone-instances.js +74 -0
- package/dist/render/cells.d.ts +70 -0
- package/dist/render/cells.js +144 -0
- package/dist/render/glsl.d.ts +66 -0
- package/dist/render/glsl.js +173 -0
- package/dist/render/index.d.ts +26 -0
- package/dist/render/index.js +16 -0
- package/dist/render/instance-sets.d.ts +97 -0
- package/dist/render/instance-sets.js +241 -0
- package/dist/render/near-instances.d.ts +75 -0
- package/dist/render/near-instances.js +219 -0
- package/dist/render/particles.d.ts +63 -0
- package/dist/render/particles.js +215 -0
- package/dist/render/pieces.d.ts +57 -0
- package/dist/render/pieces.js +151 -0
- package/dist/render/point-lights.d.ts +97 -0
- package/dist/render/point-lights.js +243 -0
- package/dist/render/renderer.d.ts +447 -0
- package/dist/render/renderer.js +1100 -0
- package/dist/render/rig.d.ts +74 -0
- package/dist/render/rig.js +576 -0
- package/dist/render/shadow-areas.d.ts +49 -0
- package/dist/render/shadow-areas.js +125 -0
- package/dist/render/shadow-glsl.d.ts +12 -0
- package/dist/render/shadow-glsl.js +146 -0
- package/dist/render/transient.d.ts +61 -0
- package/dist/render/transient.js +213 -0
- package/dist/render/value-glsl.d.ts +17 -0
- package/dist/render/value-glsl.js +208 -0
- package/dist/render/value-layers.d.ts +180 -0
- package/dist/render/value-layers.js +333 -0
- package/dist/runtime/atmosphere.d.ts +37 -0
- package/dist/runtime/atmosphere.js +304 -0
- package/dist/runtime/cloud-shell.d.ts +29 -0
- package/dist/runtime/cloud-shell.js +158 -0
- package/dist/runtime/engine.d.ts +155 -0
- package/dist/runtime/engine.js +305 -0
- package/dist/runtime/environment-link.d.ts +22 -0
- package/dist/runtime/environment-link.js +49 -0
- package/dist/runtime/index.d.ts +13 -0
- package/dist/runtime/index.js +12 -0
- package/dist/runtime/precipitation-glsl.d.ts +2 -0
- package/dist/runtime/precipitation-glsl.js +70 -0
- package/dist/runtime/precipitation.d.ts +11 -0
- package/dist/runtime/precipitation.js +78 -0
- package/dist/runtime/sky.d.ts +30 -0
- package/dist/runtime/sky.js +114 -0
- package/dist/runtime/streaming.d.ts +245 -0
- package/dist/runtime/streaming.js +602 -0
- package/dist/sim/agenda.d.ts +167 -0
- package/dist/sim/agenda.js +425 -0
- package/dist/sim/bytes.d.ts +34 -0
- package/dist/sim/bytes.js +125 -0
- package/dist/sim/destruction/apply.d.ts +59 -0
- package/dist/sim/destruction/apply.js +243 -0
- package/dist/sim/destruction/cell-states.d.ts +54 -0
- package/dist/sim/destruction/cell-states.js +198 -0
- package/dist/sim/destruction/index.d.ts +11 -0
- package/dist/sim/destruction/index.js +10 -0
- package/dist/sim/destruction/int-geometry.d.ts +17 -0
- package/dist/sim/destruction/int-geometry.js +131 -0
- package/dist/sim/destruction/pick.d.ts +13 -0
- package/dist/sim/destruction/pick.js +59 -0
- package/dist/sim/destruction/rubble.d.ts +8 -0
- package/dist/sim/destruction/rubble.js +159 -0
- package/dist/sim/destruction/state.d.ts +15 -0
- package/dist/sim/destruction/state.js +83 -0
- package/dist/sim/destruction/structure.d.ts +30 -0
- package/dist/sim/destruction/structure.js +139 -0
- package/dist/sim/events.d.ts +37 -0
- package/dist/sim/events.js +106 -0
- package/dist/sim/fixed.d.ts +21 -0
- package/dist/sim/fixed.js +64 -0
- package/dist/sim/flow-probe.d.ts +58 -0
- package/dist/sim/flow-probe.js +158 -0
- package/dist/sim/flow.d.ts +257 -0
- package/dist/sim/flow.js +967 -0
- package/dist/sim/index.d.ts +28 -0
- package/dist/sim/index.js +20 -0
- package/dist/sim/interlock.d.ts +47 -0
- package/dist/sim/interlock.js +252 -0
- package/dist/sim/movers.d.ts +228 -0
- package/dist/sim/movers.js +1083 -0
- package/dist/sim/nav.d.ts +72 -0
- package/dist/sim/nav.js +292 -0
- package/dist/sim/paths.d.ts +80 -0
- package/dist/sim/paths.js +197 -0
- package/dist/sim/regions.d.ts +87 -0
- package/dist/sim/regions.js +517 -0
- package/dist/sim/simulation.d.ts +114 -0
- package/dist/sim/simulation.js +291 -0
- package/dist/sim/states.d.ts +23 -0
- package/dist/sim/states.js +54 -0
- package/dist/sim/table.d.ts +49 -0
- package/dist/sim/table.js +126 -0
- package/dist/sim/zones.d.ts +11 -0
- package/dist/sim/zones.js +196 -0
- package/dist/tools/collision-lines.d.ts +12 -0
- package/dist/tools/collision-lines.js +50 -0
- package/dist/tools/data-view-glsl.d.ts +3 -0
- package/dist/tools/data-view-glsl.js +63 -0
- package/dist/tools/data-view-layers.d.ts +20 -0
- package/dist/tools/data-view-layers.js +60 -0
- package/dist/tools/debug-view.d.ts +30 -0
- package/dist/tools/debug-view.js +4 -0
- package/dist/tools/index.d.ts +10 -0
- package/dist/tools/index.js +7 -0
- package/dist/tools/mesh-checks.d.ts +67 -0
- package/dist/tools/mesh-checks.js +188 -0
- package/dist/tools/profile.d.ts +85 -0
- package/dist/tools/profile.js +173 -0
- package/dist/world/cell-cache.d.ts +32 -0
- package/dist/world/cell-cache.js +60 -0
- package/dist/world/cell-format.d.ts +25 -0
- package/dist/world/cell-format.js +232 -0
- package/dist/world/collide.d.ts +192 -0
- package/dist/world/collide.js +822 -0
- package/dist/world/index.d.ts +16 -0
- package/dist/world/index.js +12 -0
- package/dist/world/mesh-solids.d.ts +48 -0
- package/dist/world/mesh-solids.js +169 -0
- package/dist/world/pick.d.ts +34 -0
- package/dist/world/pick.js +177 -0
- package/dist/world/providers.d.ts +66 -0
- package/dist/world/providers.js +83 -0
- package/dist/world/sphere.d.ts +69 -0
- package/dist/world/sphere.js +271 -0
- package/dist/world/streaming.d.ts +288 -0
- package/dist/world/streaming.js +801 -0
- package/dist/world/walker.d.ts +145 -0
- package/dist/world/walker.js +502 -0
- package/docs/architecture.md +111 -0
- package/docs/cameras.md +302 -0
- package/docs/concepts.md +123 -0
- package/docs/destruction.md +127 -0
- package/docs/frames.md +199 -0
- package/docs/generators.md +575 -0
- package/docs/getting-started.md +123 -0
- package/docs/headless.md +50 -0
- package/docs/motion.md +160 -0
- package/docs/planet.md +152 -0
- package/docs/recipes.md +521 -0
- package/docs/reference.md +364 -0
- package/docs/rendering.md +823 -0
- package/docs/rigs.md +203 -0
- package/docs/simulation.md +235 -0
- package/docs/streaming.md +590 -0
- package/docs/system.md +166 -0
- package/docs/tools.md +175 -0
- package/docs/walking.md +262 -0
- package/package.json +131 -3
|
@@ -0,0 +1,822 @@
|
|
|
1
|
+
// Collision world: a base surface given as a height function plus upright solids (prisms over outlines and
|
|
2
|
+
// cylinders), kept in a uniform grid of cells. Queries visit only the cells they touch, allocate nothing and use
|
|
3
|
+
// only + − × ÷ and Math.sqrt on the hot paths, so the same inputs give the same numbers.
|
|
4
|
+
//
|
|
5
|
+
// Scope: a flat world with +y up. A curved world (a sphere, up pointing away from a centre) is not built. Where it
|
|
6
|
+
// would plug in is marked "UP:" below: the vertical span of a solid, the height of the base and the vertical
|
|
7
|
+
// distance in `clearance` would be measured along a local up instead of along y.
|
|
8
|
+
import { detSinCos } from '../base/trig.js';
|
|
9
|
+
import { pointInRings, ringSignedArea } from '../gen-core/geometry.js';
|
|
10
|
+
import { marchSurface, rayPrism } from './pick.js';
|
|
11
|
+
/** A fresh `RayHit` to pass to `raycast` (allocate once, reuse). */
|
|
12
|
+
export function newRayHit() {
|
|
13
|
+
return { t: 0, x: 0, y: 0, z: 0, nx: 0, ny: 0, nz: 0, solid: -1, tag: -1 };
|
|
14
|
+
}
|
|
15
|
+
/** A fresh `SlideResult` to pass to `slide` (allocate once, reuse). */
|
|
16
|
+
export function newSlideResult() {
|
|
17
|
+
return { x: 0, z: 0, hit: false, nx: 0, nz: 0, solid: -1 };
|
|
18
|
+
}
|
|
19
|
+
const KIND_FREE = 0;
|
|
20
|
+
const KIND_PRISM = 1;
|
|
21
|
+
const KIND_CYLINDER = 2;
|
|
22
|
+
const F_STANDABLE = 1;
|
|
23
|
+
const F_SIGHT = 2;
|
|
24
|
+
/** Cell indices are offset into a positive range and packed into one safe integer. */
|
|
25
|
+
const GRID_OFFSET = 1 << 20;
|
|
26
|
+
const GRID_SPAN = 1 << 21;
|
|
27
|
+
/** A slide moves at most half the radius per sub-step (so no side thinner than that is skipped), at least this far. */
|
|
28
|
+
const MIN_SUBSTEP = 0.02;
|
|
29
|
+
const MAX_SUBSTEPS = 4096;
|
|
30
|
+
/** Push-out passes per sub-step; a sub-step that still overlaps after them is undone (the circle stops). */
|
|
31
|
+
const RESOLVE_PASSES = 8;
|
|
32
|
+
/** Times an unresolvable sub-step is halved before the circle stops where it was. */
|
|
33
|
+
const SUBSTEP_HALVINGS = 5;
|
|
34
|
+
/** Passes for a circle that starts inside solids. */
|
|
35
|
+
const START_PASSES = 32;
|
|
36
|
+
/** Overlap below this depth, metres, is not a contact. */
|
|
37
|
+
const CONTACT_EPS = 1e-7;
|
|
38
|
+
/** Overlap still accepted after the passes (rounding), metres. */
|
|
39
|
+
const RESOLVE_TOLERANCE = 1e-5;
|
|
40
|
+
/** Half the spacing of the finite differences for the normal of the base surface, metres. */
|
|
41
|
+
const BASE_NORMAL_EPS = 0.05;
|
|
42
|
+
/** Longest ray parameter `raycast` searches. */
|
|
43
|
+
const MAX_RAY_T = 1e5;
|
|
44
|
+
function cellKeyOf(i, j) {
|
|
45
|
+
return (i + GRID_OFFSET) * GRID_SPAN + (j + GRID_OFFSET);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Upright solids over a base surface, for walking and for the camera: ground and ceiling under and over a point,
|
|
49
|
+
* a circle sliding along the sides, a ray, and how far a sphere can travel. Ids are never reused: removing a solid
|
|
50
|
+
* leaves every other id as it was.
|
|
51
|
+
*/
|
|
52
|
+
export class CollisionWorld {
|
|
53
|
+
cellSize;
|
|
54
|
+
heightAt;
|
|
55
|
+
baseMinY;
|
|
56
|
+
baseMaxY;
|
|
57
|
+
baseStep;
|
|
58
|
+
extent;
|
|
59
|
+
/** Rectangle around every solid ever added (it only grows), which ends the walk of a ray through the cells. */
|
|
60
|
+
sx0 = Infinity;
|
|
61
|
+
sx1 = -Infinity;
|
|
62
|
+
sz0 = Infinity;
|
|
63
|
+
sz1 = -Infinity;
|
|
64
|
+
// One entry per id (struct of arrays).
|
|
65
|
+
kind = [];
|
|
66
|
+
flag = [];
|
|
67
|
+
tagOf = [];
|
|
68
|
+
lowY = [];
|
|
69
|
+
highY = [];
|
|
70
|
+
x0 = [];
|
|
71
|
+
x1 = [];
|
|
72
|
+
z0 = [];
|
|
73
|
+
z1 = [];
|
|
74
|
+
/** Prism: one ring in an array (the shape `pointInRings` and `rayPrism` take); null otherwise. */
|
|
75
|
+
rings = [];
|
|
76
|
+
/** Prism: +1 when the outline runs counter-clockwise (positive `ringSignedArea`), −1 otherwise. */
|
|
77
|
+
orient = [];
|
|
78
|
+
cx = [];
|
|
79
|
+
cz = [];
|
|
80
|
+
cr = [];
|
|
81
|
+
/** Keys of the cells a solid is listed in, for removal. */
|
|
82
|
+
cellsOf = [];
|
|
83
|
+
cells = new Map();
|
|
84
|
+
live = 0;
|
|
85
|
+
// Query scratch: visit marks, the candidate list, the direction of the last `nearest`, the moving centre.
|
|
86
|
+
mark = new Uint32Array(64);
|
|
87
|
+
stamp = 0;
|
|
88
|
+
cand = new Int32Array(64);
|
|
89
|
+
dirX = 0;
|
|
90
|
+
dirZ = 0;
|
|
91
|
+
px = 0;
|
|
92
|
+
pz = 0;
|
|
93
|
+
normal = new Float64Array(3);
|
|
94
|
+
rayScratch = newRayHit();
|
|
95
|
+
constructor(options) {
|
|
96
|
+
this.heightAt = options.heightAt;
|
|
97
|
+
this.cellSize = options.cellSize ?? 8;
|
|
98
|
+
if (!(this.cellSize > 0))
|
|
99
|
+
throw new Error('CollisionWorld: cellSize must be positive');
|
|
100
|
+
this.baseMinY = options.baseMinY ?? -Infinity;
|
|
101
|
+
this.baseMaxY = options.baseMaxY ?? Infinity;
|
|
102
|
+
this.baseStep = options.baseStep ?? 0.5;
|
|
103
|
+
if (!(this.baseStep > 0))
|
|
104
|
+
throw new Error('CollisionWorld: baseStep must be positive');
|
|
105
|
+
this.extent = options.baseExtent ?? null;
|
|
106
|
+
}
|
|
107
|
+
/** Number of solids not removed. */
|
|
108
|
+
get solidCount() {
|
|
109
|
+
return this.live;
|
|
110
|
+
}
|
|
111
|
+
/** Vertical prism over a closed outline (x, z pairs), from minY to maxY. Returns the solid's id. */
|
|
112
|
+
addPrism(outline, minY, maxY, options) {
|
|
113
|
+
let n = Math.floor(outline.length / 2);
|
|
114
|
+
if (outline.length % 2 !== 0)
|
|
115
|
+
throw new Error('CollisionWorld.addPrism: the outline needs x, z pairs');
|
|
116
|
+
// A repeated closing point is dropped.
|
|
117
|
+
if (n > 3 && outline[0] === outline[(n - 1) * 2] && outline[1] === outline[(n - 1) * 2 + 1])
|
|
118
|
+
n--;
|
|
119
|
+
if (n < 3)
|
|
120
|
+
throw new Error('CollisionWorld.addPrism: the outline needs at least 3 points');
|
|
121
|
+
const ring = new Float32Array(n * 2);
|
|
122
|
+
let a = Infinity;
|
|
123
|
+
let b = -Infinity;
|
|
124
|
+
let c = Infinity;
|
|
125
|
+
let d = -Infinity;
|
|
126
|
+
for (let i = 0; i < n; i++) {
|
|
127
|
+
const x = outline[i * 2];
|
|
128
|
+
const z = outline[i * 2 + 1];
|
|
129
|
+
ring[i * 2] = x;
|
|
130
|
+
ring[i * 2 + 1] = z;
|
|
131
|
+
}
|
|
132
|
+
for (let i = 0; i < n * 2; i += 2) {
|
|
133
|
+
a = Math.min(a, ring[i]);
|
|
134
|
+
b = Math.max(b, ring[i]);
|
|
135
|
+
c = Math.min(c, ring[i + 1]);
|
|
136
|
+
d = Math.max(d, ring[i + 1]);
|
|
137
|
+
}
|
|
138
|
+
const area = ringSignedArea(ring);
|
|
139
|
+
if (area === 0)
|
|
140
|
+
throw new Error('CollisionWorld.addPrism: the outline has no area');
|
|
141
|
+
const id = this.push(KIND_PRISM, minY, maxY, a, b, c, d, options);
|
|
142
|
+
this.rings[id] = [ring];
|
|
143
|
+
this.orient[id] = area > 0 ? 1 : -1;
|
|
144
|
+
this.register(id);
|
|
145
|
+
return id;
|
|
146
|
+
}
|
|
147
|
+
/** Upright cylinder with its axis at (x, z). Returns the solid's id. */
|
|
148
|
+
addCylinder(x, z, radius, minY, maxY, options) {
|
|
149
|
+
if (!(radius > 0))
|
|
150
|
+
throw new Error('CollisionWorld.addCylinder: radius must be positive');
|
|
151
|
+
const id = this.push(KIND_CYLINDER, minY, maxY, x - radius, x + radius, z - radius, z + radius, options);
|
|
152
|
+
this.cx[id] = x;
|
|
153
|
+
this.cz[id] = z;
|
|
154
|
+
this.cr[id] = radius;
|
|
155
|
+
this.register(id);
|
|
156
|
+
return id;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Oriented box; a convenience over addPrism. `yawRad` turns it about +y the way a view's yaw does: the local +x
|
|
160
|
+
* axis points to (cos yaw, −sin yaw) in x, z.
|
|
161
|
+
*/
|
|
162
|
+
addBox(x, z, halfX, halfZ, yawRad, minY, maxY, options) {
|
|
163
|
+
const sc = detSinCos(yawRad);
|
|
164
|
+
const s = sc[0];
|
|
165
|
+
const c = sc[1];
|
|
166
|
+
// Local corners (±halfX, ±halfZ), counter-clockwise in x, z.
|
|
167
|
+
const ax = halfX * c;
|
|
168
|
+
const az = -halfX * s;
|
|
169
|
+
const bx = halfZ * s;
|
|
170
|
+
const bz = halfZ * c;
|
|
171
|
+
return this.addPrism([x - ax - bx, z - az - bz, x + ax - bx, z + az - bz, x + ax + bx, z + az + bz, x - ax + bx, z - az + bz], minY, maxY, options);
|
|
172
|
+
}
|
|
173
|
+
/** Removes a solid. Other ids stay valid; a removed or unknown id is ignored. */
|
|
174
|
+
remove(id) {
|
|
175
|
+
if (!(id >= 0 && id < this.kind.length) || this.kind[id] === KIND_FREE)
|
|
176
|
+
return;
|
|
177
|
+
const keys = this.cellsOf[id];
|
|
178
|
+
for (const key of keys) {
|
|
179
|
+
const list = this.cells.get(key);
|
|
180
|
+
if (!list)
|
|
181
|
+
continue;
|
|
182
|
+
const at = list.indexOf(id);
|
|
183
|
+
if (at >= 0)
|
|
184
|
+
list.splice(at, 1);
|
|
185
|
+
if (list.length === 0)
|
|
186
|
+
this.cells.delete(key);
|
|
187
|
+
}
|
|
188
|
+
this.kind[id] = KIND_FREE;
|
|
189
|
+
this.rings[id] = null;
|
|
190
|
+
this.cellsOf[id] = null;
|
|
191
|
+
this.live--;
|
|
192
|
+
}
|
|
193
|
+
/** Height of the base surface at a point (the `heightAt` the world was made with). */
|
|
194
|
+
baseHeightAt(x, z) {
|
|
195
|
+
// UP: the height would be measured along the local up.
|
|
196
|
+
return this.heightAt(x, z);
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Slope of the base surface at a point: writes the gradient (rise per metre along x and along z) into `out[0]`,
|
|
200
|
+
* `out[1]` and returns its length (the tangent of the slope angle). Central differences 0.05 m apart.
|
|
201
|
+
*/
|
|
202
|
+
baseSlopeAt(x, z, out) {
|
|
203
|
+
const e = BASE_NORMAL_EPS;
|
|
204
|
+
const gx = (this.heightAt(x + e, z) - this.heightAt(x - e, z)) / (2 * e);
|
|
205
|
+
const gz = (this.heightAt(x, z + e) - this.heightAt(x, z - e)) / (2 * e);
|
|
206
|
+
out[0] = gx;
|
|
207
|
+
out[1] = gz;
|
|
208
|
+
return Math.sqrt(gx * gx + gz * gz);
|
|
209
|
+
}
|
|
210
|
+
/** Whether `id` is a solid that has not been removed. */
|
|
211
|
+
has(id) {
|
|
212
|
+
return id >= 0 && id < this.kind.length && this.kind[id] !== KIND_FREE;
|
|
213
|
+
}
|
|
214
|
+
/** Calls `fn` for every solid not removed, in id order (debug drawing; allocates). */
|
|
215
|
+
forEachSolid(fn) {
|
|
216
|
+
for (let id = 0; id < this.kind.length; id++) {
|
|
217
|
+
const k = this.kind[id];
|
|
218
|
+
if (k === KIND_FREE)
|
|
219
|
+
continue;
|
|
220
|
+
const f = this.flag[id];
|
|
221
|
+
const prism = k === KIND_PRISM;
|
|
222
|
+
fn(id, {
|
|
223
|
+
kind: prism ? 'prism' : 'cylinder',
|
|
224
|
+
outline: prism ? this.rings[id][0] : null,
|
|
225
|
+
x: prism ? 0 : this.cx[id],
|
|
226
|
+
z: prism ? 0 : this.cz[id],
|
|
227
|
+
radius: prism ? 0 : this.cr[id],
|
|
228
|
+
minY: this.lowY[id],
|
|
229
|
+
maxY: this.highY[id],
|
|
230
|
+
standable: (f & F_STANDABLE) !== 0,
|
|
231
|
+
blocksSight: (f & F_SIGHT) !== 0,
|
|
232
|
+
tag: this.tagOf[id],
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Highest surface (base or a standable top) that is not above `y + maxStepUp`. The base counts always, even above
|
|
238
|
+
* that limit. With `radius` a top counts when it reaches within `radius` of the point (a disc stands on an edge).
|
|
239
|
+
*/
|
|
240
|
+
groundAt(x, z, y, maxStepUp, radius = 0) {
|
|
241
|
+
// UP: the base height and the tops would be measured along the local up.
|
|
242
|
+
let best = this.heightAt(x, z);
|
|
243
|
+
const limit = y + maxStepUp;
|
|
244
|
+
const n = this.gather(x - radius, z - radius, x + radius, z + radius);
|
|
245
|
+
for (let k = 0; k < n; k++) {
|
|
246
|
+
const id = this.cand[k];
|
|
247
|
+
if ((this.flag[id] & F_STANDABLE) === 0)
|
|
248
|
+
continue;
|
|
249
|
+
const top = this.highY[id];
|
|
250
|
+
if (top > limit || top <= best)
|
|
251
|
+
continue;
|
|
252
|
+
if (this.nearest(id, x, z) <= radius)
|
|
253
|
+
best = top;
|
|
254
|
+
}
|
|
255
|
+
return best;
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Lowest underside at or above `y` at the point (for the head when jumping); Infinity when open. Every solid counts,
|
|
259
|
+
* whatever its options. With `radius` an underside counts when the solid reaches within `radius` of the point.
|
|
260
|
+
*/
|
|
261
|
+
ceilingAt(x, z, y, radius = 0) {
|
|
262
|
+
let best = Infinity;
|
|
263
|
+
const n = this.gather(x - radius, z - radius, x + radius, z + radius);
|
|
264
|
+
for (let k = 0; k < n; k++) {
|
|
265
|
+
const id = this.cand[k];
|
|
266
|
+
const low = this.lowY[id];
|
|
267
|
+
if (low < y || low >= best)
|
|
268
|
+
continue;
|
|
269
|
+
if (this.nearest(id, x, z) <= radius)
|
|
270
|
+
best = low;
|
|
271
|
+
}
|
|
272
|
+
return best;
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Moves a circle of `radius` by (dx, dz) against the sides of solids that overlap the height span, sliding along them.
|
|
276
|
+
* A solid takes part when its span overlaps (minY, maxY) (touching does not count), whatever its options. The move is
|
|
277
|
+
* split into sub-steps of at most half the radius, so no side is skipped; a circle that starts inside solids is first
|
|
278
|
+
* pushed out the shortest way. In a corner the circle stops: a sub-step that cannot be resolved is undone.
|
|
279
|
+
*/
|
|
280
|
+
slide(x, z, radius, minY, maxY, dx, dz, out) {
|
|
281
|
+
out.hit = false;
|
|
282
|
+
out.nx = 0;
|
|
283
|
+
out.nz = 0;
|
|
284
|
+
out.solid = -1;
|
|
285
|
+
this.px = x;
|
|
286
|
+
this.pz = z;
|
|
287
|
+
// Out of whatever the circle starts in.
|
|
288
|
+
let n = this.gather(x - radius, z - radius, x + radius, z + radius);
|
|
289
|
+
if (n > 0)
|
|
290
|
+
this.resolve(n, radius, minY, maxY, START_PASSES, out);
|
|
291
|
+
const len = Math.sqrt(dx * dx + dz * dz);
|
|
292
|
+
if (len > 0) {
|
|
293
|
+
const sx0 = this.px;
|
|
294
|
+
const sz0 = this.pz;
|
|
295
|
+
// Pushes along oblique sides can carry the circle off the straight path, hence the margin of two radii.
|
|
296
|
+
const m = radius * 2;
|
|
297
|
+
n = this.gather(Math.min(sx0, sx0 + dx) - m, Math.min(sz0, sz0 + dz) - m, Math.max(sx0, sx0 + dx) + m, Math.max(sz0, sz0 + dz) + m);
|
|
298
|
+
let steps = Math.ceil(len / Math.max(radius * 0.5, MIN_SUBSTEP));
|
|
299
|
+
if (steps > MAX_SUBSTEPS)
|
|
300
|
+
steps = MAX_SUBSTEPS;
|
|
301
|
+
const sx = dx / steps;
|
|
302
|
+
const sz = dz / steps;
|
|
303
|
+
for (let s = 0; s < steps; s++) {
|
|
304
|
+
const bx = this.px;
|
|
305
|
+
const bz = this.pz;
|
|
306
|
+
// A sub-step that cannot be resolved (a narrowing corner) is undone and tried at half the length, a few times.
|
|
307
|
+
let frac = 1;
|
|
308
|
+
let done = false;
|
|
309
|
+
for (let tries = 0; tries <= SUBSTEP_HALVINGS; tries++, frac *= 0.5) {
|
|
310
|
+
this.px = bx + sx * frac;
|
|
311
|
+
this.pz = bz + sz * frac;
|
|
312
|
+
if (n === 0 || this.resolve(n, radius, minY, maxY, RESOLVE_PASSES, out)) {
|
|
313
|
+
done = true;
|
|
314
|
+
break;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
if (!done || frac < 1) {
|
|
318
|
+
if (!done) {
|
|
319
|
+
this.px = bx;
|
|
320
|
+
this.pz = bz;
|
|
321
|
+
}
|
|
322
|
+
break;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
out.x = this.px;
|
|
327
|
+
out.z = this.pz;
|
|
328
|
+
return out;
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* First hit of a ray with the base surface and the solids, for t in [0, maxT]. The direction need not be unit; `t` is
|
|
332
|
+
* measured in its lengths. Solids with `blocksSight: false` are not hit. Writes `out` and returns true on a hit.
|
|
333
|
+
*/
|
|
334
|
+
raycast(ox, oy, oz, dx, dy, dz, maxT, out) {
|
|
335
|
+
// An endless ray is cut at MAX_RAY_T so the walk through the cells ends.
|
|
336
|
+
let best = maxT < MAX_RAY_T ? maxT : MAX_RAY_T;
|
|
337
|
+
let hit = -2; // −2 none, −1 base, ≥ 0 a solid
|
|
338
|
+
let nx = 0;
|
|
339
|
+
let ny = 0;
|
|
340
|
+
let nz = 0;
|
|
341
|
+
// The base surface.
|
|
342
|
+
const tb = this.rayBase(ox, oy, oz, dx, dy, dz, best);
|
|
343
|
+
if (tb < Infinity && tb <= best) {
|
|
344
|
+
best = tb;
|
|
345
|
+
hit = -1;
|
|
346
|
+
const x = ox + dx * tb;
|
|
347
|
+
const z = oz + dz * tb;
|
|
348
|
+
const e = BASE_NORMAL_EPS;
|
|
349
|
+
const gx = (this.heightAt(x + e, z) - this.heightAt(x - e, z)) / (2 * e);
|
|
350
|
+
const gz = (this.heightAt(x, z + e) - this.heightAt(x, z - e)) / (2 * e);
|
|
351
|
+
const len = Math.sqrt(gx * gx + 1 + gz * gz);
|
|
352
|
+
nx = -gx / len;
|
|
353
|
+
ny = 1 / len;
|
|
354
|
+
nz = -gz / len;
|
|
355
|
+
}
|
|
356
|
+
// The solids, cell by cell along the ray's projection, until no later cell can hold a nearer hit.
|
|
357
|
+
this.nextStamp();
|
|
358
|
+
const s = this.cellSize;
|
|
359
|
+
let i = Math.floor(ox / s);
|
|
360
|
+
let j = Math.floor(oz / s);
|
|
361
|
+
const stepI = dx > 0 ? 1 : -1;
|
|
362
|
+
const stepJ = dz > 0 ? 1 : -1;
|
|
363
|
+
const tDeltaX = dx !== 0 ? s / Math.abs(dx) : Infinity;
|
|
364
|
+
const tDeltaZ = dz !== 0 ? s / Math.abs(dz) : Infinity;
|
|
365
|
+
let tNextX = dx !== 0 ? ((dx > 0 ? (i + 1) * s : i * s) - ox) / dx : Infinity;
|
|
366
|
+
let tNextZ = dz !== 0 ? ((dz > 0 ? (j + 1) * s : j * s) - oz) / dz : Infinity;
|
|
367
|
+
const nrm = this.normal;
|
|
368
|
+
// Past the rectangle of all solids no cell holds one.
|
|
369
|
+
const tSolids = slabExit(ox, oz, dx, dz, this.sx0, this.sz0, this.sx1, this.sz1);
|
|
370
|
+
for (;;) {
|
|
371
|
+
const list = this.cells.get(cellKeyOf(i, j));
|
|
372
|
+
if (list) {
|
|
373
|
+
for (let k = 0; k < list.length; k++) {
|
|
374
|
+
const id = list[k];
|
|
375
|
+
if (this.mark[id] === this.stamp)
|
|
376
|
+
continue;
|
|
377
|
+
this.mark[id] = this.stamp;
|
|
378
|
+
if ((this.flag[id] & F_SIGHT) === 0)
|
|
379
|
+
continue;
|
|
380
|
+
const t = this.kind[id] === KIND_PRISM
|
|
381
|
+
? rayPrism(ox, oy, oz, dx, dy, dz, this.rings[id], this.lowY[id], this.highY[id], this.x0[id], this.x1[id], this.z0[id], this.z1[id], 0, best, true, nrm)
|
|
382
|
+
: this.rayCylinder(id, ox, oy, oz, dx, dy, dz, best);
|
|
383
|
+
if (t < best) {
|
|
384
|
+
best = t;
|
|
385
|
+
hit = id;
|
|
386
|
+
nx = nrm[0];
|
|
387
|
+
ny = nrm[1];
|
|
388
|
+
nz = nrm[2];
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
const tExit = tNextX < tNextZ ? tNextX : tNextZ;
|
|
393
|
+
if (tExit >= best || tExit === Infinity || tExit > tSolids)
|
|
394
|
+
break;
|
|
395
|
+
if (tNextX < tNextZ) {
|
|
396
|
+
i += stepI;
|
|
397
|
+
tNextX += tDeltaX;
|
|
398
|
+
}
|
|
399
|
+
else {
|
|
400
|
+
j += stepJ;
|
|
401
|
+
tNextZ += tDeltaZ;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
if (hit === -2)
|
|
405
|
+
return false;
|
|
406
|
+
out.t = best;
|
|
407
|
+
out.x = ox + dx * best;
|
|
408
|
+
out.y = oy + dy * best;
|
|
409
|
+
out.z = oz + dz * best;
|
|
410
|
+
out.nx = nx;
|
|
411
|
+
out.ny = ny;
|
|
412
|
+
out.nz = nz;
|
|
413
|
+
out.solid = hit;
|
|
414
|
+
out.tag = hit >= 0 ? this.tagOf[hit] : -1;
|
|
415
|
+
return true;
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* How far a sphere of `radius` can travel from the origin along the direction, at most maxDist (for a camera boom).
|
|
419
|
+
* Solids with `blocksSight: false` do not stop it. 0 when the sphere already overlaps something at the origin. The
|
|
420
|
+
* direction need not be unit; distances are in metres. The sphere is tested every `radius` metres along the way and
|
|
421
|
+
* the contact refined by bisection; the centre line is first cast as a ray, so nothing thin is passed through.
|
|
422
|
+
*/
|
|
423
|
+
clearance(ox, oy, oz, dx, dy, dz, maxDist, radius) {
|
|
424
|
+
const len = Math.sqrt(dx * dx + dy * dy + dz * dz);
|
|
425
|
+
if (!(len > 0) || !(maxDist > 0))
|
|
426
|
+
return 0;
|
|
427
|
+
const ux = dx / len;
|
|
428
|
+
const uy = dy / len;
|
|
429
|
+
const uz = dz / len;
|
|
430
|
+
let limit = maxDist;
|
|
431
|
+
if (this.raycast(ox, oy, oz, ux, uy, uz, maxDist, this.rayScratch))
|
|
432
|
+
limit = this.rayScratch.t;
|
|
433
|
+
if (!(radius > 0))
|
|
434
|
+
return limit;
|
|
435
|
+
if (!this.sphereFree(ox, oy, oz, radius))
|
|
436
|
+
return 0;
|
|
437
|
+
let free = 0;
|
|
438
|
+
for (let s = radius;; s += radius) {
|
|
439
|
+
const at = s < limit ? s : limit;
|
|
440
|
+
if (!this.sphereFree(ox + ux * at, oy + uy * at, oz + uz * at, radius)) {
|
|
441
|
+
let lo = free;
|
|
442
|
+
let hi = at;
|
|
443
|
+
for (let k = 0; k < 12; k++) {
|
|
444
|
+
const mid = (lo + hi) / 2;
|
|
445
|
+
if (this.sphereFree(ox + ux * mid, oy + uy * mid, oz + uz * mid, radius))
|
|
446
|
+
lo = mid;
|
|
447
|
+
else
|
|
448
|
+
hi = mid;
|
|
449
|
+
}
|
|
450
|
+
return lo;
|
|
451
|
+
}
|
|
452
|
+
free = at;
|
|
453
|
+
if (at >= limit)
|
|
454
|
+
return limit;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
// --- internals ------------------------------------------------------------------------------------------------------
|
|
458
|
+
push(kind, minY, maxY, a, b, c, d, options) {
|
|
459
|
+
if (!(maxY > minY))
|
|
460
|
+
throw new Error('CollisionWorld: maxY must be above minY');
|
|
461
|
+
const id = this.kind.length;
|
|
462
|
+
const s = this.cellSize;
|
|
463
|
+
if (Math.floor(a / s) < -GRID_OFFSET || Math.floor(c / s) < -GRID_OFFSET || Math.floor(b / s) >= GRID_OFFSET || Math.floor(d / s) >= GRID_OFFSET) {
|
|
464
|
+
throw new Error('CollisionWorld: solid outside the grid range');
|
|
465
|
+
}
|
|
466
|
+
if (a < this.sx0)
|
|
467
|
+
this.sx0 = a;
|
|
468
|
+
if (b > this.sx1)
|
|
469
|
+
this.sx1 = b;
|
|
470
|
+
if (c < this.sz0)
|
|
471
|
+
this.sz0 = c;
|
|
472
|
+
if (d > this.sz1)
|
|
473
|
+
this.sz1 = d;
|
|
474
|
+
this.kind.push(kind);
|
|
475
|
+
this.flag.push(((options?.standable ?? true) ? F_STANDABLE : 0) | ((options?.blocksSight ?? true) ? F_SIGHT : 0));
|
|
476
|
+
this.tagOf.push(options?.tag ?? 0);
|
|
477
|
+
this.lowY.push(minY);
|
|
478
|
+
this.highY.push(maxY);
|
|
479
|
+
this.x0.push(a);
|
|
480
|
+
this.x1.push(b);
|
|
481
|
+
this.z0.push(c);
|
|
482
|
+
this.z1.push(d);
|
|
483
|
+
this.rings.push(null);
|
|
484
|
+
this.orient.push(0);
|
|
485
|
+
this.cx.push(0);
|
|
486
|
+
this.cz.push(0);
|
|
487
|
+
this.cr.push(0);
|
|
488
|
+
this.cellsOf.push(null);
|
|
489
|
+
if (id >= this.mark.length) {
|
|
490
|
+
const grown = new Uint32Array(this.mark.length * 2);
|
|
491
|
+
grown.set(this.mark);
|
|
492
|
+
this.mark = grown;
|
|
493
|
+
this.cand = new Int32Array(this.mark.length);
|
|
494
|
+
}
|
|
495
|
+
this.live++;
|
|
496
|
+
return id;
|
|
497
|
+
}
|
|
498
|
+
/**
|
|
499
|
+
* Lists a solid in the cells of its rectangle that its shape reaches: a cell counts when the distance from its
|
|
500
|
+
* centre to the shape is at most half its diagonal (a long turned solid is not listed in every cell of its rectangle).
|
|
501
|
+
*/
|
|
502
|
+
register(id) {
|
|
503
|
+
const s = this.cellSize;
|
|
504
|
+
const reach = s * Math.SQRT1_2;
|
|
505
|
+
const i0 = Math.floor(this.x0[id] / s);
|
|
506
|
+
const i1 = Math.floor(this.x1[id] / s);
|
|
507
|
+
const j0 = Math.floor(this.z0[id] / s);
|
|
508
|
+
const j1 = Math.floor(this.z1[id] / s);
|
|
509
|
+
const whole = i1 - i0 <= 1 && j1 - j0 <= 1;
|
|
510
|
+
const keys = [];
|
|
511
|
+
for (let i = i0; i <= i1; i++) {
|
|
512
|
+
for (let j = j0; j <= j1; j++) {
|
|
513
|
+
if (!whole && this.nearest(id, (i + 0.5) * s, (j + 0.5) * s) > reach)
|
|
514
|
+
continue;
|
|
515
|
+
const key = cellKeyOf(i, j);
|
|
516
|
+
let list = this.cells.get(key);
|
|
517
|
+
if (!list) {
|
|
518
|
+
list = [];
|
|
519
|
+
this.cells.set(key, list);
|
|
520
|
+
}
|
|
521
|
+
list.push(id);
|
|
522
|
+
keys.push(key);
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
this.cellsOf[id] = keys;
|
|
526
|
+
}
|
|
527
|
+
nextStamp() {
|
|
528
|
+
this.stamp++;
|
|
529
|
+
if (this.stamp === 0xffffffff) {
|
|
530
|
+
this.mark.fill(0);
|
|
531
|
+
this.stamp = 1;
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
/** Lists every live solid of the cells over a rectangle once, in `cand`, and returns their count. */
|
|
535
|
+
gather(xa, za, xb, zb) {
|
|
536
|
+
this.nextStamp();
|
|
537
|
+
const s = this.cellSize;
|
|
538
|
+
const i0 = Math.floor(xa / s);
|
|
539
|
+
const i1 = Math.floor(xb / s);
|
|
540
|
+
const j0 = Math.floor(za / s);
|
|
541
|
+
const j1 = Math.floor(zb / s);
|
|
542
|
+
let n = 0;
|
|
543
|
+
for (let i = i0; i <= i1; i++) {
|
|
544
|
+
for (let j = j0; j <= j1; j++) {
|
|
545
|
+
const list = this.cells.get(cellKeyOf(i, j));
|
|
546
|
+
if (!list)
|
|
547
|
+
continue;
|
|
548
|
+
for (let k = 0; k < list.length; k++) {
|
|
549
|
+
const id = list[k];
|
|
550
|
+
if (this.mark[id] === this.stamp)
|
|
551
|
+
continue;
|
|
552
|
+
this.mark[id] = this.stamp;
|
|
553
|
+
this.cand[n++] = id;
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
return n;
|
|
558
|
+
}
|
|
559
|
+
/**
|
|
560
|
+
* Signed horizontal distance from (x, z) to the side of a solid: negative inside. Leaves in `dirX`, `dirZ` the unit
|
|
561
|
+
* direction that leads out (away from the side outside, toward it inside).
|
|
562
|
+
*/
|
|
563
|
+
nearest(id, x, z) {
|
|
564
|
+
if (this.kind[id] === KIND_CYLINDER) {
|
|
565
|
+
const ex = x - this.cx[id];
|
|
566
|
+
const ez = z - this.cz[id];
|
|
567
|
+
const d = Math.sqrt(ex * ex + ez * ez);
|
|
568
|
+
if (d > 0) {
|
|
569
|
+
this.dirX = ex / d;
|
|
570
|
+
this.dirZ = ez / d;
|
|
571
|
+
}
|
|
572
|
+
else {
|
|
573
|
+
this.dirX = 1;
|
|
574
|
+
this.dirZ = 0;
|
|
575
|
+
}
|
|
576
|
+
return d - this.cr[id];
|
|
577
|
+
}
|
|
578
|
+
const rings = this.rings[id];
|
|
579
|
+
const ring = rings[0];
|
|
580
|
+
const n = ring.length / 2;
|
|
581
|
+
let best = Infinity;
|
|
582
|
+
let bx = 0;
|
|
583
|
+
let bz = 0;
|
|
584
|
+
let enx = 0;
|
|
585
|
+
let enz = 0;
|
|
586
|
+
for (let i = 0, j = n - 1; i < n; j = i++) {
|
|
587
|
+
const ax = ring[j * 2];
|
|
588
|
+
const az = ring[j * 2 + 1];
|
|
589
|
+
const ex = ring[i * 2] - ax;
|
|
590
|
+
const ez = ring[i * 2 + 1] - az;
|
|
591
|
+
const l2 = ex * ex + ez * ez;
|
|
592
|
+
let t = l2 > 0 ? ((x - ax) * ex + (z - az) * ez) / l2 : 0;
|
|
593
|
+
t = t < 0 ? 0 : t > 1 ? 1 : t;
|
|
594
|
+
const qx = ax + ex * t;
|
|
595
|
+
const qz = az + ez * t;
|
|
596
|
+
const d2 = (x - qx) * (x - qx) + (z - qz) * (z - qz);
|
|
597
|
+
if (d2 < best) {
|
|
598
|
+
best = d2;
|
|
599
|
+
bx = qx;
|
|
600
|
+
bz = qz;
|
|
601
|
+
enx = ez;
|
|
602
|
+
enz = -ex;
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
const inside = pointInRings(x, z, rings);
|
|
606
|
+
const d = Math.sqrt(best);
|
|
607
|
+
if (d > 1e-12) {
|
|
608
|
+
const s = inside ? -1 / d : 1 / d;
|
|
609
|
+
this.dirX = (x - bx) * s;
|
|
610
|
+
this.dirZ = (z - bz) * s;
|
|
611
|
+
}
|
|
612
|
+
else {
|
|
613
|
+
// On the side itself: the outward normal of that edge.
|
|
614
|
+
const o = this.orient[id];
|
|
615
|
+
const l = Math.sqrt(enx * enx + enz * enz);
|
|
616
|
+
this.dirX = (enx * o) / l;
|
|
617
|
+
this.dirZ = (enz * o) / l;
|
|
618
|
+
}
|
|
619
|
+
return inside ? -d : d;
|
|
620
|
+
}
|
|
621
|
+
/** Pushes the centre (`px`, `pz`) out of the candidates; false when overlap remains after `passes`. */
|
|
622
|
+
resolve(n, radius, minY, maxY, passes, out) {
|
|
623
|
+
for (let pass = 0; pass < passes; pass++) {
|
|
624
|
+
let moved = false;
|
|
625
|
+
for (let k = 0; k < n; k++) {
|
|
626
|
+
const id = this.cand[k];
|
|
627
|
+
// UP: the vertical span would be measured along the local up.
|
|
628
|
+
if (this.kind[id] === KIND_FREE || this.highY[id] <= minY || this.lowY[id] >= maxY)
|
|
629
|
+
continue;
|
|
630
|
+
const d = this.nearest(id, this.px, this.pz);
|
|
631
|
+
if (d >= radius - CONTACT_EPS)
|
|
632
|
+
continue;
|
|
633
|
+
const push = radius - d;
|
|
634
|
+
this.px += this.dirX * push;
|
|
635
|
+
this.pz += this.dirZ * push;
|
|
636
|
+
out.hit = true;
|
|
637
|
+
out.nx = this.dirX;
|
|
638
|
+
out.nz = this.dirZ;
|
|
639
|
+
out.solid = id;
|
|
640
|
+
moved = true;
|
|
641
|
+
}
|
|
642
|
+
if (!moved)
|
|
643
|
+
return true;
|
|
644
|
+
}
|
|
645
|
+
for (let k = 0; k < n; k++) {
|
|
646
|
+
const id = this.cand[k];
|
|
647
|
+
if (this.kind[id] === KIND_FREE || this.highY[id] <= minY || this.lowY[id] >= maxY)
|
|
648
|
+
continue;
|
|
649
|
+
if (this.nearest(id, this.px, this.pz) < radius - RESOLVE_TOLERANCE)
|
|
650
|
+
return false;
|
|
651
|
+
}
|
|
652
|
+
return true;
|
|
653
|
+
}
|
|
654
|
+
/** Whether a sphere at a point stays clear of the base and of the solids that block sight. */
|
|
655
|
+
sphereFree(x, y, z, r) {
|
|
656
|
+
// UP: the height above the base would be measured along the local up.
|
|
657
|
+
if (y - this.heightAt(x, z) < r)
|
|
658
|
+
return false;
|
|
659
|
+
const n = this.gather(x - r, z - r, x + r, z + r);
|
|
660
|
+
const r2 = r * r;
|
|
661
|
+
for (let k = 0; k < n; k++) {
|
|
662
|
+
const id = this.cand[k];
|
|
663
|
+
if ((this.flag[id] & F_SIGHT) === 0)
|
|
664
|
+
continue;
|
|
665
|
+
const low = this.lowY[id];
|
|
666
|
+
const high = this.highY[id];
|
|
667
|
+
const dv = y < low ? low - y : y > high ? y - high : 0;
|
|
668
|
+
if (dv >= r)
|
|
669
|
+
continue;
|
|
670
|
+
let dh = this.nearest(id, x, z);
|
|
671
|
+
if (dh < 0)
|
|
672
|
+
dh = 0;
|
|
673
|
+
if (dh * dh + dv * dv < r2)
|
|
674
|
+
return false;
|
|
675
|
+
}
|
|
676
|
+
return true;
|
|
677
|
+
}
|
|
678
|
+
/** Parameter of the ray's first downward passage through the base surface in [0, maxT], or Infinity. */
|
|
679
|
+
rayBase(ox, oy, oz, dx, dy, dz, maxT) {
|
|
680
|
+
let t0 = 0;
|
|
681
|
+
let t1 = maxT;
|
|
682
|
+
// Only the part of the ray between the lowest and the highest base point.
|
|
683
|
+
const hi = this.baseMaxY + 0.01;
|
|
684
|
+
const lo = this.baseMinY - 0.01;
|
|
685
|
+
if (Math.abs(dy) < 1e-12) {
|
|
686
|
+
if (oy > hi || oy < lo)
|
|
687
|
+
return Infinity;
|
|
688
|
+
}
|
|
689
|
+
else {
|
|
690
|
+
const ta = (hi - oy) / dy;
|
|
691
|
+
const tb = (lo - oy) / dy;
|
|
692
|
+
const a = ta < tb ? ta : tb;
|
|
693
|
+
const b = ta < tb ? tb : ta;
|
|
694
|
+
if (a > t0)
|
|
695
|
+
t0 = a;
|
|
696
|
+
if (b < t1)
|
|
697
|
+
t1 = b;
|
|
698
|
+
}
|
|
699
|
+
const ext = this.extent;
|
|
700
|
+
if (ext) {
|
|
701
|
+
// Only the part of the ray over the extent.
|
|
702
|
+
if (!clipSlab(ox, dx, ext[0], ext[2]))
|
|
703
|
+
return Infinity;
|
|
704
|
+
if (slabA > t0)
|
|
705
|
+
t0 = slabA;
|
|
706
|
+
if (slabB < t1)
|
|
707
|
+
t1 = slabB;
|
|
708
|
+
if (!clipSlab(oz, dz, ext[1], ext[3]))
|
|
709
|
+
return Infinity;
|
|
710
|
+
if (slabA > t0)
|
|
711
|
+
t0 = slabA;
|
|
712
|
+
if (slabB < t1)
|
|
713
|
+
t1 = slabB;
|
|
714
|
+
}
|
|
715
|
+
if (!(t1 > t0))
|
|
716
|
+
return Infinity;
|
|
717
|
+
const horiz = Math.sqrt(dx * dx + dz * dz);
|
|
718
|
+
const stepT = horiz > 1e-9 ? this.baseStep / horiz : (t1 - t0) / 4;
|
|
719
|
+
const t = marchSurface(this.heightAt, ox, oy, oz, dx, dy, dz, t0, t1, stepT, null);
|
|
720
|
+
if (Number.isNaN(t) || t > maxT)
|
|
721
|
+
return Infinity;
|
|
722
|
+
return t;
|
|
723
|
+
}
|
|
724
|
+
/** Ray against an upright cylinder (side and both caps) for t in [0, tMax); writes `normal`; Infinity on a miss. */
|
|
725
|
+
rayCylinder(id, ox, oy, oz, dx, dy, dz, tMax) {
|
|
726
|
+
const cx = this.cx[id];
|
|
727
|
+
const cz = this.cz[id];
|
|
728
|
+
const r = this.cr[id];
|
|
729
|
+
const low = this.lowY[id];
|
|
730
|
+
const high = this.highY[id];
|
|
731
|
+
const nrm = this.normal;
|
|
732
|
+
let best = tMax;
|
|
733
|
+
let face = 0; // 1 side, 2 cap
|
|
734
|
+
const fx = ox - cx;
|
|
735
|
+
const fz = oz - cz;
|
|
736
|
+
const a = dx * dx + dz * dz;
|
|
737
|
+
if (a > 1e-18) {
|
|
738
|
+
const b = fx * dx + fz * dz;
|
|
739
|
+
const c = fx * fx + fz * fz - r * r;
|
|
740
|
+
const disc = b * b - a * c;
|
|
741
|
+
if (disc >= 0) {
|
|
742
|
+
const sq = Math.sqrt(disc);
|
|
743
|
+
// Entry, then exit (a ray that starts inside).
|
|
744
|
+
for (let k = 0; k < 2; k++) {
|
|
745
|
+
const t = (k === 0 ? -b - sq : -b + sq) / a;
|
|
746
|
+
if (t < 0 || t >= best)
|
|
747
|
+
continue;
|
|
748
|
+
const y = oy + dy * t;
|
|
749
|
+
if (y < low || y > high)
|
|
750
|
+
continue;
|
|
751
|
+
best = t;
|
|
752
|
+
face = 1;
|
|
753
|
+
break;
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
if (Math.abs(dy) > 1e-12) {
|
|
758
|
+
for (let k = 0; k < 2; k++) {
|
|
759
|
+
const t = ((k === 0 ? high : low) - oy) / dy;
|
|
760
|
+
if (t < 0 || t >= best)
|
|
761
|
+
continue;
|
|
762
|
+
const ex = fx + dx * t;
|
|
763
|
+
const ez = fz + dz * t;
|
|
764
|
+
if (ex * ex + ez * ez > r * r)
|
|
765
|
+
continue;
|
|
766
|
+
best = t;
|
|
767
|
+
face = 2;
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
if (face === 0)
|
|
771
|
+
return Infinity;
|
|
772
|
+
if (face === 1) {
|
|
773
|
+
let nx = (fx + dx * best) / r;
|
|
774
|
+
let nz = (fz + dz * best) / r;
|
|
775
|
+
if (nx * dx + nz * dz > 0) {
|
|
776
|
+
nx = -nx;
|
|
777
|
+
nz = -nz;
|
|
778
|
+
}
|
|
779
|
+
nrm[0] = nx;
|
|
780
|
+
nrm[1] = 0;
|
|
781
|
+
nrm[2] = nz;
|
|
782
|
+
}
|
|
783
|
+
else {
|
|
784
|
+
nrm[0] = 0;
|
|
785
|
+
nrm[1] = dy > 0 ? -1 : 1;
|
|
786
|
+
nrm[2] = 0;
|
|
787
|
+
}
|
|
788
|
+
return best;
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
// Result of `clipSlab`: the parameter range of a ray inside one slab.
|
|
792
|
+
let slabA = 0;
|
|
793
|
+
let slabB = 0;
|
|
794
|
+
/** Range of t for which o + d·t lies in [lo, hi], into `slabA`, `slabB`; false when never. */
|
|
795
|
+
function clipSlab(o, d, lo, hi) {
|
|
796
|
+
if (d === 0) {
|
|
797
|
+
slabA = -Infinity;
|
|
798
|
+
slabB = Infinity;
|
|
799
|
+
return o >= lo && o <= hi;
|
|
800
|
+
}
|
|
801
|
+
const ta = (lo - o) / d;
|
|
802
|
+
const tb = (hi - o) / d;
|
|
803
|
+
slabA = ta < tb ? ta : tb;
|
|
804
|
+
slabB = ta < tb ? tb : ta;
|
|
805
|
+
return true;
|
|
806
|
+
}
|
|
807
|
+
/** Parameter at which a ray in x, z leaves a rectangle for good (−Infinity when it never meets it, or it is empty). */
|
|
808
|
+
function slabExit(ox, oz, dx, dz, x0, z0, x1, z1) {
|
|
809
|
+
if (!(x1 >= x0))
|
|
810
|
+
return -Infinity;
|
|
811
|
+
if (!clipSlab(ox, dx, x0, x1))
|
|
812
|
+
return -Infinity;
|
|
813
|
+
let a = slabA;
|
|
814
|
+
let b = slabB;
|
|
815
|
+
if (!clipSlab(oz, dz, z0, z1))
|
|
816
|
+
return -Infinity;
|
|
817
|
+
if (slabA > a)
|
|
818
|
+
a = slabA;
|
|
819
|
+
if (slabB < b)
|
|
820
|
+
b = slabB;
|
|
821
|
+
return b >= a ? b : -Infinity;
|
|
822
|
+
}
|