@despia-native/kernel 0.0.0-stage → 0.3.0
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 +202 -0
- package/NOTICE +1414 -0
- package/README.md +17 -2
- package/dist/.dsx-build-stamp.json +7 -0
- package/dist/api-cohort.d.ts +27 -0
- package/dist/api-cohort.d.ts.map +1 -0
- package/dist/api-cohort.js +94 -0
- package/dist/api-cohort.js.map +1 -0
- package/dist/api.d.ts +454 -0
- package/dist/api.d.ts.map +1 -0
- package/dist/api.js +2442 -0
- package/dist/api.js.map +1 -0
- package/dist/app-consts.d.ts +10 -0
- package/dist/app-consts.d.ts.map +1 -0
- package/dist/app-consts.js +39 -0
- package/dist/app-consts.js.map +1 -0
- package/dist/app-update.d.ts +12 -0
- package/dist/app-update.d.ts.map +1 -0
- package/dist/app-update.js +32 -0
- package/dist/app-update.js.map +1 -0
- package/dist/aria.d.ts +152 -0
- package/dist/aria.d.ts.map +1 -0
- package/dist/aria.js +524 -0
- package/dist/aria.js.map +1 -0
- package/dist/audio-core.d.ts +260 -0
- package/dist/audio-core.d.ts.map +1 -0
- package/dist/audio-core.js +1440 -0
- package/dist/audio-core.js.map +1 -0
- package/dist/audio-edit.d.ts +100 -0
- package/dist/audio-edit.d.ts.map +1 -0
- package/dist/audio-edit.js +489 -0
- package/dist/audio-edit.js.map +1 -0
- package/dist/audio-mix.d.ts +84 -0
- package/dist/audio-mix.d.ts.map +1 -0
- package/dist/audio-mix.js +393 -0
- package/dist/audio-mix.js.map +1 -0
- package/dist/auth-proof.d.ts +145 -0
- package/dist/auth-proof.d.ts.map +1 -0
- package/dist/auth-proof.js +644 -0
- package/dist/auth-proof.js.map +1 -0
- package/dist/availability-explain.d.ts +3 -0
- package/dist/availability-explain.d.ts.map +1 -0
- package/dist/availability-explain.js +322 -0
- package/dist/availability-explain.js.map +1 -0
- package/dist/availability.d.ts +246 -0
- package/dist/availability.d.ts.map +1 -0
- package/dist/availability.js +615 -0
- package/dist/availability.js.map +1 -0
- package/dist/background.d.ts +116 -0
- package/dist/background.d.ts.map +1 -0
- package/dist/background.js +235 -0
- package/dist/background.js.map +1 -0
- package/dist/boot-tables-flag.d.ts +2 -0
- package/dist/boot-tables-flag.d.ts.map +1 -0
- package/dist/boot-tables-flag.js +16 -0
- package/dist/boot-tables-flag.js.map +1 -0
- package/dist/boot-tables.d.ts +40 -0
- package/dist/boot-tables.d.ts.map +1 -0
- package/dist/boot-tables.js +102 -0
- package/dist/boot-tables.js.map +1 -0
- package/dist/border-edges.d.ts +5 -0
- package/dist/border-edges.d.ts.map +1 -0
- package/dist/border-edges.js +47 -0
- package/dist/border-edges.js.map +1 -0
- package/dist/bus.d.ts +1203 -0
- package/dist/bus.d.ts.map +1 -0
- package/dist/bus.js +2647 -0
- package/dist/bus.js.map +1 -0
- package/dist/calendar-core.d.ts +143 -0
- package/dist/calendar-core.d.ts.map +1 -0
- package/dist/calendar-core.js +372 -0
- package/dist/calendar-core.js.map +1 -0
- package/dist/call-value.d.ts +16 -0
- package/dist/call-value.d.ts.map +1 -0
- package/dist/call-value.js +54 -0
- package/dist/call-value.js.map +1 -0
- package/dist/camera-core.d.ts +222 -0
- package/dist/camera-core.d.ts.map +1 -0
- package/dist/camera-core.js +980 -0
- package/dist/camera-core.js.map +1 -0
- package/dist/canvas-core.d.ts +404 -0
- package/dist/canvas-core.d.ts.map +1 -0
- package/dist/canvas-core.js +2186 -0
- package/dist/canvas-core.js.map +1 -0
- package/dist/capture-core.d.ts +74 -0
- package/dist/capture-core.d.ts.map +1 -0
- package/dist/capture-core.js +158 -0
- package/dist/capture-core.js.map +1 -0
- package/dist/compile/codegen.d.ts +146 -0
- package/dist/compile/codegen.d.ts.map +1 -0
- package/dist/compile/codegen.js +1949 -0
- package/dist/compile/codegen.js.map +1 -0
- package/dist/compile/jstier.d.ts +44 -0
- package/dist/compile/jstier.d.ts.map +1 -0
- package/dist/compile/jstier.js +181 -0
- package/dist/compile/jstier.js.map +1 -0
- package/dist/compile/tier-door.d.ts +41 -0
- package/dist/compile/tier-door.d.ts.map +1 -0
- package/dist/compile/tier-door.js +167 -0
- package/dist/compile/tier-door.js.map +1 -0
- package/dist/compile/tier-realm.d.ts +49 -0
- package/dist/compile/tier-realm.d.ts.map +1 -0
- package/dist/compile/tier-realm.js +368 -0
- package/dist/compile/tier-realm.js.map +1 -0
- package/dist/compile/tier.d.ts +23 -0
- package/dist/compile/tier.d.ts.map +1 -0
- package/dist/compile/tier.js +157 -0
- package/dist/compile/tier.js.map +1 -0
- package/dist/compose-core.d.ts +86 -0
- package/dist/compose-core.d.ts.map +1 -0
- package/dist/compose-core.js +112 -0
- package/dist/compose-core.js.map +1 -0
- package/dist/config-validate.d.ts +31 -0
- package/dist/config-validate.d.ts.map +1 -0
- package/dist/config-validate.js +147 -0
- package/dist/config-validate.js.map +1 -0
- package/dist/config-values.d.ts +106 -0
- package/dist/config-values.d.ts.map +1 -0
- package/dist/config-values.js +340 -0
- package/dist/config-values.js.map +1 -0
- package/dist/contacts-core.d.ts +127 -0
- package/dist/contacts-core.d.ts.map +1 -0
- package/dist/contacts-core.js +211 -0
- package/dist/contacts-core.js.map +1 -0
- package/dist/content-transition.d.ts +58 -0
- package/dist/content-transition.d.ts.map +1 -0
- package/dist/content-transition.js +94 -0
- package/dist/content-transition.js.map +1 -0
- package/dist/controls-core.d.ts +227 -0
- package/dist/controls-core.d.ts.map +1 -0
- package/dist/controls-core.js +652 -0
- package/dist/controls-core.js.map +1 -0
- package/dist/cookies.d.ts +78 -0
- package/dist/cookies.d.ts.map +1 -0
- package/dist/cookies.js +265 -0
- package/dist/cookies.js.map +1 -0
- package/dist/corner-shape-core.d.ts +69 -0
- package/dist/corner-shape-core.d.ts.map +1 -0
- package/dist/corner-shape-core.js +261 -0
- package/dist/corner-shape-core.js.map +1 -0
- package/dist/crypto-core.d.ts +57 -0
- package/dist/crypto-core.d.ts.map +1 -0
- package/dist/crypto-core.js +128 -0
- package/dist/crypto-core.js.map +1 -0
- package/dist/dataviz.d.ts +436 -0
- package/dist/dataviz.d.ts.map +1 -0
- package/dist/dataviz.js +956 -0
- package/dist/dataviz.js.map +1 -0
- package/dist/debug.d.ts +293 -0
- package/dist/debug.d.ts.map +1 -0
- package/dist/debug.js +767 -0
- package/dist/debug.js.map +1 -0
- package/dist/device-ownership.d.ts +50 -0
- package/dist/device-ownership.d.ts.map +1 -0
- package/dist/device-ownership.js +105 -0
- package/dist/device-ownership.js.map +1 -0
- package/dist/effects.d.ts +86 -0
- package/dist/effects.d.ts.map +1 -0
- package/dist/effects.js +98 -0
- package/dist/effects.js.map +1 -0
- package/dist/element.d.ts +108 -0
- package/dist/element.d.ts.map +1 -0
- package/dist/element.js +63 -0
- package/dist/element.js.map +1 -0
- package/dist/file-watch.d.ts +16 -0
- package/dist/file-watch.d.ts.map +1 -0
- package/dist/file-watch.js +49 -0
- package/dist/file-watch.js.map +1 -0
- package/dist/filepaths.d.ts +72 -0
- package/dist/filepaths.d.ts.map +1 -0
- package/dist/filepaths.js +271 -0
- package/dist/filepaths.js.map +1 -0
- package/dist/film.d.ts +264 -0
- package/dist/film.d.ts.map +1 -0
- package/dist/film.js +313 -0
- package/dist/film.js.map +1 -0
- package/dist/fonts.d.ts +66 -0
- package/dist/fonts.d.ts.map +1 -0
- package/dist/fonts.js +135 -0
- package/dist/fonts.js.map +1 -0
- package/dist/forms.d.ts +191 -0
- package/dist/forms.d.ts.map +1 -0
- package/dist/forms.js +663 -0
- package/dist/forms.js.map +1 -0
- package/dist/geo.d.ts +289 -0
- package/dist/geo.d.ts.map +1 -0
- package/dist/geo.js +460 -0
- package/dist/geo.js.map +1 -0
- package/dist/handler-announce.d.ts +46 -0
- package/dist/handler-announce.d.ts.map +1 -0
- package/dist/handler-announce.js +96 -0
- package/dist/handler-announce.js.map +1 -0
- package/dist/handoff.d.ts +71 -0
- package/dist/handoff.d.ts.map +1 -0
- package/dist/handoff.js +250 -0
- package/dist/handoff.js.map +1 -0
- package/dist/identity-durability.d.ts +76 -0
- package/dist/identity-durability.d.ts.map +1 -0
- package/dist/identity-durability.js +150 -0
- package/dist/identity-durability.js.map +1 -0
- package/dist/image-core.d.ts +240 -0
- package/dist/image-core.d.ts.map +1 -0
- package/dist/image-core.js +618 -0
- package/dist/image-core.js.map +1 -0
- package/dist/image-schemes.d.ts +10 -0
- package/dist/image-schemes.d.ts.map +1 -0
- package/dist/image-schemes.js +28 -0
- package/dist/image-schemes.js.map +1 -0
- package/dist/index.d.ts +143 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +299 -0
- package/dist/index.js.map +1 -0
- package/dist/ink-core.d.ts +53 -0
- package/dist/ink-core.d.ts.map +1 -0
- package/dist/ink-core.js +119 -0
- package/dist/ink-core.js.map +1 -0
- package/dist/input.d.ts +148 -0
- package/dist/input.d.ts.map +1 -0
- package/dist/input.js +358 -0
- package/dist/input.js.map +1 -0
- package/dist/integrity.d.ts +63 -0
- package/dist/integrity.d.ts.map +1 -0
- package/dist/integrity.js +150 -0
- package/dist/integrity.js.map +1 -0
- package/dist/intents.d.ts +95 -0
- package/dist/intents.d.ts.map +1 -0
- package/dist/intents.js +282 -0
- package/dist/intents.js.map +1 -0
- package/dist/interpolate-core.d.ts +37 -0
- package/dist/interpolate-core.d.ts.map +1 -0
- package/dist/interpolate-core.js +443 -0
- package/dist/interpolate-core.js.map +1 -0
- package/dist/jse/color.d.ts +12 -0
- package/dist/jse/color.d.ts.map +1 -0
- package/dist/jse/color.js +196 -0
- package/dist/jse/color.js.map +1 -0
- package/dist/jse/core.d.ts +54 -0
- package/dist/jse/core.d.ts.map +1 -0
- package/dist/jse/core.js +1519 -0
- package/dist/jse/core.js.map +1 -0
- package/dist/jse/dispatch.d.ts +28 -0
- package/dist/jse/dispatch.d.ts.map +1 -0
- package/dist/jse/dispatch.js +116 -0
- package/dist/jse/dispatch.js.map +1 -0
- package/dist/jse/highlight-grammar.d.ts +15 -0
- package/dist/jse/highlight-grammar.d.ts.map +1 -0
- package/dist/jse/highlight-grammar.js +161 -0
- package/dist/jse/highlight-grammar.js.map +1 -0
- package/dist/jse/highlight-grammars.generated.d.ts +2 -0
- package/dist/jse/highlight-grammars.generated.d.ts.map +1 -0
- package/dist/jse/highlight-grammars.generated.js +4 -0
- package/dist/jse/highlight-grammars.generated.js.map +1 -0
- package/dist/jse/highlight.d.ts +37 -0
- package/dist/jse/highlight.d.ts.map +1 -0
- package/dist/jse/highlight.js +323 -0
- package/dist/jse/highlight.js.map +1 -0
- package/dist/jse/jse.d.ts +674 -0
- package/dist/jse/jse.d.ts.map +1 -0
- package/dist/jse/jse.js +5841 -0
- package/dist/jse/jse.js.map +1 -0
- package/dist/jse/pathmatch.d.ts +10 -0
- package/dist/jse/pathmatch.d.ts.map +1 -0
- package/dist/jse/pathmatch.js +73 -0
- package/dist/jse/pathmatch.js.map +1 -0
- package/dist/jse/regex.d.ts +33 -0
- package/dist/jse/regex.d.ts.map +1 -0
- package/dist/jse/regex.js +375 -0
- package/dist/jse/regex.js.map +1 -0
- package/dist/jse/stdlib.d.ts +8 -0
- package/dist/jse/stdlib.d.ts.map +1 -0
- package/dist/jse/stdlib.js +765 -0
- package/dist/jse/stdlib.js.map +1 -0
- package/dist/jse/tokens.d.ts +102 -0
- package/dist/jse/tokens.d.ts.map +1 -0
- package/dist/jse/tokens.js +982 -0
- package/dist/jse/tokens.js.map +1 -0
- package/dist/jse/values.d.ts +206 -0
- package/dist/jse/values.d.ts.map +1 -0
- package/dist/jse/values.js +846 -0
- package/dist/jse/values.js.map +1 -0
- package/dist/keyboard.d.ts +196 -0
- package/dist/keyboard.d.ts.map +1 -0
- package/dist/keyboard.js +186 -0
- package/dist/keyboard.js.map +1 -0
- package/dist/label-choices.d.ts +3 -0
- package/dist/label-choices.d.ts.map +1 -0
- package/dist/label-choices.js +168 -0
- package/dist/label-choices.js.map +1 -0
- package/dist/language.d.ts +33 -0
- package/dist/language.d.ts.map +1 -0
- package/dist/language.js +50 -0
- package/dist/language.js.map +1 -0
- package/dist/length-units.d.ts +30 -0
- package/dist/length-units.d.ts.map +1 -0
- package/dist/length-units.js +83 -0
- package/dist/length-units.js.map +1 -0
- package/dist/lightsensor.d.ts +48 -0
- package/dist/lightsensor.d.ts.map +1 -0
- package/dist/lightsensor.js +97 -0
- package/dist/lightsensor.js.map +1 -0
- package/dist/link-refresh.d.ts +27 -0
- package/dist/link-refresh.d.ts.map +1 -0
- package/dist/link-refresh.js +166 -0
- package/dist/link-refresh.js.map +1 -0
- package/dist/link.d.ts +57 -0
- package/dist/link.d.ts.map +1 -0
- package/dist/link.js +333 -0
- package/dist/link.js.map +1 -0
- package/dist/linkpreview.d.ts +114 -0
- package/dist/linkpreview.d.ts.map +1 -0
- package/dist/linkpreview.js +439 -0
- package/dist/linkpreview.js.map +1 -0
- package/dist/list-presence.d.ts +32 -0
- package/dist/list-presence.d.ts.map +1 -0
- package/dist/list-presence.js +48 -0
- package/dist/list-presence.js.map +1 -0
- package/dist/live-stream.d.ts +27 -0
- package/dist/live-stream.d.ts.map +1 -0
- package/dist/live-stream.js +274 -0
- package/dist/live-stream.js.map +1 -0
- package/dist/livelogs.d.ts +134 -0
- package/dist/livelogs.d.ts.map +1 -0
- package/dist/livelogs.js +409 -0
- package/dist/livelogs.js.map +1 -0
- package/dist/logs.d.ts +80 -0
- package/dist/logs.d.ts.map +1 -0
- package/dist/logs.js +206 -0
- package/dist/logs.js.map +1 -0
- package/dist/mcp/apps.d.ts +82 -0
- package/dist/mcp/apps.d.ts.map +1 -0
- package/dist/mcp/apps.js +219 -0
- package/dist/mcp/apps.js.map +1 -0
- package/dist/mcp/entities.d.ts +25 -0
- package/dist/mcp/entities.d.ts.map +1 -0
- package/dist/mcp/entities.js +71 -0
- package/dist/mcp/entities.js.map +1 -0
- package/dist/mcp/result.d.ts +73 -0
- package/dist/mcp/result.d.ts.map +1 -0
- package/dist/mcp/result.js +119 -0
- package/dist/mcp/result.js.map +1 -0
- package/dist/mcp/webmcp.d.ts +162 -0
- package/dist/mcp/webmcp.d.ts.map +1 -0
- package/dist/mcp/webmcp.js +214 -0
- package/dist/mcp/webmcp.js.map +1 -0
- package/dist/mcp.d.ts +5 -0
- package/dist/mcp.d.ts.map +1 -0
- package/dist/mcp.js +14 -0
- package/dist/mcp.js.map +1 -0
- package/dist/media-core.d.ts +191 -0
- package/dist/media-core.d.ts.map +1 -0
- package/dist/media-core.js +446 -0
- package/dist/media-core.js.map +1 -0
- package/dist/media-timeline-core.d.ts +94 -0
- package/dist/media-timeline-core.d.ts.map +1 -0
- package/dist/media-timeline-core.js +620 -0
- package/dist/media-timeline-core.js.map +1 -0
- package/dist/message.d.ts +42 -0
- package/dist/message.d.ts.map +1 -0
- package/dist/message.js +582 -0
- package/dist/message.js.map +1 -0
- package/dist/missing-element.d.ts +29 -0
- package/dist/missing-element.d.ts.map +1 -0
- package/dist/missing-element.js +76 -0
- package/dist/missing-element.js.map +1 -0
- package/dist/motion-core.d.ts +160 -0
- package/dist/motion-core.d.ts.map +1 -0
- package/dist/motion-core.js +752 -0
- package/dist/motion-core.js.map +1 -0
- package/dist/motion-pose.d.ts +65 -0
- package/dist/motion-pose.d.ts.map +1 -0
- package/dist/motion-pose.js +143 -0
- package/dist/motion-pose.js.map +1 -0
- package/dist/motion.d.ts +132 -0
- package/dist/motion.d.ts.map +1 -0
- package/dist/motion.js +260 -0
- package/dist/motion.js.map +1 -0
- package/dist/mount-core.d.ts +203 -0
- package/dist/mount-core.d.ts.map +1 -0
- package/dist/mount-core.js +437 -0
- package/dist/mount-core.js.map +1 -0
- package/dist/net-core.d.ts +110 -0
- package/dist/net-core.d.ts.map +1 -0
- package/dist/net-core.js +225 -0
- package/dist/net-core.js.map +1 -0
- package/dist/notify.d.ts +399 -0
- package/dist/notify.d.ts.map +1 -0
- package/dist/notify.js +964 -0
- package/dist/notify.js.map +1 -0
- package/dist/orientation-binding.d.ts +39 -0
- package/dist/orientation-binding.d.ts.map +1 -0
- package/dist/orientation-binding.js +64 -0
- package/dist/orientation-binding.js.map +1 -0
- package/dist/orientation.d.ts +55 -0
- package/dist/orientation.d.ts.map +1 -0
- package/dist/orientation.js +89 -0
- package/dist/orientation.js.map +1 -0
- package/dist/ota.d.ts +52 -0
- package/dist/ota.d.ts.map +1 -0
- package/dist/ota.js +122 -0
- package/dist/ota.js.map +1 -0
- package/dist/passkeys.d.ts +91 -0
- package/dist/passkeys.d.ts.map +1 -0
- package/dist/passkeys.js +307 -0
- package/dist/passkeys.js.map +1 -0
- package/dist/payment.d.ts +85 -0
- package/dist/payment.d.ts.map +1 -0
- package/dist/payment.js +270 -0
- package/dist/payment.js.map +1 -0
- package/dist/pending-presenter.d.ts +31 -0
- package/dist/pending-presenter.d.ts.map +1 -0
- package/dist/pending-presenter.js +119 -0
- package/dist/pending-presenter.js.map +1 -0
- package/dist/permission-core.d.ts +59 -0
- package/dist/permission-core.d.ts.map +1 -0
- package/dist/permission-core.js +102 -0
- package/dist/permission-core.js.map +1 -0
- package/dist/persist.d.ts +169 -0
- package/dist/persist.d.ts.map +1 -0
- package/dist/persist.js +361 -0
- package/dist/persist.js.map +1 -0
- package/dist/phase-plan.d.ts +114 -0
- package/dist/phase-plan.d.ts.map +1 -0
- package/dist/phase-plan.js +837 -0
- package/dist/phase-plan.js.map +1 -0
- package/dist/playback-core.d.ts +64 -0
- package/dist/playback-core.d.ts.map +1 -0
- package/dist/playback-core.js +176 -0
- package/dist/playback-core.js.map +1 -0
- package/dist/player-sources.d.ts +12 -0
- package/dist/player-sources.d.ts.map +1 -0
- package/dist/player-sources.js +44 -0
- package/dist/player-sources.js.map +1 -0
- package/dist/proxy-denylist.d.ts +11 -0
- package/dist/proxy-denylist.d.ts.map +1 -0
- package/dist/proxy-denylist.js +44 -0
- package/dist/proxy-denylist.js.map +1 -0
- package/dist/public-config.d.ts +20 -0
- package/dist/public-config.d.ts.map +1 -0
- package/dist/public-config.js +109 -0
- package/dist/public-config.js.map +1 -0
- package/dist/qoe-core.d.ts +72 -0
- package/dist/qoe-core.d.ts.map +1 -0
- package/dist/qoe-core.js +113 -0
- package/dist/qoe-core.js.map +1 -0
- package/dist/reactive-request.d.ts +80 -0
- package/dist/reactive-request.d.ts.map +1 -0
- package/dist/reactive-request.js +192 -0
- package/dist/reactive-request.js.map +1 -0
- package/dist/read-cache.d.ts +129 -0
- package/dist/read-cache.d.ts.map +1 -0
- package/dist/read-cache.js +566 -0
- package/dist/read-cache.js.map +1 -0
- package/dist/recorder-core.d.ts +57 -0
- package/dist/recorder-core.d.ts.map +1 -0
- package/dist/recorder-core.js +136 -0
- package/dist/recorder-core.js.map +1 -0
- package/dist/ref.d.ts +41 -0
- package/dist/ref.d.ts.map +1 -0
- package/dist/ref.js +98 -0
- package/dist/ref.js.map +1 -0
- package/dist/refusal.d.ts +82 -0
- package/dist/refusal.d.ts.map +1 -0
- package/dist/refusal.js +218 -0
- package/dist/refusal.js.map +1 -0
- package/dist/region-insets.d.ts +61 -0
- package/dist/region-insets.d.ts.map +1 -0
- package/dist/region-insets.js +67 -0
- package/dist/region-insets.js.map +1 -0
- package/dist/rive-core.d.ts +323 -0
- package/dist/rive-core.d.ts.map +1 -0
- package/dist/rive-core.js +677 -0
- package/dist/rive-core.js.map +1 -0
- package/dist/runner.d.ts +595 -0
- package/dist/runner.d.ts.map +1 -0
- package/dist/runner.js +4094 -0
- package/dist/runner.js.map +1 -0
- package/dist/scene/a11y.d.ts +11 -0
- package/dist/scene/a11y.d.ts.map +1 -0
- package/dist/scene/a11y.js +67 -0
- package/dist/scene/a11y.js.map +1 -0
- package/dist/scene/anim.d.ts +149 -0
- package/dist/scene/anim.d.ts.map +1 -0
- package/dist/scene/anim.js +396 -0
- package/dist/scene/anim.js.map +1 -0
- package/dist/scene/bind.d.ts +33 -0
- package/dist/scene/bind.d.ts.map +1 -0
- package/dist/scene/bind.js +66 -0
- package/dist/scene/bind.js.map +1 -0
- package/dist/scene/collide.d.ts +56 -0
- package/dist/scene/collide.d.ts.map +1 -0
- package/dist/scene/collide.js +146 -0
- package/dist/scene/collide.js.map +1 -0
- package/dist/scene/frame.d.ts +22 -0
- package/dist/scene/frame.d.ts.map +1 -0
- package/dist/scene/frame.js +57 -0
- package/dist/scene/frame.js.map +1 -0
- package/dist/scene/gltf.d.ts +79 -0
- package/dist/scene/gltf.d.ts.map +1 -0
- package/dist/scene/gltf.js +409 -0
- package/dist/scene/gltf.js.map +1 -0
- package/dist/scene/ir.d.ts +293 -0
- package/dist/scene/ir.d.ts.map +1 -0
- package/dist/scene/ir.js +692 -0
- package/dist/scene/ir.js.map +1 -0
- package/dist/scene/math.d.ts +50 -0
- package/dist/scene/math.d.ts.map +1 -0
- package/dist/scene/math.js +180 -0
- package/dist/scene/math.js.map +1 -0
- package/dist/scene/orbit.d.ts +19 -0
- package/dist/scene/orbit.d.ts.map +1 -0
- package/dist/scene/orbit.js +60 -0
- package/dist/scene/orbit.js.map +1 -0
- package/dist/scene/physics.d.ts +228 -0
- package/dist/scene/physics.d.ts.map +1 -0
- package/dist/scene/physics.js +1500 -0
- package/dist/scene/physics.js.map +1 -0
- package/dist/scene/registry.d.ts +85 -0
- package/dist/scene/registry.d.ts.map +1 -0
- package/dist/scene/registry.js +75 -0
- package/dist/scene/registry.js.map +1 -0
- package/dist/scene/skin.d.ts +82 -0
- package/dist/scene/skin.d.ts.map +1 -0
- package/dist/scene/skin.js +351 -0
- package/dist/scene/skin.js.map +1 -0
- package/dist/scene/sprite.d.ts +36 -0
- package/dist/scene/sprite.d.ts.map +1 -0
- package/dist/scene/sprite.js +107 -0
- package/dist/scene/sprite.js.map +1 -0
- package/dist/scene/viewer.d.ts +96 -0
- package/dist/scene/viewer.d.ts.map +1 -0
- package/dist/scene/viewer.js +235 -0
- package/dist/scene/viewer.js.map +1 -0
- package/dist/scoped-slots.d.ts +9 -0
- package/dist/scoped-slots.d.ts.map +1 -0
- package/dist/scoped-slots.js +42 -0
- package/dist/scoped-slots.js.map +1 -0
- package/dist/screen.d.ts +109 -0
- package/dist/screen.d.ts.map +1 -0
- package/dist/screen.js +309 -0
- package/dist/screen.js.map +1 -0
- package/dist/scroll.d.ts +525 -0
- package/dist/scroll.d.ts.map +1 -0
- package/dist/scroll.js +1226 -0
- package/dist/scroll.js.map +1 -0
- package/dist/settings-plane.d.ts +58 -0
- package/dist/settings-plane.d.ts.map +1 -0
- package/dist/settings-plane.js +86 -0
- package/dist/settings-plane.js.map +1 -0
- package/dist/share-inbox.d.ts +56 -0
- package/dist/share-inbox.d.ts.map +1 -0
- package/dist/share-inbox.js +114 -0
- package/dist/share-inbox.js.map +1 -0
- package/dist/shared-transition.d.ts +152 -0
- package/dist/shared-transition.d.ts.map +1 -0
- package/dist/shared-transition.js +257 -0
- package/dist/shared-transition.js.map +1 -0
- package/dist/sheet-content-padding.d.ts +24 -0
- package/dist/sheet-content-padding.d.ts.map +1 -0
- package/dist/sheet-content-padding.js +32 -0
- package/dist/sheet-content-padding.js.map +1 -0
- package/dist/shot-devices.d.ts +36 -0
- package/dist/shot-devices.d.ts.map +1 -0
- package/dist/shot-devices.js +90 -0
- package/dist/shot-devices.js.map +1 -0
- package/dist/shot-guards.d.ts +100 -0
- package/dist/shot-guards.d.ts.map +1 -0
- package/dist/shot-guards.js +260 -0
- package/dist/shot-guards.js.map +1 -0
- package/dist/shot-scope.d.ts +140 -0
- package/dist/shot-scope.d.ts.map +1 -0
- package/dist/shot-scope.js +302 -0
- package/dist/shot-scope.js.map +1 -0
- package/dist/slot-partition.d.ts +9 -0
- package/dist/slot-partition.d.ts.map +1 -0
- package/dist/slot-partition.js +38 -0
- package/dist/slot-partition.js.map +1 -0
- package/dist/snackbar.d.ts +165 -0
- package/dist/snackbar.d.ts.map +1 -0
- package/dist/snackbar.js +220 -0
- package/dist/snackbar.js.map +1 -0
- package/dist/socket.d.ts +96 -0
- package/dist/socket.d.ts.map +1 -0
- package/dist/socket.js +370 -0
- package/dist/socket.js.map +1 -0
- package/dist/spotlight.d.ts +77 -0
- package/dist/spotlight.d.ts.map +1 -0
- package/dist/spotlight.js +174 -0
- package/dist/spotlight.js.map +1 -0
- package/dist/state-primitive.d.ts +37 -0
- package/dist/state-primitive.d.ts.map +1 -0
- package/dist/state-primitive.js +91 -0
- package/dist/state-primitive.js.map +1 -0
- package/dist/store-query.d.ts +25 -0
- package/dist/store-query.d.ts.map +1 -0
- package/dist/store-query.js +88 -0
- package/dist/store-query.js.map +1 -0
- package/dist/store-value.d.ts +48 -0
- package/dist/store-value.d.ts.map +1 -0
- package/dist/store-value.js +264 -0
- package/dist/store-value.js.map +1 -0
- package/dist/store-versioning.d.ts +70 -0
- package/dist/store-versioning.d.ts.map +1 -0
- package/dist/store-versioning.js +188 -0
- package/dist/store-versioning.js.map +1 -0
- package/dist/store.d.ts +282 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/store.js +1144 -0
- package/dist/store.js.map +1 -0
- package/dist/strings.d.ts +141 -0
- package/dist/strings.d.ts.map +1 -0
- package/dist/strings.js +581 -0
- package/dist/strings.js.map +1 -0
- package/dist/telemetry.d.ts +95 -0
- package/dist/telemetry.d.ts.map +1 -0
- package/dist/telemetry.js +255 -0
- package/dist/telemetry.js.map +1 -0
- package/dist/transform-core.d.ts +228 -0
- package/dist/transform-core.d.ts.map +1 -0
- package/dist/transform-core.js +921 -0
- package/dist/transform-core.js.map +1 -0
- package/dist/transition-core.d.ts +58 -0
- package/dist/transition-core.d.ts.map +1 -0
- package/dist/transition-core.js +352 -0
- package/dist/transition-core.js.map +1 -0
- package/dist/trim-trailing.d.ts +3 -0
- package/dist/trim-trailing.d.ts.map +1 -0
- package/dist/trim-trailing.js +14 -0
- package/dist/trim-trailing.js.map +1 -0
- package/dist/upload.d.ts +51 -0
- package/dist/upload.d.ts.map +1 -0
- package/dist/upload.js +68 -0
- package/dist/upload.js.map +1 -0
- package/dist/vendor-face.d.ts +44 -0
- package/dist/vendor-face.d.ts.map +1 -0
- package/dist/vendor-face.js +80 -0
- package/dist/vendor-face.js.map +1 -0
- package/dist/vendor-session.d.ts +166 -0
- package/dist/vendor-session.d.ts.map +1 -0
- package/dist/vendor-session.js +423 -0
- package/dist/vendor-session.js.map +1 -0
- package/dist/vendor-surface.d.ts +290 -0
- package/dist/vendor-surface.d.ts.map +1 -0
- package/dist/vendor-surface.js +439 -0
- package/dist/vendor-surface.js.map +1 -0
- package/dist/version-rules.generated.d.ts +17 -0
- package/dist/version-rules.generated.d.ts.map +1 -0
- package/dist/version-rules.generated.js +99 -0
- package/dist/version-rules.generated.js.map +1 -0
- package/dist/view-transition-core.d.ts +99 -0
- package/dist/view-transition-core.d.ts.map +1 -0
- package/dist/view-transition-core.js +127 -0
- package/dist/view-transition-core.js.map +1 -0
- package/dist/view-transition-door.d.ts +26 -0
- package/dist/view-transition-door.d.ts.map +1 -0
- package/dist/view-transition-door.js +88 -0
- package/dist/view-transition-door.js.map +1 -0
- package/dist/window-identity.d.ts +3 -0
- package/dist/window-identity.d.ts.map +1 -0
- package/dist/window-identity.js +27 -0
- package/dist/window-identity.js.map +1 -0
- package/package.json +139 -4
|
@@ -0,0 +1,1500 @@
|
|
|
1
|
+
//
|
|
2
|
+
// scene/physics.ts - the DSX Scene G2 physics kernel (dsx-game.md §2 G2), platform-
|
|
3
|
+
// neutral and corpus-pinned (OpenSource/Conformance/scene/physics.json — every law
|
|
4
|
+
// spelled out in that file's _note and the corpus README). The shape:
|
|
5
|
+
//
|
|
6
|
+
// - THE FIXED-TICK LAW: the simulation steps at EXACTLY 60 Hz (dt = 1/60 s), decoupled
|
|
7
|
+
// from rendering. The renderer interpolates body positions between the last two
|
|
8
|
+
// steps (alpha = accumulator/dt); the accumulator caps at 5 steps per frame and
|
|
9
|
+
// DISCARDS the excess (the spiral-of-death guard — the sim slows, never spirals).
|
|
10
|
+
// `on:tick` fires per fixed step with { dt: 1/60, tick: n }; on:frame stays
|
|
11
|
+
// render-rate.
|
|
12
|
+
// - THE DETERMINISM LAW: same initial state + same inputs → identical states to 6
|
|
13
|
+
// decimals on every runner. All math in doubles, fixed document-order iteration,
|
|
14
|
+
// only +,-,*,/ and sqrt (Math.hypot is NOT used in the solver — sqrt of a sum of
|
|
15
|
+
// squares is identically rounded on every IEEE runtime; hypot is not so pinned).
|
|
16
|
+
// - BODIES: static · dynamic · kinematic (follows its authored/bound transform,
|
|
17
|
+
// pushes with derived velocity) · character (move-and-slide; speed/jump attrs;
|
|
18
|
+
// never sleeps; infinite mass against dynamics). Dynamic bodies integrate angular
|
|
19
|
+
// velocity and world torque against a shape-derived diagonal inertia tensor, then
|
|
20
|
+
// apply deterministic linear angular damping. Rotation is stored in DSX degrees;
|
|
21
|
+
// angular velocity is radians/second. A normalized quaternion is the integration
|
|
22
|
+
// state, while DSX Euler angles remain the public/render plane. Box contacts use a
|
|
23
|
+
// 15-axis OBB SAT, clipped face/support-edge manifolds and every impulse is applied
|
|
24
|
+
// at its deterministic contact point with persistent coherent warm starting.
|
|
25
|
+
// Capsule colliders remain a named absence.
|
|
26
|
+
// - THE SOLVER: semi-implicit linear integration; sphere/box contacts with the P5 collide depth
|
|
27
|
+
// laws + pinned normals; impulse resolution with restitution (max, with the 0.5
|
|
28
|
+
// approach-speed micro-bounce guard) and Coulomb friction (sqrt mixing, clamped to
|
|
29
|
+
// ±mu·j); Baumgarte positional correction (percent 0.8, slop 0.005); character
|
|
30
|
+
// move-and-slide with full depenetration, velocity projection and ground detection
|
|
31
|
+
// (surface normal up.y > 0.7); sleep at |v| < 0.05 for 60 ticks, wake on write or
|
|
32
|
+
// energetic contact. Triggers overlap without forces (enter/exit tracker).
|
|
33
|
+
//
|
|
34
|
+
// This module owns the NUMBERS; the render adapters (packages/dom scene.ts, the
|
|
35
|
+
// JVM/Swift twins when they land from this corpus) own only the wiring.
|
|
36
|
+
//
|
|
37
|
+
import { mat4Identity, mat4Multiply, mat4Trs, mat4Invert, transformPoint } from "./math.js";
|
|
38
|
+
import { spriteAnchorOffset, spriteSizeOf } from "./sprite.js";
|
|
39
|
+
import { nodeBoundingRadius, readScalar, readString, readVec, resolvedProps, } from "./ir.js";
|
|
40
|
+
// ── the pinned constants (corpus `constants` — the runner asserts these) ─────────────
|
|
41
|
+
export const SCENE_PHYSICS_DT = 1 / 60;
|
|
42
|
+
export const SCENE_PHYSICS_MAX_STEPS = 5;
|
|
43
|
+
export const SCENE_PHYSICS_DEFAULT_GRAVITY = [0, -9.81, 0];
|
|
44
|
+
export const SCENE_PHYSICS_CORRECTION_PERCENT = 0.8;
|
|
45
|
+
export const SCENE_PHYSICS_SLOP = 0.005;
|
|
46
|
+
export const SCENE_PHYSICS_RESTITUTION_MIN_SPEED = 0.5;
|
|
47
|
+
export const SCENE_PHYSICS_GROUND_NORMAL_Y = 0.7;
|
|
48
|
+
export const SCENE_PHYSICS_SLEEP_SPEED = 0.05;
|
|
49
|
+
export const SCENE_PHYSICS_SLEEP_TICKS = 60;
|
|
50
|
+
export const SCENE_PHYSICS_SLIDE_ITERATIONS = 4;
|
|
51
|
+
export const SCENE_PHYSICS_MANIFOLD_ITERATIONS = 32;
|
|
52
|
+
export const SCENE_PHYSICS_DEFAULT_SPEED = 5;
|
|
53
|
+
export const SCENE_PHYSICS_DEFAULT_JUMP = 8;
|
|
54
|
+
export const SCENE_PHYSICS_DEFAULT_ANGULAR_DAMPING = 0;
|
|
55
|
+
export const SCENE_PHYSICS_RADIANS_TO_DEGREES = 57.29577951308232;
|
|
56
|
+
export const SCENE_PHYSICS_DEGREES_TO_RADIANS = 0.017453292519943295;
|
|
57
|
+
/** G6 (sprite.json): the 2D extrusion of a `<sprite>` box collider — effectively
|
|
58
|
+
* infinite at scene scales, so the minimum-overlap axis of a box↔box contact is
|
|
59
|
+
* ALWAYS in the XY plane (the 2D semantics of an extruded rectangle) */
|
|
60
|
+
export const SCENE_SPRITE_COLLIDER_HALF_Z = 1000;
|
|
61
|
+
export const SCENE_PHYSICS_KINDS = new Set(["static", "dynamic", "kinematic", "character"]);
|
|
62
|
+
/** Solver-private: contact history must never leak onto the public world/store plane. */
|
|
63
|
+
const scenePhysicsContactCaches = new WeakMap();
|
|
64
|
+
function contactCache(world) {
|
|
65
|
+
let cache = scenePhysicsContactCaches.get(world);
|
|
66
|
+
if (cache === undefined) {
|
|
67
|
+
cache = new Map();
|
|
68
|
+
scenePhysicsContactCaches.set(world, cache);
|
|
69
|
+
}
|
|
70
|
+
return cache;
|
|
71
|
+
}
|
|
72
|
+
/** Carry coherent warm-start state across a renderer-driven world rebuild. Only pairs
|
|
73
|
+
* whose retained bodies keep the same document order are eligible: reversing a pair
|
|
74
|
+
* changes manifold feature ownership, so that cache is deliberately recomputed. */
|
|
75
|
+
export function carryScenePhysicsContactState(previous, next, idMap) {
|
|
76
|
+
const source = scenePhysicsContactCaches.get(previous);
|
|
77
|
+
if (source === undefined)
|
|
78
|
+
return;
|
|
79
|
+
const carried = new Map();
|
|
80
|
+
for (const [key, value] of source) {
|
|
81
|
+
const a = idMap.get(value.a);
|
|
82
|
+
const b = idMap.get(value.b);
|
|
83
|
+
if (a === undefined || b === undefined)
|
|
84
|
+
continue;
|
|
85
|
+
const nextA = next.byId.get(a);
|
|
86
|
+
const nextB = next.byId.get(b);
|
|
87
|
+
if (nextA === undefined || nextB === undefined || nextA.index >= nextB.index)
|
|
88
|
+
continue;
|
|
89
|
+
const oldPrefix = `${value.a}\u0000${value.b}\u0000`;
|
|
90
|
+
if (!key.startsWith(oldPrefix))
|
|
91
|
+
continue;
|
|
92
|
+
const nextKey = `${a}\u0000${b}\u0000${key.slice(oldPrefix.length)}`;
|
|
93
|
+
carried.set(nextKey, {
|
|
94
|
+
a, b, normal: value.normal, normalAxis: [...value.normalAxis],
|
|
95
|
+
tangent: [...value.tangent],
|
|
96
|
+
localAnchorA: [...value.localAnchorA],
|
|
97
|
+
localAnchorB: [...value.localAnchorB],
|
|
98
|
+
lastTick: value.lastTick,
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
if (carried.size > 0)
|
|
102
|
+
scenePhysicsContactCaches.set(next, carried);
|
|
103
|
+
}
|
|
104
|
+
function invalidateContactCache(world, id) {
|
|
105
|
+
const cache = scenePhysicsContactCaches.get(world);
|
|
106
|
+
if (cache === undefined)
|
|
107
|
+
return;
|
|
108
|
+
for (const [key, value] of cache)
|
|
109
|
+
if (value.a === id || value.b === id)
|
|
110
|
+
cache.delete(key);
|
|
111
|
+
}
|
|
112
|
+
// ── construction ─────────────────────────────────────────────────────────────────────
|
|
113
|
+
export function createScenePhysicsWorld(gravity, specs, mode2d = false) {
|
|
114
|
+
const bodies = specs.map((spec, index) => {
|
|
115
|
+
const mass = spec.mass ?? 1;
|
|
116
|
+
const invInertia = scenePhysicsInverseInertia(spec.kind, spec.shape, mass);
|
|
117
|
+
const rotation = [...(spec.rotation ?? [0, 0, 0])];
|
|
118
|
+
return {
|
|
119
|
+
id: spec.id,
|
|
120
|
+
kind: spec.kind,
|
|
121
|
+
shape: spec.shape.kind,
|
|
122
|
+
radius: spec.shape.kind === "sphere" ? spec.shape.radius : 0,
|
|
123
|
+
half: spec.shape.kind === "box" ? [...spec.shape.half] : [0, 0, 0],
|
|
124
|
+
position: [...(spec.position ?? [0, 0, 0])],
|
|
125
|
+
previous: [...(spec.position ?? [0, 0, 0])],
|
|
126
|
+
velocity: [...(spec.velocity ?? [0, 0, 0])],
|
|
127
|
+
rotation,
|
|
128
|
+
orientation: quaternionFromEuler(rotation),
|
|
129
|
+
previousRotation: [...rotation],
|
|
130
|
+
angularVelocity: [...(spec.angularVelocity ?? [0, 0, 0])],
|
|
131
|
+
invInertia,
|
|
132
|
+
torque: [...(spec.torque ?? [0, 0, 0])],
|
|
133
|
+
angularDamping: Math.max(spec.angularDamping ?? SCENE_PHYSICS_DEFAULT_ANGULAR_DAMPING, 0),
|
|
134
|
+
invMass: spec.kind === "dynamic" && mass > 0 ? 1 / mass : 0,
|
|
135
|
+
bounce: spec.bounce ?? 0,
|
|
136
|
+
friction: spec.friction ?? 0.5,
|
|
137
|
+
trigger: spec.trigger ?? false,
|
|
138
|
+
layer: spec.layer ?? "default",
|
|
139
|
+
collides: spec.collides ?? null,
|
|
140
|
+
speed: spec.speed ?? SCENE_PHYSICS_DEFAULT_SPEED,
|
|
141
|
+
jump: spec.jump ?? SCENE_PHYSICS_DEFAULT_JUMP,
|
|
142
|
+
grounded: false,
|
|
143
|
+
sleeping: false,
|
|
144
|
+
sleepCount: 0,
|
|
145
|
+
zLock: (spec.position ?? [0, 0, 0])[2],
|
|
146
|
+
vzLock: (spec.velocity ?? [0, 0, 0])[2],
|
|
147
|
+
rotation2dLock: [...rotation],
|
|
148
|
+
mode2d,
|
|
149
|
+
index,
|
|
150
|
+
};
|
|
151
|
+
});
|
|
152
|
+
const world = {
|
|
153
|
+
gravity: [...gravity],
|
|
154
|
+
mode2d,
|
|
155
|
+
bodies,
|
|
156
|
+
byId: new Map(bodies.map((b) => [b.id, b])),
|
|
157
|
+
tick: 0,
|
|
158
|
+
solidOverlap: new Set(),
|
|
159
|
+
triggerOverlap: new Set(),
|
|
160
|
+
triggerOrder: [],
|
|
161
|
+
};
|
|
162
|
+
scenePhysicsContactCaches.set(world, new Map());
|
|
163
|
+
return world;
|
|
164
|
+
}
|
|
165
|
+
/** Solid-sphere and solid-box diagonal inertia in body-local principal axes. */
|
|
166
|
+
export function scenePhysicsInverseInertia(kind, shape, mass) {
|
|
167
|
+
if (kind !== "dynamic" || mass <= 0)
|
|
168
|
+
return [0, 0, 0];
|
|
169
|
+
if (shape.kind === "sphere") {
|
|
170
|
+
const inertia = 0.4 * mass * shape.radius * shape.radius;
|
|
171
|
+
const inv = inertia > 0 ? 1 / inertia : 0;
|
|
172
|
+
return [inv, inv, inv];
|
|
173
|
+
}
|
|
174
|
+
const [hx, hy, hz] = shape.half;
|
|
175
|
+
const ix = mass * (hy * hy + hz * hz) / 3;
|
|
176
|
+
const iy = mass * (hx * hx + hz * hz) / 3;
|
|
177
|
+
const iz = mass * (hx * hx + hy * hy) / 3;
|
|
178
|
+
return [ix > 0 ? 1 / ix : 0, iy > 0 ? 1 / iy : 0, iz > 0 ? 1 / iz : 0];
|
|
179
|
+
}
|
|
180
|
+
/** THE VELOCITY-WRITE LAW: sets the velocity verbatim and wakes the body */
|
|
181
|
+
export function scenePhysicsWriteVelocity(world, id, v) {
|
|
182
|
+
const body = world.byId.get(id);
|
|
183
|
+
if (body === undefined)
|
|
184
|
+
return false;
|
|
185
|
+
body.velocity = [...v];
|
|
186
|
+
invalidateContactCache(world, id);
|
|
187
|
+
body.sleeping = false;
|
|
188
|
+
body.sleepCount = 0;
|
|
189
|
+
return true;
|
|
190
|
+
}
|
|
191
|
+
/** Sets angular velocity in radians/second and wakes the body. */
|
|
192
|
+
export function scenePhysicsWriteAngularVelocity(world, id, v) {
|
|
193
|
+
const body = world.byId.get(id);
|
|
194
|
+
if (body === undefined)
|
|
195
|
+
return false;
|
|
196
|
+
body.angularVelocity = [...v];
|
|
197
|
+
invalidateContactCache(world, id);
|
|
198
|
+
body.sleeping = false;
|
|
199
|
+
body.sleepCount = 0;
|
|
200
|
+
return true;
|
|
201
|
+
}
|
|
202
|
+
/** Sets the persistent world torque and wakes the body. */
|
|
203
|
+
export function scenePhysicsWriteTorque(world, id, torque) {
|
|
204
|
+
const body = world.byId.get(id);
|
|
205
|
+
if (body === undefined)
|
|
206
|
+
return false;
|
|
207
|
+
body.torque = [...torque];
|
|
208
|
+
invalidateContactCache(world, id);
|
|
209
|
+
body.sleeping = false;
|
|
210
|
+
body.sleepCount = 0;
|
|
211
|
+
return true;
|
|
212
|
+
}
|
|
213
|
+
/** Rotation teleport in DSX Euler degrees: reset the interpolation anchor and angular
|
|
214
|
+
* velocity so authored/bus writes never leave hidden spin behind. */
|
|
215
|
+
export function scenePhysicsTeleportRotation(world, id, rotation) {
|
|
216
|
+
const body = world.byId.get(id);
|
|
217
|
+
if (body === undefined)
|
|
218
|
+
return false;
|
|
219
|
+
body.rotation = [...rotation];
|
|
220
|
+
body.orientation = quaternionFromEuler(rotation);
|
|
221
|
+
body.previousRotation = [...rotation];
|
|
222
|
+
body.angularVelocity = [0, 0, 0];
|
|
223
|
+
invalidateContactCache(world, id);
|
|
224
|
+
body.sleeping = false;
|
|
225
|
+
body.sleepCount = 0;
|
|
226
|
+
return true;
|
|
227
|
+
}
|
|
228
|
+
/** THE TELEPORT LAW: an authored/bound/bus position write moves the body, RESETS its
|
|
229
|
+
* velocity to zero, resets the interpolation anchor (no glide across a teleport),
|
|
230
|
+
* and wakes it */
|
|
231
|
+
export function scenePhysicsTeleport(world, id, p) {
|
|
232
|
+
const body = world.byId.get(id);
|
|
233
|
+
if (body === undefined)
|
|
234
|
+
return false;
|
|
235
|
+
body.position = [...p];
|
|
236
|
+
body.previous = [...p];
|
|
237
|
+
body.velocity = [0, 0, 0];
|
|
238
|
+
invalidateContactCache(world, id);
|
|
239
|
+
body.zLock = p[2]; // G6: a teleport RE-ANCHORS the 2D z-lock (vzLock never moves)
|
|
240
|
+
body.sleeping = false;
|
|
241
|
+
body.sleepCount = 0;
|
|
242
|
+
return true;
|
|
243
|
+
}
|
|
244
|
+
/** the v1 kinematic-write wake law: no island graph (named absence) — a kinematic
|
|
245
|
+
* position write wakes every sleeping body */
|
|
246
|
+
export function scenePhysicsWakeAll(world) {
|
|
247
|
+
for (const body of world.bodies) {
|
|
248
|
+
if (body.sleeping) {
|
|
249
|
+
body.sleeping = false;
|
|
250
|
+
body.sleepCount = 0;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
// ── the contact laws ─────────────────────────────────────────────────────────────────
|
|
255
|
+
/** deterministic length — sqrt is correctly rounded on every IEEE runtime */
|
|
256
|
+
function length3(v) {
|
|
257
|
+
return Math.sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
|
|
258
|
+
}
|
|
259
|
+
function dot3(a, b) {
|
|
260
|
+
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
|
|
261
|
+
}
|
|
262
|
+
function cross3(a, b) {
|
|
263
|
+
return [
|
|
264
|
+
a[1] * b[2] - a[2] * b[1],
|
|
265
|
+
a[2] * b[0] - a[0] * b[2],
|
|
266
|
+
a[0] * b[1] - a[1] * b[0],
|
|
267
|
+
];
|
|
268
|
+
}
|
|
269
|
+
function subtract3(a, b) {
|
|
270
|
+
return [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
|
|
271
|
+
}
|
|
272
|
+
function quaternionMultiply(a, b) {
|
|
273
|
+
return [
|
|
274
|
+
a[3] * b[0] + a[0] * b[3] + a[1] * b[2] - a[2] * b[1],
|
|
275
|
+
a[3] * b[1] - a[0] * b[2] + a[1] * b[3] + a[2] * b[0],
|
|
276
|
+
a[3] * b[2] + a[0] * b[1] - a[1] * b[0] + a[2] * b[3],
|
|
277
|
+
a[3] * b[3] - a[0] * b[0] - a[1] * b[1] - a[2] * b[2],
|
|
278
|
+
];
|
|
279
|
+
}
|
|
280
|
+
function normalizeQuaternion(q) {
|
|
281
|
+
const length = Math.sqrt(q[0] * q[0] + q[1] * q[1] + q[2] * q[2] + q[3] * q[3]);
|
|
282
|
+
if (length === 0)
|
|
283
|
+
return [0, 0, 0, 1];
|
|
284
|
+
return [q[0] / length, q[1] / length, q[2] / length, q[3] / length];
|
|
285
|
+
}
|
|
286
|
+
/** DSX Euler law is Rz*Ry*Rx: X acts on the object first, then Y, then Z. */
|
|
287
|
+
function quaternionFromEuler(rotation) {
|
|
288
|
+
const hx = rotation[0] * SCENE_PHYSICS_DEGREES_TO_RADIANS / 2;
|
|
289
|
+
const hy = rotation[1] * SCENE_PHYSICS_DEGREES_TO_RADIANS / 2;
|
|
290
|
+
const hz = rotation[2] * SCENE_PHYSICS_DEGREES_TO_RADIANS / 2;
|
|
291
|
+
const qx = [Math.sin(hx), 0, 0, Math.cos(hx)];
|
|
292
|
+
const qy = [0, Math.sin(hy), 0, Math.cos(hy)];
|
|
293
|
+
const qz = [0, 0, Math.sin(hz), Math.cos(hz)];
|
|
294
|
+
return normalizeQuaternion(quaternionMultiply(quaternionMultiply(qz, qy), qx));
|
|
295
|
+
}
|
|
296
|
+
function quaternionAxes(q) {
|
|
297
|
+
const [x, y, z, w] = q;
|
|
298
|
+
return [
|
|
299
|
+
[1 - 2 * (y * y + z * z), 2 * (x * y + w * z), 2 * (x * z - w * y)],
|
|
300
|
+
[2 * (x * y - w * z), 1 - 2 * (x * x + z * z), 2 * (y * z + w * x)],
|
|
301
|
+
[2 * (x * z + w * y), 2 * (y * z - w * x), 1 - 2 * (x * x + y * y)],
|
|
302
|
+
];
|
|
303
|
+
}
|
|
304
|
+
const scenePhysicsAxesCache = new WeakMap();
|
|
305
|
+
const scenePhysicsInertiaCache = new WeakMap();
|
|
306
|
+
function bodyAxes(body) {
|
|
307
|
+
const cached = scenePhysicsAxesCache.get(body), q = body.orientation;
|
|
308
|
+
if (cached !== undefined && cached.orientation[0] === q[0] && cached.orientation[1] === q[1]
|
|
309
|
+
&& cached.orientation[2] === q[2] && cached.orientation[3] === q[3])
|
|
310
|
+
return cached.axes;
|
|
311
|
+
const axes = quaternionAxes(q);
|
|
312
|
+
scenePhysicsAxesCache.set(body, { orientation: [...q], axes });
|
|
313
|
+
return axes;
|
|
314
|
+
}
|
|
315
|
+
function bodyInertiaMatrices(body) {
|
|
316
|
+
const cached = scenePhysicsInertiaCache.get(body), q = body.orientation;
|
|
317
|
+
if (cached !== undefined && cached.orientation[0] === q[0] && cached.orientation[1] === q[1]
|
|
318
|
+
&& cached.orientation[2] === q[2] && cached.orientation[3] === q[3])
|
|
319
|
+
return cached;
|
|
320
|
+
const axes = bodyAxes(body);
|
|
321
|
+
const inverse = [[0, 0, 0], [0, 0, 0], [0, 0, 0]];
|
|
322
|
+
const tensor = [[0, 0, 0], [0, 0, 0], [0, 0, 0]];
|
|
323
|
+
for (let axis = 0; axis < 3; axis += 1) {
|
|
324
|
+
const inv = body.invInertia[axis], inertia = inv > 0 ? 1 / inv : 0;
|
|
325
|
+
for (let row = 0; row < 3; row += 1)
|
|
326
|
+
for (let column = 0; column < 3; column += 1) {
|
|
327
|
+
const projection = axes[axis][row] * axes[axis][column];
|
|
328
|
+
inverse[row][column] += projection * inv;
|
|
329
|
+
tensor[row][column] += projection * inertia;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
const entry = { orientation: [...q], inverse, tensor };
|
|
333
|
+
scenePhysicsInertiaCache.set(body, entry);
|
|
334
|
+
return entry;
|
|
335
|
+
}
|
|
336
|
+
function unwrapDegrees(value, reference) {
|
|
337
|
+
let result = value;
|
|
338
|
+
while (result - reference > 180)
|
|
339
|
+
result -= 360;
|
|
340
|
+
while (result - reference < -180)
|
|
341
|
+
result += 360;
|
|
342
|
+
return result;
|
|
343
|
+
}
|
|
344
|
+
function eulerFromQuaternion(q, previous) {
|
|
345
|
+
const axes = quaternionAxes(q);
|
|
346
|
+
const m00 = axes[0][0], m10 = axes[0][1], m20 = axes[0][2];
|
|
347
|
+
const m11 = axes[1][1], m12 = axes[2][1], m21 = axes[1][2], m22 = axes[2][2];
|
|
348
|
+
const ry = Math.asin(Math.max(-1, Math.min(1, -m20)));
|
|
349
|
+
const cy = Math.cos(ry);
|
|
350
|
+
const rx = Math.abs(cy) > 1e-9 ? Math.atan2(m21, m22) : Math.atan2(-m12, m11);
|
|
351
|
+
const rz = Math.abs(cy) > 1e-9 ? Math.atan2(m10, m00) : 0;
|
|
352
|
+
const principal = [
|
|
353
|
+
unwrapDegrees(rx * SCENE_PHYSICS_RADIANS_TO_DEGREES, previous[0]),
|
|
354
|
+
unwrapDegrees(ry * SCENE_PHYSICS_RADIANS_TO_DEGREES, previous[1]),
|
|
355
|
+
unwrapDegrees(rz * SCENE_PHYSICS_RADIANS_TO_DEGREES, previous[2]),
|
|
356
|
+
];
|
|
357
|
+
const alternateY = ry >= 0 ? Math.PI - ry : -Math.PI - ry;
|
|
358
|
+
const alternate = [
|
|
359
|
+
unwrapDegrees((rx + Math.PI) * SCENE_PHYSICS_RADIANS_TO_DEGREES, previous[0]),
|
|
360
|
+
unwrapDegrees(alternateY * SCENE_PHYSICS_RADIANS_TO_DEGREES, previous[1]),
|
|
361
|
+
unwrapDegrees((rz + Math.PI) * SCENE_PHYSICS_RADIANS_TO_DEGREES, previous[2]),
|
|
362
|
+
];
|
|
363
|
+
const principalDistance = (principal[0] - previous[0]) ** 2
|
|
364
|
+
+ (principal[1] - previous[1]) ** 2 + (principal[2] - previous[2]) ** 2;
|
|
365
|
+
const alternateDistance = (alternate[0] - previous[0]) ** 2
|
|
366
|
+
+ (alternate[1] - previous[1]) ** 2 + (alternate[2] - previous[2]) ** 2;
|
|
367
|
+
return alternateDistance < principalDistance ? alternate : principal;
|
|
368
|
+
}
|
|
369
|
+
function shortestArcAngularVelocity(from, to, dt) {
|
|
370
|
+
const conjugate = [-from[0], -from[1], -from[2], from[3]];
|
|
371
|
+
let delta = normalizeQuaternion(quaternionMultiply(to, conjugate));
|
|
372
|
+
if (delta[3] < 0)
|
|
373
|
+
delta = [-delta[0], -delta[1], -delta[2], -delta[3]];
|
|
374
|
+
const vectorLength = Math.sqrt(delta[0] * delta[0] + delta[1] * delta[1] + delta[2] * delta[2]);
|
|
375
|
+
if (vectorLength < 1e-12)
|
|
376
|
+
return [0, 0, 0];
|
|
377
|
+
const angle = 2 * Math.atan2(vectorLength, Math.max(delta[3], 0));
|
|
378
|
+
const scale = angle / (vectorLength * dt);
|
|
379
|
+
return [delta[0] * scale, delta[1] * scale, delta[2] * scale];
|
|
380
|
+
}
|
|
381
|
+
/** Exponential-map step with world-space angular velocity. */
|
|
382
|
+
function integrateOrientation(body, dt) {
|
|
383
|
+
const speed = length3(body.angularVelocity);
|
|
384
|
+
if (speed === 0)
|
|
385
|
+
return;
|
|
386
|
+
const halfAngle = speed * dt / 2;
|
|
387
|
+
const scale = Math.sin(halfAngle) / speed;
|
|
388
|
+
const delta = [
|
|
389
|
+
body.angularVelocity[0] * scale,
|
|
390
|
+
body.angularVelocity[1] * scale,
|
|
391
|
+
body.angularVelocity[2] * scale,
|
|
392
|
+
Math.cos(halfAngle),
|
|
393
|
+
];
|
|
394
|
+
body.orientation = normalizeQuaternion(quaternionMultiply(delta, body.orientation));
|
|
395
|
+
body.rotation = eulerFromQuaternion(body.orientation, body.rotation);
|
|
396
|
+
}
|
|
397
|
+
function closestPointOnBox(point, box) {
|
|
398
|
+
const axes = bodyAxes(box);
|
|
399
|
+
const d = subtract3(point, box.position);
|
|
400
|
+
const out = [...box.position];
|
|
401
|
+
for (let i = 0; i < 3; i += 1) {
|
|
402
|
+
const amount = Math.min(Math.max(dot3(d, axes[i]), -box.half[i]), box.half[i]);
|
|
403
|
+
out[0] += axes[i][0] * amount;
|
|
404
|
+
out[1] += axes[i][1] * amount;
|
|
405
|
+
out[2] += axes[i][2] * amount;
|
|
406
|
+
}
|
|
407
|
+
return out;
|
|
408
|
+
}
|
|
409
|
+
function localContactAnchor(point, body) {
|
|
410
|
+
const axes = bodyAxes(body), d = subtract3(point, body.position);
|
|
411
|
+
return [dot3(d, axes[0]), dot3(d, axes[1]), dot3(d, axes[2])];
|
|
412
|
+
}
|
|
413
|
+
function layersCollide(a, b) {
|
|
414
|
+
if (a.collides !== null && !a.collides.includes(b.layer))
|
|
415
|
+
return false;
|
|
416
|
+
if (b.collides !== null && !b.collides.includes(a.layer))
|
|
417
|
+
return false;
|
|
418
|
+
return true;
|
|
419
|
+
}
|
|
420
|
+
function boxVertices(box) {
|
|
421
|
+
const axes = bodyAxes(box);
|
|
422
|
+
const vertices = [];
|
|
423
|
+
for (const sx of [-1, 1])
|
|
424
|
+
for (const sy of [-1, 1])
|
|
425
|
+
for (const sz of [-1, 1]) {
|
|
426
|
+
const vertex = [...box.position];
|
|
427
|
+
const signs = [sx, sy, sz];
|
|
428
|
+
for (let i = 0; i < 3; i += 1)
|
|
429
|
+
for (let k = 0; k < 3; k += 1) {
|
|
430
|
+
vertex[k] += axes[i][k] * box.half[i] * signs[i];
|
|
431
|
+
}
|
|
432
|
+
vertices.push(vertex);
|
|
433
|
+
}
|
|
434
|
+
return vertices;
|
|
435
|
+
}
|
|
436
|
+
function pointInsideBox(point, box, margin = 1e-8) {
|
|
437
|
+
const axes = bodyAxes(box);
|
|
438
|
+
const d = subtract3(point, box.position);
|
|
439
|
+
for (let i = 0; i < 3; i += 1)
|
|
440
|
+
if (Math.abs(dot3(d, axes[i])) > box.half[i] + margin)
|
|
441
|
+
return false;
|
|
442
|
+
return true;
|
|
443
|
+
}
|
|
444
|
+
function supportFace(box, direction) {
|
|
445
|
+
const axes = bodyAxes(box);
|
|
446
|
+
let faceAxis = 0, alignment = Math.abs(dot3(axes[0], direction));
|
|
447
|
+
for (let i = 1; i < 3; i += 1) {
|
|
448
|
+
const next = Math.abs(dot3(axes[i], direction));
|
|
449
|
+
if (next > alignment) {
|
|
450
|
+
faceAxis = i;
|
|
451
|
+
alignment = next;
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
const sign = dot3(axes[faceAxis], direction) >= 0 ? 1 : -1;
|
|
455
|
+
const center = [
|
|
456
|
+
box.position[0] + axes[faceAxis][0] * box.half[faceAxis] * sign,
|
|
457
|
+
box.position[1] + axes[faceAxis][1] * box.half[faceAxis] * sign,
|
|
458
|
+
box.position[2] + axes[faceAxis][2] * box.half[faceAxis] * sign,
|
|
459
|
+
];
|
|
460
|
+
const sideAxes = [0, 1, 2].filter((axis) => axis !== faceAxis);
|
|
461
|
+
const corners = [[-1, -1], [1, -1], [1, 1], [-1, 1]];
|
|
462
|
+
const faceFeature = `f${faceAxis}${sign > 0 ? "+" : "-"}`;
|
|
463
|
+
return { feature: faceFeature, points: corners.map(([su, sv]) => {
|
|
464
|
+
const point = [...center];
|
|
465
|
+
for (let k = 0; k < 3; k += 1) {
|
|
466
|
+
point[k] += axes[sideAxes[0]][k] * box.half[sideAxes[0]] * su
|
|
467
|
+
+ axes[sideAxes[1]][k] * box.half[sideAxes[1]] * sv;
|
|
468
|
+
}
|
|
469
|
+
return { point, feature: `${faceFeature}:v${su > 0 ? 1 : 0}${sv > 0 ? 1 : 0}` };
|
|
470
|
+
}) };
|
|
471
|
+
}
|
|
472
|
+
function cross2(a, b, c) {
|
|
473
|
+
return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
|
|
474
|
+
}
|
|
475
|
+
function polygonArea2(points) {
|
|
476
|
+
let area = 0;
|
|
477
|
+
for (let i = 0; i < points.length; i += 1) {
|
|
478
|
+
const a = points[i], b = points[(i + 1) % points.length];
|
|
479
|
+
area += a.x * b.y - a.y * b.x;
|
|
480
|
+
}
|
|
481
|
+
return area;
|
|
482
|
+
}
|
|
483
|
+
function clipPolygon(subject, clip, clipFeature) {
|
|
484
|
+
let output = subject;
|
|
485
|
+
for (let edgeIndex = 0; edgeIndex < clip.length; edgeIndex += 1) {
|
|
486
|
+
const edgeA = clip[edgeIndex], edgeB = clip[(edgeIndex + 1) % clip.length];
|
|
487
|
+
const input = output;
|
|
488
|
+
output = [];
|
|
489
|
+
if (input.length === 0)
|
|
490
|
+
break;
|
|
491
|
+
let start = input[input.length - 1];
|
|
492
|
+
let startDistance = cross2(edgeA, edgeB, start);
|
|
493
|
+
for (const end of input) {
|
|
494
|
+
const endDistance = cross2(edgeA, edgeB, end);
|
|
495
|
+
const startInside = startDistance >= -1e-9, endInside = endDistance >= -1e-9;
|
|
496
|
+
if (endInside !== startInside) {
|
|
497
|
+
const denominator = startDistance - endDistance;
|
|
498
|
+
const t = denominator === 0 ? 0 : startDistance / denominator;
|
|
499
|
+
const edge = [start.feature, end.feature].sort().join("|");
|
|
500
|
+
output.push({
|
|
501
|
+
x: start.x + (end.x - start.x) * t,
|
|
502
|
+
y: start.y + (end.y - start.y) * t,
|
|
503
|
+
feature: `i(${edge};${clipFeature}:e${edgeIndex})`,
|
|
504
|
+
});
|
|
505
|
+
}
|
|
506
|
+
if (endInside)
|
|
507
|
+
output.push(end);
|
|
508
|
+
start = end;
|
|
509
|
+
startDistance = endDistance;
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
return output;
|
|
513
|
+
}
|
|
514
|
+
function clippedFaceManifold(a, b, normal, plane) {
|
|
515
|
+
const absNormal = [Math.abs(normal[0]), Math.abs(normal[1]), Math.abs(normal[2])];
|
|
516
|
+
const seed = absNormal[0] <= absNormal[1] && absNormal[0] <= absNormal[2]
|
|
517
|
+
? [1, 0, 0] : absNormal[1] <= absNormal[2] ? [0, 1, 0] : [0, 0, 1];
|
|
518
|
+
const rawU = cross3(seed, normal), ul = length3(rawU);
|
|
519
|
+
const u = [rawU[0] / ul, rawU[1] / ul, rawU[2] / ul];
|
|
520
|
+
const v = cross3(normal, u);
|
|
521
|
+
const faceA = supportFace(a, normal), faceB = supportFace(b, [-normal[0], -normal[1], -normal[2]]);
|
|
522
|
+
const project = (entry) => ({
|
|
523
|
+
x: dot3(entry.point, u), y: dot3(entry.point, v), feature: entry.feature,
|
|
524
|
+
});
|
|
525
|
+
let polygonA = faceA.points.map(project);
|
|
526
|
+
let polygonB = faceB.points.map(project);
|
|
527
|
+
if (polygonArea2(polygonA) < 0)
|
|
528
|
+
polygonA = [...polygonA].reverse();
|
|
529
|
+
if (polygonArea2(polygonB) < 0)
|
|
530
|
+
polygonB = [...polygonB].reverse();
|
|
531
|
+
let clipped = clipPolygon(polygonA, polygonB, faceB.feature);
|
|
532
|
+
if (clipped.length === 0)
|
|
533
|
+
return null;
|
|
534
|
+
clipped = clipped.filter((point, index) => !clipped.slice(0, index).some((prior) => {
|
|
535
|
+
const dx = prior.x - point.x, dy = prior.y - point.y;
|
|
536
|
+
return dx * dx + dy * dy <= 1e-14;
|
|
537
|
+
}));
|
|
538
|
+
clipped.sort((left, right) => left.x === right.x ? left.y - right.y : left.x - right.x);
|
|
539
|
+
let entries = clipped.map((entry) => ({
|
|
540
|
+
point: [u[0] * entry.x + v[0] * entry.y + normal[0] * plane,
|
|
541
|
+
u[1] * entry.x + v[1] * entry.y + normal[1] * plane,
|
|
542
|
+
u[2] * entry.x + v[2] * entry.y + normal[2] * plane],
|
|
543
|
+
feature: `${faceA.feature}|${faceB.feature}|${entry.feature}`,
|
|
544
|
+
}));
|
|
545
|
+
if (entries.length > 4) {
|
|
546
|
+
const selected = [entries[0]];
|
|
547
|
+
while (selected.length < 4) {
|
|
548
|
+
let best = entries.find((entry) => !selected.includes(entry));
|
|
549
|
+
let bestDistance = -Infinity;
|
|
550
|
+
for (const entry of entries) {
|
|
551
|
+
if (selected.includes(entry))
|
|
552
|
+
continue;
|
|
553
|
+
let nearest = Infinity;
|
|
554
|
+
for (const chosen of selected) {
|
|
555
|
+
const delta = subtract3(entry.point, chosen.point);
|
|
556
|
+
nearest = Math.min(nearest, dot3(delta, delta));
|
|
557
|
+
}
|
|
558
|
+
if (nearest > bestDistance) {
|
|
559
|
+
best = entry;
|
|
560
|
+
bestDistance = nearest;
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
selected.push(best);
|
|
564
|
+
}
|
|
565
|
+
entries = selected;
|
|
566
|
+
}
|
|
567
|
+
return entries;
|
|
568
|
+
}
|
|
569
|
+
function supportEdge(box, edgeAxis, direction, maximum) {
|
|
570
|
+
const axes = bodyAxes(box);
|
|
571
|
+
const center = [...box.position];
|
|
572
|
+
const signs = [0, 0, 0];
|
|
573
|
+
for (let axis = 0; axis < 3; axis += 1) {
|
|
574
|
+
if (axis === edgeAxis)
|
|
575
|
+
continue;
|
|
576
|
+
const projection = dot3(axes[axis], direction);
|
|
577
|
+
const sign = (maximum ? projection >= 0 : projection < 0) ? 1 : -1;
|
|
578
|
+
signs[axis] = sign;
|
|
579
|
+
for (let k = 0; k < 3; k += 1)
|
|
580
|
+
center[k] += axes[axis][k] * box.half[axis] * sign;
|
|
581
|
+
}
|
|
582
|
+
const along = axes[edgeAxis], half = box.half[edgeAxis];
|
|
583
|
+
return {
|
|
584
|
+
start: [center[0] - along[0] * half, center[1] - along[1] * half, center[2] - along[2] * half],
|
|
585
|
+
end: [center[0] + along[0] * half, center[1] + along[1] * half, center[2] + along[2] * half],
|
|
586
|
+
feature: `e${edgeAxis}:${signs.map((sign, axis) => axis === edgeAxis ? "x" : sign > 0 ? "+" : "-").join("")}`,
|
|
587
|
+
};
|
|
588
|
+
}
|
|
589
|
+
function closestSegmentPoints(a0, a1, b0, b1) {
|
|
590
|
+
const d1 = subtract3(a1, a0), d2 = subtract3(b1, b0), r = subtract3(a0, b0);
|
|
591
|
+
const aa = dot3(d1, d1), ee = dot3(d2, d2), f = dot3(d2, r);
|
|
592
|
+
let s = 0, t = 0;
|
|
593
|
+
if (aa <= 1e-12 && ee <= 1e-12)
|
|
594
|
+
return [a0, b0];
|
|
595
|
+
if (aa <= 1e-12)
|
|
596
|
+
t = Math.min(Math.max(f / ee, 0), 1);
|
|
597
|
+
else {
|
|
598
|
+
const c = dot3(d1, r);
|
|
599
|
+
if (ee <= 1e-12)
|
|
600
|
+
s = Math.min(Math.max(-c / aa, 0), 1);
|
|
601
|
+
else {
|
|
602
|
+
const b = dot3(d1, d2), denominator = aa * ee - b * b;
|
|
603
|
+
if (Math.abs(denominator) > 1e-12)
|
|
604
|
+
s = Math.min(Math.max((b * f - c * ee) / denominator, 0), 1);
|
|
605
|
+
t = (b * s + f) / ee;
|
|
606
|
+
if (t < 0) {
|
|
607
|
+
t = 0;
|
|
608
|
+
s = Math.min(Math.max(-c / aa, 0), 1);
|
|
609
|
+
}
|
|
610
|
+
else if (t > 1) {
|
|
611
|
+
t = 1;
|
|
612
|
+
s = Math.min(Math.max((b - c) / aa, 0), 1);
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
return [
|
|
617
|
+
[a0[0] + d1[0] * s, a0[1] + d1[1] * s, a0[2] + d1[2] * s],
|
|
618
|
+
[b0[0] + d2[0] * t, b0[1] + d2[1] * t, b0[2] + d2[2] * t],
|
|
619
|
+
];
|
|
620
|
+
}
|
|
621
|
+
function boxManifoldPoints(a, b, normal, depth, winner) {
|
|
622
|
+
if (winner.kind === "edge") {
|
|
623
|
+
const edgeA = supportEdge(a, winner.axisA, normal, true);
|
|
624
|
+
const edgeB = supportEdge(b, winner.axisB, normal, false);
|
|
625
|
+
const [pointA, pointB] = closestSegmentPoints(edgeA.start, edgeA.end, edgeB.start, edgeB.end);
|
|
626
|
+
return [{
|
|
627
|
+
point: [(pointA[0] + pointB[0]) / 2, (pointA[1] + pointB[1]) / 2, (pointA[2] + pointB[2]) / 2],
|
|
628
|
+
feature: `${edgeA.feature}|${edgeB.feature}`,
|
|
629
|
+
}];
|
|
630
|
+
}
|
|
631
|
+
const verticesA = boxVertices(a), verticesB = boxVertices(b);
|
|
632
|
+
let supportA = -Infinity, supportB = Infinity;
|
|
633
|
+
for (const v of verticesA)
|
|
634
|
+
supportA = Math.max(supportA, dot3(v, normal));
|
|
635
|
+
for (const v of verticesB)
|
|
636
|
+
supportB = Math.min(supportB, dot3(v, normal));
|
|
637
|
+
const plane = (supportA + supportB) / 2;
|
|
638
|
+
const clipped = clippedFaceManifold(a, b, normal, plane);
|
|
639
|
+
if (clipped !== null)
|
|
640
|
+
return clipped;
|
|
641
|
+
const candidates = [];
|
|
642
|
+
verticesA.forEach((point, index) => {
|
|
643
|
+
if (supportA - dot3(point, normal) <= depth + SCENE_PHYSICS_SLOP
|
|
644
|
+
&& pointInsideBox(point, b, depth + SCENE_PHYSICS_SLOP)) {
|
|
645
|
+
candidates.push({ point, feature: `a${index}` });
|
|
646
|
+
}
|
|
647
|
+
});
|
|
648
|
+
verticesB.forEach((point, index) => {
|
|
649
|
+
if (dot3(point, normal) - supportB <= depth + SCENE_PHYSICS_SLOP
|
|
650
|
+
&& pointInsideBox(point, a, depth + SCENE_PHYSICS_SLOP)) {
|
|
651
|
+
candidates.push({ point, feature: `b${index}` });
|
|
652
|
+
}
|
|
653
|
+
});
|
|
654
|
+
const points = [];
|
|
655
|
+
for (const candidate of candidates) {
|
|
656
|
+
const distance = dot3(candidate.point, normal) - plane;
|
|
657
|
+
const point = [
|
|
658
|
+
candidate.point[0] - normal[0] * distance,
|
|
659
|
+
candidate.point[1] - normal[1] * distance,
|
|
660
|
+
candidate.point[2] - normal[2] * distance,
|
|
661
|
+
];
|
|
662
|
+
if (!points.some((p) => length3(subtract3(p.point, point)) <= 1e-7)) {
|
|
663
|
+
points.push({ point, feature: candidate.feature });
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
if (points.length > 4) {
|
|
667
|
+
const selected = [points[0]];
|
|
668
|
+
while (selected.length < 4) {
|
|
669
|
+
let best = null;
|
|
670
|
+
for (const entry of points) {
|
|
671
|
+
if (selected.includes(entry))
|
|
672
|
+
continue;
|
|
673
|
+
let nearest = Infinity;
|
|
674
|
+
for (const chosen of selected) {
|
|
675
|
+
const delta = subtract3(entry.point, chosen.point);
|
|
676
|
+
nearest = Math.min(nearest, dot3(delta, delta));
|
|
677
|
+
}
|
|
678
|
+
if (best === null || nearest > best.distance)
|
|
679
|
+
best = { entry, distance: nearest };
|
|
680
|
+
}
|
|
681
|
+
selected.push(best.entry);
|
|
682
|
+
}
|
|
683
|
+
return selected;
|
|
684
|
+
}
|
|
685
|
+
if (points.length > 0)
|
|
686
|
+
return points;
|
|
687
|
+
const onA = closestPointOnBox(b.position, a), onB = closestPointOnBox(a.position, b);
|
|
688
|
+
return [{ point: [(onA[0] + onB[0]) / 2, (onA[1] + onB[1]) / 2, (onA[2] + onB[2]) / 2], feature: "fallback" }];
|
|
689
|
+
}
|
|
690
|
+
/** depth per the P5 collide corpus; normal points FROM a TOWARD b. Ties take the
|
|
691
|
+
* smallest axis index; axis sign is sign(centerB − centerA) with ties +1. */
|
|
692
|
+
export function scenePhysicsContact(a, b) {
|
|
693
|
+
if (a.shape === "sphere" && b.shape === "sphere") {
|
|
694
|
+
const d = [
|
|
695
|
+
b.position[0] - a.position[0], b.position[1] - a.position[1], b.position[2] - a.position[2],
|
|
696
|
+
];
|
|
697
|
+
const dist = length3(d);
|
|
698
|
+
const depth = a.radius + b.radius - dist;
|
|
699
|
+
if (depth <= 0)
|
|
700
|
+
return null;
|
|
701
|
+
const normal = dist === 0 ? [0, 1, 0] : [d[0] / dist, d[1] / dist, d[2] / dist];
|
|
702
|
+
const point = [
|
|
703
|
+
(a.position[0] + normal[0] * a.radius + b.position[0] - normal[0] * b.radius) / 2,
|
|
704
|
+
(a.position[1] + normal[1] * a.radius + b.position[1] - normal[1] * b.radius) / 2,
|
|
705
|
+
(a.position[2] + normal[2] * a.radius + b.position[2] - normal[2] * b.radius) / 2,
|
|
706
|
+
];
|
|
707
|
+
return { depth, normal, point, points: [point], features: ["sphere"] };
|
|
708
|
+
}
|
|
709
|
+
if (a.shape === "box" && b.shape === "box") {
|
|
710
|
+
const axesA = bodyAxes(a);
|
|
711
|
+
const axesB = bodyAxes(b);
|
|
712
|
+
const delta = subtract3(b.position, a.position);
|
|
713
|
+
let depth = Infinity;
|
|
714
|
+
let normal = [0, 0, 0];
|
|
715
|
+
const candidates = [
|
|
716
|
+
...axesA.map((axis, index) => ({ axis, winner: { kind: "faceA", axis: index } })),
|
|
717
|
+
...axesB.map((axis, index) => ({ axis, winner: { kind: "faceB", axis: index } })),
|
|
718
|
+
];
|
|
719
|
+
for (let axisAIndex = 0; axisAIndex < axesA.length; axisAIndex += 1) {
|
|
720
|
+
for (let axisBIndex = 0; axisBIndex < axesB.length; axisBIndex += 1) {
|
|
721
|
+
const axisA = axesA[axisAIndex], axisB = axesB[axisBIndex];
|
|
722
|
+
const axis = cross3(axisA, axisB);
|
|
723
|
+
const length = length3(axis);
|
|
724
|
+
if (length > 1e-9)
|
|
725
|
+
candidates.push({
|
|
726
|
+
axis: [axis[0] / length, axis[1] / length, axis[2] / length],
|
|
727
|
+
winner: { kind: "edge", axisA: axisAIndex, axisB: axisBIndex },
|
|
728
|
+
});
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
let winner = { kind: "faceA", axis: 0 };
|
|
732
|
+
for (const candidate of candidates) {
|
|
733
|
+
const axis = candidate.axis;
|
|
734
|
+
let radiusA = 0, radiusB = 0;
|
|
735
|
+
for (let i = 0; i < 3; i += 1) {
|
|
736
|
+
radiusA += a.half[i] * Math.abs(dot3(axis, axesA[i]));
|
|
737
|
+
radiusB += b.half[i] * Math.abs(dot3(axis, axesB[i]));
|
|
738
|
+
}
|
|
739
|
+
const distance = dot3(delta, axis);
|
|
740
|
+
const overlap = radiusA + radiusB - Math.abs(distance);
|
|
741
|
+
if (overlap <= 0)
|
|
742
|
+
return null;
|
|
743
|
+
if (overlap < depth) {
|
|
744
|
+
depth = overlap;
|
|
745
|
+
normal = distance >= 0 ? [...axis] : [-axis[0], -axis[1], -axis[2]];
|
|
746
|
+
winner = candidate.winner;
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
const manifold = boxManifoldPoints(a, b, normal, depth, winner);
|
|
750
|
+
const points = manifold.map((entry) => entry.point);
|
|
751
|
+
return { depth, normal, point: points[0], points, features: manifold.map((entry) => entry.feature) };
|
|
752
|
+
}
|
|
753
|
+
const sphere = a.shape === "sphere" ? a : b;
|
|
754
|
+
const box = a.shape === "box" ? a : b;
|
|
755
|
+
const axes = bodyAxes(box);
|
|
756
|
+
const local = subtract3(sphere.position, box.position);
|
|
757
|
+
const qLocal = [0, 0, 0];
|
|
758
|
+
for (let i = 0; i < 3; i += 1) {
|
|
759
|
+
qLocal[i] = Math.min(Math.max(dot3(local, axes[i]), -box.half[i]), box.half[i]);
|
|
760
|
+
}
|
|
761
|
+
let q = [...box.position];
|
|
762
|
+
for (let i = 0; i < 3; i += 1)
|
|
763
|
+
for (let k = 0; k < 3; k += 1)
|
|
764
|
+
q[k] += axes[i][k] * qLocal[i];
|
|
765
|
+
const d = [sphere.position[0] - q[0], sphere.position[1] - q[1], sphere.position[2] - q[2]];
|
|
766
|
+
const dist = length3(d);
|
|
767
|
+
let depth;
|
|
768
|
+
let towardSphere;
|
|
769
|
+
if (dist > 0) {
|
|
770
|
+
depth = sphere.radius - dist;
|
|
771
|
+
if (depth <= 0)
|
|
772
|
+
return null;
|
|
773
|
+
towardSphere = [d[0] / dist, d[1] / dist, d[2] / dist];
|
|
774
|
+
}
|
|
775
|
+
else {
|
|
776
|
+
let inside = Infinity;
|
|
777
|
+
let axis = -1;
|
|
778
|
+
for (let i = 0; i < 3; i += 1) {
|
|
779
|
+
const toFace = box.half[i] - Math.abs(dot3(local, axes[i]));
|
|
780
|
+
if (toFace < inside) {
|
|
781
|
+
inside = toFace;
|
|
782
|
+
axis = i;
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
depth = sphere.radius + inside;
|
|
786
|
+
const sign = dot3(local, axes[axis]) >= 0 ? 1 : -1;
|
|
787
|
+
towardSphere = [axes[axis][0] * sign, axes[axis][1] * sign, axes[axis][2] * sign];
|
|
788
|
+
qLocal[axis] = box.half[axis] * sign;
|
|
789
|
+
q = [...box.position];
|
|
790
|
+
for (let i = 0; i < 3; i += 1)
|
|
791
|
+
for (let k = 0; k < 3; k += 1)
|
|
792
|
+
q[k] += axes[i][k] * qLocal[i];
|
|
793
|
+
}
|
|
794
|
+
return a.shape === "sphere"
|
|
795
|
+
? { depth, normal: [-towardSphere[0], -towardSphere[1], -towardSphere[2]], point: q, points: [q], features: ["sphere-box"] }
|
|
796
|
+
: { depth, normal: towardSphere, point: q, points: [q], features: ["sphere-box"] };
|
|
797
|
+
}
|
|
798
|
+
function inverseInertiaWorld(body, value) {
|
|
799
|
+
if (body.mode2d)
|
|
800
|
+
return [0, 0, value[2] * body.invInertia[2]];
|
|
801
|
+
const matrix = bodyInertiaMatrices(body).inverse;
|
|
802
|
+
return matrix.map((row) => dot3(row, value));
|
|
803
|
+
}
|
|
804
|
+
function inertiaWorld(body, value) {
|
|
805
|
+
if (body.mode2d) {
|
|
806
|
+
const inertia = body.invInertia[2] > 0 ? 1 / body.invInertia[2] : 0;
|
|
807
|
+
return [0, 0, value[2] * inertia];
|
|
808
|
+
}
|
|
809
|
+
const matrix = bodyInertiaMatrices(body).tensor;
|
|
810
|
+
return matrix.map((row) => dot3(row, value));
|
|
811
|
+
}
|
|
812
|
+
function pointVelocity(body, r) {
|
|
813
|
+
const angular = cross3(body.angularVelocity, r);
|
|
814
|
+
return [body.velocity[0] + angular[0], body.velocity[1] + angular[1], body.velocity[2] + angular[2]];
|
|
815
|
+
}
|
|
816
|
+
function impulseDenominator(body, r, direction, effectiveInvMass = body.invMass) {
|
|
817
|
+
// Sleeping dynamics participate as immutable supports. Their stored mass and
|
|
818
|
+
// inertia remain intact for the eventual wake, but neither contributes while
|
|
819
|
+
// the constraint treats the body as solver-static.
|
|
820
|
+
if (effectiveInvMass === 0)
|
|
821
|
+
return 0;
|
|
822
|
+
const rxn = cross3(r, direction);
|
|
823
|
+
return effectiveInvMass + dot3(direction, cross3(inverseInertiaWorld(body, rxn), r));
|
|
824
|
+
}
|
|
825
|
+
function applyImpulse(body, r, impulse, sign, effectiveInvMass = body.invMass) {
|
|
826
|
+
if (effectiveInvMass === 0)
|
|
827
|
+
return;
|
|
828
|
+
for (let i = 0; i < 3; i += 1)
|
|
829
|
+
body.velocity[i] += impulse[i] * effectiveInvMass * sign;
|
|
830
|
+
const angular = inverseInertiaWorld(body, cross3(r, impulse));
|
|
831
|
+
for (let i = 0; i < 3; i += 1)
|
|
832
|
+
body.angularVelocity[i] += angular[i] * sign;
|
|
833
|
+
}
|
|
834
|
+
// ── the step (THE STEP ORDER — pinned in the corpus _note, mirrored verbatim) ────────
|
|
835
|
+
function pairKey(a, b) {
|
|
836
|
+
return `${a}\u0000${b}`;
|
|
837
|
+
}
|
|
838
|
+
export function stepScenePhysicsWorld(world, intents) {
|
|
839
|
+
const g = world.gravity;
|
|
840
|
+
const dt = SCENE_PHYSICS_DT;
|
|
841
|
+
// 1 ── kinematic drive: position from the authored/bound plane, velocity derived
|
|
842
|
+
for (const b of world.bodies) {
|
|
843
|
+
if (b.kind !== "kinematic")
|
|
844
|
+
continue;
|
|
845
|
+
b.previous = [...b.position];
|
|
846
|
+
b.previousRotation = [...b.rotation];
|
|
847
|
+
const p = intents?.[b.id]?.position;
|
|
848
|
+
if (p !== undefined) {
|
|
849
|
+
if (p[0] !== b.position[0] || p[1] !== b.position[1] || p[2] !== b.position[2]) {
|
|
850
|
+
invalidateContactCache(world, b.id);
|
|
851
|
+
}
|
|
852
|
+
b.velocity = [
|
|
853
|
+
(p[0] - b.position[0]) / dt, (p[1] - b.position[1]) / dt, (p[2] - b.position[2]) / dt,
|
|
854
|
+
];
|
|
855
|
+
b.position = [...p];
|
|
856
|
+
}
|
|
857
|
+
else {
|
|
858
|
+
b.velocity = [0, 0, 0];
|
|
859
|
+
}
|
|
860
|
+
const rotation = intents?.[b.id]?.rotation;
|
|
861
|
+
if (rotation !== undefined) {
|
|
862
|
+
if (rotation[0] !== b.rotation[0] || rotation[1] !== b.rotation[1] || rotation[2] !== b.rotation[2]) {
|
|
863
|
+
invalidateContactCache(world, b.id);
|
|
864
|
+
}
|
|
865
|
+
const targetOrientation = quaternionFromEuler(rotation);
|
|
866
|
+
b.angularVelocity = shortestArcAngularVelocity(b.orientation, targetOrientation, dt);
|
|
867
|
+
b.rotation = [...rotation];
|
|
868
|
+
b.orientation = targetOrientation;
|
|
869
|
+
}
|
|
870
|
+
else {
|
|
871
|
+
b.angularVelocity = [0, 0, 0];
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
if (world.mode2d) {
|
|
875
|
+
for (const b of world.bodies) {
|
|
876
|
+
b.angularVelocity[0] = 0;
|
|
877
|
+
b.angularVelocity[1] = 0;
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
// 2 ── character intent · 3 ── integrate (semi-implicit Euler: v += g·dt, x += v·dt)
|
|
881
|
+
for (const b of world.bodies) {
|
|
882
|
+
if (b.kind === "static" || b.kind === "kinematic" || b.sleeping)
|
|
883
|
+
continue;
|
|
884
|
+
if (b.kind === "character") {
|
|
885
|
+
const move = intents?.[b.id]?.move;
|
|
886
|
+
let mx = move === undefined ? 0 : move[0];
|
|
887
|
+
let mz = move === undefined ? 0 : move[1];
|
|
888
|
+
const ln = Math.sqrt(mx * mx + mz * mz);
|
|
889
|
+
if (ln > 1) {
|
|
890
|
+
mx /= ln;
|
|
891
|
+
mz /= ln;
|
|
892
|
+
}
|
|
893
|
+
b.velocity[0] = mx * b.speed;
|
|
894
|
+
b.velocity[2] = mz * b.speed;
|
|
895
|
+
}
|
|
896
|
+
b.previous = [...b.position];
|
|
897
|
+
b.previousRotation = [...b.rotation];
|
|
898
|
+
b.velocity[0] += g[0] * dt;
|
|
899
|
+
b.velocity[1] += g[1] * dt;
|
|
900
|
+
b.velocity[2] += g[2] * dt;
|
|
901
|
+
b.position[0] += b.velocity[0] * dt;
|
|
902
|
+
b.position[1] += b.velocity[1] * dt;
|
|
903
|
+
b.position[2] += b.velocity[2] * dt;
|
|
904
|
+
if (b.kind === "dynamic") {
|
|
905
|
+
const drag = Math.max(0, 1 - b.angularDamping * dt);
|
|
906
|
+
const angularMomentum = inertiaWorld(b, b.angularVelocity);
|
|
907
|
+
for (let k = 0; k < 3; k += 1) {
|
|
908
|
+
angularMomentum[k] = (angularMomentum[k] + b.torque[k] * dt) * drag;
|
|
909
|
+
}
|
|
910
|
+
b.angularVelocity = inverseInertiaWorld(b, angularMomentum);
|
|
911
|
+
integrateOrientation(b, dt);
|
|
912
|
+
// Torque-free anisotropic bodies keep WORLD angular momentum as their inertia
|
|
913
|
+
// tensor rotates; recompute omega against the new orientation after the step.
|
|
914
|
+
b.angularVelocity = inverseInertiaWorld(b, angularMomentum);
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
// 4 ── solid pass: pairs in document order (i < j)
|
|
918
|
+
const solidNow = [];
|
|
919
|
+
const bodies = world.bodies;
|
|
920
|
+
const solverContacts = [];
|
|
921
|
+
for (let i = 0; i < bodies.length; i += 1) {
|
|
922
|
+
for (let j = i + 1; j < bodies.length; j += 1) {
|
|
923
|
+
const a = bodies[i], b = bodies[j];
|
|
924
|
+
if (a.trigger || b.trigger)
|
|
925
|
+
continue;
|
|
926
|
+
const aStill = a.kind === "static" || a.kind === "kinematic";
|
|
927
|
+
const bStill = b.kind === "static" || b.kind === "kinematic";
|
|
928
|
+
if (aStill && bStill)
|
|
929
|
+
continue;
|
|
930
|
+
if (a.kind === "character" && b.kind === "character")
|
|
931
|
+
continue; // named non-interaction
|
|
932
|
+
if ((a.kind === "character" && bStill) || (b.kind === "character" && aStill))
|
|
933
|
+
continue; // pass 5
|
|
934
|
+
if (!layersCollide(a, b))
|
|
935
|
+
continue;
|
|
936
|
+
const c = scenePhysicsContact(a, b);
|
|
937
|
+
if (c === null)
|
|
938
|
+
continue;
|
|
939
|
+
const { depth } = c;
|
|
940
|
+
let n = [...c.normal];
|
|
941
|
+
if (world.mode2d) {
|
|
942
|
+
const planarLength = Math.sqrt(n[0] * n[0] + n[1] * n[1]);
|
|
943
|
+
if (planarLength <= 1e-12)
|
|
944
|
+
continue;
|
|
945
|
+
n = [n[0] / planarLength, n[1] / planarLength, 0];
|
|
946
|
+
}
|
|
947
|
+
solidNow.push([a.id, b.id]);
|
|
948
|
+
// the wake law
|
|
949
|
+
for (const [s, o] of [[a, b], [b, a]]) {
|
|
950
|
+
if (s.kind === "dynamic" && s.sleeping) {
|
|
951
|
+
// energetic contact ONLY: a kinematic wakes a sleeper when it is MOVING
|
|
952
|
+
// (its velocity derives from the resolved plane) — a stationary platform
|
|
953
|
+
// must let the world sleep, or the frame loop never stops
|
|
954
|
+
const surfaceVelocity = pointVelocity(o, subtract3(c.point, o.position));
|
|
955
|
+
const surfaceSpeed = world.mode2d
|
|
956
|
+
? Math.sqrt(surfaceVelocity[0] * surfaceVelocity[0] + surfaceVelocity[1] * surfaceVelocity[1])
|
|
957
|
+
: length3(surfaceVelocity);
|
|
958
|
+
// An awake dynamic already accumulating quiet sleep ticks is a resting
|
|
959
|
+
// neighbour, not an impactor. Its pre-solve velocity includes one gravity
|
|
960
|
+
// step (~0.1635 m/s at 60 Hz), which must not make staggered stack sleepers
|
|
961
|
+
// wake each other forever. Explicit writes/real impacts reset sleepCount to 0.
|
|
962
|
+
const energeticDynamic = o.kind === "dynamic" && !o.sleeping && o.sleepCount === 0;
|
|
963
|
+
if (o.kind === "character" ||
|
|
964
|
+
((o.kind === "kinematic" || energeticDynamic)
|
|
965
|
+
&& surfaceSpeed > SCENE_PHYSICS_SLEEP_SPEED)) {
|
|
966
|
+
s.sleeping = false;
|
|
967
|
+
s.sleepCount = 0;
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
// A sleeper remains a solid support with zero effective mass. Dropping the pair
|
|
972
|
+
// entirely makes a body resting on a just-slept neighbour lose its floor for one
|
|
973
|
+
// tick, repeatedly resetting staggered stack sleep. Energetic surface motion above
|
|
974
|
+
// already wakes the sleeper before these masses are selected.
|
|
975
|
+
const invA = a.kind === "character" || (a.kind === "dynamic" && a.sleeping) ? 0 : a.invMass;
|
|
976
|
+
const invB = b.kind === "character" || (b.kind === "dynamic" && b.sleeping) ? 0 : b.invMass;
|
|
977
|
+
const invSum = invA + invB;
|
|
978
|
+
if (invSum === 0)
|
|
979
|
+
continue;
|
|
980
|
+
const cache = contactCache(world);
|
|
981
|
+
const constraints = c.points.map((point, pointIndex) => {
|
|
982
|
+
const key = `${pairKey(a.id, b.id)}\u0000${c.features[pointIndex]}`;
|
|
983
|
+
const localAnchorA = localContactAnchor(point, a);
|
|
984
|
+
const localAnchorB = localContactAnchor(point, b);
|
|
985
|
+
let cached = cache.get(key);
|
|
986
|
+
if (cached === undefined) {
|
|
987
|
+
cached = { a: a.id, b: b.id, normal: 0, normalAxis: [...n],
|
|
988
|
+
tangent: [0, 0, 0], localAnchorA, localAnchorB, lastTick: world.tick };
|
|
989
|
+
cache.set(key, cached);
|
|
990
|
+
}
|
|
991
|
+
else if (dot3(cached.normalAxis, n) < 0.95
|
|
992
|
+
|| length3(subtract3(cached.localAnchorA, localAnchorA)) > 0.02
|
|
993
|
+
|| length3(subtract3(cached.localAnchorB, localAnchorB)) > 0.02) {
|
|
994
|
+
cached.normal = 0;
|
|
995
|
+
cached.tangent = [0, 0, 0];
|
|
996
|
+
}
|
|
997
|
+
cached.normalAxis = [...n];
|
|
998
|
+
cached.localAnchorA = localAnchorA;
|
|
999
|
+
cached.localAnchorB = localAnchorB;
|
|
1000
|
+
cached.lastTick = world.tick;
|
|
1001
|
+
const rA = subtract3(point, a.position), rB = subtract3(point, b.position);
|
|
1002
|
+
const approach = dot3(subtract3(pointVelocity(b, rB), pointVelocity(a, rA)), n);
|
|
1003
|
+
const restitution = -approach > SCENE_PHYSICS_RESTITUTION_MIN_SPEED
|
|
1004
|
+
? Math.max(a.bounce, b.bounce) : 0;
|
|
1005
|
+
return { rA, rB, cached, target: approach < 0 ? -restitution * approach : 0,
|
|
1006
|
+
normalDenominator: impulseDenominator(a, rA, n, invA)
|
|
1007
|
+
+ impulseDenominator(b, rB, n, invB),
|
|
1008
|
+
tangentAxis: [0, 0, 0], tangentDenominator: 0 };
|
|
1009
|
+
});
|
|
1010
|
+
solverContacts.push({ a, b, depth, n, invA, invB, invSum, constraints });
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
// Warm start every contact before the GLOBAL fixed-iteration fold. Global ordering
|
|
1014
|
+
// is what lets a stack transmit support impulses through more than one body/tick.
|
|
1015
|
+
for (const solver of solverContacts) {
|
|
1016
|
+
const { a, b, n, invA, invB } = solver;
|
|
1017
|
+
for (const constraint of solver.constraints) {
|
|
1018
|
+
const tangentNormal = dot3(constraint.cached.tangent, n);
|
|
1019
|
+
constraint.cached.tangent = [
|
|
1020
|
+
constraint.cached.tangent[0] - n[0] * tangentNormal,
|
|
1021
|
+
constraint.cached.tangent[1] - n[1] * tangentNormal,
|
|
1022
|
+
constraint.cached.tangent[2] - n[2] * tangentNormal,
|
|
1023
|
+
];
|
|
1024
|
+
const warm = [
|
|
1025
|
+
n[0] * constraint.cached.normal + constraint.cached.tangent[0],
|
|
1026
|
+
n[1] * constraint.cached.normal + constraint.cached.tangent[1],
|
|
1027
|
+
n[2] * constraint.cached.normal + constraint.cached.tangent[2],
|
|
1028
|
+
];
|
|
1029
|
+
applyImpulse(a, constraint.rA, warm, -1, invA);
|
|
1030
|
+
applyImpulse(b, constraint.rB, warm, 1, invB);
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
for (let manifoldIteration = 0; manifoldIteration < SCENE_PHYSICS_MANIFOLD_ITERATIONS; manifoldIteration += 1) {
|
|
1034
|
+
for (const solver of solverContacts) {
|
|
1035
|
+
const { a, b, n, invA, invB } = solver;
|
|
1036
|
+
for (const constraint of solver.constraints) {
|
|
1037
|
+
const { rA, rB, cached } = constraint;
|
|
1038
|
+
const rel = dot3(subtract3(pointVelocity(b, rB), pointVelocity(a, rA)), n);
|
|
1039
|
+
const normalDenominator = constraint.normalDenominator;
|
|
1040
|
+
const deltaNormal = normalDenominator > 0 ? (constraint.target - rel) / normalDenominator : 0;
|
|
1041
|
+
const previousNormal = cached.normal;
|
|
1042
|
+
cached.normal = Math.max(previousNormal + deltaNormal, 0);
|
|
1043
|
+
const appliedNormal = cached.normal - previousNormal;
|
|
1044
|
+
const normalImpulse = [n[0] * appliedNormal, n[1] * appliedNormal, n[2] * appliedNormal];
|
|
1045
|
+
applyImpulse(a, rA, normalImpulse, -1, invA);
|
|
1046
|
+
applyImpulse(b, rB, normalImpulse, 1, invB);
|
|
1047
|
+
const rv = subtract3(pointVelocity(b, rB), pointVelocity(a, rA));
|
|
1048
|
+
const rn = dot3(rv, n);
|
|
1049
|
+
const t = [
|
|
1050
|
+
rv[0] - n[0] * rn,
|
|
1051
|
+
rv[1] - n[1] * rn,
|
|
1052
|
+
world.mode2d ? 0 : rv[2] - n[2] * rn,
|
|
1053
|
+
];
|
|
1054
|
+
const tl = length3(t);
|
|
1055
|
+
if (tl > 1e-9) {
|
|
1056
|
+
const tn = [t[0] / tl, t[1] / tl, t[2] / tl];
|
|
1057
|
+
if (dot3(constraint.tangentAxis, tn) < 0.999999) {
|
|
1058
|
+
constraint.tangentAxis = tn;
|
|
1059
|
+
constraint.tangentDenominator = impulseDenominator(a, rA, tn, invA)
|
|
1060
|
+
+ impulseDenominator(b, rB, tn, invB);
|
|
1061
|
+
}
|
|
1062
|
+
const tangentDenominator = constraint.tangentDenominator;
|
|
1063
|
+
const deltaTangent = tangentDenominator > 0 ? -dot3(rv, tn) / tangentDenominator : 0;
|
|
1064
|
+
const previousTangent = [...cached.tangent];
|
|
1065
|
+
cached.tangent = [
|
|
1066
|
+
cached.tangent[0] + tn[0] * deltaTangent,
|
|
1067
|
+
cached.tangent[1] + tn[1] * deltaTangent,
|
|
1068
|
+
cached.tangent[2] + tn[2] * deltaTangent,
|
|
1069
|
+
];
|
|
1070
|
+
const tangentProjection = dot3(cached.tangent, n);
|
|
1071
|
+
for (let k = 0; k < 3; k += 1)
|
|
1072
|
+
cached.tangent[k] -= n[k] * tangentProjection;
|
|
1073
|
+
const mu = Math.sqrt(a.friction * b.friction);
|
|
1074
|
+
const cap = mu * cached.normal;
|
|
1075
|
+
const tangentLength = length3(cached.tangent);
|
|
1076
|
+
if (tangentLength > cap && tangentLength > 0) {
|
|
1077
|
+
const scale = cap / tangentLength;
|
|
1078
|
+
for (let k = 0; k < 3; k += 1)
|
|
1079
|
+
cached.tangent[k] *= scale;
|
|
1080
|
+
}
|
|
1081
|
+
const tangentImpulse = subtract3(cached.tangent, previousTangent);
|
|
1082
|
+
applyImpulse(a, rA, tangentImpulse, -1, invA);
|
|
1083
|
+
applyImpulse(b, rB, tangentImpulse, 1, invB);
|
|
1084
|
+
}
|
|
1085
|
+
}
|
|
1086
|
+
}
|
|
1087
|
+
}
|
|
1088
|
+
// Baumgarte positional correction, per contact, sequential after velocity solve.
|
|
1089
|
+
for (const solver of solverContacts) {
|
|
1090
|
+
const { a, b, depth, n, invA, invB, invSum } = solver;
|
|
1091
|
+
const corr = SCENE_PHYSICS_CORRECTION_PERCENT
|
|
1092
|
+
* Math.max(depth - SCENE_PHYSICS_SLOP, 0) / invSum;
|
|
1093
|
+
for (let k = 0; k < 3; k += 1) {
|
|
1094
|
+
a.position[k] -= n[k] * (corr * invA);
|
|
1095
|
+
b.position[k] += n[k] * (corr * invB);
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
// 5 ── character move-and-slide vs static/kinematic (full depenetration, deepest first)
|
|
1099
|
+
for (const ch of bodies) {
|
|
1100
|
+
if (ch.kind !== "character")
|
|
1101
|
+
continue;
|
|
1102
|
+
ch.grounded = false;
|
|
1103
|
+
for (let iter = 0; iter < SCENE_PHYSICS_SLIDE_ITERATIONS; iter += 1) {
|
|
1104
|
+
let best = null;
|
|
1105
|
+
for (const other of bodies) {
|
|
1106
|
+
if ((other.kind !== "static" && other.kind !== "kinematic") || other.trigger)
|
|
1107
|
+
continue;
|
|
1108
|
+
if (!layersCollide(ch, other))
|
|
1109
|
+
continue;
|
|
1110
|
+
const c = scenePhysicsContact(ch, other);
|
|
1111
|
+
if (c !== null && (best === null || c.depth > best.c.depth))
|
|
1112
|
+
best = { c, other };
|
|
1113
|
+
}
|
|
1114
|
+
if (best === null)
|
|
1115
|
+
break;
|
|
1116
|
+
const { depth, normal: n } = best.c;
|
|
1117
|
+
solidNow.push(ch.index < best.other.index ? [ch.id, best.other.id] : [best.other.id, ch.id]);
|
|
1118
|
+
for (let k = 0; k < 3; k += 1)
|
|
1119
|
+
ch.position[k] -= n[k] * depth;
|
|
1120
|
+
const vn = ch.velocity[0] * n[0] + ch.velocity[1] * n[1] + ch.velocity[2] * n[2];
|
|
1121
|
+
if (vn > 0) {
|
|
1122
|
+
for (let k = 0; k < 3; k += 1)
|
|
1123
|
+
ch.velocity[k] -= n[k] * vn;
|
|
1124
|
+
}
|
|
1125
|
+
if (-n[1] > SCENE_PHYSICS_GROUND_NORMAL_Y)
|
|
1126
|
+
ch.grounded = true;
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1129
|
+
// 6 ── trigger overlaps (exactly one trigger per pair; no forces)
|
|
1130
|
+
const triggerNow = [];
|
|
1131
|
+
for (let i = 0; i < bodies.length; i += 1) {
|
|
1132
|
+
for (let j = i + 1; j < bodies.length; j += 1) {
|
|
1133
|
+
const a = bodies[i], b = bodies[j];
|
|
1134
|
+
if (a.trigger === b.trigger)
|
|
1135
|
+
continue;
|
|
1136
|
+
if (!layersCollide(a, b))
|
|
1137
|
+
continue;
|
|
1138
|
+
if (scenePhysicsContact(a, b) !== null)
|
|
1139
|
+
triggerNow.push([a.id, b.id]);
|
|
1140
|
+
}
|
|
1141
|
+
}
|
|
1142
|
+
// 7 ── events (enter-tracker semantics, both directions per pair)
|
|
1143
|
+
const collisions = [];
|
|
1144
|
+
const enters = [];
|
|
1145
|
+
const exits = [];
|
|
1146
|
+
const seenSolid = new Set();
|
|
1147
|
+
for (const [pa, pb] of solidNow) {
|
|
1148
|
+
const key = pairKey(pa, pb);
|
|
1149
|
+
if (seenSolid.has(key))
|
|
1150
|
+
continue;
|
|
1151
|
+
seenSolid.add(key);
|
|
1152
|
+
if (!world.solidOverlap.has(key)) {
|
|
1153
|
+
collisions.push({ id: pa, other: pb });
|
|
1154
|
+
collisions.push({ id: pb, other: pa });
|
|
1155
|
+
}
|
|
1156
|
+
}
|
|
1157
|
+
world.solidOverlap = seenSolid;
|
|
1158
|
+
const triggerSet = new Set(triggerNow.map(([pa, pb]) => pairKey(pa, pb)));
|
|
1159
|
+
for (const [pa, pb] of triggerNow) {
|
|
1160
|
+
if (!world.triggerOverlap.has(pairKey(pa, pb))) {
|
|
1161
|
+
enters.push({ id: pa, other: pb });
|
|
1162
|
+
enters.push({ id: pb, other: pa });
|
|
1163
|
+
}
|
|
1164
|
+
}
|
|
1165
|
+
for (const key of world.triggerOrder) {
|
|
1166
|
+
if (!triggerSet.has(key)) {
|
|
1167
|
+
const [pa, pb] = key.split("\u0000");
|
|
1168
|
+
exits.push({ id: pa, other: pb });
|
|
1169
|
+
exits.push({ id: pb, other: pa });
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
world.triggerOrder = world.triggerOrder.filter((key) => triggerSet.has(key));
|
|
1173
|
+
for (const [pa, pb] of triggerNow) {
|
|
1174
|
+
const key = pairKey(pa, pb);
|
|
1175
|
+
if (!world.triggerOrder.includes(key))
|
|
1176
|
+
world.triggerOrder.push(key);
|
|
1177
|
+
}
|
|
1178
|
+
world.triggerOverlap = triggerSet;
|
|
1179
|
+
// 8 ── sleep pass (dynamic only; characters and kinematics never sleep)
|
|
1180
|
+
for (const b of bodies) {
|
|
1181
|
+
if (b.kind !== "dynamic" || b.sleeping)
|
|
1182
|
+
continue;
|
|
1183
|
+
if (length3(b.velocity) < SCENE_PHYSICS_SLEEP_SPEED
|
|
1184
|
+
&& length3(b.angularVelocity) < SCENE_PHYSICS_SLEEP_SPEED
|
|
1185
|
+
&& length3(b.torque) === 0) {
|
|
1186
|
+
b.sleepCount += 1;
|
|
1187
|
+
if (b.sleepCount >= SCENE_PHYSICS_SLEEP_TICKS) {
|
|
1188
|
+
b.sleeping = true;
|
|
1189
|
+
b.velocity = [0, 0, 0];
|
|
1190
|
+
b.angularVelocity = [0, 0, 0];
|
|
1191
|
+
}
|
|
1192
|
+
}
|
|
1193
|
+
else {
|
|
1194
|
+
b.sleepCount = 0;
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
// 8.5 ── G6 THE Z-LOCK PASS (mode="2d" only; the solve already masked angular
|
|
1198
|
+
// inertia to world Z and contact/friction impulses to planar XY):
|
|
1199
|
+
// the last act of every step re-pins each body's z position (and its interpolation
|
|
1200
|
+
// anchor) and z velocity to the recorded locks, so one solver serves both modes.
|
|
1201
|
+
if (world.mode2d) {
|
|
1202
|
+
for (const b of bodies) {
|
|
1203
|
+
b.position[2] = b.zLock;
|
|
1204
|
+
b.previous[2] = b.zLock;
|
|
1205
|
+
b.velocity[2] = b.vzLock;
|
|
1206
|
+
b.rotation[0] = b.rotation2dLock[0];
|
|
1207
|
+
b.rotation[1] = b.rotation2dLock[1];
|
|
1208
|
+
b.previousRotation[0] = b.rotation2dLock[0];
|
|
1209
|
+
b.previousRotation[1] = b.rotation2dLock[1];
|
|
1210
|
+
b.angularVelocity[0] = 0;
|
|
1211
|
+
b.angularVelocity[1] = 0;
|
|
1212
|
+
b.orientation = quaternionFromEuler(b.rotation);
|
|
1213
|
+
}
|
|
1214
|
+
}
|
|
1215
|
+
const cache = contactCache(world);
|
|
1216
|
+
for (const [key, value] of cache)
|
|
1217
|
+
if (value.lastTick !== world.tick)
|
|
1218
|
+
cache.delete(key);
|
|
1219
|
+
const tick = world.tick;
|
|
1220
|
+
world.tick += 1;
|
|
1221
|
+
return { tick, dt, collisions, enters, exits };
|
|
1222
|
+
}
|
|
1223
|
+
export function createScenePhysicsAccumulator() {
|
|
1224
|
+
let acc = 0;
|
|
1225
|
+
return {
|
|
1226
|
+
advance(frameSeconds) {
|
|
1227
|
+
acc = Math.min(acc + frameSeconds, SCENE_PHYSICS_MAX_STEPS * SCENE_PHYSICS_DT);
|
|
1228
|
+
const steps = Math.floor(acc / SCENE_PHYSICS_DT);
|
|
1229
|
+
acc -= steps * SCENE_PHYSICS_DT;
|
|
1230
|
+
return { steps, alpha: acc / SCENE_PHYSICS_DT };
|
|
1231
|
+
},
|
|
1232
|
+
};
|
|
1233
|
+
}
|
|
1234
|
+
/** the pure fold the corpus pins: frame durations in MILLISECONDS → per-frame steps
|
|
1235
|
+
* and alpha (the runner divides by 1000 exactly as the surface does) */
|
|
1236
|
+
export function scenePhysicsSchedule(framesMs) {
|
|
1237
|
+
const accumulator = createScenePhysicsAccumulator();
|
|
1238
|
+
return framesMs.map((ms) => accumulator.advance(ms / 1000));
|
|
1239
|
+
}
|
|
1240
|
+
/** THE INTERPOLATION LAW: rendered = prev + (curr − prev)·alpha, componentwise */
|
|
1241
|
+
export function scenePhysicsInterpolate(prev, curr, alpha) {
|
|
1242
|
+
return [
|
|
1243
|
+
prev[0] + (curr[0] - prev[0]) * alpha,
|
|
1244
|
+
prev[1] + (curr[1] - prev[1]) * alpha,
|
|
1245
|
+
prev[2] + (curr[2] - prev[2]) * alpha,
|
|
1246
|
+
];
|
|
1247
|
+
}
|
|
1248
|
+
const PHYSICS_ELIGIBLE = new Set(["box", "sphere", "plane", "model", "sprite"]);
|
|
1249
|
+
function isIdentity(m) {
|
|
1250
|
+
const identity = mat4Identity();
|
|
1251
|
+
for (let i = 0; i < 16; i += 1) {
|
|
1252
|
+
if (Math.abs(m[i] - identity[i]) > 1e-9)
|
|
1253
|
+
return false;
|
|
1254
|
+
}
|
|
1255
|
+
return true;
|
|
1256
|
+
}
|
|
1257
|
+
function largestBasis(m) {
|
|
1258
|
+
return Math.max(Math.sqrt(m[0] * m[0] + m[1] * m[1] + m[2] * m[2]), Math.sqrt(m[4] * m[4] + m[5] * m[5] + m[6] * m[6]), Math.sqrt(m[8] * m[8] + m[9] * m[9] + m[10] * m[10]));
|
|
1259
|
+
}
|
|
1260
|
+
function basisScales(m) {
|
|
1261
|
+
return [
|
|
1262
|
+
Math.sqrt(m[0] * m[0] + m[1] * m[1] + m[2] * m[2]),
|
|
1263
|
+
Math.sqrt(m[4] * m[4] + m[5] * m[5] + m[6] * m[6]),
|
|
1264
|
+
Math.sqrt(m[8] * m[8] + m[9] * m[9] + m[10] * m[10]),
|
|
1265
|
+
];
|
|
1266
|
+
}
|
|
1267
|
+
/** Extract the root-frame Rz*Ry*Rx rotation from a TRS matrix. Transformed-parent
|
|
1268
|
+
* shear is already diagnosed; Gram-Schmidt gives that boundary a stable OBB. */
|
|
1269
|
+
function rootRotation(m, reference) {
|
|
1270
|
+
const scales = basisScales(m);
|
|
1271
|
+
let x = scales[0] > 0 ? [m[0] / scales[0], m[1] / scales[0], m[2] / scales[0]] : [1, 0, 0];
|
|
1272
|
+
const rawY = scales[1] > 0 ? [m[4] / scales[1], m[5] / scales[1], m[6] / scales[1]] : [0, 1, 0];
|
|
1273
|
+
const xy = dot3(rawY, x);
|
|
1274
|
+
let y = [rawY[0] - x[0] * xy, rawY[1] - x[1] * xy, rawY[2] - x[2] * xy];
|
|
1275
|
+
const yl = length3(y);
|
|
1276
|
+
y = yl > 1e-9 ? [y[0] / yl, y[1] / yl, y[2] / yl] : [0, 1, 0];
|
|
1277
|
+
let z = cross3(x, y);
|
|
1278
|
+
const rawZ = scales[2] > 0 ? [m[8] / scales[2], m[9] / scales[2], m[10] / scales[2]] : [0, 0, 1];
|
|
1279
|
+
if (dot3(z, rawZ) < 0)
|
|
1280
|
+
z = [-z[0], -z[1], -z[2]];
|
|
1281
|
+
x = cross3(y, z);
|
|
1282
|
+
const ry = Math.asin(Math.max(-1, Math.min(1, -x[2])));
|
|
1283
|
+
const cy = Math.cos(ry);
|
|
1284
|
+
const rx = Math.abs(cy) > 1e-9 ? Math.atan2(y[2], z[2]) : Math.atan2(-z[1], y[1]);
|
|
1285
|
+
const rz = Math.abs(cy) > 1e-9 ? Math.atan2(x[1], x[0]) : 0;
|
|
1286
|
+
return [
|
|
1287
|
+
unwrapDegrees(rx * SCENE_PHYSICS_RADIANS_TO_DEGREES, reference[0]),
|
|
1288
|
+
unwrapDegrees(ry * SCENE_PHYSICS_RADIANS_TO_DEGREES, reference[1]),
|
|
1289
|
+
unwrapDegrees(rz * SCENE_PHYSICS_RADIANS_TO_DEGREES, reference[2]),
|
|
1290
|
+
];
|
|
1291
|
+
}
|
|
1292
|
+
function orientedHalfFromWorld(m, localHalf, rotation) {
|
|
1293
|
+
const axes = quaternionAxes(quaternionFromEuler(rotation));
|
|
1294
|
+
const center = [m[12], m[13], m[14]];
|
|
1295
|
+
const half = [0, 0, 0];
|
|
1296
|
+
for (const sx of [-1, 1])
|
|
1297
|
+
for (const sy of [-1, 1])
|
|
1298
|
+
for (const sz of [-1, 1]) {
|
|
1299
|
+
const corner = transformPoint(m, [localHalf[0] * sx, localHalf[1] * sy, localHalf[2] * sz]);
|
|
1300
|
+
const delta = subtract3(corner, center);
|
|
1301
|
+
for (let i = 0; i < 3; i += 1)
|
|
1302
|
+
half[i] = Math.max(half[i], Math.abs(dot3(delta, axes[i])));
|
|
1303
|
+
}
|
|
1304
|
+
return half;
|
|
1305
|
+
}
|
|
1306
|
+
/** THE GRAVITY LAW: `<scene gravity="x y z">`, default 0 −9.81 0, malformed → default
|
|
1307
|
+
* with one diagnostic (Article 7) */
|
|
1308
|
+
export function scenePhysicsGravity(ir, resolve, diag) {
|
|
1309
|
+
return readVec(null, "gravity", SCENE_PHYSICS_DEFAULT_GRAVITY, resolve, diag, ir.attrs);
|
|
1310
|
+
}
|
|
1311
|
+
/** extract every physics body from the scene tree. THE EXTRACTION LAWS:
|
|
1312
|
+
* - a `physics` word outside static|dynamic|kinematic|character is NOT a body (one
|
|
1313
|
+
* diagnostic); only box/sphere/plane/model nodes are eligible (one diagnostic);
|
|
1314
|
+
* - colliders derive from the authored world transform: boxes retain local half
|
|
1315
|
+
* extents times root-basis scale and a root-frame orientation (OBBs, not AABBs);
|
|
1316
|
+
* explicit collider="sphere"|"box" follows the same scale laws;
|
|
1317
|
+
* laws; another word → auto with one diagnostic;
|
|
1318
|
+
* - bodies without an id get `#N` (1-based, extraction order);
|
|
1319
|
+
* - a transformed ancestor draws ONE diagnostic and the body simulates in scene-root
|
|
1320
|
+
* space (v1 — bodies are root-frame citizens);
|
|
1321
|
+
* - `modelHalf` supplies a loaded model's local half extents (renderer-owned;
|
|
1322
|
+
* [0.5, 0.5, 0.5] until known). */
|
|
1323
|
+
export function extractScenePhysics(ir, resolve, diag, modelHalf) {
|
|
1324
|
+
const bodies = [];
|
|
1325
|
+
let serial = 0;
|
|
1326
|
+
const walk = (nodes, parent, parentTransformed) => {
|
|
1327
|
+
for (const node of nodes) {
|
|
1328
|
+
if (node.kind === "animate")
|
|
1329
|
+
continue;
|
|
1330
|
+
const props = resolvedProps(node, resolve, diag);
|
|
1331
|
+
const world = mat4Multiply(parent, mat4Trs(props.position, props.rotation, props.scale));
|
|
1332
|
+
const physicsRaw = node.attrs["physics"] === undefined
|
|
1333
|
+
? "" : readString(node, "physics", "", resolve, node.attrs);
|
|
1334
|
+
if (physicsRaw.length > 0) {
|
|
1335
|
+
const body = extractBody(node, props, world, physicsRaw, parentTransformed);
|
|
1336
|
+
if (body !== null)
|
|
1337
|
+
bodies.push(body);
|
|
1338
|
+
}
|
|
1339
|
+
walk(node.children, world, parentTransformed || !isIdentity(world));
|
|
1340
|
+
}
|
|
1341
|
+
};
|
|
1342
|
+
const extractBody = (node, props, world, physicsRaw, parentTransformed) => {
|
|
1343
|
+
if (!SCENE_PHYSICS_KINDS.has(physicsRaw)) {
|
|
1344
|
+
diag?.({ code: "unknown_physics", message: `physics="${physicsRaw}" is not static, dynamic, kinematic or character — not a body` });
|
|
1345
|
+
return null;
|
|
1346
|
+
}
|
|
1347
|
+
if (!PHYSICS_ELIGIBLE.has(node.kind)) {
|
|
1348
|
+
diag?.({ code: "unknown_physics", message: `<${node.kind}> cannot be a physics body — only box, sphere, plane and model` });
|
|
1349
|
+
return null;
|
|
1350
|
+
}
|
|
1351
|
+
const kind = physicsRaw;
|
|
1352
|
+
if (parentTransformed) {
|
|
1353
|
+
diag?.({ code: "physics_nested", message: `a physics body under a transformed parent simulates in scene-root space (v1)` });
|
|
1354
|
+
}
|
|
1355
|
+
const colliderRaw = readString(node, "collider", "auto", resolve, node.attrs);
|
|
1356
|
+
let collider = colliderRaw;
|
|
1357
|
+
// G6: `circle` is the 2D spelling of `sphere` (the sprite word) — one shape set
|
|
1358
|
+
if (collider === "circle")
|
|
1359
|
+
collider = "sphere";
|
|
1360
|
+
if (collider !== "auto" && collider !== "sphere" && collider !== "box") {
|
|
1361
|
+
diag?.({ code: "unknown_collide", message: `collider="${colliderRaw}" is not auto, sphere, circle or box — using auto` });
|
|
1362
|
+
collider = "auto";
|
|
1363
|
+
}
|
|
1364
|
+
let shape;
|
|
1365
|
+
let position;
|
|
1366
|
+
if (node.kind === "sprite") {
|
|
1367
|
+
// G6 THE SPRITE COLLIDER LAW: the quad's own rectangle, centered on the QUAD
|
|
1368
|
+
// (the anchor offset applied), extruded SCENE_SPRITE_COLLIDER_HALF_Z in z so a
|
|
1369
|
+
// box↔box minimum-overlap axis always lands in the XY plane. `collider="circle"`
|
|
1370
|
+
// takes radius = half the SMALLER extent. The RESOLVED `size` is what colliders
|
|
1371
|
+
// read — the texture-aspect default is a render-time refinement (the honest v1).
|
|
1372
|
+
const [w, h] = spriteSizeOf(props);
|
|
1373
|
+
const quadWorld = mat4Multiply(world, mat4Trs(spriteAnchorOffset(props, w, h), [0, 0, 0], [1, 1, 1]));
|
|
1374
|
+
if (collider === "sphere") {
|
|
1375
|
+
shape = { kind: "sphere", radius: Math.min(w, h) / 2 * largestBasis(world) };
|
|
1376
|
+
position = [quadWorld[12], quadWorld[13], quadWorld[14]];
|
|
1377
|
+
}
|
|
1378
|
+
else {
|
|
1379
|
+
const rotation = rootRotation(quadWorld, props.rotation);
|
|
1380
|
+
position = [quadWorld[12], quadWorld[13], quadWorld[14]];
|
|
1381
|
+
shape = {
|
|
1382
|
+
kind: "box",
|
|
1383
|
+
half: orientedHalfFromWorld(quadWorld, [w / 2, h / 2, SCENE_SPRITE_COLLIDER_HALF_Z], rotation),
|
|
1384
|
+
};
|
|
1385
|
+
}
|
|
1386
|
+
}
|
|
1387
|
+
else if (collider === "sphere" || (collider === "auto" && node.kind === "sphere")) {
|
|
1388
|
+
const local = node.kind === "model"
|
|
1389
|
+
? sphereRadiusOfHalf(modelHalf?.(node) ?? [0.5, 0.5, 0.5])
|
|
1390
|
+
: nodeBoundingRadius(node, props) ?? 0.5;
|
|
1391
|
+
shape = { kind: "sphere", radius: local * largestBasis(world) };
|
|
1392
|
+
position = [world[12], world[13], world[14]];
|
|
1393
|
+
}
|
|
1394
|
+
else {
|
|
1395
|
+
const localHalf = node.kind === "box"
|
|
1396
|
+
? [props.boxSize[0] / 2, props.boxSize[1] / 2, props.boxSize[2] / 2]
|
|
1397
|
+
: node.kind === "sphere" ? [props.radius, props.radius, props.radius]
|
|
1398
|
+
: node.kind === "plane" ? [props.planeSize[0] / 2, props.planeSize[1] / 2, 0]
|
|
1399
|
+
: modelHalf?.(node) ?? [0.5, 0.5, 0.5];
|
|
1400
|
+
const rotation = rootRotation(world, props.rotation);
|
|
1401
|
+
position = [world[12], world[13], world[14]];
|
|
1402
|
+
shape = {
|
|
1403
|
+
kind: "box",
|
|
1404
|
+
half: orientedHalfFromWorld(world, localHalf, rotation),
|
|
1405
|
+
};
|
|
1406
|
+
}
|
|
1407
|
+
serial += 1;
|
|
1408
|
+
let mass = kind === "dynamic" ? readScalar(node, "mass", 1, resolve, diag, node.attrs) : 1;
|
|
1409
|
+
if (mass <= 0) {
|
|
1410
|
+
diag?.({ code: "malformed_number", message: `mass="${mass}" is not positive — using 1` });
|
|
1411
|
+
mass = 1;
|
|
1412
|
+
}
|
|
1413
|
+
const bounceRaw = readScalar(node, "bounce", 0, resolve, diag, node.attrs);
|
|
1414
|
+
const frictionRaw = readScalar(node, "friction", 0.5, resolve, diag, node.attrs);
|
|
1415
|
+
const collidesRaw = node.attrs["collides"] === undefined
|
|
1416
|
+
? null : readString(node, "collides", "", resolve, node.attrs);
|
|
1417
|
+
return {
|
|
1418
|
+
id: node.id ?? `#${serial}`,
|
|
1419
|
+
node,
|
|
1420
|
+
kind,
|
|
1421
|
+
shape,
|
|
1422
|
+
position,
|
|
1423
|
+
velocity: readVec(node, "velocity", [0, 0, 0], resolve, diag, node.attrs),
|
|
1424
|
+
rotation: rootRotation(world, props.rotation),
|
|
1425
|
+
angularVelocity: readVec(node, "angular-velocity", [0, 0, 0], resolve, diag, node.attrs),
|
|
1426
|
+
torque: readVec(node, "torque", [0, 0, 0], resolve, diag, node.attrs),
|
|
1427
|
+
angularDamping: Math.max(readScalar(node, "angular-damping", SCENE_PHYSICS_DEFAULT_ANGULAR_DAMPING, resolve, diag, node.attrs), 0),
|
|
1428
|
+
mass,
|
|
1429
|
+
bounce: Math.min(Math.max(bounceRaw, 0), 1),
|
|
1430
|
+
friction: Math.max(frictionRaw, 0),
|
|
1431
|
+
trigger: readString(node, "trigger", "", resolve, node.attrs) === "true",
|
|
1432
|
+
layer: readString(node, "layer", "default", resolve, node.attrs),
|
|
1433
|
+
collides: collidesRaw === null
|
|
1434
|
+
? null : collidesRaw.trim().split(/\s+/).filter((p) => p.length > 0),
|
|
1435
|
+
speed: kind === "character"
|
|
1436
|
+
? readScalar(node, "speed", SCENE_PHYSICS_DEFAULT_SPEED, resolve, diag, node.attrs)
|
|
1437
|
+
: SCENE_PHYSICS_DEFAULT_SPEED,
|
|
1438
|
+
jump: kind === "character"
|
|
1439
|
+
? readScalar(node, "jump", SCENE_PHYSICS_DEFAULT_JUMP, resolve, diag, node.attrs)
|
|
1440
|
+
: SCENE_PHYSICS_DEFAULT_JUMP,
|
|
1441
|
+
};
|
|
1442
|
+
};
|
|
1443
|
+
walk(ir.nodes, mat4Identity(), false);
|
|
1444
|
+
return { gravity: scenePhysicsGravity(ir, resolve, diag), bodies, mode2d: ir.mode === "2d" };
|
|
1445
|
+
}
|
|
1446
|
+
// ── the parent-frame law (G2 × G1) ───────────────────────────────────────────────────
|
|
1447
|
+
//
|
|
1448
|
+
// Bodies SIMULATE in scene-root space (the extraction freezes each body's WORLD
|
|
1449
|
+
// position; a transformed parent is diagnosed, not obeyed). But a node's authored
|
|
1450
|
+
// `position` is LOCAL to its parent, and the renderer composes parentWorld · local.
|
|
1451
|
+
// So the solver's root-space result must be expressed in the parent's frame before it
|
|
1452
|
+
// lands on the node, and an authored/bus position write must be lifted out of the
|
|
1453
|
+
// parent's frame before it teleports the body. Without this pair, every body under a
|
|
1454
|
+
// transformed parent — which a G1 prefab instance ALWAYS is, since the expansion root
|
|
1455
|
+
// carries the instance transform — renders double-offset.
|
|
1456
|
+
//
|
|
1457
|
+
// Both are identities when the parent is the scene root (the fast path callers take).
|
|
1458
|
+
/** root-space → the parent's local frame (what the node's `position` must hold) */
|
|
1459
|
+
export function scenePhysicsToLocal(rootPosition, parentWorld) {
|
|
1460
|
+
if (parentWorld === null)
|
|
1461
|
+
return [...rootPosition];
|
|
1462
|
+
const inverse = mat4Invert(parentWorld);
|
|
1463
|
+
if (inverse === null)
|
|
1464
|
+
return [...rootPosition]; // degenerate parent: honest passthrough
|
|
1465
|
+
return transformPoint(inverse, rootPosition);
|
|
1466
|
+
}
|
|
1467
|
+
/** the parent's local frame → root space (what a position write means to the solver) */
|
|
1468
|
+
export function scenePhysicsToRoot(localPosition, parentWorld) {
|
|
1469
|
+
if (parentWorld === null)
|
|
1470
|
+
return [...localPosition];
|
|
1471
|
+
return transformPoint(parentWorld, localPosition);
|
|
1472
|
+
}
|
|
1473
|
+
/** the node's local DSX Euler rotation -> the solver's scene-root orientation.
|
|
1474
|
+
* This deliberately extracts the same orthonormal frame as body extraction, so a
|
|
1475
|
+
* stationary kinematic nested below non-uniform scale does not manufacture an
|
|
1476
|
+
* angular velocity on its first tick. */
|
|
1477
|
+
export function scenePhysicsRotationToRoot(localRotation, parentWorld, reference = localRotation) {
|
|
1478
|
+
if (parentWorld === null)
|
|
1479
|
+
return [...localRotation];
|
|
1480
|
+
const local = mat4Trs([0, 0, 0], localRotation, [1, 1, 1]);
|
|
1481
|
+
return rootRotation(mat4Multiply(parentWorld, local), reference);
|
|
1482
|
+
}
|
|
1483
|
+
/** solver scene-root orientation -> the node's local DSX Euler rotation. The
|
|
1484
|
+
* inverse-parent fold is followed by the same stable orthonormal extraction used
|
|
1485
|
+
* for colliders; a singular parent is an honest passthrough. */
|
|
1486
|
+
export function scenePhysicsRotationToLocal(root, parentWorld, reference = root) {
|
|
1487
|
+
if (parentWorld === null)
|
|
1488
|
+
return [...root];
|
|
1489
|
+
const inverse = mat4Invert(parentWorld);
|
|
1490
|
+
if (inverse === null)
|
|
1491
|
+
return [...root];
|
|
1492
|
+
const rootMatrix = mat4Trs([0, 0, 0], root, [1, 1, 1]);
|
|
1493
|
+
return rootRotation(mat4Multiply(inverse, rootMatrix), reference);
|
|
1494
|
+
}
|
|
1495
|
+
/** a model's auto SPHERE collider radius from its local half extents (the
|
|
1496
|
+
* bounding-sphere convention: half-diagonal) */
|
|
1497
|
+
function sphereRadiusOfHalf(half) {
|
|
1498
|
+
return Math.sqrt(half[0] * half[0] + half[1] * half[1] + half[2] * half[2]);
|
|
1499
|
+
}
|
|
1500
|
+
//# sourceMappingURL=physics.js.map
|