@egret/engine 0.0.0-stage → 7.0.1-rc.3
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 +201 -0
- package/NOTICE +21 -0
- package/README.md +101 -2
- package/README.zh-CN.md +102 -0
- package/dist/Engine.d.ts +77 -0
- package/dist/Engine.js +464 -0
- package/dist/animation/index.d.ts +6 -0
- package/dist/animation/index.js +5 -0
- package/dist/animation/sequenceController.d.ts +22 -0
- package/dist/animation/sequenceController.js +618 -0
- package/dist/animation/sequenceCoordinator.d.ts +26 -0
- package/dist/animation/sequenceCoordinator.js +161 -0
- package/dist/animation/sequenceInput.d.ts +25 -0
- package/dist/animation/sequenceInput.js +108 -0
- package/dist/animation/sequenceMath.d.ts +35 -0
- package/dist/animation/sequenceMath.js +164 -0
- package/dist/animation/sequenceResetSlot.d.ts +7 -0
- package/dist/animation/sequenceResetSlot.js +367 -0
- package/dist/animation/sequenceResetTypes.d.ts +42 -0
- package/dist/animation/sequenceResetTypes.js +2 -0
- package/dist/animation/types.d.ts +69 -0
- package/dist/animation/types.js +2 -0
- package/dist/assetBundle.d.ts +52 -0
- package/dist/assetBundle.js +173 -0
- package/dist/assetGroup.d.ts +17 -0
- package/dist/assetGroup.js +332 -0
- package/dist/assetGroupTypes.d.ts +15 -0
- package/dist/assetGroupTypes.js +2 -0
- package/dist/audio.d.ts +4 -0
- package/dist/audio.js +4 -0
- package/dist/audioCues.d.ts +43 -0
- package/dist/audioCues.js +127 -0
- package/dist/audioMixer.d.ts +7 -0
- package/dist/audioMixer.js +785 -0
- package/dist/audioTypes.d.ts +233 -0
- package/dist/audioTypes.js +3 -0
- package/dist/bitmapBindingTicket.d.ts +24 -0
- package/dist/bitmapBindingTicket.js +215 -0
- package/dist/bitmapButton.d.ts +4 -0
- package/dist/bitmapButton.js +407 -0
- package/dist/bitmapButtonTypes.d.ts +29 -0
- package/dist/bitmapButtonTypes.js +2 -0
- package/dist/bitmapFontParse.d.ts +10 -0
- package/dist/bitmapFontParse.js +246 -0
- package/dist/bitmapFontTypes.d.ts +109 -0
- package/dist/bitmapFontTypes.js +2 -0
- package/dist/bitmapResourceSlot.d.ts +25 -0
- package/dist/bitmapResourceSlot.js +340 -0
- package/dist/bitmapText.d.ts +6 -0
- package/dist/bitmapText.js +320 -0
- package/dist/bitmapfont.d.ts +5 -0
- package/dist/bitmapfont.js +5 -0
- package/dist/camera.d.ts +4 -0
- package/dist/camera.js +4 -0
- package/dist/camera2d.d.ts +7 -0
- package/dist/camera2d.js +411 -0
- package/dist/cameraTypes.d.ts +114 -0
- package/dist/cameraTypes.js +2 -0
- package/dist/closeHost.d.ts +4 -0
- package/dist/closeHost.js +66 -0
- package/dist/contentArtifacts.d.ts +12 -0
- package/dist/contentArtifacts.js +153 -0
- package/dist/contentAtlasPages.d.ts +13 -0
- package/dist/contentAtlasPages.js +117 -0
- package/dist/contentLoader.d.ts +6 -0
- package/dist/contentLoader.js +327 -0
- package/dist/contentTypes.d.ts +33 -0
- package/dist/contentTypes.js +2 -0
- package/dist/createEngine.d.ts +7 -0
- package/dist/createEngine.js +25 -0
- package/dist/createEngineForProfile.d.ts +5 -0
- package/dist/createEngineForProfile.js +42 -0
- package/dist/displaySubtreeTicket.d.ts +25 -0
- package/dist/displaySubtreeTicket.js +76 -0
- package/dist/fontContentLoader.d.ts +6 -0
- package/dist/fontContentLoader.js +298 -0
- package/dist/fontContentTypes.d.ts +34 -0
- package/dist/fontContentTypes.js +2 -0
- package/dist/gameClock.d.ts +24 -0
- package/dist/gameClock.js +246 -0
- package/dist/gameClockTypes.d.ts +31 -0
- package/dist/gameClockTypes.js +2 -0
- package/dist/gameRun.d.ts +50 -0
- package/dist/gameRun.js +97 -0
- package/dist/gamepad.d.ts +5 -0
- package/dist/gamepad.js +5 -0
- package/dist/gamepadInput.d.ts +4 -0
- package/dist/gamepadInput.js +281 -0
- package/dist/gamepadKeys.d.ts +4 -0
- package/dist/gamepadKeys.js +205 -0
- package/dist/gamepadTypes.d.ts +134 -0
- package/dist/gamepadTypes.js +3 -0
- package/dist/hover.d.ts +4 -0
- package/dist/hover.js +4 -0
- package/dist/hoverInput.d.ts +8 -0
- package/dist/hoverInput.js +503 -0
- package/dist/hoverInputTypes.d.ts +119 -0
- package/dist/hoverInputTypes.js +2 -0
- package/dist/index.d.ts +49 -0
- package/dist/index.js +20 -0
- package/dist/input.d.ts +6 -0
- package/dist/input.js +5 -0
- package/dist/inputActions.d.ts +81 -0
- package/dist/inputActions.js +330 -0
- package/dist/internal/contentFactoryState.d.ts +19 -0
- package/dist/internal/contentFactoryState.js +42 -0
- package/dist/internal/mouseInputFactoryState.d.ts +57 -0
- package/dist/internal/mouseInputFactoryState.js +125 -0
- package/dist/internal/sequenceControllerFactoryState.d.ts +42 -0
- package/dist/internal/sequenceControllerFactoryState.js +98 -0
- package/dist/internal/textFieldFactoryState.d.ts +11 -0
- package/dist/internal/textFieldFactoryState.js +32 -0
- package/dist/internal/texturedFrameState.d.ts +4 -0
- package/dist/internal/texturedFrameState.js +12 -0
- package/dist/internal/uiFactoryState.d.ts +64 -0
- package/dist/internal/uiFactoryState.js +157 -0
- package/dist/internal/uiRootState.d.ts +67 -0
- package/dist/internal/uiRootState.js +103 -0
- package/dist/keyboardInput.d.ts +12 -0
- package/dist/keyboardInput.js +578 -0
- package/dist/keyboardInputTypes.d.ts +81 -0
- package/dist/keyboardInputTypes.js +2 -0
- package/dist/kit.d.ts +14 -0
- package/dist/kit.js +9 -0
- package/dist/layout.d.ts +5 -0
- package/dist/layout.js +132 -0
- package/dist/layoutTypes.d.ts +28 -0
- package/dist/layoutTypes.js +2 -0
- package/dist/manifestAssetGroup.d.ts +10 -0
- package/dist/manifestAssetGroup.js +477 -0
- package/dist/manifestResourceState.d.ts +16 -0
- package/dist/manifestResourceState.js +57 -0
- package/dist/manifestResourceTypes.d.ts +25 -0
- package/dist/manifestResourceTypes.js +2 -0
- package/dist/motion.d.ts +33 -0
- package/dist/motion.js +206 -0
- package/dist/mouseInput.d.ts +102 -0
- package/dist/mouseInput.js +562 -0
- package/dist/mouseInputTypes.d.ts +37 -0
- package/dist/mouseInputTypes.js +2 -0
- package/dist/paragraph.d.ts +8 -0
- package/dist/paragraph.js +7 -0
- package/dist/particleEmitter.d.ts +4 -0
- package/dist/particleEmitter.js +292 -0
- package/dist/particleTypes.d.ts +107 -0
- package/dist/particleTypes.js +2 -0
- package/dist/particles.d.ts +4 -0
- package/dist/particles.js +4 -0
- package/dist/physics2d.d.ts +4 -0
- package/dist/physics2d.js +4 -0
- package/dist/physics2dGeometry.d.ts +39 -0
- package/dist/physics2dGeometry.js +230 -0
- package/dist/physics2dTypes.d.ts +167 -0
- package/dist/physics2dTypes.js +2 -0
- package/dist/physics2dWorld.d.ts +4 -0
- package/dist/physics2dWorld.js +623 -0
- package/dist/pointerInput.d.ts +11 -0
- package/dist/pointerInput.js +299 -0
- package/dist/pointerInputTypes.d.ts +38 -0
- package/dist/pointerInputTypes.js +2 -0
- package/dist/random.d.ts +29 -0
- package/dist/random.js +131 -0
- package/dist/rendering/ownedPreparationFailure2D.d.ts +5 -0
- package/dist/rendering/ownedPreparationFailure2D.js +33 -0
- package/dist/rendering/packSharedDraws2D.d.ts +50 -0
- package/dist/rendering/packSharedDraws2D.js +331 -0
- package/dist/rendering/packedOrientation.d.ts +4 -0
- package/dist/rendering/packedOrientation.js +7 -0
- package/dist/rendering/prepareRectangles2D.d.ts +92 -0
- package/dist/rendering/prepareRectangles2D.js +481 -0
- package/dist/rendering/prepareSharedDraws2D.d.ts +88 -0
- package/dist/rendering/prepareSharedDraws2D.js +652 -0
- package/dist/rendering/preparedBatches2D.d.ts +17 -0
- package/dist/rendering/preparedBatches2D.js +30 -0
- package/dist/rendering/preparedSharedBatches2D.d.ts +44 -0
- package/dist/rendering/preparedSharedBatches2D.js +247 -0
- package/dist/rendering/sharedColor2D.d.ts +18 -0
- package/dist/rendering/sharedColor2D.js +68 -0
- package/dist/rendering/sharedEffects2D.d.ts +23 -0
- package/dist/rendering/sharedEffects2D.js +118 -0
- package/dist/rendering/sharedPreparationRetention2D.d.ts +120 -0
- package/dist/rendering/sharedPreparationRetention2D.js +357 -0
- package/dist/rendering/srgbBytes.d.ts +3 -0
- package/dist/rendering/srgbBytes.js +64 -0
- package/dist/rendering/texturedPreparationBudget2D.d.ts +31 -0
- package/dist/rendering/texturedPreparationBudget2D.js +81 -0
- package/dist/rendering/world3d/Animation3D.d.ts +95 -0
- package/dist/rendering/world3d/Animation3D.js +380 -0
- package/dist/rendering/world3d/AnimationTexture3D.d.ts +157 -0
- package/dist/rendering/world3d/AnimationTexture3D.js +601 -0
- package/dist/rendering/world3d/Batch3D.d.ts +71 -0
- package/dist/rendering/world3d/Batch3D.js +329 -0
- package/dist/rendering/world3d/Camera3D.d.ts +57 -0
- package/dist/rendering/world3d/Camera3D.js +129 -0
- package/dist/rendering/world3d/Character3D.d.ts +123 -0
- package/dist/rendering/world3d/Character3D.js +303 -0
- package/dist/rendering/world3d/Cloth3D.d.ts +115 -0
- package/dist/rendering/world3d/Cloth3D.js +498 -0
- package/dist/rendering/world3d/Compression3D.d.ts +21 -0
- package/dist/rendering/world3d/Compression3D.js +702 -0
- package/dist/rendering/world3d/Curves3D.d.ts +111 -0
- package/dist/rendering/world3d/Curves3D.js +455 -0
- package/dist/rendering/world3d/Decal3D.d.ts +41 -0
- package/dist/rendering/world3d/Decal3D.js +241 -0
- package/dist/rendering/world3d/Geometry3D.d.ts +85 -0
- package/dist/rendering/world3d/Geometry3D.js +326 -0
- package/dist/rendering/world3d/GeometryBuilder3D.d.ts +51 -0
- package/dist/rendering/world3d/GeometryBuilder3D.js +196 -0
- package/dist/rendering/world3d/Gizmo3D.d.ts +75 -0
- package/dist/rendering/world3d/Gizmo3D.js +512 -0
- package/dist/rendering/world3d/Gltf3D.d.ts +67 -0
- package/dist/rendering/world3d/Gltf3D.js +976 -0
- package/dist/rendering/world3d/GpuParticles3D.d.ts +107 -0
- package/dist/rendering/world3d/GpuParticles3D.js +400 -0
- package/dist/rendering/world3d/Hdr3D.d.ts +33 -0
- package/dist/rendering/world3d/Hdr3D.js +190 -0
- package/dist/rendering/world3d/Jelly3D.d.ts +75 -0
- package/dist/rendering/world3d/Jelly3D.js +266 -0
- package/dist/rendering/world3d/Ktx23D.d.ts +27 -0
- package/dist/rendering/world3d/Ktx23D.js +112 -0
- package/dist/rendering/world3d/Light3D.d.ts +75 -0
- package/dist/rendering/world3d/Light3D.js +93 -0
- package/dist/rendering/world3d/Material3D.d.ts +243 -0
- package/dist/rendering/world3d/Material3D.js +267 -0
- package/dist/rendering/world3d/Mesh3D.d.ts +108 -0
- package/dist/rendering/world3d/Mesh3D.js +323 -0
- package/dist/rendering/world3d/Meshopt3D.d.ts +12 -0
- package/dist/rendering/world3d/Meshopt3D.js +275 -0
- package/dist/rendering/world3d/Navigation3D.d.ts +173 -0
- package/dist/rendering/world3d/Navigation3D.js +971 -0
- package/dist/rendering/world3d/Object3D.d.ts +107 -0
- package/dist/rendering/world3d/Object3D.js +305 -0
- package/dist/rendering/world3d/Particles3D.d.ts +85 -0
- package/dist/rendering/world3d/Particles3D.js +274 -0
- package/dist/rendering/world3d/Physics3D.d.ts +416 -0
- package/dist/rendering/world3d/Physics3D.js +2835 -0
- package/dist/rendering/world3d/PhysicsHull3D.d.ts +35 -0
- package/dist/rendering/world3d/PhysicsHull3D.js +227 -0
- package/dist/rendering/world3d/PhysicsMesh3D.d.ts +39 -0
- package/dist/rendering/world3d/PhysicsMesh3D.js +338 -0
- package/dist/rendering/world3d/PhysicsQueries3D.d.ts +24 -0
- package/dist/rendering/world3d/PhysicsQueries3D.js +323 -0
- package/dist/rendering/world3d/PhysicsRemote3D.d.ts +174 -0
- package/dist/rendering/world3d/PhysicsRemote3D.js +534 -0
- package/dist/rendering/world3d/Ragdoll3D.d.ts +69 -0
- package/dist/rendering/world3d/Ragdoll3D.js +200 -0
- package/dist/rendering/world3d/Raycast3D.d.ts +37 -0
- package/dist/rendering/world3d/Raycast3D.js +246 -0
- package/dist/rendering/world3d/RenderList3D.d.ts +88 -0
- package/dist/rendering/world3d/RenderList3D.js +490 -0
- package/dist/rendering/world3d/Retarget3D.d.ts +20 -0
- package/dist/rendering/world3d/Retarget3D.js +185 -0
- package/dist/rendering/world3d/Serialize3D.d.ts +49 -0
- package/dist/rendering/world3d/Serialize3D.js +607 -0
- package/dist/rendering/world3d/StateMachine3D.d.ts +78 -0
- package/dist/rendering/world3d/StateMachine3D.js +277 -0
- package/dist/rendering/world3d/Terrain3D.d.ts +227 -0
- package/dist/rendering/world3d/Terrain3D.js +803 -0
- package/dist/rendering/world3d/Text3D.d.ts +90 -0
- package/dist/rendering/world3d/Text3D.js +558 -0
- package/dist/rendering/world3d/Texture3D.d.ts +133 -0
- package/dist/rendering/world3d/Texture3D.js +252 -0
- package/dist/rendering/world3d/Trail3D.d.ts +59 -0
- package/dist/rendering/world3d/Trail3D.js +193 -0
- package/dist/rendering/world3d/Vehicle3D.d.ts +97 -0
- package/dist/rendering/world3d/Vehicle3D.js +226 -0
- package/dist/rendering/world3d/VolumetricFog3D.d.ts +55 -0
- package/dist/rendering/world3d/VolumetricFog3D.js +229 -0
- package/dist/rendering/world3d/Water3D.d.ts +163 -0
- package/dist/rendering/world3d/Water3D.js +606 -0
- package/dist/rendering/world3d/core3D.d.ts +34 -0
- package/dist/rendering/world3d/core3D.js +99 -0
- package/dist/rendering/world3d/debug3D.d.ts +64 -0
- package/dist/rendering/world3d/debug3D.js +257 -0
- package/dist/rendering/world3d/decoders3D.d.ts +42 -0
- package/dist/rendering/world3d/decoders3D.js +133 -0
- package/dist/rendering/world3d/geometries3D.d.ts +99 -0
- package/dist/rendering/world3d/geometries3D.js +605 -0
- package/dist/rendering/world3d/helpers3D.d.ts +65 -0
- package/dist/rendering/world3d/helpers3D.js +177 -0
- package/dist/rendering/world3d/index.d.ts +91 -0
- package/dist/rendering/world3d/index.js +56 -0
- package/dist/rendering/world3d/math3D.d.ts +78 -0
- package/dist/rendering/world3d/math3D.js +640 -0
- package/dist/rendering/world3d/primitives3D.d.ts +60 -0
- package/dist/rendering/world3d/primitives3D.js +196 -0
- package/dist/resources.d.ts +8 -0
- package/dist/resources.js +6 -0
- package/dist/saveStore.d.ts +56 -0
- package/dist/saveStore.js +163 -0
- package/dist/semanticList.d.ts +122 -0
- package/dist/semanticList.js +174 -0
- package/dist/semanticTree.d.ts +10 -0
- package/dist/semanticTree.js +532 -0
- package/dist/semanticTypes.d.ts +167 -0
- package/dist/semanticTypes.js +2 -0
- package/dist/semantics.d.ts +6 -0
- package/dist/semantics.js +5 -0
- package/dist/sequencePlayback.d.ts +19 -0
- package/dist/sequencePlayback.js +391 -0
- package/dist/sequencePlaybackTypes.d.ts +21 -0
- package/dist/sequencePlaybackTypes.js +2 -0
- package/dist/sequenceTargetClaims.d.ts +10 -0
- package/dist/sequenceTargetClaims.js +31 -0
- package/dist/srgbBytes.d.ts +3 -0
- package/dist/srgbBytes.js +64 -0
- package/dist/startEngine.d.ts +5 -0
- package/dist/startEngine.js +31 -0
- package/dist/surfaceOwnership.d.ts +3 -0
- package/dist/surfaceOwnership.js +15 -0
- package/dist/text.d.ts +23 -0
- package/dist/text.js +4 -0
- package/dist/textField.d.ts +6 -0
- package/dist/textField.js +6 -0
- package/dist/tileMapDraw.d.ts +7 -0
- package/dist/tileMapDraw.js +310 -0
- package/dist/tiledImport.d.ts +9 -0
- package/dist/tiledImport.js +357 -0
- package/dist/tilemap.d.ts +5 -0
- package/dist/tilemap.js +5 -0
- package/dist/tilemapTypes.d.ts +169 -0
- package/dist/tilemapTypes.js +2 -0
- package/dist/tween.d.ts +15 -0
- package/dist/tween.js +329 -0
- package/dist/tweenTypes.d.ts +27 -0
- package/dist/tweenTypes.js +2 -0
- package/dist/ui/controlKeys.d.ts +109 -0
- package/dist/ui/controlKeys.js +249 -0
- package/dist/ui/controls.d.ts +52 -0
- package/dist/ui/controls.js +288 -0
- package/dist/ui/editing.d.ts +3 -0
- package/dist/ui/editing.js +3 -0
- package/dist/ui/index.d.ts +19 -0
- package/dist/ui/index.js +11 -0
- package/dist/ui/keyboardFocus.d.ts +32 -0
- package/dist/ui/keyboardFocus.js +146 -0
- package/dist/ui/layerTypes.d.ts +91 -0
- package/dist/ui/layerTypes.js +2 -0
- package/dist/ui/layers.d.ts +4 -0
- package/dist/ui/layers.js +586 -0
- package/dist/ui/leafContent.d.ts +21 -0
- package/dist/ui/leafContent.js +136 -0
- package/dist/ui/rangeControl.d.ts +50 -0
- package/dist/ui/rangeControl.js +167 -0
- package/dist/ui/scrollControl.d.ts +72 -0
- package/dist/ui/scrollControl.js +218 -0
- package/dist/ui/stateControls.d.ts +82 -0
- package/dist/ui/stateControls.js +328 -0
- package/dist/ui/textEditor.d.ts +53 -0
- package/dist/ui/textEditor.js +370 -0
- package/dist/ui/textEditorTypes.d.ts +78 -0
- package/dist/ui/textEditorTypes.js +2 -0
- package/dist/ui/textFieldControl.d.ts +96 -0
- package/dist/ui/textFieldControl.js +259 -0
- package/dist/ui/theme.d.ts +59 -0
- package/dist/ui/theme.js +165 -0
- package/dist/ui/types.d.ts +265 -0
- package/dist/ui/types.js +2 -0
- package/dist/ui/uiRoot.d.ts +4 -0
- package/dist/ui/uiRoot.js +1497 -0
- package/dist/ui/uiRootDisplay.d.ts +46 -0
- package/dist/ui/uiRootDisplay.js +148 -0
- package/dist/ui/uiRootModel.d.ts +112 -0
- package/dist/ui/uiRootModel.js +60 -0
- package/dist/ui/virtualList.d.ts +20 -0
- package/dist/ui/virtualList.js +105 -0
- package/dist/video.d.ts +4 -0
- package/dist/video.js +4 -0
- package/dist/videoPlayer.d.ts +4 -0
- package/dist/videoPlayer.js +215 -0
- package/dist/videoTypes.d.ts +74 -0
- package/dist/videoTypes.js +2 -0
- package/dist/web/BrowserAssetBundle.d.ts +39 -0
- package/dist/web/BrowserAssetBundle.js +136 -0
- package/dist/web/BrowserAudio.d.ts +41 -0
- package/dist/web/BrowserAudio.js +473 -0
- package/dist/web/BrowserFrameLoop.d.ts +65 -0
- package/dist/web/BrowserFrameLoop.js +125 -0
- package/dist/web/BrowserGame.d.ts +112 -0
- package/dist/web/BrowserGame.js +352 -0
- package/dist/web/BrowserGamepadInput.d.ts +35 -0
- package/dist/web/BrowserGamepadInput.js +134 -0
- package/dist/web/BrowserHoverInput.d.ts +24 -0
- package/dist/web/BrowserHoverInput.js +266 -0
- package/dist/web/BrowserImageTextures.d.ts +66 -0
- package/dist/web/BrowserImageTextures.js +169 -0
- package/dist/web/BrowserInputActions.d.ts +53 -0
- package/dist/web/BrowserInputActions.js +342 -0
- package/dist/web/BrowserKeyboardInput.d.ts +4 -0
- package/dist/web/BrowserKeyboardInput.js +223 -0
- package/dist/web/BrowserPlay.d.ts +289 -0
- package/dist/web/BrowserPlay.js +1301 -0
- package/dist/web/BrowserPointerInput.d.ts +4 -0
- package/dist/web/BrowserPointerInput.js +511 -0
- package/dist/web/BrowserSemantics.d.ts +34 -0
- package/dist/web/BrowserSemantics.js +418 -0
- package/dist/web/BrowserStatsPanel.d.ts +46 -0
- package/dist/web/BrowserStatsPanel.js +137 -0
- package/dist/web/BrowserStorage.d.ts +33 -0
- package/dist/web/BrowserStorage.js +132 -0
- package/dist/web/BrowserTextEditor.d.ts +49 -0
- package/dist/web/BrowserTextEditor.js +839 -0
- package/dist/web/BrowserTextInput.d.ts +83 -0
- package/dist/web/BrowserTextInput.js +524 -0
- package/dist/web/BrowserTextureProvider.d.ts +52 -0
- package/dist/web/BrowserTextureProvider.js +935 -0
- package/dist/web/BrowserVideo.d.ts +28 -0
- package/dist/web/BrowserVideo.js +211 -0
- package/dist/web/CanvasKitFontSetProvider.d.ts +29 -0
- package/dist/web/CanvasKitFontSetProvider.js +215 -0
- package/dist/web/CanvasKitParagraphProvider.d.ts +169 -0
- package/dist/web/CanvasKitParagraphProvider.js +691 -0
- package/dist/web/CanvasTextRasterProvider.d.ts +4 -0
- package/dist/web/CanvasTextRasterProvider.js +312 -0
- package/dist/web/ManifestByteSource.d.ts +147 -0
- package/dist/web/ManifestByteSource.js +974 -0
- package/dist/web/ManifestFontContentLoader.d.ts +12 -0
- package/dist/web/ManifestFontContentLoader.js +30 -0
- package/dist/web/MouseInput.d.ts +4 -0
- package/dist/web/MouseInput.js +218 -0
- package/dist/web/WebGL2Host.d.ts +5 -0
- package/dist/web/WebGL2Host.js +2672 -0
- package/dist/web/WebGPUHost.d.ts +87 -0
- package/dist/web/WebGPUHost.js +3349 -0
- package/dist/web/audio.d.ts +3 -0
- package/dist/web/audio.js +4 -0
- package/dist/web/canvasKitParagraphGeometry.d.ts +27 -0
- package/dist/web/canvasKitParagraphGeometry.js +83 -0
- package/dist/web/editing.d.ts +5 -0
- package/dist/web/editing.js +4 -0
- package/dist/web/editorFonts.d.ts +50 -0
- package/dist/web/editorFonts.js +297 -0
- package/dist/web/game.d.ts +5 -0
- package/dist/web/game.js +6 -0
- package/dist/web/gamepad.d.ts +3 -0
- package/dist/web/gamepad.js +4 -0
- package/dist/web/hover.d.ts +3 -0
- package/dist/web/hover.js +4 -0
- package/dist/web/image.d.ts +4 -0
- package/dist/web/image.js +4 -0
- package/dist/web/imageProjections2D.d.ts +14 -0
- package/dist/web/imageProjections2D.js +2 -0
- package/dist/web/input.d.ts +6 -0
- package/dist/web/input.js +4 -0
- package/dist/web/keyboard.d.ts +3 -0
- package/dist/web/keyboard.js +3 -0
- package/dist/web/kit.d.ts +10 -0
- package/dist/web/kit.js +7 -0
- package/dist/web/loop.d.ts +3 -0
- package/dist/web/loop.js +4 -0
- package/dist/web/paragraph.d.ts +6 -0
- package/dist/web/paragraph.js +4 -0
- package/dist/web/play.d.ts +5 -0
- package/dist/web/play.js +6 -0
- package/dist/web/playSfx.d.ts +36 -0
- package/dist/web/playSfx.js +112 -0
- package/dist/web/pointer.d.ts +3 -0
- package/dist/web/pointer.js +3 -0
- package/dist/web/prepareSharedSubmission2D.d.ts +27 -0
- package/dist/web/prepareSharedSubmission2D.js +57 -0
- package/dist/web/resources.d.ts +27 -0
- package/dist/web/resources.js +96 -0
- package/dist/web/semantics.d.ts +3 -0
- package/dist/web/semantics.js +4 -0
- package/dist/web/sharedDeviceSession.d.ts +64 -0
- package/dist/web/sharedDeviceSession.js +2 -0
- package/dist/web/sharedEffectShaders.d.ts +9 -0
- package/dist/web/sharedEffectShaders.js +165 -0
- package/dist/web/sharedFrameUpload.d.ts +25 -0
- package/dist/web/sharedFrameUpload.js +2 -0
- package/dist/web/sharedHostDeferred.d.ts +7 -0
- package/dist/web/sharedHostDeferred.js +12 -0
- package/dist/web/sharedRetentionLedger.d.ts +85 -0
- package/dist/web/sharedRetentionLedger.js +391 -0
- package/dist/web/sharedSubmissionStorage2D.d.ts +56 -0
- package/dist/web/sharedSubmissionStorage2D.js +73 -0
- package/dist/web/sharedWebGL2Arena.d.ts +40 -0
- package/dist/web/sharedWebGL2Arena.js +77 -0
- package/dist/web/sharedWebGL2Options.d.ts +30 -0
- package/dist/web/sharedWebGL2Options.js +96 -0
- package/dist/web/sharedWebGL2Shaders.d.ts +20 -0
- package/dist/web/sharedWebGL2Shaders.js +314 -0
- package/dist/web/sharedWebGPUOptions.d.ts +37 -0
- package/dist/web/sharedWebGPUOptions.js +126 -0
- package/dist/web/text.d.ts +3 -0
- package/dist/web/text.js +3 -0
- package/dist/web/textInput.d.ts +3 -0
- package/dist/web/textInput.js +3 -0
- package/dist/web/video.d.ts +3 -0
- package/dist/web/video.js +4 -0
- package/dist/web/webgl2.d.ts +7 -0
- package/dist/web/webgl2.js +4 -0
- package/dist/web/webgl2LayerState.d.ts +50 -0
- package/dist/web/webgl2LayerState.js +102 -0
- package/dist/web/webgl2Native.d.ts +70 -0
- package/dist/web/webgl2Native.js +568 -0
- package/dist/web/webgl2Types.d.ts +132 -0
- package/dist/web/webgl2Types.js +2 -0
- package/dist/web/webgpu.d.ts +6 -0
- package/dist/web/webgpu.js +2 -0
- package/dist/web/webgpuBindingCache.d.ts +26 -0
- package/dist/web/webgpuBindingCache.js +223 -0
- package/dist/web/webgpuBufferPool.d.ts +51 -0
- package/dist/web/webgpuBufferPool.js +58 -0
- package/dist/web/webgpuImageResidency.d.ts +53 -0
- package/dist/web/webgpuImageResidency.js +61 -0
- package/dist/web/webgpuLifetime.d.ts +45 -0
- package/dist/web/webgpuLifetime.js +64 -0
- package/dist/web/webgpuRetentionPool.d.ts +66 -0
- package/dist/web/webgpuRetentionPool.js +84 -0
- package/dist/web/webgpuSharedPass.d.ts +7 -0
- package/dist/web/webgpuSharedPass.js +211 -0
- package/dist/web/webgpuSharedResolvePass.d.ts +3 -0
- package/dist/web/webgpuSharedResolvePass.js +29 -0
- package/dist/web/world3d/Audio3D.d.ts +68 -0
- package/dist/web/world3d/Audio3D.js +212 -0
- package/dist/web/world3d/BrowserPlay3D.d.ts +182 -0
- package/dist/web/world3d/BrowserPlay3D.js +851 -0
- package/dist/web/world3d/FlyControls3D.d.ts +55 -0
- package/dist/web/world3d/FlyControls3D.js +160 -0
- package/dist/web/world3d/FollowControls3D.d.ts +28 -0
- package/dist/web/world3d/FollowControls3D.js +117 -0
- package/dist/web/world3d/OrbitControls3D.d.ts +55 -0
- package/dist/web/world3d/OrbitControls3D.js +187 -0
- package/dist/web/world3d/TransformControls3D.d.ts +34 -0
- package/dist/web/world3d/TransformControls3D.js +125 -0
- package/dist/web/world3d/WebGL2Renderer3D.d.ts +133 -0
- package/dist/web/world3d/WebGL2Renderer3D.js +1173 -0
- package/dist/web/world3d/WebGPURenderer3D.d.ts +110 -0
- package/dist/web/world3d/WebGPURenderer3D.js +1915 -0
- package/dist/web/world3d/World3DView.d.ts +51 -0
- package/dist/web/world3d/World3DView.js +77 -0
- package/dist/web/world3d/app3D.d.ts +136 -0
- package/dist/web/world3d/app3D.js +304 -0
- package/dist/web/world3d/assets3D.d.ts +38 -0
- package/dist/web/world3d/assets3D.js +112 -0
- package/dist/web/world3d/atmosphere3D.d.ts +47 -0
- package/dist/web/world3d/atmosphere3D.js +178 -0
- package/dist/web/world3d/frame3D.d.ts +142 -0
- package/dist/web/world3d/frame3D.js +388 -0
- package/dist/web/world3d/gpuResources3D.d.ts +98 -0
- package/dist/web/world3d/gpuResources3D.js +901 -0
- package/dist/web/world3d/host3D.d.ts +23 -0
- package/dist/web/world3d/host3D.js +61 -0
- package/dist/web/world3d/index.d.ts +38 -0
- package/dist/web/world3d/index.js +23 -0
- package/dist/web/world3d/loaders3D.d.ts +26 -0
- package/dist/web/world3d/loaders3D.js +138 -0
- package/dist/web/world3d/markers3D.d.ts +49 -0
- package/dist/web/world3d/markers3D.js +130 -0
- package/dist/web/world3d/materialWebGL23D.d.ts +34 -0
- package/dist/web/world3d/materialWebGL23D.js +218 -0
- package/dist/web/world3d/overlay3D.d.ts +28 -0
- package/dist/web/world3d/overlay3D.js +170 -0
- package/dist/web/world3d/post3D.d.ts +112 -0
- package/dist/web/world3d/post3D.js +459 -0
- package/dist/web/world3d/postWebGL23D.d.ts +24 -0
- package/dist/web/world3d/postWebGL23D.js +237 -0
- package/dist/web/world3d/postWebGPU3D.d.ts +23 -0
- package/dist/web/world3d/postWebGPU3D.js +255 -0
- package/dist/web/world3d/probe3D.d.ts +14 -0
- package/dist/web/world3d/probe3D.js +66 -0
- package/dist/web/world3d/shaders3D.d.ts +43 -0
- package/dist/web/world3d/shaders3D.js +851 -0
- package/dist/web/world3d/stats3D.d.ts +31 -0
- package/dist/web/world3d/stats3D.js +80 -0
- package/dist/web/world3d/webGL2FrameMetrics3D.d.ts +66 -0
- package/dist/web/world3d/webGL2FrameMetrics3D.js +185 -0
- package/dist/web/world3d/webgpu.d.ts +35 -0
- package/dist/web/world3d/webgpu.js +22 -0
- package/dist/web/world3d/webgpuPacking3D.d.ts +13 -0
- package/dist/web/world3d/webgpuPacking3D.js +85 -0
- package/dist/web/world3d/wgsl3D.d.ts +68 -0
- package/dist/web/world3d/wgsl3D.js +781 -0
- package/package.json +220 -4
|
@@ -0,0 +1,2835 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// A small rigid-body world for gameplay: spheres, boxes, capsules and planes;
|
|
3
|
+
// static, kinematic and dynamic bodies; a sweep-and-prune broad phase; an
|
|
4
|
+
// impulse solver with friction and restitution; sleeping; ray and overlap
|
|
5
|
+
// queries; and a capsule character controller. It is deliberately compact:
|
|
6
|
+
// good for pickups, projectiles, simple stacking and character movement, not
|
|
7
|
+
// a replacement for a full physics engine.
|
|
8
|
+
import { World3DError, requireFinite3D, requirePositive3D } from './core3D.js';
|
|
9
|
+
import { ConvexHullShape3D } from './PhysicsHull3D.js';
|
|
10
|
+
import { TriangleMeshShape3D } from './PhysicsMesh3D.js';
|
|
11
|
+
import { StaticTree, rayShape } from './PhysicsQueries3D.js';
|
|
12
|
+
export const raycastExcludingBody3D = Symbol('physics raycast excluding one body');
|
|
13
|
+
export const PHYSICS3D_LIMITS = Object.freeze({
|
|
14
|
+
maxBodies: 16384,
|
|
15
|
+
maxSubSteps: 8,
|
|
16
|
+
maxContactsPerPair: 8,
|
|
17
|
+
maxCompoundChildren: 32,
|
|
18
|
+
maxSolverSteps: 8
|
|
19
|
+
});
|
|
20
|
+
const v3 = (x = 0, y = 0, z = 0) => new Float64Array([x, y, z]);
|
|
21
|
+
const dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
|
|
22
|
+
function cross(out, a, b) {
|
|
23
|
+
const x = a[1] * b[2] - a[2] * b[1], y = a[2] * b[0] - a[0] * b[2], z = a[0] * b[1] - a[1] * b[0];
|
|
24
|
+
out[0] = x;
|
|
25
|
+
out[1] = y;
|
|
26
|
+
out[2] = z;
|
|
27
|
+
return out;
|
|
28
|
+
}
|
|
29
|
+
const clamp = (value, low, high) => (value < low ? low : value > high ? high : value);
|
|
30
|
+
let nextBodyId = 1;
|
|
31
|
+
export class RigidBody3D {
|
|
32
|
+
id = nextBodyId++;
|
|
33
|
+
type;
|
|
34
|
+
shape;
|
|
35
|
+
position = v3();
|
|
36
|
+
/** Unit quaternion, xyzw. Call wake() after writing position or orientation directly. */
|
|
37
|
+
quaternion = new Float64Array([0, 0, 0, 1]);
|
|
38
|
+
linearVelocity = v3();
|
|
39
|
+
angularVelocity = v3();
|
|
40
|
+
force = v3();
|
|
41
|
+
torque = v3();
|
|
42
|
+
mass;
|
|
43
|
+
inverseMass;
|
|
44
|
+
restitution;
|
|
45
|
+
friction;
|
|
46
|
+
linearDamping;
|
|
47
|
+
angularDamping;
|
|
48
|
+
isTrigger;
|
|
49
|
+
layer;
|
|
50
|
+
mask;
|
|
51
|
+
fixedRotation;
|
|
52
|
+
continuous;
|
|
53
|
+
node;
|
|
54
|
+
userData;
|
|
55
|
+
isSleeping = false;
|
|
56
|
+
/** @internal */ rotation = new Float64Array(9);
|
|
57
|
+
/** @internal */ inverseInertiaLocal = v3();
|
|
58
|
+
/** @internal */ inverseInertiaWorld = new Float64Array(9);
|
|
59
|
+
/** @internal */ aabb = new Float64Array(6);
|
|
60
|
+
/** @internal */ sleepTime = 0;
|
|
61
|
+
/** @internal */ slot = 0;
|
|
62
|
+
/** @internal */ world;
|
|
63
|
+
/** @internal Stand-in bodies for the parts of a compound shape, kept at their world poses by refresh(). */
|
|
64
|
+
parts = [];
|
|
65
|
+
/** @internal Local placement of each part: position, then quaternion. */
|
|
66
|
+
partPoses = [];
|
|
67
|
+
/** @internal Half extents of the box around a compound, used to weigh it. */
|
|
68
|
+
compoundHalf = v3();
|
|
69
|
+
constructor(options) {
|
|
70
|
+
const shape = options?.shape;
|
|
71
|
+
if (!shape)
|
|
72
|
+
throw new World3DError('WORLD3D_INPUT_INVALID', 'shape');
|
|
73
|
+
if (shape.type === 'sphere')
|
|
74
|
+
requirePositive3D(shape.radius, 'sphere radius');
|
|
75
|
+
else if (shape.type === 'box') {
|
|
76
|
+
for (const half of shape.halfExtents)
|
|
77
|
+
requirePositive3D(half, 'box halfExtents');
|
|
78
|
+
}
|
|
79
|
+
else if (shape.type === 'capsule') {
|
|
80
|
+
requirePositive3D(shape.radius, 'capsule radius');
|
|
81
|
+
if (!(requireFinite3D(shape.halfHeight, 'capsule halfHeight') >= 0))
|
|
82
|
+
throw new World3DError('WORLD3D_INPUT_INVALID', 'capsule halfHeight');
|
|
83
|
+
}
|
|
84
|
+
else if (shape.type === 'mesh') {
|
|
85
|
+
if (!(shape.mesh instanceof TriangleMeshShape3D))
|
|
86
|
+
throw new World3DError('WORLD3D_INPUT_INVALID', 'mesh shape');
|
|
87
|
+
}
|
|
88
|
+
else if (shape.type === 'convex') {
|
|
89
|
+
if (!(shape.hull instanceof ConvexHullShape3D))
|
|
90
|
+
throw new World3DError('WORLD3D_INPUT_INVALID', 'convex shape');
|
|
91
|
+
}
|
|
92
|
+
else if (shape.type === 'compound') {
|
|
93
|
+
if (!Array.isArray(shape.children) ||
|
|
94
|
+
shape.children.length === 0 ||
|
|
95
|
+
shape.children.length > PHYSICS3D_LIMITS.maxCompoundChildren)
|
|
96
|
+
throw new World3DError('WORLD3D_INPUT_INVALID', 'compound children');
|
|
97
|
+
const half = this.compoundHalf;
|
|
98
|
+
for (const child of shape.children) {
|
|
99
|
+
const kind = child?.shape?.type;
|
|
100
|
+
if (kind !== 'sphere' && kind !== 'box' && kind !== 'capsule' && kind !== 'convex')
|
|
101
|
+
throw new World3DError('WORLD3D_INPUT_INVALID', 'compound parts are spheres, boxes, capsules or convex hulls');
|
|
102
|
+
const at = child.position ?? [0, 0, 0], turn = child.quaternion ?? [0, 0, 0, 1], size = Math.sqrt(turn[0] * turn[0] + turn[1] * turn[1] + turn[2] * turn[2] + turn[3] * turn[3]);
|
|
103
|
+
for (const value of at)
|
|
104
|
+
requireFinite3D(value, 'compound child position');
|
|
105
|
+
if (!(size > 0))
|
|
106
|
+
throw new World3DError('WORLD3D_INPUT_INVALID', 'compound child quaternion');
|
|
107
|
+
// The part is validated by building it; it never joins a world of its own.
|
|
108
|
+
const part = new RigidBody3D({
|
|
109
|
+
type: 'kinematic',
|
|
110
|
+
shape: child.shape,
|
|
111
|
+
position: at,
|
|
112
|
+
quaternion: [turn[0] / size, turn[1] / size, turn[2] / size, turn[3] / size]
|
|
113
|
+
});
|
|
114
|
+
this.parts.push(part);
|
|
115
|
+
this.partPoses.push(new Float64Array([at[0], at[1], at[2], turn[0] / size, turn[1] / size, turn[2] / size, turn[3] / size]));
|
|
116
|
+
const box = part.aabb;
|
|
117
|
+
half[0] = Math.max(half[0], Math.abs(box[0]), Math.abs(box[3]));
|
|
118
|
+
half[1] = Math.max(half[1], Math.abs(box[1]), Math.abs(box[4]));
|
|
119
|
+
half[2] = Math.max(half[2], Math.abs(box[2]), Math.abs(box[5]));
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
else if (shape.type !== 'plane')
|
|
123
|
+
throw new World3DError('WORLD3D_INPUT_INVALID', 'shape type');
|
|
124
|
+
this.shape = shape;
|
|
125
|
+
const fixed = shape.type === 'plane' || shape.type === 'mesh';
|
|
126
|
+
this.type = options.type ?? (fixed ? 'static' : 'dynamic');
|
|
127
|
+
if (shape.type === 'plane' ? this.type !== 'static' : shape.type === 'mesh' && this.type === 'dynamic')
|
|
128
|
+
throw new World3DError('WORLD3D_INPUT_INVALID', 'planes must be static and meshes static or kinematic');
|
|
129
|
+
this.mass = this.type === 'dynamic' ? requirePositive3D(options.mass ?? 1, 'mass') : 0;
|
|
130
|
+
this.inverseMass = this.mass > 0 ? 1 / this.mass : 0;
|
|
131
|
+
if (options.position)
|
|
132
|
+
this.position.set(options.position);
|
|
133
|
+
if (options.quaternion)
|
|
134
|
+
this.quaternion.set(options.quaternion);
|
|
135
|
+
this.restitution = options.restitution ?? 0.2;
|
|
136
|
+
this.friction = options.friction ?? 0.5;
|
|
137
|
+
this.linearDamping = options.linearDamping ?? 0.01;
|
|
138
|
+
this.angularDamping = options.angularDamping ?? 0.05;
|
|
139
|
+
this.isTrigger = options.isTrigger ?? false;
|
|
140
|
+
this.layer = options.layer ?? 1;
|
|
141
|
+
this.mask = options.mask ?? -1;
|
|
142
|
+
this.fixedRotation = options.fixedRotation ?? false;
|
|
143
|
+
this.continuous = options.continuous ?? false;
|
|
144
|
+
this.node = options.node;
|
|
145
|
+
this.userData = options.userData;
|
|
146
|
+
if (this.type === 'dynamic') {
|
|
147
|
+
// Principal moments of inertia about the local axes.
|
|
148
|
+
const m = this.mass;
|
|
149
|
+
let ix, iy, iz;
|
|
150
|
+
if (shape.type === 'sphere')
|
|
151
|
+
ix = iy = iz = 0.4 * m * shape.radius * shape.radius;
|
|
152
|
+
else if (shape.type === 'box' || shape.type === 'convex' || shape.type === 'compound') {
|
|
153
|
+
// A hull or a compound is weighed like the box around it.
|
|
154
|
+
const [x, y, z] = shape.type === 'box'
|
|
155
|
+
? shape.halfExtents
|
|
156
|
+
: shape.type === 'convex'
|
|
157
|
+
? shape.hull.halfExtents
|
|
158
|
+
: this.compoundHalf;
|
|
159
|
+
ix = (m / 3) * (y * y + z * z);
|
|
160
|
+
iy = (m / 3) * (x * x + z * z);
|
|
161
|
+
iz = (m / 3) * (x * x + y * y);
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
// Treat the capsule as a cylinder spanning its full length.
|
|
165
|
+
const capsule = shape, r = capsule.radius, length = 2 * (capsule.halfHeight + r);
|
|
166
|
+
iy = 0.5 * m * r * r;
|
|
167
|
+
ix = iz = (m * (3 * r * r + length * length)) / 12;
|
|
168
|
+
}
|
|
169
|
+
this.inverseInertiaLocal.set([1 / ix, 1 / iy, 1 / iz]);
|
|
170
|
+
}
|
|
171
|
+
this.refresh();
|
|
172
|
+
}
|
|
173
|
+
wake() {
|
|
174
|
+
this.isSleeping = false;
|
|
175
|
+
this.sleepTime = 0;
|
|
176
|
+
}
|
|
177
|
+
setPosition(x, y, z) {
|
|
178
|
+
this.position[0] = x;
|
|
179
|
+
this.position[1] = y;
|
|
180
|
+
this.position[2] = z;
|
|
181
|
+
this.wake();
|
|
182
|
+
this.refresh();
|
|
183
|
+
this.world?.staticMoved(this);
|
|
184
|
+
return this;
|
|
185
|
+
}
|
|
186
|
+
setQuaternion(x, y, z, w) {
|
|
187
|
+
const length = Math.sqrt(x * x + y * y + z * z + w * w) || 1;
|
|
188
|
+
this.quaternion[0] = x / length;
|
|
189
|
+
this.quaternion[1] = y / length;
|
|
190
|
+
this.quaternion[2] = z / length;
|
|
191
|
+
this.quaternion[3] = w / length;
|
|
192
|
+
this.wake();
|
|
193
|
+
this.refresh();
|
|
194
|
+
this.world?.staticMoved(this);
|
|
195
|
+
return this;
|
|
196
|
+
}
|
|
197
|
+
setLinearVelocity(x, y, z) {
|
|
198
|
+
this.linearVelocity[0] = x;
|
|
199
|
+
this.linearVelocity[1] = y;
|
|
200
|
+
this.linearVelocity[2] = z;
|
|
201
|
+
this.wake();
|
|
202
|
+
return this;
|
|
203
|
+
}
|
|
204
|
+
setAngularVelocity(x, y, z) {
|
|
205
|
+
this.angularVelocity[0] = x;
|
|
206
|
+
this.angularVelocity[1] = y;
|
|
207
|
+
this.angularVelocity[2] = z;
|
|
208
|
+
this.wake();
|
|
209
|
+
return this;
|
|
210
|
+
}
|
|
211
|
+
/** Accumulate a force through the center of mass; cleared after each step. */
|
|
212
|
+
applyForce(x, y, z) {
|
|
213
|
+
this.force[0] += x;
|
|
214
|
+
this.force[1] += y;
|
|
215
|
+
this.force[2] += z;
|
|
216
|
+
this.wake();
|
|
217
|
+
}
|
|
218
|
+
applyTorque(x, y, z) {
|
|
219
|
+
this.torque[0] += x;
|
|
220
|
+
this.torque[1] += y;
|
|
221
|
+
this.torque[2] += z;
|
|
222
|
+
this.wake();
|
|
223
|
+
}
|
|
224
|
+
/** Change velocity at once; give `point` in world space to add spin. */
|
|
225
|
+
applyImpulse(x, y, z, point) {
|
|
226
|
+
if (this.type !== 'dynamic')
|
|
227
|
+
return;
|
|
228
|
+
this.wake();
|
|
229
|
+
this.linearVelocity[0] += x * this.inverseMass;
|
|
230
|
+
this.linearVelocity[1] += y * this.inverseMass;
|
|
231
|
+
this.linearVelocity[2] += z * this.inverseMass;
|
|
232
|
+
if (point && !this.fixedRotation) {
|
|
233
|
+
const r = [point[0] - this.position[0], point[1] - this.position[1], point[2] - this.position[2]];
|
|
234
|
+
cross(scratchA, r, [x, y, z]);
|
|
235
|
+
applyInverseInertia(scratchB, this.inverseInertiaWorld, scratchA);
|
|
236
|
+
this.angularVelocity[0] += scratchB[0];
|
|
237
|
+
this.angularVelocity[1] += scratchB[1];
|
|
238
|
+
this.angularVelocity[2] += scratchB[2];
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
/** @internal Recompute the rotation matrix, world inertia and bounds from the pose. */
|
|
242
|
+
refresh() {
|
|
243
|
+
const [x, y, z, w] = this.quaternion, r = this.rotation;
|
|
244
|
+
r[0] = 1 - 2 * (y * y + z * z);
|
|
245
|
+
r[1] = 2 * (x * y - z * w);
|
|
246
|
+
r[2] = 2 * (x * z + y * w);
|
|
247
|
+
r[3] = 2 * (x * y + z * w);
|
|
248
|
+
r[4] = 1 - 2 * (x * x + z * z);
|
|
249
|
+
r[5] = 2 * (y * z - x * w);
|
|
250
|
+
r[6] = 2 * (x * z - y * w);
|
|
251
|
+
r[7] = 2 * (y * z + x * w);
|
|
252
|
+
r[8] = 1 - 2 * (x * x + y * y);
|
|
253
|
+
const inv = this.inverseInertiaLocal, world = this.inverseInertiaWorld;
|
|
254
|
+
if (this.type === 'dynamic' && !this.fixedRotation) {
|
|
255
|
+
// R * diag(inv) * R^T
|
|
256
|
+
for (let i = 0; i < 3; i++)
|
|
257
|
+
for (let j = 0; j < 3; j++)
|
|
258
|
+
world[i * 3 + j] =
|
|
259
|
+
r[i * 3] * inv[0] * r[j * 3] +
|
|
260
|
+
r[i * 3 + 1] * inv[1] * r[j * 3 + 1] +
|
|
261
|
+
r[i * 3 + 2] * inv[2] * r[j * 3 + 2];
|
|
262
|
+
}
|
|
263
|
+
else
|
|
264
|
+
world.fill(0);
|
|
265
|
+
const p = this.position, box = this.aabb, shape = this.shape;
|
|
266
|
+
let ex, ey, ez;
|
|
267
|
+
if (shape.type === 'compound') {
|
|
268
|
+
// Each part follows the body; the bounds are the union of theirs.
|
|
269
|
+
box[0] = box[1] = box[2] = Infinity;
|
|
270
|
+
box[3] = box[4] = box[5] = -Infinity;
|
|
271
|
+
for (let i = 0; i < this.parts.length; i++) {
|
|
272
|
+
const part = this.parts[i], pose = this.partPoses[i], lx = pose[0], ly = pose[1], lz = pose[2], a = pose[3], b = pose[4], c = pose[5], d = pose[6];
|
|
273
|
+
part.position[0] = p[0] + r[0] * lx + r[1] * ly + r[2] * lz;
|
|
274
|
+
part.position[1] = p[1] + r[3] * lx + r[4] * ly + r[5] * lz;
|
|
275
|
+
part.position[2] = p[2] + r[6] * lx + r[7] * ly + r[8] * lz;
|
|
276
|
+
const q = part.quaternion;
|
|
277
|
+
q[0] = w * a + x * d + y * c - z * b;
|
|
278
|
+
q[1] = w * b - x * c + y * d + z * a;
|
|
279
|
+
q[2] = w * c + x * b - y * a + z * d;
|
|
280
|
+
q[3] = w * d - x * a - y * b - z * c;
|
|
281
|
+
part.refresh();
|
|
282
|
+
const other = part.aabb;
|
|
283
|
+
for (let k = 0; k < 3; k++) {
|
|
284
|
+
box[k] = Math.min(box[k], other[k]);
|
|
285
|
+
box[k + 3] = Math.max(box[k + 3], other[k + 3]);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
if (shape.type === 'sphere')
|
|
291
|
+
ex = ey = ez = shape.radius;
|
|
292
|
+
else if (shape.type === 'convex')
|
|
293
|
+
ex = ey = ez = shape.hull.radius;
|
|
294
|
+
else if (shape.type === 'box') {
|
|
295
|
+
const h = shape.halfExtents;
|
|
296
|
+
ex = Math.abs(r[0]) * h[0] + Math.abs(r[1]) * h[1] + Math.abs(r[2]) * h[2];
|
|
297
|
+
ey = Math.abs(r[3]) * h[0] + Math.abs(r[4]) * h[1] + Math.abs(r[5]) * h[2];
|
|
298
|
+
ez = Math.abs(r[6]) * h[0] + Math.abs(r[7]) * h[1] + Math.abs(r[8]) * h[2];
|
|
299
|
+
}
|
|
300
|
+
else if (shape.type === 'capsule') {
|
|
301
|
+
ex = Math.abs(r[1]) * shape.halfHeight + shape.radius;
|
|
302
|
+
ey = Math.abs(r[4]) * shape.halfHeight + shape.radius;
|
|
303
|
+
ez = Math.abs(r[7]) * shape.halfHeight + shape.radius;
|
|
304
|
+
}
|
|
305
|
+
else if (shape.type === 'mesh') {
|
|
306
|
+
// Rotate the local bounds about their own center.
|
|
307
|
+
const b = shape.mesh.bounds, cx = (b[0] + b[3]) / 2, cy = (b[1] + b[4]) / 2, cz = (b[2] + b[5]) / 2, hx = (b[3] - b[0]) / 2, hy = (b[4] - b[1]) / 2, hz = (b[5] - b[2]) / 2;
|
|
308
|
+
ex = Math.abs(r[0]) * hx + Math.abs(r[1]) * hy + Math.abs(r[2]) * hz;
|
|
309
|
+
ey = Math.abs(r[3]) * hx + Math.abs(r[4]) * hy + Math.abs(r[5]) * hz;
|
|
310
|
+
ez = Math.abs(r[6]) * hx + Math.abs(r[7]) * hy + Math.abs(r[8]) * hz;
|
|
311
|
+
const wx = p[0] + r[0] * cx + r[1] * cy + r[2] * cz, wy = p[1] + r[3] * cx + r[4] * cy + r[5] * cz, wz = p[2] + r[6] * cx + r[7] * cy + r[8] * cz;
|
|
312
|
+
box[0] = wx - ex;
|
|
313
|
+
box[1] = wy - ey;
|
|
314
|
+
box[2] = wz - ez;
|
|
315
|
+
box[3] = wx + ex;
|
|
316
|
+
box[4] = wy + ey;
|
|
317
|
+
box[5] = wz + ez;
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
else
|
|
321
|
+
ex = ey = ez = Infinity;
|
|
322
|
+
box[0] = p[0] - ex;
|
|
323
|
+
box[1] = p[1] - ey;
|
|
324
|
+
box[2] = p[2] - ez;
|
|
325
|
+
box[3] = p[0] + ex;
|
|
326
|
+
box[4] = p[1] + ey;
|
|
327
|
+
box[5] = p[2] + ez;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
const scratchA = v3(), scratchB = v3(), scratchC = v3(), scratchD = v3();
|
|
331
|
+
function applyInverseInertia(out, m, v) {
|
|
332
|
+
const x = v[0], y = v[1], z = v[2];
|
|
333
|
+
out[0] = m[0] * x + m[1] * y + m[2] * z;
|
|
334
|
+
out[1] = m[3] * x + m[4] * y + m[5] * z;
|
|
335
|
+
out[2] = m[6] * x + m[7] * y + m[8] * z;
|
|
336
|
+
return out;
|
|
337
|
+
}
|
|
338
|
+
/** Contacts for one pair; normals point from the first body toward the second. */
|
|
339
|
+
class Manifold {
|
|
340
|
+
count = 0;
|
|
341
|
+
/** Identifies which part of the shapes the next contacts come from, so impulses can be matched across steps. */
|
|
342
|
+
feature = 0;
|
|
343
|
+
contacts = Array.from({ length: 8 }, () => ({
|
|
344
|
+
px: 0,
|
|
345
|
+
py: 0,
|
|
346
|
+
pz: 0,
|
|
347
|
+
nx: 0,
|
|
348
|
+
ny: 0,
|
|
349
|
+
nz: 0,
|
|
350
|
+
depth: 0,
|
|
351
|
+
feature: 0
|
|
352
|
+
}));
|
|
353
|
+
add(px, py, pz, nx, ny, nz, depth) {
|
|
354
|
+
if (this.count >= this.contacts.length)
|
|
355
|
+
return;
|
|
356
|
+
const c = this.contacts[this.count++];
|
|
357
|
+
c.px = px;
|
|
358
|
+
c.py = py;
|
|
359
|
+
c.pz = pz;
|
|
360
|
+
c.nx = nx;
|
|
361
|
+
c.ny = ny;
|
|
362
|
+
c.nz = nz;
|
|
363
|
+
c.depth = depth;
|
|
364
|
+
c.feature = this.feature;
|
|
365
|
+
}
|
|
366
|
+
/** Like add(), but when full it replaces the shallowest contact if the new one is deeper. */
|
|
367
|
+
addDeep(px, py, pz, nx, ny, nz, depth) {
|
|
368
|
+
if (this.count < this.contacts.length) {
|
|
369
|
+
this.add(px, py, pz, nx, ny, nz, depth);
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
let shallow = 0;
|
|
373
|
+
for (let i = 1; i < this.count; i++)
|
|
374
|
+
if (this.contacts[i].depth < this.contacts[shallow].depth)
|
|
375
|
+
shallow = i;
|
|
376
|
+
const c = this.contacts[shallow];
|
|
377
|
+
if (depth <= c.depth)
|
|
378
|
+
return;
|
|
379
|
+
c.px = px;
|
|
380
|
+
c.py = py;
|
|
381
|
+
c.pz = pz;
|
|
382
|
+
c.nx = nx;
|
|
383
|
+
c.ny = ny;
|
|
384
|
+
c.nz = nz;
|
|
385
|
+
c.depth = depth;
|
|
386
|
+
c.feature = this.feature;
|
|
387
|
+
}
|
|
388
|
+
/** Keep the deepest contacts and reverse normals when the pair was tested in swapped order. */
|
|
389
|
+
finish(flip) {
|
|
390
|
+
if (this.count > PHYSICS3D_LIMITS.maxContactsPerPair) {
|
|
391
|
+
const kept = this.contacts.slice(0, this.count).sort((a, b) => b.depth - a.depth);
|
|
392
|
+
for (let i = 0; i < this.count; i++)
|
|
393
|
+
Object.assign(this.contacts[i], kept[i]);
|
|
394
|
+
this.count = PHYSICS3D_LIMITS.maxContactsPerPair;
|
|
395
|
+
}
|
|
396
|
+
if (flip)
|
|
397
|
+
for (let i = 0; i < this.count; i++) {
|
|
398
|
+
const c = this.contacts[i];
|
|
399
|
+
c.nx = -c.nx;
|
|
400
|
+
c.ny = -c.ny;
|
|
401
|
+
c.nz = -c.nz;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
const segA = v3(), segB = v3(), segC = v3(), segD = v3(), local = v3(), closest = v3();
|
|
406
|
+
function capsuleSegment(body, a, b) {
|
|
407
|
+
const h = body.shape.halfHeight, r = body.rotation, p = body.position;
|
|
408
|
+
a[0] = p[0] - r[1] * h;
|
|
409
|
+
a[1] = p[1] - r[4] * h;
|
|
410
|
+
a[2] = p[2] - r[7] * h;
|
|
411
|
+
b[0] = p[0] + r[1] * h;
|
|
412
|
+
b[1] = p[1] + r[4] * h;
|
|
413
|
+
b[2] = p[2] + r[7] * h;
|
|
414
|
+
}
|
|
415
|
+
function sphereSphere(m, ax, ay, az, ra, bx, by, bz, rb) {
|
|
416
|
+
const dx = bx - ax, dy = by - ay, dz = bz - az, d2 = dx * dx + dy * dy + dz * dz, sum = ra + rb;
|
|
417
|
+
if (d2 >= sum * sum)
|
|
418
|
+
return;
|
|
419
|
+
const d = Math.sqrt(d2), nx = d > 1e-9 ? dx / d : 0, ny = d > 1e-9 ? dy / d : 1, nz = d > 1e-9 ? dz / d : 0;
|
|
420
|
+
m.add(ax + nx * (ra - (sum - d) * 0.5), ay + ny * (ra - (sum - d) * 0.5), az + nz * (ra - (sum - d) * 0.5), nx, ny, nz, sum - d);
|
|
421
|
+
}
|
|
422
|
+
/** Sphere (first) against a plane body (second). */
|
|
423
|
+
function spherePlane(m, x, y, z, radius, plane) {
|
|
424
|
+
const r = plane.rotation, nx = r[1], ny = r[4], nz = r[7], p = plane.position;
|
|
425
|
+
const distance = (x - p[0]) * nx + (y - p[1]) * ny + (z - p[2]) * nz;
|
|
426
|
+
if (distance >= radius)
|
|
427
|
+
return;
|
|
428
|
+
m.add(x - nx * distance, y - ny * distance, z - nz * distance, -nx, -ny, -nz, radius - distance);
|
|
429
|
+
}
|
|
430
|
+
/** Sphere (first) against a box body (second). */
|
|
431
|
+
function sphereBox(m, x, y, z, radius, box) {
|
|
432
|
+
const r = box.rotation, p = box.position, h = box.shape.halfExtents;
|
|
433
|
+
const dx = x - p[0], dy = y - p[1], dz = z - p[2];
|
|
434
|
+
local[0] = r[0] * dx + r[3] * dy + r[6] * dz;
|
|
435
|
+
local[1] = r[1] * dx + r[4] * dy + r[7] * dz;
|
|
436
|
+
local[2] = r[2] * dx + r[5] * dy + r[8] * dz;
|
|
437
|
+
let inside = true;
|
|
438
|
+
for (let i = 0; i < 3; i++) {
|
|
439
|
+
const limit = h[i];
|
|
440
|
+
closest[i] = clamp(local[i], -limit, limit);
|
|
441
|
+
if (closest[i] !== local[i])
|
|
442
|
+
inside = false;
|
|
443
|
+
}
|
|
444
|
+
let lx, ly, lz, depth;
|
|
445
|
+
if (inside) {
|
|
446
|
+
// Center inside the box: leave through the nearest face.
|
|
447
|
+
let axis = 0, best = Infinity;
|
|
448
|
+
for (let i = 0; i < 3; i++) {
|
|
449
|
+
const gap = h[i] - Math.abs(local[i]);
|
|
450
|
+
if (gap < best) {
|
|
451
|
+
best = gap;
|
|
452
|
+
axis = i;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
const sign = local[axis] >= 0 ? 1 : -1;
|
|
456
|
+
lx = axis === 0 ? sign : 0;
|
|
457
|
+
ly = axis === 1 ? sign : 0;
|
|
458
|
+
lz = axis === 2 ? sign : 0;
|
|
459
|
+
depth = best + radius;
|
|
460
|
+
}
|
|
461
|
+
else {
|
|
462
|
+
lx = local[0] - closest[0];
|
|
463
|
+
ly = local[1] - closest[1];
|
|
464
|
+
lz = local[2] - closest[2];
|
|
465
|
+
const d = Math.sqrt(lx * lx + ly * ly + lz * lz);
|
|
466
|
+
if (d >= radius)
|
|
467
|
+
return;
|
|
468
|
+
lx /= d;
|
|
469
|
+
ly /= d;
|
|
470
|
+
lz /= d;
|
|
471
|
+
depth = radius - d;
|
|
472
|
+
}
|
|
473
|
+
// Local normal points from the box toward the sphere; the manifold wants sphere -> box.
|
|
474
|
+
const nx = r[0] * lx + r[1] * ly + r[2] * lz, ny = r[3] * lx + r[4] * ly + r[5] * lz, nz = r[6] * lx + r[7] * ly + r[8] * lz;
|
|
475
|
+
m.add(x - nx * (radius - depth * 0.5), y - ny * (radius - depth * 0.5), z - nz * (radius - depth * 0.5), -nx, -ny, -nz, depth);
|
|
476
|
+
}
|
|
477
|
+
function closestOnSegment(out, a, b, x, y, z) {
|
|
478
|
+
const dx = b[0] - a[0], dy = b[1] - a[1], dz = b[2] - a[2], length2 = dx * dx + dy * dy + dz * dz;
|
|
479
|
+
const t = length2 > 0 ? clamp(((x - a[0]) * dx + (y - a[1]) * dy + (z - a[2]) * dz) / length2, 0, 1) : 0;
|
|
480
|
+
out[0] = a[0] + dx * t;
|
|
481
|
+
out[1] = a[1] + dy * t;
|
|
482
|
+
out[2] = a[2] + dz * t;
|
|
483
|
+
return t;
|
|
484
|
+
}
|
|
485
|
+
/** Closest points between segments p1-q1 and p2-q2, written to c1 and c2. */
|
|
486
|
+
function closestBetweenSegments(p1, q1, p2, q2, c1, c2) {
|
|
487
|
+
const ux = q1[0] - p1[0], uy = q1[1] - p1[1], uz = q1[2] - p1[2];
|
|
488
|
+
const vx = q2[0] - p2[0], vy = q2[1] - p2[1], vz = q2[2] - p2[2];
|
|
489
|
+
const rx = p1[0] - p2[0], ry = p1[1] - p2[1], rz = p1[2] - p2[2];
|
|
490
|
+
const a = ux * ux + uy * uy + uz * uz, e = vx * vx + vy * vy + vz * vz, f = vx * rx + vy * ry + vz * rz;
|
|
491
|
+
let s = 0, t = 0;
|
|
492
|
+
if (a <= 1e-12 && e <= 1e-12) {
|
|
493
|
+
s = t = 0;
|
|
494
|
+
}
|
|
495
|
+
else if (a <= 1e-12)
|
|
496
|
+
t = clamp(f / e, 0, 1);
|
|
497
|
+
else {
|
|
498
|
+
const c = ux * rx + uy * ry + uz * rz;
|
|
499
|
+
if (e <= 1e-12)
|
|
500
|
+
s = clamp(-c / a, 0, 1);
|
|
501
|
+
else {
|
|
502
|
+
const b = ux * vx + uy * vy + uz * vz, denominator = a * e - b * b;
|
|
503
|
+
s = denominator > 1e-12 ? clamp((b * f - c * e) / denominator, 0, 1) : 0.5;
|
|
504
|
+
t = (b * s + f) / e;
|
|
505
|
+
if (t < 0) {
|
|
506
|
+
t = 0;
|
|
507
|
+
s = clamp(-c / a, 0, 1);
|
|
508
|
+
}
|
|
509
|
+
else if (t > 1) {
|
|
510
|
+
t = 1;
|
|
511
|
+
s = clamp((b - c) / a, 0, 1);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
c1[0] = p1[0] + ux * s;
|
|
516
|
+
c1[1] = p1[1] + uy * s;
|
|
517
|
+
c1[2] = p1[2] + uz * s;
|
|
518
|
+
c2[0] = p2[0] + vx * t;
|
|
519
|
+
c2[1] = p2[1] + vy * t;
|
|
520
|
+
c2[2] = p2[2] + vz * t;
|
|
521
|
+
}
|
|
522
|
+
function capsuleBox(m, capsule, box) {
|
|
523
|
+
const radius = capsule.shape.radius;
|
|
524
|
+
capsuleSegment(capsule, segA, segB);
|
|
525
|
+
// Walk toward the segment point nearest the box, then test that point and both caps
|
|
526
|
+
// so a capsule lying on a face gets support along its whole length.
|
|
527
|
+
const r = box.rotation, p = box.position, h = box.shape.halfExtents;
|
|
528
|
+
let t = closestOnSegment(segC, segA, segB, p[0], p[1], p[2]);
|
|
529
|
+
for (let i = 0; i < 4; i++) {
|
|
530
|
+
const dx = segC[0] - p[0], dy = segC[1] - p[1], dz = segC[2] - p[2];
|
|
531
|
+
const lx = clamp(r[0] * dx + r[3] * dy + r[6] * dz, -h[0], h[0]), ly = clamp(r[1] * dx + r[4] * dy + r[7] * dz, -h[1], h[1]), lz = clamp(r[2] * dx + r[5] * dy + r[8] * dz, -h[2], h[2]);
|
|
532
|
+
t = closestOnSegment(segC, segA, segB, p[0] + r[0] * lx + r[1] * ly + r[2] * lz, p[1] + r[3] * lx + r[4] * ly + r[5] * lz, p[2] + r[6] * lx + r[7] * ly + r[8] * lz);
|
|
533
|
+
}
|
|
534
|
+
if (t > 0.05 && t < 0.95)
|
|
535
|
+
sphereBox(m, segC[0], segC[1], segC[2], radius, box);
|
|
536
|
+
m.feature = 1;
|
|
537
|
+
sphereBox(m, segA[0], segA[1], segA[2], radius, box);
|
|
538
|
+
m.feature = 2;
|
|
539
|
+
sphereBox(m, segB[0], segB[1], segB[2], radius, box);
|
|
540
|
+
}
|
|
541
|
+
/**
|
|
542
|
+
* Box corners this close to a surface join the manifold before they touch. The solver only
|
|
543
|
+
* stops them from closing the gap within one step, which keeps resting faces from rocking.
|
|
544
|
+
*/
|
|
545
|
+
const MARGIN = 0.02;
|
|
546
|
+
const corner = v3();
|
|
547
|
+
function boxCorner(out, box, index) {
|
|
548
|
+
const r = box.rotation, p = box.position, h = box.shape.halfExtents;
|
|
549
|
+
const x = index & 1 ? h[0] : -h[0], y = index & 2 ? h[1] : -h[1], z = index & 4 ? h[2] : -h[2];
|
|
550
|
+
out[0] = p[0] + r[0] * x + r[1] * y + r[2] * z;
|
|
551
|
+
out[1] = p[1] + r[3] * x + r[4] * y + r[5] * z;
|
|
552
|
+
out[2] = p[2] + r[6] * x + r[7] * y + r[8] * z;
|
|
553
|
+
return out;
|
|
554
|
+
}
|
|
555
|
+
function boxPlane(m, box, plane) {
|
|
556
|
+
const r = plane.rotation, nx = r[1], ny = r[4], nz = r[7], p = plane.position;
|
|
557
|
+
for (let i = 0; i < 8; i++) {
|
|
558
|
+
boxCorner(corner, box, i);
|
|
559
|
+
m.feature = i;
|
|
560
|
+
const distance = (corner[0] - p[0]) * nx + (corner[1] - p[1]) * ny + (corner[2] - p[2]) * nz;
|
|
561
|
+
if (distance < MARGIN)
|
|
562
|
+
m.add(corner[0] - nx * distance * 0.5, corner[1] - ny * distance * 0.5, corner[2] - nz * distance * 0.5, -nx, -ny, -nz, -distance);
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
const axisScratch = v3(), bestAxis = v3(), faceA = new Float64Array(32), faceB = new Float64Array(32);
|
|
566
|
+
function boxProjection(box, axis) {
|
|
567
|
+
const r = box.rotation, h = box.shape.halfExtents;
|
|
568
|
+
return (Math.abs(axis[0] * r[0] + axis[1] * r[3] + axis[2] * r[6]) * h[0] +
|
|
569
|
+
Math.abs(axis[0] * r[1] + axis[1] * r[4] + axis[2] * r[7]) * h[1] +
|
|
570
|
+
Math.abs(axis[0] * r[2] + axis[1] * r[5] + axis[2] * r[8]) * h[2]);
|
|
571
|
+
}
|
|
572
|
+
function insideBox(box, point, tolerance) {
|
|
573
|
+
const r = box.rotation, p = box.position, h = box.shape.halfExtents;
|
|
574
|
+
const dx = point[0] - p[0], dy = point[1] - p[1], dz = point[2] - p[2];
|
|
575
|
+
return (Math.abs(r[0] * dx + r[3] * dy + r[6] * dz) <= h[0] + tolerance &&
|
|
576
|
+
Math.abs(r[1] * dx + r[4] * dy + r[7] * dz) <= h[1] + tolerance &&
|
|
577
|
+
Math.abs(r[2] * dx + r[5] * dy + r[8] * dz) <= h[2] + tolerance);
|
|
578
|
+
}
|
|
579
|
+
function boxBox(m, a, b) {
|
|
580
|
+
const ra = a.rotation, rb = b.rotation, d = [b.position[0] - a.position[0], b.position[1] - a.position[1], b.position[2] - a.position[2]];
|
|
581
|
+
let best = Infinity, bestDepth = 0, bestIndex = -1;
|
|
582
|
+
// Separating-axis test over the 6 face normals and 9 edge cross products.
|
|
583
|
+
const test = (x, y, z, bias, index) => {
|
|
584
|
+
const length = Math.sqrt(x * x + y * y + z * z);
|
|
585
|
+
if (length < 1e-6)
|
|
586
|
+
return true;
|
|
587
|
+
axisScratch[0] = x / length;
|
|
588
|
+
axisScratch[1] = y / length;
|
|
589
|
+
axisScratch[2] = z / length;
|
|
590
|
+
const overlap = boxProjection(a, axisScratch) + boxProjection(b, axisScratch) - Math.abs(dot(axisScratch, d));
|
|
591
|
+
if (overlap <= 0)
|
|
592
|
+
return false;
|
|
593
|
+
// Face axes win ties so resting contacts stay on faces.
|
|
594
|
+
if (overlap * bias < best) {
|
|
595
|
+
best = overlap * bias;
|
|
596
|
+
bestAxis.set(axisScratch);
|
|
597
|
+
bestDepth = overlap;
|
|
598
|
+
bestIndex = index;
|
|
599
|
+
}
|
|
600
|
+
return true;
|
|
601
|
+
};
|
|
602
|
+
// The first box's faces win near-ties, so two parallel faces do not swap roles from step to step.
|
|
603
|
+
for (let i = 0; i < 3; i++)
|
|
604
|
+
if (!test(ra[i], ra[3 + i], ra[6 + i], 1, i) || !test(rb[i], rb[3 + i], rb[6 + i], 1.02, 3 + i))
|
|
605
|
+
return;
|
|
606
|
+
for (let i = 0; i < 3; i++)
|
|
607
|
+
for (let j = 0; j < 3; j++) {
|
|
608
|
+
const ax = ra[i], ay = ra[3 + i], az = ra[6 + i], bx = rb[j], by = rb[3 + j], bz = rb[6 + j];
|
|
609
|
+
if (!test(ay * bz - az * by, az * bx - ax * bz, ax * by - ay * bx, 1.05, 6))
|
|
610
|
+
return;
|
|
611
|
+
}
|
|
612
|
+
if (dot(bestAxis, d) < 0) {
|
|
613
|
+
bestAxis[0] = -bestAxis[0];
|
|
614
|
+
bestAxis[1] = -bestAxis[1];
|
|
615
|
+
bestAxis[2] = -bestAxis[2];
|
|
616
|
+
}
|
|
617
|
+
const nx = bestAxis[0], ny = bestAxis[1], nz = bestAxis[2], extentA = boxProjection(a, bestAxis), extentB = boxProjection(b, bestAxis);
|
|
618
|
+
const before = m.count;
|
|
619
|
+
if (bestIndex < 6) {
|
|
620
|
+
// Face against face, edge or corner: the part of the other box's nearest face that lies over this face.
|
|
621
|
+
const onA = bestIndex < 3, reference = onA ? a : b, incident = onA ? b : a, rr = reference.rotation, ri = incident.rotation, axis = bestIndex % 3;
|
|
622
|
+
const hr = reference.shape.halfExtents, hi = incident.shape.halfExtents;
|
|
623
|
+
// Outward normal of the reference face, toward the incident box.
|
|
624
|
+
const fx = onA ? nx : -nx, fy = onA ? ny : -ny, fz = onA ? nz : -nz;
|
|
625
|
+
// The incident face is the one that most squarely faces the reference face.
|
|
626
|
+
let facing = 0, turned = 0;
|
|
627
|
+
for (let j = 0; j < 3; j++) {
|
|
628
|
+
const along = ri[j] * fx + ri[3 + j] * fy + ri[6 + j] * fz;
|
|
629
|
+
if (Math.abs(along) > Math.abs(turned)) {
|
|
630
|
+
turned = along;
|
|
631
|
+
facing = j;
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
const side = turned > 0 ? -1 : 1, p1 = (facing + 1) % 3, p2 = (facing + 2) % 3, ip = incident.position, rp = reference.position;
|
|
635
|
+
const cx = ip[0] + ri[facing] * side * hi[facing], cy = ip[1] + ri[3 + facing] * side * hi[facing], cz = ip[2] + ri[6 + facing] * side * hi[facing];
|
|
636
|
+
let count = 4;
|
|
637
|
+
for (let k = 0; k < 4; k++) {
|
|
638
|
+
const s1 = (k === 0 || k === 3 ? -1 : 1) * hi[p1], s2 = (k < 2 ? -1 : 1) * hi[p2];
|
|
639
|
+
faceA[k * 4] = cx + ri[p1] * s1 + ri[p2] * s2;
|
|
640
|
+
faceA[k * 4 + 1] = cy + ri[3 + p1] * s1 + ri[3 + p2] * s2;
|
|
641
|
+
faceA[k * 4 + 2] = cz + ri[6 + p1] * s1 + ri[6 + p2] * s2;
|
|
642
|
+
faceA[k * 4 + 3] = k;
|
|
643
|
+
}
|
|
644
|
+
// Cut the incident face by the four sides of the reference face.
|
|
645
|
+
let from = faceA, to = faceB;
|
|
646
|
+
for (let plane = 0; plane < 4 && count > 0; plane++) {
|
|
647
|
+
// A hair of slack keeps corners that sit exactly on the rim from being cut away one step and kept the next.
|
|
648
|
+
const across = (axis + 1 + (plane >> 1)) % 3, sign = plane & 1 ? -1 : 1, limit = hr[across] + 1e-3;
|
|
649
|
+
const ux = rr[across] * sign, uy = rr[3 + across] * sign, uz = rr[6 + across] * sign;
|
|
650
|
+
let kept = 0;
|
|
651
|
+
for (let k = 0; k < count; k++) {
|
|
652
|
+
const o = k * 4, next = ((k + 1) % count) * 4;
|
|
653
|
+
const d0 = (from[o] - rp[0]) * ux + (from[o + 1] - rp[1]) * uy + (from[o + 2] - rp[2]) * uz - limit;
|
|
654
|
+
const d1 = (from[next] - rp[0]) * ux + (from[next + 1] - rp[1]) * uy + (from[next + 2] - rp[2]) * uz - limit;
|
|
655
|
+
if (d0 <= 0 && kept < 8) {
|
|
656
|
+
to[kept * 4] = from[o];
|
|
657
|
+
to[kept * 4 + 1] = from[o + 1];
|
|
658
|
+
to[kept * 4 + 2] = from[o + 2];
|
|
659
|
+
to[kept * 4 + 3] = from[o + 3];
|
|
660
|
+
kept++;
|
|
661
|
+
}
|
|
662
|
+
if (d0 < 0 !== d1 < 0 && d0 !== 0 && d1 !== 0 && kept < 8) {
|
|
663
|
+
const t = d0 / (d0 - d1);
|
|
664
|
+
to[kept * 4] = from[o] + (from[next] - from[o]) * t;
|
|
665
|
+
to[kept * 4 + 1] = from[o + 1] + (from[next + 1] - from[o + 1]) * t;
|
|
666
|
+
to[kept * 4 + 2] = from[o + 2] + (from[next + 2] - from[o + 2]) * t;
|
|
667
|
+
// A cut point is named after the edge it lies on and the side that cut it.
|
|
668
|
+
to[kept * 4 + 3] = 4 + plane * 4 + (from[o + 3] & 3);
|
|
669
|
+
kept++;
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
count = kept;
|
|
673
|
+
const swap = from;
|
|
674
|
+
from = to;
|
|
675
|
+
to = swap;
|
|
676
|
+
}
|
|
677
|
+
const reach = hr[axis];
|
|
678
|
+
for (let k = 0; k < count; k++) {
|
|
679
|
+
const o = k * 4, x = from[o], y = from[o + 1], z = from[o + 2];
|
|
680
|
+
// How far the point sits below the reference face.
|
|
681
|
+
const depth = reach - ((x - rp[0]) * fx + (y - rp[1]) * fy + (z - rp[2]) * fz);
|
|
682
|
+
if (depth <= -MARGIN)
|
|
683
|
+
continue;
|
|
684
|
+
m.feature = bestIndex * 32 + from[o + 3];
|
|
685
|
+
// Report the point halfway between the two surfaces.
|
|
686
|
+
m.add(x + fx * depth * 0.5, y + fy * depth * 0.5, z + fz * depth * 0.5, nx, ny, nz, depth);
|
|
687
|
+
}
|
|
688
|
+
if (m.count > before)
|
|
689
|
+
return;
|
|
690
|
+
}
|
|
691
|
+
for (let i = 0; i < 8; i++) {
|
|
692
|
+
boxCorner(corner, b, i);
|
|
693
|
+
m.feature = i;
|
|
694
|
+
if (insideBox(a, corner, MARGIN)) {
|
|
695
|
+
const depth = extentA -
|
|
696
|
+
((corner[0] - a.position[0]) * nx + (corner[1] - a.position[1]) * ny + (corner[2] - a.position[2]) * nz);
|
|
697
|
+
if (depth > -MARGIN)
|
|
698
|
+
m.add(corner[0], corner[1], corner[2], nx, ny, nz, depth);
|
|
699
|
+
}
|
|
700
|
+
boxCorner(corner, a, i);
|
|
701
|
+
m.feature = 8 + i;
|
|
702
|
+
if (insideBox(b, corner, MARGIN)) {
|
|
703
|
+
const depth = extentB +
|
|
704
|
+
((corner[0] - b.position[0]) * nx + (corner[1] - b.position[1]) * ny + (corner[2] - b.position[2]) * nz);
|
|
705
|
+
if (depth > -MARGIN)
|
|
706
|
+
m.add(corner[0], corner[1], corner[2], nx, ny, nz, depth);
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
if (m.count === before) {
|
|
710
|
+
m.feature = 16;
|
|
711
|
+
// Edge against edge: no corner is inside, so use the midpoint of the two facing surfaces.
|
|
712
|
+
const px = (a.position[0] + nx * extentA + b.position[0] - nx * extentB) * 0.5, py = (a.position[1] + ny * extentA + b.position[1] - ny * extentB) * 0.5;
|
|
713
|
+
const pz = (a.position[2] + nz * extentA + b.position[2] - nz * extentB) * 0.5;
|
|
714
|
+
m.add(px, py, pz, nx, ny, nz, bestDepth);
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
function isMoving(body) {
|
|
718
|
+
if (body.type === 'static' || body.isSleeping)
|
|
719
|
+
return false;
|
|
720
|
+
const v = body.linearVelocity, w = body.angularVelocity;
|
|
721
|
+
return (v[0] * v[0] + v[1] * v[1] + v[2] * v[2] > 4 * SLEEP_LINEAR * SLEEP_LINEAR ||
|
|
722
|
+
w[0] * w[0] + w[1] * w[1] + w[2] * w[2] > 4 * SLEEP_ANGULAR * SLEEP_ANGULAR);
|
|
723
|
+
}
|
|
724
|
+
const candidates = [];
|
|
725
|
+
let candidateCount = 0;
|
|
726
|
+
const meshPoint = [0, 0, 0], meshNormal = [0, 0, 0], meshSegA = [0, 0, 0], meshSegB = [0, 0, 0];
|
|
727
|
+
function toMeshLocal(out, body, x, y, z) {
|
|
728
|
+
const r = body.rotation, p = body.position, dx = x - p[0], dy = y - p[1], dz = z - p[2];
|
|
729
|
+
out[0] = r[0] * dx + r[3] * dy + r[6] * dz;
|
|
730
|
+
out[1] = r[1] * dx + r[4] * dy + r[7] * dz;
|
|
731
|
+
out[2] = r[2] * dx + r[5] * dy + r[8] * dz;
|
|
732
|
+
}
|
|
733
|
+
/** Test a sphere, given in the mesh's local space, against one triangle and record a candidate contact. */
|
|
734
|
+
function sphereTriangle(mesh, triangle, x, y, z, radius, feature) {
|
|
735
|
+
const face = mesh.closestPoint(triangle, x, y, z, meshPoint);
|
|
736
|
+
let nx = meshPoint[0] - x, ny = meshPoint[1] - y, nz = meshPoint[2] - z;
|
|
737
|
+
const distance = Math.sqrt(nx * nx + ny * ny + nz * nz);
|
|
738
|
+
if (distance >= radius)
|
|
739
|
+
return;
|
|
740
|
+
mesh.triangleNormal(triangle, meshNormal);
|
|
741
|
+
let depth = radius - distance;
|
|
742
|
+
// Triangles are solid on their front side only. A center that has sunk behind the
|
|
743
|
+
// face is pushed back out through it instead of further in.
|
|
744
|
+
const side = (x - meshPoint[0]) * meshNormal[0] + (y - meshPoint[1]) * meshNormal[1] + (z - meshPoint[2]) * meshNormal[2];
|
|
745
|
+
if (face && side < 0) {
|
|
746
|
+
nx = -meshNormal[0];
|
|
747
|
+
ny = -meshNormal[1];
|
|
748
|
+
nz = -meshNormal[2];
|
|
749
|
+
depth = radius + distance;
|
|
750
|
+
}
|
|
751
|
+
else if (distance > 1e-9) {
|
|
752
|
+
nx /= distance;
|
|
753
|
+
ny /= distance;
|
|
754
|
+
nz /= distance;
|
|
755
|
+
}
|
|
756
|
+
else {
|
|
757
|
+
nx = -meshNormal[0];
|
|
758
|
+
ny = -meshNormal[1];
|
|
759
|
+
nz = -meshNormal[2];
|
|
760
|
+
}
|
|
761
|
+
// Back-facing edge hits belong to the far side of a thin wall.
|
|
762
|
+
if (!face && nx * meshNormal[0] + ny * meshNormal[1] + nz * meshNormal[2] > 0.01)
|
|
763
|
+
return;
|
|
764
|
+
let c = candidates[candidateCount];
|
|
765
|
+
if (!c)
|
|
766
|
+
c = candidates[candidateCount] = { x: 0, y: 0, z: 0, nx: 0, ny: 0, nz: 0, depth: 0, face: false, feature: 0 };
|
|
767
|
+
candidateCount++;
|
|
768
|
+
c.x = meshPoint[0];
|
|
769
|
+
c.y = meshPoint[1];
|
|
770
|
+
c.z = meshPoint[2];
|
|
771
|
+
c.nx = nx;
|
|
772
|
+
c.ny = ny;
|
|
773
|
+
c.nz = nz;
|
|
774
|
+
c.depth = depth;
|
|
775
|
+
c.face = face;
|
|
776
|
+
c.feature = feature;
|
|
777
|
+
}
|
|
778
|
+
/** Turn the candidates gathered for one body into world-space contacts. */
|
|
779
|
+
function emitMeshContacts(m, meshBody) {
|
|
780
|
+
const r = meshBody.rotation, p = meshBody.position;
|
|
781
|
+
for (let i = 0; i < candidateCount; i++) {
|
|
782
|
+
const c = candidates[i];
|
|
783
|
+
let keep = true;
|
|
784
|
+
for (let j = 0; j < candidateCount && keep; j++) {
|
|
785
|
+
if (j === i)
|
|
786
|
+
continue;
|
|
787
|
+
const other = candidates[j], alike = c.nx * other.nx + c.ny * other.ny + c.nz * other.nz;
|
|
788
|
+
// An edge or corner hit that leans away from a deeper contact on the same surface
|
|
789
|
+
// would only add a sideways bump at the seams of a tessellated floor.
|
|
790
|
+
if (!c.face && other.depth > c.depth + 1e-6 && (alike > 0.9 || (other.face && alike > 0.7)))
|
|
791
|
+
keep = false;
|
|
792
|
+
// Neighbouring triangles report the same point twice; keep the first.
|
|
793
|
+
else if (j < i &&
|
|
794
|
+
c.face === other.face &&
|
|
795
|
+
alike > 0.999 &&
|
|
796
|
+
Math.abs(c.x - other.x) + Math.abs(c.y - other.y) + Math.abs(c.z - other.z) < 1e-3)
|
|
797
|
+
keep = false;
|
|
798
|
+
}
|
|
799
|
+
if (!keep)
|
|
800
|
+
continue;
|
|
801
|
+
m.feature = c.feature;
|
|
802
|
+
m.addDeep(p[0] + r[0] * c.x + r[1] * c.y + r[2] * c.z, p[1] + r[3] * c.x + r[4] * c.y + r[5] * c.z, p[2] + r[6] * c.x + r[7] * c.y + r[8] * c.z, r[0] * c.nx + r[1] * c.ny + r[2] * c.nz, r[3] * c.nx + r[4] * c.ny + r[5] * c.nz, r[6] * c.nx + r[7] * c.ny + r[8] * c.nz, c.depth);
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
// The sphere being tested, kept here so one visitor serves every query instead of a new closure per pair.
|
|
806
|
+
let sphereQueryMesh, sphereQueryX = 0, sphereQueryY = 0, sphereQueryZ = 0, sphereQueryRadius = 0, sphereQueryFeature = 0;
|
|
807
|
+
const visitSphereTriangle = (triangle) => sphereTriangle(sphereQueryMesh, triangle, sphereQueryX, sphereQueryY, sphereQueryZ, sphereQueryRadius, sphereQueryFeature + (triangle & 1023));
|
|
808
|
+
function sphereMesh(m, x, y, z, radius, meshBody, feature) {
|
|
809
|
+
const mesh = meshBody.shape.mesh;
|
|
810
|
+
toMeshLocal(meshSegA, meshBody, x, y, z);
|
|
811
|
+
const lx = meshSegA[0], ly = meshSegA[1], lz = meshSegA[2];
|
|
812
|
+
candidateCount = 0;
|
|
813
|
+
sphereQueryMesh = mesh;
|
|
814
|
+
sphereQueryX = lx;
|
|
815
|
+
sphereQueryY = ly;
|
|
816
|
+
sphereQueryZ = lz;
|
|
817
|
+
sphereQueryRadius = radius;
|
|
818
|
+
sphereQueryFeature = feature;
|
|
819
|
+
mesh.query(lx - radius, ly - radius, lz - radius, lx + radius, ly + radius, lz + radius, visitSphereTriangle);
|
|
820
|
+
sphereQueryMesh = undefined;
|
|
821
|
+
emitMeshContacts(m, meshBody);
|
|
822
|
+
}
|
|
823
|
+
function capsuleMesh(m, capsule, meshBody) {
|
|
824
|
+
const mesh = meshBody.shape.mesh, radius = capsule.shape.radius;
|
|
825
|
+
capsuleSegment(capsule, segA, segB);
|
|
826
|
+
toMeshLocal(meshSegA, meshBody, segA[0], segA[1], segA[2]);
|
|
827
|
+
toMeshLocal(meshSegB, meshBody, segB[0], segB[1], segB[2]);
|
|
828
|
+
const ax = meshSegA[0], ay = meshSegA[1], az = meshSegA[2], bx = meshSegB[0], by = meshSegB[1], bz = meshSegB[2];
|
|
829
|
+
const dx = bx - ax, dy = by - ay, dz = bz - az, radius2 = radius * radius;
|
|
830
|
+
let triangle = 0, bestDistance2 = radius2, bestFace = false, bestX = 0, bestY = 0, bestZ = 0;
|
|
831
|
+
const consider = (x, y, z) => {
|
|
832
|
+
const face = mesh.closestPoint(triangle, x, y, z, meshPoint);
|
|
833
|
+
const px = x - meshPoint[0], py = y - meshPoint[1], pz = z - meshPoint[2];
|
|
834
|
+
const distance2 = px * px + py * py + pz * pz;
|
|
835
|
+
if (distance2 >= radius2 || distance2 > bestDistance2 || (distance2 === bestDistance2 && (!face || bestFace)))
|
|
836
|
+
return;
|
|
837
|
+
// Keep the one-sided edge rule used by the sphere contact solver.
|
|
838
|
+
const side = px * meshNormal[0] + py * meshNormal[1] + pz * meshNormal[2];
|
|
839
|
+
if (!face && distance2 > 1e-18 && side < -0.01 * Math.sqrt(distance2))
|
|
840
|
+
return;
|
|
841
|
+
bestDistance2 = distance2;
|
|
842
|
+
bestFace = face;
|
|
843
|
+
bestX = x;
|
|
844
|
+
bestY = y;
|
|
845
|
+
bestZ = z;
|
|
846
|
+
};
|
|
847
|
+
const positions = mesh.positions, indices = mesh.indices;
|
|
848
|
+
candidateCount = 0;
|
|
849
|
+
mesh.query(Math.min(ax, bx) - radius, Math.min(ay, by) - radius, Math.min(az, bz) - radius, Math.max(ax, bx) + radius, Math.max(ay, by) + radius, Math.max(az, bz) + radius, index => {
|
|
850
|
+
triangle = index;
|
|
851
|
+
bestDistance2 = radius2;
|
|
852
|
+
bestFace = false;
|
|
853
|
+
mesh.triangleNormal(triangle, meshNormal);
|
|
854
|
+
// A minimum is attained at an axis endpoint, at a triangle edge, or at a crossing of the triangle plane.
|
|
855
|
+
consider(ax, ay, az);
|
|
856
|
+
consider(bx, by, bz);
|
|
857
|
+
const base = triangle * 3, vertex = indices[base] * 3;
|
|
858
|
+
const alongNormal = dx * meshNormal[0] + dy * meshNormal[1] + dz * meshNormal[2];
|
|
859
|
+
if (alongNormal !== 0) {
|
|
860
|
+
const offset = (positions[vertex] - ax) * meshNormal[0] +
|
|
861
|
+
(positions[vertex + 1] - ay) * meshNormal[1] +
|
|
862
|
+
(positions[vertex + 2] - az) * meshNormal[2];
|
|
863
|
+
const t = offset / alongNormal;
|
|
864
|
+
if (t > 0 && t < 1)
|
|
865
|
+
consider(ax + dx * t, ay + dy * t, az + dz * t);
|
|
866
|
+
}
|
|
867
|
+
for (let edge = 0; edge < 3; edge++) {
|
|
868
|
+
const first = indices[base + edge] * 3, second = indices[base + ((edge + 1) % 3)] * 3;
|
|
869
|
+
for (let i = 0; i < 3; i++) {
|
|
870
|
+
segC[i] = positions[first + i];
|
|
871
|
+
segD[i] = positions[second + i];
|
|
872
|
+
}
|
|
873
|
+
closestBetweenSegments(meshSegA, meshSegB, segC, segD, scratchC, scratchD);
|
|
874
|
+
consider(scratchC[0], scratchC[1], scratchC[2]);
|
|
875
|
+
}
|
|
876
|
+
if (bestDistance2 < radius2)
|
|
877
|
+
sphereTriangle(mesh, triangle, bestX, bestY, bestZ, radius, triangle & 1023);
|
|
878
|
+
});
|
|
879
|
+
emitMeshContacts(m, meshBody);
|
|
880
|
+
}
|
|
881
|
+
// ---- boxes and hulls as generic convex solids ----
|
|
882
|
+
const solidNormal = [0, 0, 0], solidPoint = v3();
|
|
883
|
+
function solidVertexCount(body) {
|
|
884
|
+
return body.shape.type === 'convex' ? body.shape.hull.points.length / 3 : 8;
|
|
885
|
+
}
|
|
886
|
+
function solidVertex(out, body, index) {
|
|
887
|
+
if (body.shape.type !== 'convex')
|
|
888
|
+
return boxCorner(out, body, index);
|
|
889
|
+
const points = body.shape.hull.points, r = body.rotation, p = body.position, x = points[index * 3], y = points[index * 3 + 1], z = points[index * 3 + 2];
|
|
890
|
+
out[0] = p[0] + r[0] * x + r[1] * y + r[2] * z;
|
|
891
|
+
out[1] = p[1] + r[3] * x + r[4] * y + r[5] * z;
|
|
892
|
+
out[2] = p[2] + r[6] * x + r[7] * y + r[8] * z;
|
|
893
|
+
return out;
|
|
894
|
+
}
|
|
895
|
+
/** Signed distance of a world point to a box or hull (negative inside), with the world normal of the nearest face. */
|
|
896
|
+
function solidDistance(body, x, y, z, normal) {
|
|
897
|
+
const r = body.rotation, p = body.position, dx = x - p[0], dy = y - p[1], dz = z - p[2];
|
|
898
|
+
const lx = r[0] * dx + r[3] * dy + r[6] * dz, ly = r[1] * dx + r[4] * dy + r[7] * dz, lz = r[2] * dx + r[5] * dy + r[8] * dz;
|
|
899
|
+
let distance, nx = 0, ny = 0, nz = 0;
|
|
900
|
+
if (body.shape.type === 'convex') {
|
|
901
|
+
distance = body.shape.hull.signedDistance(lx, ly, lz, solidNormal);
|
|
902
|
+
nx = solidNormal[0];
|
|
903
|
+
ny = solidNormal[1];
|
|
904
|
+
nz = solidNormal[2];
|
|
905
|
+
}
|
|
906
|
+
else {
|
|
907
|
+
const h = body.shape.halfExtents, ax = Math.abs(lx) - h[0], ay = Math.abs(ly) - h[1], az = Math.abs(lz) - h[2];
|
|
908
|
+
if (ax >= ay && ax >= az) {
|
|
909
|
+
distance = ax;
|
|
910
|
+
nx = lx >= 0 ? 1 : -1;
|
|
911
|
+
}
|
|
912
|
+
else if (ay >= az) {
|
|
913
|
+
distance = ay;
|
|
914
|
+
ny = ly >= 0 ? 1 : -1;
|
|
915
|
+
}
|
|
916
|
+
else {
|
|
917
|
+
distance = az;
|
|
918
|
+
nz = lz >= 0 ? 1 : -1;
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
normal[0] = r[0] * nx + r[1] * ny + r[2] * nz;
|
|
922
|
+
normal[1] = r[3] * nx + r[4] * ny + r[5] * nz;
|
|
923
|
+
normal[2] = r[6] * nx + r[7] * ny + r[8] * nz;
|
|
924
|
+
return distance;
|
|
925
|
+
}
|
|
926
|
+
const faceNormal = [0, 0, 0];
|
|
927
|
+
/** Sphere (first) against a hull (second), treating the hull as the intersection of its face planes. */
|
|
928
|
+
function sphereSolid(m, x, y, z, radius, solid) {
|
|
929
|
+
const distance = solidDistance(solid, x, y, z, faceNormal);
|
|
930
|
+
if (distance >= radius)
|
|
931
|
+
return;
|
|
932
|
+
const nx = faceNormal[0], ny = faceNormal[1], nz = faceNormal[2];
|
|
933
|
+
m.add(x - nx * (distance + (radius - distance) * 0.5), y - ny * (distance + (radius - distance) * 0.5), z - nz * (distance + (radius - distance) * 0.5), -nx, -ny, -nz, radius - distance);
|
|
934
|
+
}
|
|
935
|
+
/** Corners of each solid that have entered the other; `a` is the first body of the pair. */
|
|
936
|
+
function solidSolid(m, a, b) {
|
|
937
|
+
const countA = solidVertexCount(a), countB = solidVertexCount(b);
|
|
938
|
+
for (let i = 0; i < countA; i++) {
|
|
939
|
+
solidVertex(solidPoint, a, i);
|
|
940
|
+
const distance = solidDistance(b, solidPoint[0], solidPoint[1], solidPoint[2], faceNormal);
|
|
941
|
+
// The face normal of b points back toward a, so the pair normal is its opposite.
|
|
942
|
+
if (distance < MARGIN) {
|
|
943
|
+
m.feature = i;
|
|
944
|
+
m.addDeep(solidPoint[0], solidPoint[1], solidPoint[2], -faceNormal[0], -faceNormal[1], -faceNormal[2], -distance);
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
for (let i = 0; i < countB; i++) {
|
|
948
|
+
solidVertex(solidPoint, b, i);
|
|
949
|
+
const distance = solidDistance(a, solidPoint[0], solidPoint[1], solidPoint[2], faceNormal);
|
|
950
|
+
if (distance < MARGIN) {
|
|
951
|
+
m.feature = 64 + i;
|
|
952
|
+
m.addDeep(solidPoint[0], solidPoint[1], solidPoint[2], faceNormal[0], faceNormal[1], faceNormal[2], -distance);
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
solidEdges(m, a, b);
|
|
956
|
+
}
|
|
957
|
+
const BOX_EDGES = [0, 1, 2, 3, 4, 5, 6, 7, 0, 2, 1, 3, 4, 6, 5, 7, 0, 4, 1, 5, 2, 6, 3, 7];
|
|
958
|
+
function solidEdgeCount(body) {
|
|
959
|
+
return body.shape.type === 'convex' ? body.shape.hull.edges.length / 2 : 12;
|
|
960
|
+
}
|
|
961
|
+
function solidEdge(body, index, a, b) {
|
|
962
|
+
const list = body.shape.type === 'convex' ? body.shape.hull.edges : BOX_EDGES;
|
|
963
|
+
solidVertex(a, body, list[index * 2]);
|
|
964
|
+
solidVertex(b, body, list[index * 2 + 1]);
|
|
965
|
+
}
|
|
966
|
+
const clipRange = [0, 0], clipA = v3(), clipB = v3();
|
|
967
|
+
/** Part of the world segment a-b that lies inside a box or hull, as parameters in clipRange; false when none does. */
|
|
968
|
+
function clipSegmentSolid(body, a, b) {
|
|
969
|
+
const r = body.rotation, p = body.position;
|
|
970
|
+
let dx = a[0] - p[0], dy = a[1] - p[1], dz = a[2] - p[2];
|
|
971
|
+
const ax = r[0] * dx + r[3] * dy + r[6] * dz, ay = r[1] * dx + r[4] * dy + r[7] * dz, az = r[2] * dx + r[5] * dy + r[8] * dz;
|
|
972
|
+
dx = b[0] - p[0];
|
|
973
|
+
dy = b[1] - p[1];
|
|
974
|
+
dz = b[2] - p[2];
|
|
975
|
+
const ex = r[0] * dx + r[3] * dy + r[6] * dz - ax, ey = r[1] * dx + r[4] * dy + r[7] * dz - ay, ez = r[2] * dx + r[5] * dy + r[8] * dz - az;
|
|
976
|
+
let enter = 0, exit = 1;
|
|
977
|
+
const cut = (side, along) => {
|
|
978
|
+
// side: signed distance of the start to a plane; along: its change over the segment.
|
|
979
|
+
if (Math.abs(along) < 1e-12)
|
|
980
|
+
return side <= 0;
|
|
981
|
+
const t = -side / along;
|
|
982
|
+
if (along < 0) {
|
|
983
|
+
if (t > enter)
|
|
984
|
+
enter = t;
|
|
985
|
+
}
|
|
986
|
+
else if (t < exit)
|
|
987
|
+
exit = t;
|
|
988
|
+
return enter < exit;
|
|
989
|
+
};
|
|
990
|
+
if (body.shape.type === 'convex') {
|
|
991
|
+
const planes = body.shape.hull.planes;
|
|
992
|
+
for (let i = 0; i < planes.length; i += 4) {
|
|
993
|
+
const nx = planes[i], ny = planes[i + 1], nz = planes[i + 2];
|
|
994
|
+
if (!cut(nx * ax + ny * ay + nz * az + planes[i + 3], nx * ex + ny * ey + nz * ez))
|
|
995
|
+
return false;
|
|
996
|
+
}
|
|
997
|
+
}
|
|
998
|
+
else {
|
|
999
|
+
const h = body.shape.halfExtents;
|
|
1000
|
+
if (!cut(ax - h[0], ex) ||
|
|
1001
|
+
!cut(-ax - h[0], -ex) ||
|
|
1002
|
+
!cut(ay - h[1], ey) ||
|
|
1003
|
+
!cut(-ay - h[1], -ey) ||
|
|
1004
|
+
!cut(az - h[2], ez) ||
|
|
1005
|
+
!cut(-az - h[2], -ez))
|
|
1006
|
+
return false;
|
|
1007
|
+
}
|
|
1008
|
+
clipRange[0] = enter;
|
|
1009
|
+
clipRange[1] = exit;
|
|
1010
|
+
return true;
|
|
1011
|
+
}
|
|
1012
|
+
const edgeA = v3(), edgeB = v3(), otherA = v3(), otherB = v3(), nearA = v3(), nearB = v3();
|
|
1013
|
+
/**
|
|
1014
|
+
* Edge against edge: an edge of `a` that passes through `b` while both its
|
|
1015
|
+
* ends stay outside touches no corner, so the corner tests miss it. The
|
|
1016
|
+
* contact sits between that edge and the nearest edge of `b`, along the
|
|
1017
|
+
* direction perpendicular to both.
|
|
1018
|
+
*/
|
|
1019
|
+
function solidEdges(m, a, b) {
|
|
1020
|
+
const countA = solidEdgeCount(a), countB = solidEdgeCount(b);
|
|
1021
|
+
for (let i = 0; i < countA; i++) {
|
|
1022
|
+
solidEdge(a, i, edgeA, edgeB);
|
|
1023
|
+
if (!clipSegmentSolid(b, edgeA, edgeB))
|
|
1024
|
+
continue;
|
|
1025
|
+
const enter = clipRange[0], exit = clipRange[1];
|
|
1026
|
+
if (enter < 1e-4 || exit > 1 - 1e-4)
|
|
1027
|
+
continue; // an end is inside: a corner contact covers it
|
|
1028
|
+
const dx = edgeB[0] - edgeA[0], dy = edgeB[1] - edgeA[1], dz = edgeB[2] - edgeA[2];
|
|
1029
|
+
clipA[0] = edgeA[0] + dx * enter;
|
|
1030
|
+
clipA[1] = edgeA[1] + dy * enter;
|
|
1031
|
+
clipA[2] = edgeA[2] + dz * enter;
|
|
1032
|
+
clipB[0] = edgeA[0] + dx * exit;
|
|
1033
|
+
clipB[1] = edgeA[1] + dy * exit;
|
|
1034
|
+
clipB[2] = edgeA[2] + dz * exit;
|
|
1035
|
+
let best = Infinity, nx = 0, ny = 0, nz = 0, depth = 0, px = 0, py = 0, pz = 0;
|
|
1036
|
+
for (let j = 0; j < countB; j++) {
|
|
1037
|
+
solidEdge(b, j, otherA, otherB);
|
|
1038
|
+
closestBetweenSegments(clipA, clipB, otherA, otherB, nearA, nearB);
|
|
1039
|
+
const gap = Math.hypot(nearA[0] - nearB[0], nearA[1] - nearB[1], nearA[2] - nearB[2]);
|
|
1040
|
+
if (gap >= best)
|
|
1041
|
+
continue;
|
|
1042
|
+
const fx = otherB[0] - otherA[0], fy = otherB[1] - otherA[1], fz = otherB[2] - otherA[2];
|
|
1043
|
+
let cx = dy * fz - dz * fy, cy = dz * fx - dx * fz, cz = dx * fy - dy * fx;
|
|
1044
|
+
const length = Math.sqrt(cx * cx + cy * cy + cz * cz);
|
|
1045
|
+
if (length < 1e-9)
|
|
1046
|
+
continue; // parallel edges: a face or corner contact describes it better
|
|
1047
|
+
cx /= length;
|
|
1048
|
+
cy /= length;
|
|
1049
|
+
cz /= length;
|
|
1050
|
+
// b's inside lies beyond its edge, which fixes the direction from a to b.
|
|
1051
|
+
if (cx * (b.position[0] - nearB[0]) + cy * (b.position[1] - nearB[1]) + cz * (b.position[2] - nearB[2]) <
|
|
1052
|
+
0) {
|
|
1053
|
+
cx = -cx;
|
|
1054
|
+
cy = -cy;
|
|
1055
|
+
cz = -cz;
|
|
1056
|
+
}
|
|
1057
|
+
const overlap = cx * (nearA[0] - nearB[0]) + cy * (nearA[1] - nearB[1]) + cz * (nearA[2] - nearB[2]);
|
|
1058
|
+
if (overlap <= 0)
|
|
1059
|
+
continue;
|
|
1060
|
+
best = gap;
|
|
1061
|
+
nx = cx;
|
|
1062
|
+
ny = cy;
|
|
1063
|
+
nz = cz;
|
|
1064
|
+
depth = overlap;
|
|
1065
|
+
px = (nearA[0] + nearB[0]) / 2;
|
|
1066
|
+
py = (nearA[1] + nearB[1]) / 2;
|
|
1067
|
+
pz = (nearA[2] + nearB[2]) / 2;
|
|
1068
|
+
}
|
|
1069
|
+
if (best < Infinity) {
|
|
1070
|
+
m.feature = 256 + i;
|
|
1071
|
+
m.addDeep(px, py, pz, nx, ny, nz, depth);
|
|
1072
|
+
}
|
|
1073
|
+
}
|
|
1074
|
+
}
|
|
1075
|
+
function solidPlane(m, solid, plane) {
|
|
1076
|
+
const r = plane.rotation, nx = r[1], ny = r[4], nz = r[7], p = plane.position, count = solidVertexCount(solid);
|
|
1077
|
+
for (let i = 0; i < count; i++) {
|
|
1078
|
+
solidVertex(solidPoint, solid, i);
|
|
1079
|
+
const distance = (solidPoint[0] - p[0]) * nx + (solidPoint[1] - p[1]) * ny + (solidPoint[2] - p[2]) * nz;
|
|
1080
|
+
if (distance < MARGIN) {
|
|
1081
|
+
m.feature = i;
|
|
1082
|
+
m.addDeep(solidPoint[0] - nx * distance * 0.5, solidPoint[1] - ny * distance * 0.5, solidPoint[2] - nz * distance * 0.5, -nx, -ny, -nz, -distance);
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
}
|
|
1086
|
+
/** Boxes and hulls rest on meshes through their corners, which is enough for props on floors and slopes. */
|
|
1087
|
+
const meshCenter = [0, 0, 0], meshSeen = new Int32Array(96);
|
|
1088
|
+
function pushCandidate(x, y, z, nx, ny, nz, depth, feature) {
|
|
1089
|
+
let c = candidates[candidateCount];
|
|
1090
|
+
if (!c)
|
|
1091
|
+
c = candidates[candidateCount] = { x: 0, y: 0, z: 0, nx: 0, ny: 0, nz: 0, depth: 0, face: false, feature: 0 };
|
|
1092
|
+
candidateCount++;
|
|
1093
|
+
c.x = x;
|
|
1094
|
+
c.y = y;
|
|
1095
|
+
c.z = z;
|
|
1096
|
+
c.nx = nx;
|
|
1097
|
+
c.ny = ny;
|
|
1098
|
+
c.nz = nz;
|
|
1099
|
+
c.depth = depth;
|
|
1100
|
+
c.face = true;
|
|
1101
|
+
c.feature = feature;
|
|
1102
|
+
}
|
|
1103
|
+
function solidMesh(m, solid, meshBody) {
|
|
1104
|
+
const mesh = meshBody.shape.mesh, radius = 0.03, count = solidVertexCount(solid);
|
|
1105
|
+
candidateCount = 0;
|
|
1106
|
+
toMeshLocal(meshCenter, meshBody, solid.position[0], solid.position[1], solid.position[2]);
|
|
1107
|
+
const cx = meshCenter[0], cy = meshCenter[1], cz = meshCenter[2], p = mesh.positions, indices = mesh.indices;
|
|
1108
|
+
let x0 = Infinity, y0 = Infinity, z0 = Infinity, x1 = -Infinity, y1 = -Infinity, z1 = -Infinity;
|
|
1109
|
+
for (let i = 0; i < count; i++) {
|
|
1110
|
+
solidVertex(solidPoint, solid, i);
|
|
1111
|
+
toMeshLocal(meshSegA, meshBody, solidPoint[0], solidPoint[1], solidPoint[2]);
|
|
1112
|
+
const lx = meshSegA[0], ly = meshSegA[1], lz = meshSegA[2];
|
|
1113
|
+
x0 = Math.min(x0, lx);
|
|
1114
|
+
y0 = Math.min(y0, ly);
|
|
1115
|
+
z0 = Math.min(z0, lz);
|
|
1116
|
+
x1 = Math.max(x1, lx);
|
|
1117
|
+
y1 = Math.max(y1, ly);
|
|
1118
|
+
z1 = Math.max(z1, lz);
|
|
1119
|
+
mesh.query(lx - radius, ly - radius, lz - radius, lx + radius, ly + radius, lz + radius, triangle => sphereTriangle(mesh, triangle, lx, ly, lz, radius, (i & 31) * 1024 + (triangle & 1023)));
|
|
1120
|
+
// A corner that went through the surface in one step: the line from the
|
|
1121
|
+
// solid's center to the corner crosses a triangle the center is still in front of.
|
|
1122
|
+
mesh.query(Math.min(lx, cx), Math.min(ly, cy), Math.min(lz, cz), Math.max(lx, cx), Math.max(ly, cy), Math.max(lz, cz), triangle => {
|
|
1123
|
+
mesh.triangleNormal(triangle, meshNormal);
|
|
1124
|
+
const a = indices[triangle * 3] * 3, nx = meshNormal[0], ny = meshNormal[1], nz = meshNormal[2];
|
|
1125
|
+
const below = (lx - p[a]) * nx + (ly - p[a + 1]) * ny + (lz - p[a + 2]) * nz, above = (cx - p[a]) * nx + (cy - p[a + 1]) * ny + (cz - p[a + 2]) * nz;
|
|
1126
|
+
if (above <= 0 || below >= -radius)
|
|
1127
|
+
return;
|
|
1128
|
+
const t = above / (above - below), qx = cx + (lx - cx) * t, qy = cy + (ly - cy) * t, qz = cz + (lz - cz) * t;
|
|
1129
|
+
mesh.closestPoint(triangle, qx, qy, qz, meshPoint);
|
|
1130
|
+
if (Math.abs(meshPoint[0] - qx) + Math.abs(meshPoint[1] - qy) + Math.abs(meshPoint[2] - qz) > 1e-4)
|
|
1131
|
+
return;
|
|
1132
|
+
pushCandidate(lx - nx * below, ly - ny * below, lz - nz * below, -nx, -ny, -nz, radius - below, (i & 31) * 1024 + (triangle & 1023));
|
|
1133
|
+
});
|
|
1134
|
+
}
|
|
1135
|
+
// Points of the surface that poke into the solid between its corners, such as a ridge under a crate.
|
|
1136
|
+
let seen = 0;
|
|
1137
|
+
const r = meshBody.rotation, origin = meshBody.position;
|
|
1138
|
+
mesh.query(x0, y0, z0, x1, y1, z1, triangle => {
|
|
1139
|
+
mesh.triangleNormal(triangle, meshNormal);
|
|
1140
|
+
const a = indices[triangle * 3] * 3;
|
|
1141
|
+
if ((cx - p[a]) * meshNormal[0] + (cy - p[a + 1]) * meshNormal[1] + (cz - p[a + 2]) * meshNormal[2] <= 0)
|
|
1142
|
+
return;
|
|
1143
|
+
for (let corner = 0; corner < 3; corner++) {
|
|
1144
|
+
const vertex = indices[triangle * 3 + corner];
|
|
1145
|
+
let known = false;
|
|
1146
|
+
for (let k = 0; k < seen && !known; k++)
|
|
1147
|
+
known = meshSeen[k] === vertex;
|
|
1148
|
+
if (known || seen >= meshSeen.length)
|
|
1149
|
+
continue;
|
|
1150
|
+
meshSeen[seen++] = vertex;
|
|
1151
|
+
const vx = p[vertex * 3], vy = p[vertex * 3 + 1], vz = p[vertex * 3 + 2];
|
|
1152
|
+
const distance = solidDistance(solid, origin[0] + r[0] * vx + r[1] * vy + r[2] * vz, origin[1] + r[3] * vx + r[4] * vy + r[5] * vz, origin[2] + r[6] * vx + r[7] * vy + r[8] * vz, faceNormal);
|
|
1153
|
+
if (distance >= 0)
|
|
1154
|
+
continue;
|
|
1155
|
+
const wx = faceNormal[0], wy = faceNormal[1], wz = faceNormal[2];
|
|
1156
|
+
pushCandidate(vx, vy, vz, r[0] * wx + r[3] * wy + r[6] * wz, r[1] * wx + r[4] * wy + r[7] * wz, r[2] * wx + r[5] * wy + r[8] * wz, -distance, 32768 + (vertex & 32767));
|
|
1157
|
+
}
|
|
1158
|
+
});
|
|
1159
|
+
emitMeshContacts(m, meshBody);
|
|
1160
|
+
}
|
|
1161
|
+
const ORDER = { sphere: 0, capsule: 1, box: 2, convex: 3, plane: 4, mesh: 5, compound: 6 };
|
|
1162
|
+
const partManifold = new Manifold();
|
|
1163
|
+
/** Fill `m` with the contacts between two bodies; returns false when they do not touch. */
|
|
1164
|
+
function collide(m, first, second) {
|
|
1165
|
+
if (first.parts.length === 0 && second.parts.length === 0)
|
|
1166
|
+
return collideSimple(m, first, second);
|
|
1167
|
+
// A compound meets the other body part by part; the contacts are gathered under the whole bodies.
|
|
1168
|
+
m.count = 0;
|
|
1169
|
+
m.feature = 0;
|
|
1170
|
+
const inner = partManifold, as = first.parts, bs = second.parts, countA = as.length || 1, countB = bs.length || 1;
|
|
1171
|
+
for (let i = 0; i < countA; i++) {
|
|
1172
|
+
const a = as.length ? as[i] : first, box = a.aabb;
|
|
1173
|
+
for (let j = 0; j < countB; j++) {
|
|
1174
|
+
const b = bs.length ? bs[j] : second, other = b.aabb;
|
|
1175
|
+
if (box[0] > other[3] ||
|
|
1176
|
+
box[3] < other[0] ||
|
|
1177
|
+
box[1] > other[4] ||
|
|
1178
|
+
box[4] < other[1] ||
|
|
1179
|
+
box[2] > other[5] ||
|
|
1180
|
+
box[5] < other[2])
|
|
1181
|
+
continue;
|
|
1182
|
+
if (!collideSimple(inner, a, b))
|
|
1183
|
+
continue;
|
|
1184
|
+
for (let k = 0; k < inner.count; k++) {
|
|
1185
|
+
const c = inner.contacts[k];
|
|
1186
|
+
// Keep contacts of different parts apart when impulses are matched across steps.
|
|
1187
|
+
m.feature = ((i * 32 + j) * 61 + c.feature) & 32767;
|
|
1188
|
+
m.addDeep(c.px, c.py, c.pz, c.nx, c.ny, c.nz, c.depth);
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
}
|
|
1192
|
+
return m.count > 0;
|
|
1193
|
+
}
|
|
1194
|
+
function collideSimple(m, first, second) {
|
|
1195
|
+
m.count = 0;
|
|
1196
|
+
m.feature = 0;
|
|
1197
|
+
// Dispatch on an ordered pair so each combination is written once.
|
|
1198
|
+
const flip = ORDER[first.shape.type] > ORDER[second.shape.type], a = flip ? second : first, b = flip ? first : second;
|
|
1199
|
+
const pa = a.position, pb = b.position, sa = a.shape, sb = b.shape;
|
|
1200
|
+
if (sb.type === 'mesh') {
|
|
1201
|
+
if (sa.type === 'sphere')
|
|
1202
|
+
sphereMesh(m, pa[0], pa[1], pa[2], sa.radius, b, 0);
|
|
1203
|
+
else if (sa.type === 'capsule')
|
|
1204
|
+
capsuleMesh(m, a, b);
|
|
1205
|
+
else if (sa.type === 'box' || sa.type === 'convex')
|
|
1206
|
+
solidMesh(m, a, b);
|
|
1207
|
+
}
|
|
1208
|
+
else if (sa.type === 'sphere') {
|
|
1209
|
+
if (sb.type === 'sphere')
|
|
1210
|
+
sphereSphere(m, pa[0], pa[1], pa[2], sa.radius, pb[0], pb[1], pb[2], sb.radius);
|
|
1211
|
+
else if (sb.type === 'capsule') {
|
|
1212
|
+
capsuleSegment(b, segA, segB);
|
|
1213
|
+
closestOnSegment(segC, segA, segB, pa[0], pa[1], pa[2]);
|
|
1214
|
+
sphereSphere(m, pa[0], pa[1], pa[2], sa.radius, segC[0], segC[1], segC[2], sb.radius);
|
|
1215
|
+
}
|
|
1216
|
+
else if (sb.type === 'box')
|
|
1217
|
+
sphereBox(m, pa[0], pa[1], pa[2], sa.radius, b);
|
|
1218
|
+
else if (sb.type === 'convex')
|
|
1219
|
+
sphereSolid(m, pa[0], pa[1], pa[2], sa.radius, b);
|
|
1220
|
+
else if (sb.type === 'plane')
|
|
1221
|
+
spherePlane(m, pa[0], pa[1], pa[2], sa.radius, b);
|
|
1222
|
+
}
|
|
1223
|
+
else if (sa.type === 'capsule') {
|
|
1224
|
+
if (sb.type === 'capsule') {
|
|
1225
|
+
capsuleSegment(a, segA, segB);
|
|
1226
|
+
capsuleSegment(b, segC, segD);
|
|
1227
|
+
closestBetweenSegments(segA, segB, segC, segD, scratchC, scratchD);
|
|
1228
|
+
sphereSphere(m, scratchC[0], scratchC[1], scratchC[2], sa.radius, scratchD[0], scratchD[1], scratchD[2], sb.radius);
|
|
1229
|
+
}
|
|
1230
|
+
else if (sb.type === 'box')
|
|
1231
|
+
capsuleBox(m, a, b);
|
|
1232
|
+
else if (sb.type === 'convex') {
|
|
1233
|
+
// Spheres at both caps and the middle stand in for the capsule against the hull's faces.
|
|
1234
|
+
capsuleSegment(a, segA, segB);
|
|
1235
|
+
sphereSolid(m, segA[0], segA[1], segA[2], sa.radius, b);
|
|
1236
|
+
m.feature = 1;
|
|
1237
|
+
sphereSolid(m, segB[0], segB[1], segB[2], sa.radius, b);
|
|
1238
|
+
m.feature = 2;
|
|
1239
|
+
sphereSolid(m, (segA[0] + segB[0]) / 2, (segA[1] + segB[1]) / 2, (segA[2] + segB[2]) / 2, sa.radius, b);
|
|
1240
|
+
}
|
|
1241
|
+
else if (sb.type === 'plane') {
|
|
1242
|
+
capsuleSegment(a, segA, segB);
|
|
1243
|
+
spherePlane(m, segA[0], segA[1], segA[2], sa.radius, b);
|
|
1244
|
+
m.feature = 1;
|
|
1245
|
+
spherePlane(m, segB[0], segB[1], segB[2], sa.radius, b);
|
|
1246
|
+
}
|
|
1247
|
+
}
|
|
1248
|
+
else if (sa.type === 'box') {
|
|
1249
|
+
if (sb.type === 'box')
|
|
1250
|
+
boxBox(m, a, b);
|
|
1251
|
+
else if (sb.type === 'convex')
|
|
1252
|
+
solidSolid(m, a, b);
|
|
1253
|
+
else if (sb.type === 'plane')
|
|
1254
|
+
boxPlane(m, a, b);
|
|
1255
|
+
}
|
|
1256
|
+
else if (sa.type === 'convex') {
|
|
1257
|
+
if (sb.type === 'convex')
|
|
1258
|
+
solidSolid(m, a, b);
|
|
1259
|
+
else if (sb.type === 'plane')
|
|
1260
|
+
solidPlane(m, a, b);
|
|
1261
|
+
}
|
|
1262
|
+
if (m.count === 0)
|
|
1263
|
+
return false;
|
|
1264
|
+
m.finish(flip);
|
|
1265
|
+
return true;
|
|
1266
|
+
}
|
|
1267
|
+
/** A constraint between two bodies, or between a body and the world. Create it with world.createJoint(). */
|
|
1268
|
+
export class Joint3D {
|
|
1269
|
+
type;
|
|
1270
|
+
bodyA;
|
|
1271
|
+
bodyB;
|
|
1272
|
+
collideConnected;
|
|
1273
|
+
enabled = true;
|
|
1274
|
+
/** 'distance' joints: rest length, and whether it only limits stretching. */
|
|
1275
|
+
length = 0;
|
|
1276
|
+
rope = false;
|
|
1277
|
+
/** 'hinge' joints: angle range in radians, or undefined for a free hinge. */
|
|
1278
|
+
limits;
|
|
1279
|
+
/** 'hinge' joints: target speed in radians per second, and the torque available to reach it (0 = off). 'slider' joints: travel speed. */
|
|
1280
|
+
motorSpeed = 0;
|
|
1281
|
+
maxMotorTorque = 0;
|
|
1282
|
+
/** 'slider' joints: the force available to reach `motorSpeed` (0 = off). */
|
|
1283
|
+
maxMotorForce = 0;
|
|
1284
|
+
/** 'distance' joints: swings per second of the spring (0 = rigid), and how quickly it settles. */
|
|
1285
|
+
stiffness = 0;
|
|
1286
|
+
damping = 0;
|
|
1287
|
+
/** @internal */ slide = v3();
|
|
1288
|
+
/** @internal */ slideMass = 0;
|
|
1289
|
+
/** 'point' joints: largest swing angle in radians, or undefined for none. */
|
|
1290
|
+
coneLimit;
|
|
1291
|
+
/** @internal */ points = [];
|
|
1292
|
+
/** @internal */ axisA = v3(0, 1, 0);
|
|
1293
|
+
/** @internal */ axisB = v3(0, 1, 0);
|
|
1294
|
+
/** @internal */ sideA = v3(1, 0, 0);
|
|
1295
|
+
/** @internal */ sideB = v3(1, 0, 0);
|
|
1296
|
+
/** @internal */ row = v3();
|
|
1297
|
+
/** @internal */ rowMass = 0;
|
|
1298
|
+
/** @internal */ rowArm = 1;
|
|
1299
|
+
/** @internal */ rowDirection = v3();
|
|
1300
|
+
/** @internal */ rowA = v3();
|
|
1301
|
+
/** @internal */ rowB = v3();
|
|
1302
|
+
/** @internal */ rowLow = NaN;
|
|
1303
|
+
/** @internal */ rowHigh = NaN;
|
|
1304
|
+
/** @internal */ limitImpulse = 0;
|
|
1305
|
+
/** @internal */ limitImpulseHigh = 0;
|
|
1306
|
+
/** @internal */ motorImpulse = 0;
|
|
1307
|
+
/** @internal */ current = 0;
|
|
1308
|
+
/** @internal */ accumulated = 0;
|
|
1309
|
+
/** @internal */ world;
|
|
1310
|
+
constructor(options) {
|
|
1311
|
+
const a = options?.bodyA, b = options.bodyB;
|
|
1312
|
+
if (!(a instanceof RigidBody3D) || (b !== undefined && !(b instanceof RigidBody3D)) || a === b)
|
|
1313
|
+
throw new World3DError('WORLD3D_INPUT_INVALID', 'joint bodies');
|
|
1314
|
+
if (!['point', 'hinge', 'fixed', 'distance', 'slider'].includes(options.type))
|
|
1315
|
+
throw new World3DError('WORLD3D_INPUT_INVALID', 'joint type');
|
|
1316
|
+
this.type = options.type;
|
|
1317
|
+
this.bodyA = a;
|
|
1318
|
+
this.bodyB = b;
|
|
1319
|
+
this.collideConnected = options.collideConnected ?? false;
|
|
1320
|
+
const anchor = options.anchor ?? [a.position[0], a.position[1], a.position[2]];
|
|
1321
|
+
const add = (ax, ay, az, bx = ax, by = ay, bz = az) => {
|
|
1322
|
+
this.points.push({
|
|
1323
|
+
localA: toLocal(a, ax, ay, az),
|
|
1324
|
+
localB: b ? toLocal(b, bx, by, bz) : v3(bx, by, bz),
|
|
1325
|
+
ra: v3(),
|
|
1326
|
+
rb: v3(),
|
|
1327
|
+
bias: v3(),
|
|
1328
|
+
inverse: new Float64Array(9)
|
|
1329
|
+
});
|
|
1330
|
+
};
|
|
1331
|
+
if (this.type === 'distance') {
|
|
1332
|
+
const other = options.anchorB ?? (b ? [b.position[0], b.position[1], b.position[2]] : anchor);
|
|
1333
|
+
add(anchor[0], anchor[1], anchor[2], other[0], other[1], other[2]);
|
|
1334
|
+
this.length = options.length ?? Math.hypot(other[0] - anchor[0], other[1] - anchor[1], other[2] - anchor[2]);
|
|
1335
|
+
if (!(this.length >= 0))
|
|
1336
|
+
throw new World3DError('WORLD3D_INPUT_INVALID', 'joint length');
|
|
1337
|
+
this.rope = options.rope ?? false;
|
|
1338
|
+
this.stiffness = requireFinite3D(options.stiffness ?? 0, 'joint stiffness');
|
|
1339
|
+
this.damping = requireFinite3D(options.damping ?? 0, 'joint damping');
|
|
1340
|
+
if (this.stiffness < 0 || this.damping < 0)
|
|
1341
|
+
throw new World3DError('WORLD3D_INPUT_INVALID', 'joint stiffness and damping');
|
|
1342
|
+
}
|
|
1343
|
+
else if (options.stiffness !== undefined || options.damping !== undefined)
|
|
1344
|
+
throw new World3DError('WORLD3D_INPUT_INVALID', 'springs need a distance joint');
|
|
1345
|
+
if (options.maxMotorForce !== undefined && this.type !== 'slider')
|
|
1346
|
+
throw new World3DError('WORLD3D_INPUT_INVALID', 'maxMotorForce needs a slider joint');
|
|
1347
|
+
if (this.type === 'hinge' || this.type === 'point' || this.type === 'slider') {
|
|
1348
|
+
const axis = options.axis ?? [0, 1, 0], size = Math.sqrt(axis[0] * axis[0] + axis[1] * axis[1] + axis[2] * axis[2]);
|
|
1349
|
+
if (!(size > 0))
|
|
1350
|
+
throw new World3DError('WORLD3D_INPUT_INVALID', 'joint axis');
|
|
1351
|
+
const x = axis[0] / size, y = axis[1] / size, z = axis[2] / size;
|
|
1352
|
+
// Any direction across the axis serves as the zero mark for measuring the angle.
|
|
1353
|
+
const seed = Math.abs(x) < 0.57 ? [1, 0, 0] : [0, 1, 0], side = cross(v3(), [x, y, z], seed), length = Math.sqrt(side[0] * side[0] + side[1] * side[1] + side[2] * side[2]);
|
|
1354
|
+
side[0] /= length;
|
|
1355
|
+
side[1] /= length;
|
|
1356
|
+
side[2] /= length;
|
|
1357
|
+
const turn = (body, out, wx, wy, wz) => {
|
|
1358
|
+
if (!body) {
|
|
1359
|
+
out[0] = wx;
|
|
1360
|
+
out[1] = wy;
|
|
1361
|
+
out[2] = wz;
|
|
1362
|
+
return;
|
|
1363
|
+
}
|
|
1364
|
+
const r = body.rotation;
|
|
1365
|
+
out[0] = r[0] * wx + r[3] * wy + r[6] * wz;
|
|
1366
|
+
out[1] = r[1] * wx + r[4] * wy + r[7] * wz;
|
|
1367
|
+
out[2] = r[2] * wx + r[5] * wy + r[8] * wz;
|
|
1368
|
+
};
|
|
1369
|
+
turn(a, this.axisA, x, y, z);
|
|
1370
|
+
turn(b, this.axisB, x, y, z);
|
|
1371
|
+
turn(a, this.sideA, side[0], side[1], side[2]);
|
|
1372
|
+
turn(b, this.sideB, side[0], side[1], side[2]);
|
|
1373
|
+
if (options.limits) {
|
|
1374
|
+
if (this.type === 'point' ||
|
|
1375
|
+
!(options.limits[0] <= options.limits[1]) ||
|
|
1376
|
+
(this.type === 'hinge' && (options.limits[0] < -Math.PI || options.limits[1] > Math.PI)))
|
|
1377
|
+
throw new World3DError('WORLD3D_INPUT_INVALID', 'joint limits');
|
|
1378
|
+
this.limits = [options.limits[0], options.limits[1]];
|
|
1379
|
+
}
|
|
1380
|
+
if (options.coneLimit !== undefined) {
|
|
1381
|
+
if (this.type !== 'point' || !(options.coneLimit >= 0 && options.coneLimit <= Math.PI))
|
|
1382
|
+
throw new World3DError('WORLD3D_INPUT_INVALID', 'joint coneLimit');
|
|
1383
|
+
this.coneLimit = options.coneLimit;
|
|
1384
|
+
}
|
|
1385
|
+
this.motorSpeed = requireFinite3D(options.motorSpeed ?? 0, 'joint motorSpeed');
|
|
1386
|
+
this.maxMotorTorque = requireFinite3D(options.maxMotorTorque ?? 0, 'joint maxMotorTorque');
|
|
1387
|
+
if (this.maxMotorTorque < 0 || (this.maxMotorTorque > 0 && this.type !== 'hinge'))
|
|
1388
|
+
throw new World3DError('WORLD3D_INPUT_INVALID', 'joint maxMotorTorque');
|
|
1389
|
+
this.maxMotorForce = requireFinite3D(options.maxMotorForce ?? 0, 'joint maxMotorForce');
|
|
1390
|
+
if (this.maxMotorForce < 0)
|
|
1391
|
+
throw new World3DError('WORLD3D_INPUT_INVALID', 'joint maxMotorForce');
|
|
1392
|
+
}
|
|
1393
|
+
else if (options.limits || options.coneLimit !== undefined || options.maxMotorTorque)
|
|
1394
|
+
throw new World3DError('WORLD3D_INPUT_INVALID', 'limits and motors need a hinge or point joint');
|
|
1395
|
+
if (this.type === 'distance') {
|
|
1396
|
+
/* built above */
|
|
1397
|
+
}
|
|
1398
|
+
else if (this.type === 'point')
|
|
1399
|
+
add(anchor[0], anchor[1], anchor[2]);
|
|
1400
|
+
else if (this.type === 'hinge') {
|
|
1401
|
+
// Two pivots along the pin leave exactly one free rotation: about the pin.
|
|
1402
|
+
const axis = options.axis ?? [0, 1, 0], size = Math.sqrt(axis[0] * axis[0] + axis[1] * axis[1] + axis[2] * axis[2]);
|
|
1403
|
+
if (!(size > 0))
|
|
1404
|
+
throw new World3DError('WORLD3D_INPUT_INVALID', 'joint axis');
|
|
1405
|
+
const k = 0.5 / size;
|
|
1406
|
+
add(anchor[0] - axis[0] * k, anchor[1] - axis[1] * k, anchor[2] - axis[2] * k);
|
|
1407
|
+
add(anchor[0] + axis[0] * k, anchor[1] + axis[1] * k, anchor[2] + axis[2] * k);
|
|
1408
|
+
}
|
|
1409
|
+
else {
|
|
1410
|
+
// Three pivots that are not in a line lock every rotation.
|
|
1411
|
+
add(anchor[0], anchor[1], anchor[2]);
|
|
1412
|
+
add(anchor[0] + 0.5, anchor[1], anchor[2]);
|
|
1413
|
+
add(anchor[0], anchor[1] + 0.5, anchor[2]);
|
|
1414
|
+
}
|
|
1415
|
+
}
|
|
1416
|
+
/**
|
|
1417
|
+
* 'hinge' joints: how far bodyA has turned about the pin since creation,
|
|
1418
|
+
* relative to bodyB or the world, in radians. 'point' joints: how far
|
|
1419
|
+
* the two bodies have swung apart, measured between their copies of the axis.
|
|
1420
|
+
*/
|
|
1421
|
+
/** 'slider' joints: how far bodyA has travelled along the axis since creation, relative to bodyB or the world. */
|
|
1422
|
+
get translation() {
|
|
1423
|
+
if (this.type !== 'slider')
|
|
1424
|
+
return 0;
|
|
1425
|
+
const a = this.bodyA, b = this.bodyB, point = this.points[0], n = rotateInto(v3(), a, this.axisA);
|
|
1426
|
+
rotateInto(jointA, a, point.localA);
|
|
1427
|
+
if (b)
|
|
1428
|
+
rotateInto(jointB, b, point.localB);
|
|
1429
|
+
let travel = 0;
|
|
1430
|
+
for (let i = 0; i < 3; i++)
|
|
1431
|
+
travel += (a.position[i] + jointA[i] - (b ? b.position[i] + jointB[i] : point.localB[i])) * n[i];
|
|
1432
|
+
return travel;
|
|
1433
|
+
}
|
|
1434
|
+
get angle() {
|
|
1435
|
+
const a = this.bodyA, b = this.bodyB;
|
|
1436
|
+
if (this.type === 'point') {
|
|
1437
|
+
rotateInto(jointA, a, this.axisA);
|
|
1438
|
+
if (b)
|
|
1439
|
+
rotateInto(jointB, b, this.axisB);
|
|
1440
|
+
else
|
|
1441
|
+
jointB.set(this.axisB);
|
|
1442
|
+
return Math.acos(clamp(dot(jointA, jointB), -1, 1));
|
|
1443
|
+
}
|
|
1444
|
+
if (this.type !== 'hinge')
|
|
1445
|
+
return 0;
|
|
1446
|
+
const n = rotateInto(v3(), a, this.axisA);
|
|
1447
|
+
rotateInto(jointA, a, this.sideA);
|
|
1448
|
+
if (b)
|
|
1449
|
+
rotateInto(jointB, b, this.sideB);
|
|
1450
|
+
else
|
|
1451
|
+
jointB.set(this.sideB);
|
|
1452
|
+
cross(jointC, jointB, jointA);
|
|
1453
|
+
return Math.atan2(dot(jointC, n), dot(jointA, jointB));
|
|
1454
|
+
}
|
|
1455
|
+
}
|
|
1456
|
+
function toLocal(body, x, y, z) {
|
|
1457
|
+
const r = body.rotation, p = body.position, dx = x - p[0], dy = y - p[1], dz = z - p[2];
|
|
1458
|
+
return v3(r[0] * dx + r[3] * dy + r[6] * dz, r[1] * dx + r[4] * dy + r[7] * dz, r[2] * dx + r[5] * dy + r[8] * dz);
|
|
1459
|
+
}
|
|
1460
|
+
function rotateInto(out, body, local) {
|
|
1461
|
+
const r = body.rotation, x = local[0], y = local[1], z = local[2];
|
|
1462
|
+
out[0] = r[0] * x + r[1] * y + r[2] * z;
|
|
1463
|
+
out[1] = r[3] * x + r[4] * y + r[5] * z;
|
|
1464
|
+
out[2] = r[6] * x + r[7] * y + r[8] * z;
|
|
1465
|
+
return out;
|
|
1466
|
+
}
|
|
1467
|
+
const jointA = v3(), jointB = v3(), jointC = v3(), jointK = new Float64Array(9), JOINT_BAUMGARTE = 0.2;
|
|
1468
|
+
/** u^T K v for a 3x3 matrix. */
|
|
1469
|
+
function quadratic(k, u, v) {
|
|
1470
|
+
return (u[0] * (k[0] * v[0] + k[1] * v[1] + k[2] * v[2]) +
|
|
1471
|
+
u[1] * (k[3] * v[0] + k[4] * v[1] + k[5] * v[2]) +
|
|
1472
|
+
u[2] * (k[6] * v[0] + k[7] * v[1] + k[8] * v[2]));
|
|
1473
|
+
}
|
|
1474
|
+
/** Add one body's contribution to the 3x3 effective-mass matrix of a pivot at offset r. */
|
|
1475
|
+
function addPivotMass(k, body, r) {
|
|
1476
|
+
if (body.type !== 'dynamic' || body.isSleeping)
|
|
1477
|
+
return;
|
|
1478
|
+
k[0] += body.inverseMass;
|
|
1479
|
+
k[4] += body.inverseMass;
|
|
1480
|
+
k[8] += body.inverseMass;
|
|
1481
|
+
for (let i = 0; i < 3; i++) {
|
|
1482
|
+
// Column i is (I^-1 (r x e_i)) x r.
|
|
1483
|
+
jointA[0] = jointA[1] = jointA[2] = 0;
|
|
1484
|
+
jointA[i] = 1;
|
|
1485
|
+
cross(jointB, r, jointA);
|
|
1486
|
+
applyInverseInertia(jointC, body.inverseInertiaWorld, jointB);
|
|
1487
|
+
cross(jointB, jointC, r);
|
|
1488
|
+
k[i] += jointB[0];
|
|
1489
|
+
k[3 + i] += jointB[1];
|
|
1490
|
+
k[6 + i] += jointB[2];
|
|
1491
|
+
}
|
|
1492
|
+
}
|
|
1493
|
+
function applyPivotImpulse(body, r, x, y, z, sign) {
|
|
1494
|
+
if (body.type !== 'dynamic' || body.isSleeping)
|
|
1495
|
+
return;
|
|
1496
|
+
const v = body.linearVelocity, w = body.angularVelocity;
|
|
1497
|
+
v[0] += x * body.inverseMass * sign;
|
|
1498
|
+
v[1] += y * body.inverseMass * sign;
|
|
1499
|
+
v[2] += z * body.inverseMass * sign;
|
|
1500
|
+
jointA[0] = x;
|
|
1501
|
+
jointA[1] = y;
|
|
1502
|
+
jointA[2] = z;
|
|
1503
|
+
cross(jointB, r, jointA);
|
|
1504
|
+
applyInverseInertia(jointC, body.inverseInertiaWorld, jointB);
|
|
1505
|
+
w[0] += jointC[0] * sign;
|
|
1506
|
+
w[1] += jointC[1] * sign;
|
|
1507
|
+
w[2] += jointC[2] * sign;
|
|
1508
|
+
}
|
|
1509
|
+
function pivotVelocity(out, body, r) {
|
|
1510
|
+
if (!body) {
|
|
1511
|
+
out[0] = out[1] = out[2] = 0;
|
|
1512
|
+
return out;
|
|
1513
|
+
}
|
|
1514
|
+
cross(out, body.angularVelocity, r);
|
|
1515
|
+
out[0] += body.linearVelocity[0];
|
|
1516
|
+
out[1] += body.linearVelocity[1];
|
|
1517
|
+
out[2] += body.linearVelocity[2];
|
|
1518
|
+
return out;
|
|
1519
|
+
}
|
|
1520
|
+
const SLOP = 0.01, BAUMGARTE = 0.15, SLEEP_LINEAR = 0.15, SLEEP_ANGULAR = 0.2, SLEEP_DELAY = 0.5;
|
|
1521
|
+
export class PhysicsWorld3D {
|
|
1522
|
+
gravity = v3(0, -9.81, 0);
|
|
1523
|
+
fixedStep;
|
|
1524
|
+
maxSubSteps;
|
|
1525
|
+
iterations;
|
|
1526
|
+
solverSteps;
|
|
1527
|
+
allowSleep;
|
|
1528
|
+
/** Called when two bodies start or stop touching. Triggers report here too. */
|
|
1529
|
+
onContactBegin;
|
|
1530
|
+
onContactEnd;
|
|
1531
|
+
/**
|
|
1532
|
+
* Called after every step for each pair of solid bodies that pressed on each other, with
|
|
1533
|
+
* where, in which direction and how hard: enough to pick a sound, deal damage or squash a shape.
|
|
1534
|
+
*/
|
|
1535
|
+
onContact;
|
|
1536
|
+
#report = {
|
|
1537
|
+
a: undefined,
|
|
1538
|
+
b: undefined,
|
|
1539
|
+
point: v3(),
|
|
1540
|
+
normal: v3(),
|
|
1541
|
+
impulse: 0,
|
|
1542
|
+
began: false
|
|
1543
|
+
};
|
|
1544
|
+
#bodies = [];
|
|
1545
|
+
#manifold = new Manifold();
|
|
1546
|
+
#contacts = [];
|
|
1547
|
+
#contactCount = 0;
|
|
1548
|
+
#touching = new Map();
|
|
1549
|
+
#previousTouching = new Map();
|
|
1550
|
+
#accumulator = 0;
|
|
1551
|
+
/** Bodies that can move, kept sorted along x from step to step. */
|
|
1552
|
+
#sorted = [];
|
|
1553
|
+
#planes = [];
|
|
1554
|
+
#statics = new StaticTree();
|
|
1555
|
+
#joints = [];
|
|
1556
|
+
#jointPairs = new Map();
|
|
1557
|
+
#warm = new Map();
|
|
1558
|
+
#warmFeatures = new Int16Array(8);
|
|
1559
|
+
#warmImpulses = new Float64Array(24);
|
|
1560
|
+
#stamp = 0;
|
|
1561
|
+
constructor(options = {}) {
|
|
1562
|
+
if (options.gravity)
|
|
1563
|
+
this.gravity.set(options.gravity);
|
|
1564
|
+
this.fixedStep = requirePositive3D(options.fixedStep ?? 1 / 60, 'fixedStep');
|
|
1565
|
+
this.maxSubSteps = Math.min(Math.max(Math.floor(options.maxSubSteps ?? 4), 1), PHYSICS3D_LIMITS.maxSubSteps);
|
|
1566
|
+
this.iterations = Math.min(Math.max(Math.floor(options.iterations ?? 8), 1), 64);
|
|
1567
|
+
this.solverSteps = Math.min(Math.max(Math.floor(options.solverSteps ?? 1), 1), PHYSICS3D_LIMITS.maxSolverSteps);
|
|
1568
|
+
this.allowSleep = options.allowSleep ?? true;
|
|
1569
|
+
}
|
|
1570
|
+
get bodies() {
|
|
1571
|
+
return this.#bodies;
|
|
1572
|
+
}
|
|
1573
|
+
/** @internal A static body was repositioned: its place among the others is worked out again. */
|
|
1574
|
+
staticMoved(body) {
|
|
1575
|
+
if (body.type === 'static')
|
|
1576
|
+
this.#statics.touch();
|
|
1577
|
+
}
|
|
1578
|
+
/** Visit every body whose bounds meet a box: planes, bodies that can move, then static ones. */
|
|
1579
|
+
near(lx, ly, lz, hx, hy, hz, visit) {
|
|
1580
|
+
for (const plane of this.#planes)
|
|
1581
|
+
visit(plane);
|
|
1582
|
+
for (const body of this.#sorted) {
|
|
1583
|
+
const b = body.aabb;
|
|
1584
|
+
if (b[0] > hx || b[3] < lx || b[1] > hy || b[4] < ly || b[2] > hz || b[5] < lz)
|
|
1585
|
+
continue;
|
|
1586
|
+
visit(body);
|
|
1587
|
+
}
|
|
1588
|
+
this.#statics.query(lx, ly, lz, hx, hy, hz, visit);
|
|
1589
|
+
}
|
|
1590
|
+
/** Contact points solved in the last step. */
|
|
1591
|
+
get contactCount() {
|
|
1592
|
+
return this.#contactCount;
|
|
1593
|
+
}
|
|
1594
|
+
addBody(body) {
|
|
1595
|
+
if (!(body instanceof RigidBody3D) || body.world)
|
|
1596
|
+
throw new World3DError('WORLD3D_INPUT_INVALID', 'body');
|
|
1597
|
+
if (this.#bodies.length >= PHYSICS3D_LIMITS.maxBodies)
|
|
1598
|
+
throw new World3DError('WORLD3D_BUDGET', 'bodies');
|
|
1599
|
+
body.world = this;
|
|
1600
|
+
this.#bodies.push(body);
|
|
1601
|
+
if (body.shape.type === 'plane')
|
|
1602
|
+
this.#planes.push(body);
|
|
1603
|
+
else if (body.type === 'static')
|
|
1604
|
+
this.#statics.add(body);
|
|
1605
|
+
else
|
|
1606
|
+
this.#sorted.push(body);
|
|
1607
|
+
// Static bodies never move, so their node is placed once, here.
|
|
1608
|
+
const node = body.node;
|
|
1609
|
+
if (node) {
|
|
1610
|
+
node.position.set(body.position);
|
|
1611
|
+
node.quaternion.set(body.quaternion);
|
|
1612
|
+
node.markTransformDirty();
|
|
1613
|
+
}
|
|
1614
|
+
return body;
|
|
1615
|
+
}
|
|
1616
|
+
/** Create a body from options and add it. */
|
|
1617
|
+
createBody(options) {
|
|
1618
|
+
return this.addBody(new RigidBody3D(options));
|
|
1619
|
+
}
|
|
1620
|
+
get joints() {
|
|
1621
|
+
return this.#joints;
|
|
1622
|
+
}
|
|
1623
|
+
/** Connect two bodies, or pin one to the world. Anchors are given in world space as the bodies stand now. */
|
|
1624
|
+
createJoint(options) {
|
|
1625
|
+
const joint = new Joint3D(options);
|
|
1626
|
+
if (joint.bodyA.world !== this || (joint.bodyB && joint.bodyB.world !== this))
|
|
1627
|
+
throw new World3DError('WORLD3D_INPUT_INVALID', 'joint bodies are not in this world');
|
|
1628
|
+
joint.world = this;
|
|
1629
|
+
this.#joints.push(joint);
|
|
1630
|
+
if (joint.bodyB && !joint.collideConnected) {
|
|
1631
|
+
const key = this.pairKey(joint.bodyA, joint.bodyB);
|
|
1632
|
+
this.#jointPairs.set(key, (this.#jointPairs.get(key) ?? 0) + 1);
|
|
1633
|
+
}
|
|
1634
|
+
joint.bodyA.wake();
|
|
1635
|
+
joint.bodyB?.wake();
|
|
1636
|
+
return joint;
|
|
1637
|
+
}
|
|
1638
|
+
removeJoint(joint) {
|
|
1639
|
+
const index = this.#joints.indexOf(joint);
|
|
1640
|
+
if (index < 0)
|
|
1641
|
+
return;
|
|
1642
|
+
this.#joints.splice(index, 1);
|
|
1643
|
+
joint.world = undefined;
|
|
1644
|
+
if (joint.bodyB && !joint.collideConnected) {
|
|
1645
|
+
const key = this.pairKey(joint.bodyA, joint.bodyB), count = (this.#jointPairs.get(key) ?? 1) - 1;
|
|
1646
|
+
if (count > 0)
|
|
1647
|
+
this.#jointPairs.set(key, count);
|
|
1648
|
+
else
|
|
1649
|
+
this.#jointPairs.delete(key);
|
|
1650
|
+
}
|
|
1651
|
+
joint.bodyA.wake();
|
|
1652
|
+
joint.bodyB?.wake();
|
|
1653
|
+
}
|
|
1654
|
+
prepareJoints(dt) {
|
|
1655
|
+
for (const joint of this.#joints) {
|
|
1656
|
+
const a = joint.bodyA, b = joint.bodyB;
|
|
1657
|
+
// A moving partner wakes a sleeper, so a chain cannot hang frozen in mid-swing.
|
|
1658
|
+
if (a.isSleeping && b && isMoving(b))
|
|
1659
|
+
a.wake();
|
|
1660
|
+
if (b?.isSleeping && isMoving(a))
|
|
1661
|
+
b.wake();
|
|
1662
|
+
joint.accumulated = 0;
|
|
1663
|
+
joint.limitImpulse = 0;
|
|
1664
|
+
joint.limitImpulseHigh = 0;
|
|
1665
|
+
joint.motorImpulse = 0;
|
|
1666
|
+
joint.rowMass = 0;
|
|
1667
|
+
joint.rowLow = joint.rowHigh = NaN;
|
|
1668
|
+
joint.slideMass = 0;
|
|
1669
|
+
const sliding = joint.type === 'slider' && joint.enabled;
|
|
1670
|
+
if (sliding) {
|
|
1671
|
+
rotateInto(joint.slide, a, joint.axisA);
|
|
1672
|
+
joint.current = joint.translation;
|
|
1673
|
+
if (joint.limits) {
|
|
1674
|
+
joint.rowLow = joint.current - joint.limits[0];
|
|
1675
|
+
joint.rowHigh = joint.current - joint.limits[1];
|
|
1676
|
+
}
|
|
1677
|
+
if (joint.maxMotorForce > 0 && joint.motorSpeed !== 0) {
|
|
1678
|
+
a.wake();
|
|
1679
|
+
b?.wake();
|
|
1680
|
+
}
|
|
1681
|
+
}
|
|
1682
|
+
const motor = joint.type === 'hinge' && joint.maxMotorTorque > 0;
|
|
1683
|
+
if (joint.enabled && (motor || joint.limits || joint.coneLimit !== undefined)) {
|
|
1684
|
+
// One rotational equation: about the pin for a hinge, about the swing direction for a cone.
|
|
1685
|
+
const row = joint.row;
|
|
1686
|
+
let active = true;
|
|
1687
|
+
if (joint.type === 'hinge') {
|
|
1688
|
+
// Reversed pin, so that a positive turn of bodyA relative to bodyB counts as positive.
|
|
1689
|
+
rotateInto(row, a, joint.axisA);
|
|
1690
|
+
row[0] = -row[0];
|
|
1691
|
+
row[1] = -row[1];
|
|
1692
|
+
row[2] = -row[2];
|
|
1693
|
+
joint.current = joint.angle;
|
|
1694
|
+
if (joint.limits) {
|
|
1695
|
+
joint.rowLow = joint.current - joint.limits[0];
|
|
1696
|
+
joint.rowHigh = joint.current - joint.limits[1];
|
|
1697
|
+
}
|
|
1698
|
+
if (motor && joint.motorSpeed !== 0) {
|
|
1699
|
+
a.wake();
|
|
1700
|
+
b?.wake();
|
|
1701
|
+
}
|
|
1702
|
+
}
|
|
1703
|
+
else {
|
|
1704
|
+
rotateInto(jointA, a, joint.axisA);
|
|
1705
|
+
if (b)
|
|
1706
|
+
rotateInto(jointB, b, joint.axisB);
|
|
1707
|
+
else
|
|
1708
|
+
jointB.set(joint.axisB);
|
|
1709
|
+
cross(row, jointA, jointB);
|
|
1710
|
+
const size = Math.sqrt(row[0] * row[0] + row[1] * row[1] + row[2] * row[2]);
|
|
1711
|
+
joint.current = Math.atan2(size, dot(jointA, jointB));
|
|
1712
|
+
if (size > 1e-6) {
|
|
1713
|
+
row[0] /= size;
|
|
1714
|
+
row[1] /= size;
|
|
1715
|
+
row[2] /= size;
|
|
1716
|
+
joint.rowHigh = joint.current - joint.coneLimit;
|
|
1717
|
+
}
|
|
1718
|
+
else
|
|
1719
|
+
active = false;
|
|
1720
|
+
}
|
|
1721
|
+
if (active)
|
|
1722
|
+
this.prepareRow(joint);
|
|
1723
|
+
}
|
|
1724
|
+
for (const point of joint.points) {
|
|
1725
|
+
rotateInto(point.ra, a, point.localA);
|
|
1726
|
+
if (b)
|
|
1727
|
+
rotateInto(point.rb, b, point.localB);
|
|
1728
|
+
else
|
|
1729
|
+
point.rb.fill(0);
|
|
1730
|
+
// Positional error: where b's pivot is relative to a's.
|
|
1731
|
+
for (let i = 0; i < 3; i++)
|
|
1732
|
+
point.bias[i] =
|
|
1733
|
+
(((b ? b.position[i] + point.rb[i] : point.localB[i]) - a.position[i] - point.ra[i]) *
|
|
1734
|
+
JOINT_BAUMGARTE) /
|
|
1735
|
+
dt;
|
|
1736
|
+
const k = jointK;
|
|
1737
|
+
k.fill(0);
|
|
1738
|
+
addPivotMass(k, a, point.ra);
|
|
1739
|
+
if (b)
|
|
1740
|
+
addPivotMass(k, b, point.rb);
|
|
1741
|
+
if (sliding) {
|
|
1742
|
+
// Hold the pivot only across the rail: solve in the two directions at right angles to it.
|
|
1743
|
+
const n = joint.slide, inverse = point.inverse;
|
|
1744
|
+
if (point === joint.points[0]) {
|
|
1745
|
+
const along = quadratic(k, n, n);
|
|
1746
|
+
joint.slideMass = along > 1e-12 ? 1 / along : 0;
|
|
1747
|
+
}
|
|
1748
|
+
cross(jointA, n, Math.abs(n[0]) < 0.57 ? [1, 0, 0] : [0, 1, 0]);
|
|
1749
|
+
const size = Math.sqrt(jointA[0] * jointA[0] + jointA[1] * jointA[1] + jointA[2] * jointA[2]);
|
|
1750
|
+
jointA[0] /= size;
|
|
1751
|
+
jointA[1] /= size;
|
|
1752
|
+
jointA[2] /= size;
|
|
1753
|
+
cross(jointB, n, jointA);
|
|
1754
|
+
const k11 = quadratic(k, jointA, jointA), k12 = quadratic(k, jointA, jointB), k22 = quadratic(k, jointB, jointB), det = k11 * k22 - k12 * k12;
|
|
1755
|
+
if (Math.abs(det) < 1e-12) {
|
|
1756
|
+
inverse.fill(0);
|
|
1757
|
+
continue;
|
|
1758
|
+
}
|
|
1759
|
+
const i11 = k22 / det, i12 = -k12 / det, i22 = k11 / det;
|
|
1760
|
+
for (let row = 0; row < 3; row++)
|
|
1761
|
+
for (let column = 0; column < 3; column++) {
|
|
1762
|
+
inverse[row * 3 + column] =
|
|
1763
|
+
jointA[row] * (i11 * jointA[column] + i12 * jointB[column]) +
|
|
1764
|
+
jointB[row] * (i12 * jointA[column] + i22 * jointB[column]);
|
|
1765
|
+
}
|
|
1766
|
+
continue;
|
|
1767
|
+
}
|
|
1768
|
+
// Invert the symmetric 3x3 by cofactors.
|
|
1769
|
+
const c0 = k[4] * k[8] - k[5] * k[7], c1 = k[5] * k[6] - k[3] * k[8], c2 = k[3] * k[7] - k[4] * k[6], determinant = k[0] * c0 + k[1] * c1 + k[2] * c2, inverse = point.inverse;
|
|
1770
|
+
if (Math.abs(determinant) < 1e-12) {
|
|
1771
|
+
inverse.fill(0);
|
|
1772
|
+
continue;
|
|
1773
|
+
}
|
|
1774
|
+
const d = 1 / determinant;
|
|
1775
|
+
inverse[0] = c0 * d;
|
|
1776
|
+
inverse[1] = (k[2] * k[7] - k[1] * k[8]) * d;
|
|
1777
|
+
inverse[2] = (k[1] * k[5] - k[2] * k[4]) * d;
|
|
1778
|
+
inverse[3] = c1 * d;
|
|
1779
|
+
inverse[4] = (k[0] * k[8] - k[2] * k[6]) * d;
|
|
1780
|
+
inverse[5] = (k[2] * k[3] - k[0] * k[5]) * d;
|
|
1781
|
+
inverse[6] = c2 * d;
|
|
1782
|
+
inverse[7] = (k[1] * k[6] - k[0] * k[7]) * d;
|
|
1783
|
+
inverse[8] = (k[0] * k[4] - k[1] * k[3]) * d;
|
|
1784
|
+
}
|
|
1785
|
+
}
|
|
1786
|
+
}
|
|
1787
|
+
/**
|
|
1788
|
+
* Set up the rotational equation of a joint. Turning impulses are applied
|
|
1789
|
+
* as a push at the point where the more mobile body would pivot freely
|
|
1790
|
+
* about the joint (its center of percussion), which leaves the pivot
|
|
1791
|
+
* itself undisturbed and so settles in few iterations even for a small
|
|
1792
|
+
* weight on a long arm.
|
|
1793
|
+
*/
|
|
1794
|
+
prepareRow(joint) {
|
|
1795
|
+
const a = joint.bodyA, b = joint.bodyB, u = joint.row, pivot = scratchA;
|
|
1796
|
+
pivot.fill(0);
|
|
1797
|
+
for (const point of joint.points) {
|
|
1798
|
+
rotateInto(jointA, a, point.localA);
|
|
1799
|
+
pivot[0] += jointA[0];
|
|
1800
|
+
pivot[1] += jointA[1];
|
|
1801
|
+
pivot[2] += jointA[2];
|
|
1802
|
+
}
|
|
1803
|
+
const share = 1 / joint.points.length;
|
|
1804
|
+
pivot[0] = a.position[0] + pivot[0] * share;
|
|
1805
|
+
pivot[1] = a.position[1] + pivot[1] * share;
|
|
1806
|
+
pivot[2] = a.position[2] + pivot[2] * share;
|
|
1807
|
+
let best = 0, arm = 0, ex = 0, ey = 0, ez = 0;
|
|
1808
|
+
for (const body of [a, b]) {
|
|
1809
|
+
if (!body || body.type !== 'dynamic' || body.isSleeping)
|
|
1810
|
+
continue;
|
|
1811
|
+
applyInverseInertia(jointC, body.inverseInertiaWorld, u);
|
|
1812
|
+
const spin = dot(u, jointC);
|
|
1813
|
+
// Offset of the center from the pivot, across the turning direction.
|
|
1814
|
+
let x = body.position[0] - pivot[0], y = body.position[1] - pivot[1], z = body.position[2] - pivot[2];
|
|
1815
|
+
const along = x * u[0] + y * u[1] + z * u[2];
|
|
1816
|
+
x -= u[0] * along;
|
|
1817
|
+
y -= u[1] * along;
|
|
1818
|
+
z -= u[2] * along;
|
|
1819
|
+
const offset = Math.sqrt(x * x + y * y + z * z), inertia = spin > 1e-12 ? 1 / spin : Infinity, mobility = 1 / (inertia + body.mass * offset * offset);
|
|
1820
|
+
if (mobility <= best)
|
|
1821
|
+
continue;
|
|
1822
|
+
best = mobility;
|
|
1823
|
+
if (offset > 1e-4 && inertia < Infinity) {
|
|
1824
|
+
arm = offset + inertia / (body.mass * offset);
|
|
1825
|
+
ex = x / offset;
|
|
1826
|
+
ey = y / offset;
|
|
1827
|
+
ez = z / offset;
|
|
1828
|
+
}
|
|
1829
|
+
else
|
|
1830
|
+
arm = 0;
|
|
1831
|
+
}
|
|
1832
|
+
if (best === 0)
|
|
1833
|
+
return;
|
|
1834
|
+
if (arm === 0) {
|
|
1835
|
+
// Centered on the pivot: a far-away push acts as a pure twist.
|
|
1836
|
+
cross(jointB, u, Math.abs(u[0]) < 0.57 ? [1, 0, 0] : [0, 1, 0]);
|
|
1837
|
+
const size = Math.sqrt(jointB[0] * jointB[0] + jointB[1] * jointB[1] + jointB[2] * jointB[2]);
|
|
1838
|
+
ex = jointB[0] / size;
|
|
1839
|
+
ey = jointB[1] / size;
|
|
1840
|
+
ez = jointB[2] / size;
|
|
1841
|
+
arm = 1000;
|
|
1842
|
+
}
|
|
1843
|
+
const t = cross(joint.rowDirection, u, [ex, ey, ez]), px = pivot[0] + ex * arm, py = pivot[1] + ey * arm, pz = pivot[2] + ez * arm;
|
|
1844
|
+
joint.rowArm = arm;
|
|
1845
|
+
joint.rowA[0] = px - a.position[0];
|
|
1846
|
+
joint.rowA[1] = py - a.position[1];
|
|
1847
|
+
joint.rowA[2] = pz - a.position[2];
|
|
1848
|
+
if (b) {
|
|
1849
|
+
joint.rowB[0] = px - b.position[0];
|
|
1850
|
+
joint.rowB[1] = py - b.position[1];
|
|
1851
|
+
joint.rowB[2] = pz - b.position[2];
|
|
1852
|
+
}
|
|
1853
|
+
let k = 0;
|
|
1854
|
+
for (const [body, r] of [
|
|
1855
|
+
[a, joint.rowA],
|
|
1856
|
+
[b, joint.rowB]
|
|
1857
|
+
]) {
|
|
1858
|
+
if (!body || body.type !== 'dynamic' || body.isSleeping)
|
|
1859
|
+
continue;
|
|
1860
|
+
cross(jointB, r, t);
|
|
1861
|
+
applyInverseInertia(jointC, body.inverseInertiaWorld, jointB);
|
|
1862
|
+
k += body.inverseMass + dot(jointB, jointC);
|
|
1863
|
+
}
|
|
1864
|
+
joint.rowMass = k > 1e-12 ? (arm * arm) / k : 0;
|
|
1865
|
+
}
|
|
1866
|
+
solveJoint(joint, dt) {
|
|
1867
|
+
const a = joint.bodyA, b = joint.bodyB;
|
|
1868
|
+
if (!joint.enabled || ((a.type !== 'dynamic' || a.isSleeping) && (!b || b.type !== 'dynamic' || b.isSleeping)))
|
|
1869
|
+
return;
|
|
1870
|
+
if (joint.rowMass > 0) {
|
|
1871
|
+
const t = joint.rowDirection, arm = joint.rowArm;
|
|
1872
|
+
const turn = (impulse) => {
|
|
1873
|
+
if (impulse === 0)
|
|
1874
|
+
return;
|
|
1875
|
+
const push = impulse / arm;
|
|
1876
|
+
applyPivotImpulse(a, joint.rowA, t[0] * push, t[1] * push, t[2] * push, -1);
|
|
1877
|
+
if (b)
|
|
1878
|
+
applyPivotImpulse(b, joint.rowB, t[0] * push, t[1] * push, t[2] * push, 1);
|
|
1879
|
+
};
|
|
1880
|
+
const rate = () => {
|
|
1881
|
+
const va = pivotVelocity(scratchC, a, joint.rowA), vb = pivotVelocity(scratchD, b, joint.rowB);
|
|
1882
|
+
return ((vb[0] - va[0]) * t[0] + (vb[1] - va[1]) * t[1] + (vb[2] - va[2]) * t[2]) / arm;
|
|
1883
|
+
};
|
|
1884
|
+
if (joint.type === 'hinge' && joint.maxMotorTorque > 0) {
|
|
1885
|
+
const most = joint.maxMotorTorque * dt, total = clamp(joint.motorImpulse + (joint.motorSpeed - rate()) * joint.rowMass, -most, most);
|
|
1886
|
+
turn(total - joint.motorImpulse);
|
|
1887
|
+
joint.motorImpulse = total;
|
|
1888
|
+
}
|
|
1889
|
+
// A limit lets the angle close the remaining gap this step and no more; past the limit it steers back.
|
|
1890
|
+
const low = joint.rowLow, high = joint.rowHigh;
|
|
1891
|
+
if (low === low) {
|
|
1892
|
+
const total = Math.max(joint.limitImpulse + (-rate() - (low > 0 ? low : low * JOINT_BAUMGARTE) / dt) * joint.rowMass, 0);
|
|
1893
|
+
turn(total - joint.limitImpulse);
|
|
1894
|
+
joint.limitImpulse = total;
|
|
1895
|
+
}
|
|
1896
|
+
if (high === high) {
|
|
1897
|
+
const total = Math.min(joint.limitImpulseHigh + (-rate() - (high < 0 ? high : high * JOINT_BAUMGARTE) / dt) * joint.rowMass, 0);
|
|
1898
|
+
turn(total - joint.limitImpulseHigh);
|
|
1899
|
+
joint.limitImpulseHigh = total;
|
|
1900
|
+
}
|
|
1901
|
+
}
|
|
1902
|
+
if (joint.slideMass > 0) {
|
|
1903
|
+
// Along the rail: a motor, and stops at both ends of the travel.
|
|
1904
|
+
const n = joint.slide, point = joint.points[0], mass = joint.slideMass;
|
|
1905
|
+
const push = (impulse) => {
|
|
1906
|
+
if (impulse === 0)
|
|
1907
|
+
return;
|
|
1908
|
+
applyPivotImpulse(a, point.ra, n[0] * impulse, n[1] * impulse, n[2] * impulse, 1);
|
|
1909
|
+
if (b)
|
|
1910
|
+
applyPivotImpulse(b, point.rb, n[0] * impulse, n[1] * impulse, n[2] * impulse, -1);
|
|
1911
|
+
};
|
|
1912
|
+
const speed = () => {
|
|
1913
|
+
const va = pivotVelocity(scratchC, a, point.ra), vb = pivotVelocity(scratchD, b, point.rb);
|
|
1914
|
+
return (va[0] - vb[0]) * n[0] + (va[1] - vb[1]) * n[1] + (va[2] - vb[2]) * n[2];
|
|
1915
|
+
};
|
|
1916
|
+
if (joint.maxMotorForce > 0) {
|
|
1917
|
+
const most = joint.maxMotorForce * dt, total = clamp(joint.motorImpulse + (joint.motorSpeed - speed()) * mass, -most, most);
|
|
1918
|
+
push(total - joint.motorImpulse);
|
|
1919
|
+
joint.motorImpulse = total;
|
|
1920
|
+
}
|
|
1921
|
+
const low = joint.rowLow, high = joint.rowHigh;
|
|
1922
|
+
if (low === low) {
|
|
1923
|
+
const total = Math.max(joint.limitImpulse + (-speed() - (low > 0 ? low : low * JOINT_BAUMGARTE) / dt) * mass, 0);
|
|
1924
|
+
push(total - joint.limitImpulse);
|
|
1925
|
+
joint.limitImpulse = total;
|
|
1926
|
+
}
|
|
1927
|
+
if (high === high) {
|
|
1928
|
+
const total = Math.min(joint.limitImpulseHigh + (-speed() - (high < 0 ? high : high * JOINT_BAUMGARTE) / dt) * mass, 0);
|
|
1929
|
+
push(total - joint.limitImpulseHigh);
|
|
1930
|
+
joint.limitImpulseHigh = total;
|
|
1931
|
+
}
|
|
1932
|
+
}
|
|
1933
|
+
for (const point of joint.points) {
|
|
1934
|
+
const va = pivotVelocity(scratchC, a, point.ra), vb = pivotVelocity(scratchD, b, point.rb);
|
|
1935
|
+
const dx = vb[0] - va[0], dy = vb[1] - va[1], dz = vb[2] - va[2];
|
|
1936
|
+
if (joint.type === 'distance') {
|
|
1937
|
+
// One equation along the line between the two attachments.
|
|
1938
|
+
let nx = (b ? b.position[0] + point.rb[0] : point.localB[0]) - a.position[0] - point.ra[0], ny = (b ? b.position[1] + point.rb[1] : point.localB[1]) - a.position[1] - point.ra[1];
|
|
1939
|
+
let nz = (b ? b.position[2] + point.rb[2] : point.localB[2]) - a.position[2] - point.ra[2];
|
|
1940
|
+
const length = Math.sqrt(nx * nx + ny * ny + nz * nz);
|
|
1941
|
+
if (length < 1e-9 || (joint.rope && length <= joint.length))
|
|
1942
|
+
continue;
|
|
1943
|
+
nx /= length;
|
|
1944
|
+
ny /= length;
|
|
1945
|
+
nz /= length;
|
|
1946
|
+
jointA[0] = nx;
|
|
1947
|
+
jointA[1] = ny;
|
|
1948
|
+
jointA[2] = nz;
|
|
1949
|
+
let mass = 0;
|
|
1950
|
+
for (const [body, r] of [
|
|
1951
|
+
[a, point.ra],
|
|
1952
|
+
[b, point.rb]
|
|
1953
|
+
]) {
|
|
1954
|
+
if (!body || body.type !== 'dynamic' || body.isSleeping)
|
|
1955
|
+
continue;
|
|
1956
|
+
cross(jointB, r, [nx, ny, nz]);
|
|
1957
|
+
applyInverseInertia(jointC, body.inverseInertiaWorld, jointB);
|
|
1958
|
+
mass += body.inverseMass + jointB[0] * jointC[0] + jointB[1] * jointC[1] + jointB[2] * jointC[2];
|
|
1959
|
+
}
|
|
1960
|
+
if (mass <= 0)
|
|
1961
|
+
continue;
|
|
1962
|
+
if (joint.stiffness > 0) {
|
|
1963
|
+
// A spring: the link gives way in proportion to how hard it is pulled, and loses energy as it moves.
|
|
1964
|
+
const weight = 1 / mass, omega = 2 * Math.PI * joint.stiffness, spring = weight * omega * omega, loss = 2 * weight * joint.damping * omega;
|
|
1965
|
+
const gamma = 1 / (dt * (loss + dt * spring)), beta = dt * spring * gamma;
|
|
1966
|
+
let soft = -(dx * nx + dy * ny + dz * nz + beta * (length - joint.length) + gamma * joint.accumulated) /
|
|
1967
|
+
(mass + gamma);
|
|
1968
|
+
if (joint.rope) {
|
|
1969
|
+
const total = Math.min(joint.accumulated + soft, 0);
|
|
1970
|
+
soft = total - joint.accumulated;
|
|
1971
|
+
}
|
|
1972
|
+
joint.accumulated += soft;
|
|
1973
|
+
applyPivotImpulse(a, point.ra, nx * soft, ny * soft, nz * soft, -1);
|
|
1974
|
+
if (b)
|
|
1975
|
+
applyPivotImpulse(b, point.rb, nx * soft, ny * soft, nz * soft, 1);
|
|
1976
|
+
continue;
|
|
1977
|
+
}
|
|
1978
|
+
let lambda = -(dx * nx + dy * ny + dz * nz + ((length - joint.length) * JOINT_BAUMGARTE) / dt) / mass;
|
|
1979
|
+
if (joint.rope) {
|
|
1980
|
+
// A rope only pulls: the running total may never push the ends apart.
|
|
1981
|
+
const total = Math.min(joint.accumulated + lambda, 0);
|
|
1982
|
+
lambda = total - joint.accumulated;
|
|
1983
|
+
joint.accumulated = total;
|
|
1984
|
+
}
|
|
1985
|
+
applyPivotImpulse(a, point.ra, nx * lambda, ny * lambda, nz * lambda, -1);
|
|
1986
|
+
if (b)
|
|
1987
|
+
applyPivotImpulse(b, point.rb, nx * lambda, ny * lambda, nz * lambda, 1);
|
|
1988
|
+
continue;
|
|
1989
|
+
}
|
|
1990
|
+
const m = point.inverse, rx = -dx - point.bias[0], ry = -dy - point.bias[1], rz = -dz - point.bias[2];
|
|
1991
|
+
const px = m[0] * rx + m[1] * ry + m[2] * rz, py = m[3] * rx + m[4] * ry + m[5] * rz, pz = m[6] * rx + m[7] * ry + m[8] * rz;
|
|
1992
|
+
applyPivotImpulse(a, point.ra, px, py, pz, -1);
|
|
1993
|
+
if (b)
|
|
1994
|
+
applyPivotImpulse(b, point.rb, px, py, pz, 1);
|
|
1995
|
+
}
|
|
1996
|
+
}
|
|
1997
|
+
removeBody(body) {
|
|
1998
|
+
if (body.world !== this)
|
|
1999
|
+
return;
|
|
2000
|
+
for (const joint of [...this.#joints])
|
|
2001
|
+
if (joint.bodyA === body || joint.bodyB === body)
|
|
2002
|
+
this.removeJoint(joint);
|
|
2003
|
+
body.world = undefined;
|
|
2004
|
+
this.#bodies.splice(this.#bodies.indexOf(body), 1);
|
|
2005
|
+
if (body.shape.type !== 'plane' && body.type === 'static')
|
|
2006
|
+
this.#statics.remove(body);
|
|
2007
|
+
else {
|
|
2008
|
+
const list = body.shape.type === 'plane' ? this.#planes : this.#sorted;
|
|
2009
|
+
list.splice(list.indexOf(body), 1);
|
|
2010
|
+
}
|
|
2011
|
+
// After a step the pairs that were touching live in the previous-step map.
|
|
2012
|
+
for (const [key, pair] of this.#previousTouching) {
|
|
2013
|
+
if (pair[0] !== body && pair[1] !== body)
|
|
2014
|
+
continue;
|
|
2015
|
+
this.#previousTouching.delete(key);
|
|
2016
|
+
pair[0].wake();
|
|
2017
|
+
pair[1].wake();
|
|
2018
|
+
this.onContactEnd?.(pair[0], pair[1]);
|
|
2019
|
+
}
|
|
2020
|
+
}
|
|
2021
|
+
/**
|
|
2022
|
+
* Advance by `deltaSeconds` in fixed steps, carrying the remainder to the
|
|
2023
|
+
* next call. Returns the number of steps taken.
|
|
2024
|
+
*/
|
|
2025
|
+
step(deltaSeconds) {
|
|
2026
|
+
this.#accumulator += Math.max(0, requireFinite3D(deltaSeconds, 'deltaSeconds'));
|
|
2027
|
+
let steps = 0;
|
|
2028
|
+
while (this.#accumulator >= this.fixedStep && steps < this.maxSubSteps) {
|
|
2029
|
+
this.fixedUpdate(this.fixedStep);
|
|
2030
|
+
this.#accumulator -= this.fixedStep;
|
|
2031
|
+
steps++;
|
|
2032
|
+
}
|
|
2033
|
+
// Drop time the step budget could not absorb instead of spiralling.
|
|
2034
|
+
if (this.#accumulator > this.fixedStep)
|
|
2035
|
+
this.#accumulator = 0;
|
|
2036
|
+
if (steps > 0)
|
|
2037
|
+
this.syncNodes();
|
|
2038
|
+
return steps;
|
|
2039
|
+
}
|
|
2040
|
+
syncNodes() {
|
|
2041
|
+
for (const body of this.#bodies) {
|
|
2042
|
+
const node = body.node;
|
|
2043
|
+
if (!node || body.type === 'static')
|
|
2044
|
+
continue;
|
|
2045
|
+
node.position.set(body.position);
|
|
2046
|
+
node.quaternion.set(body.quaternion);
|
|
2047
|
+
node.markTransformDirty();
|
|
2048
|
+
}
|
|
2049
|
+
}
|
|
2050
|
+
fixedUpdate(dt) {
|
|
2051
|
+
// Static bodies and planes take no part in what follows; only the bodies that can move are walked.
|
|
2052
|
+
const bodies = this.#sorted, g = this.gravity, joints = this.#joints, contacts = this.#contacts;
|
|
2053
|
+
const slices = Math.min(Math.max(Math.floor(this.solverSteps), 1), PHYSICS3D_LIMITS.maxSolverSteps), h = dt / slices;
|
|
2054
|
+
this.#stamp++;
|
|
2055
|
+
let count = 0;
|
|
2056
|
+
for (let slice = 0; slice < slices; slice++) {
|
|
2057
|
+
for (const body of bodies) {
|
|
2058
|
+
if (body.type !== 'dynamic' || body.isSleeping)
|
|
2059
|
+
continue;
|
|
2060
|
+
const v = body.linearVelocity, w = body.angularVelocity;
|
|
2061
|
+
v[0] += (g[0] + body.force[0] * body.inverseMass) * h;
|
|
2062
|
+
v[1] += (g[1] + body.force[1] * body.inverseMass) * h;
|
|
2063
|
+
v[2] += (g[2] + body.force[2] * body.inverseMass) * h;
|
|
2064
|
+
applyInverseInertia(scratchA, body.inverseInertiaWorld, body.torque);
|
|
2065
|
+
w[0] += scratchA[0] * h;
|
|
2066
|
+
w[1] += scratchA[1] * h;
|
|
2067
|
+
w[2] += scratchA[2] * h;
|
|
2068
|
+
const linear = 1 / (1 + body.linearDamping * h), angular = 1 / (1 + body.angularDamping * h);
|
|
2069
|
+
v[0] *= linear;
|
|
2070
|
+
v[1] *= linear;
|
|
2071
|
+
v[2] *= linear;
|
|
2072
|
+
w[0] *= angular;
|
|
2073
|
+
w[1] *= angular;
|
|
2074
|
+
w[2] *= angular;
|
|
2075
|
+
}
|
|
2076
|
+
if (slice === 0) {
|
|
2077
|
+
this.findContacts(h);
|
|
2078
|
+
count = this.#contactCount;
|
|
2079
|
+
}
|
|
2080
|
+
else {
|
|
2081
|
+
// Contacts are found once per step; later slices only correct what is left of each overlap.
|
|
2082
|
+
for (let i = 0; i < count; i++) {
|
|
2083
|
+
const c = contacts[i];
|
|
2084
|
+
c.bias = c.depth < 0 ? c.depth / h : (BAUMGARTE / h) * Math.max(c.depth - SLOP, 0);
|
|
2085
|
+
c.bounce = false;
|
|
2086
|
+
}
|
|
2087
|
+
}
|
|
2088
|
+
// Warm start: re-apply the last impulses so stacks begin close to equilibrium.
|
|
2089
|
+
for (let i = 0; i < count; i++) {
|
|
2090
|
+
const c = contacts[i];
|
|
2091
|
+
if (c.normalImpulse !== 0)
|
|
2092
|
+
this.applyRow(c, 0, c.normalImpulse);
|
|
2093
|
+
if (c.tangentImpulse1 !== 0)
|
|
2094
|
+
this.applyRow(c, 15, c.tangentImpulse1);
|
|
2095
|
+
if (c.tangentImpulse2 !== 0)
|
|
2096
|
+
this.applyRow(c, 30, c.tangentImpulse2);
|
|
2097
|
+
}
|
|
2098
|
+
this.prepareJoints(h);
|
|
2099
|
+
for (let iteration = 0; iteration < this.iterations; iteration++) {
|
|
2100
|
+
for (let i = 0; i < count; i++)
|
|
2101
|
+
this.solve(contacts[i]);
|
|
2102
|
+
for (let i = 0; i < joints.length; i++)
|
|
2103
|
+
this.solveJoint(joints[i], h);
|
|
2104
|
+
}
|
|
2105
|
+
for (let i = 0; i < count; i++) {
|
|
2106
|
+
const c = contacts[i];
|
|
2107
|
+
c.total += c.normalImpulse;
|
|
2108
|
+
// Moving apart along the normal leaves less overlap for the next slice.
|
|
2109
|
+
if (slices > 1)
|
|
2110
|
+
c.depth -= this.rowVelocity(c, 0) * h;
|
|
2111
|
+
}
|
|
2112
|
+
for (const body of bodies) {
|
|
2113
|
+
if (body.type === 'static' || body.isSleeping)
|
|
2114
|
+
continue;
|
|
2115
|
+
const v = body.linearVelocity, w = body.angularVelocity, p = body.position, q = body.quaternion;
|
|
2116
|
+
const reach = body.continuous && body.type === 'dynamic' ? this.sweep(body, h) : 1;
|
|
2117
|
+
p[0] += v[0] * h * reach;
|
|
2118
|
+
p[1] += v[1] * h * reach;
|
|
2119
|
+
p[2] += v[2] * h * reach;
|
|
2120
|
+
if (!body.fixedRotation && (w[0] !== 0 || w[1] !== 0 || w[2] !== 0)) {
|
|
2121
|
+
// q += 0.5 * h * (w, 0) * q
|
|
2122
|
+
const x = q[0], y = q[1], z = q[2], r = q[3], half = 0.5 * h;
|
|
2123
|
+
q[0] = x + half * (w[0] * r + w[1] * z - w[2] * y);
|
|
2124
|
+
q[1] = y + half * (w[1] * r + w[2] * x - w[0] * z);
|
|
2125
|
+
q[2] = z + half * (w[2] * r + w[0] * y - w[1] * x);
|
|
2126
|
+
q[3] = r - half * (w[0] * x + w[1] * y + w[2] * z);
|
|
2127
|
+
const length = Math.sqrt(q[0] * q[0] + q[1] * q[1] + q[2] * q[2] + q[3] * q[3]) || 1;
|
|
2128
|
+
q[0] /= length;
|
|
2129
|
+
q[1] /= length;
|
|
2130
|
+
q[2] /= length;
|
|
2131
|
+
q[3] /= length;
|
|
2132
|
+
}
|
|
2133
|
+
body.refresh();
|
|
2134
|
+
}
|
|
2135
|
+
// The push that separated overlapping bodies has done its work in the move above. Solving once
|
|
2136
|
+
// more without it takes that speed back out, so a resting pile is not kept simmering.
|
|
2137
|
+
this.#relaxing = true;
|
|
2138
|
+
for (let i = 0; i < count; i++)
|
|
2139
|
+
this.solve(contacts[i]);
|
|
2140
|
+
this.#relaxing = false;
|
|
2141
|
+
}
|
|
2142
|
+
for (let i = 0; i < count; i++) {
|
|
2143
|
+
const c = contacts[i], at = c.slot * 3;
|
|
2144
|
+
c.warm.impulses[at] = c.normalImpulse;
|
|
2145
|
+
c.warm.impulses[at + 1] = c.tangentImpulse1;
|
|
2146
|
+
c.warm.impulses[at + 2] = c.tangentImpulse2;
|
|
2147
|
+
}
|
|
2148
|
+
if ((this.#stamp & 63) === 0)
|
|
2149
|
+
for (const [key, entry] of this.#warm)
|
|
2150
|
+
if (entry.stamp !== this.#stamp)
|
|
2151
|
+
this.#warm.delete(key);
|
|
2152
|
+
if (this.onContact) {
|
|
2153
|
+
// The contacts of one pair sit next to each other; report each pair once.
|
|
2154
|
+
const report = this.#report, point = report.point, normal = report.normal;
|
|
2155
|
+
for (let i = 0; i < count;) {
|
|
2156
|
+
const first = contacts[i], a = first.a, b = first.b;
|
|
2157
|
+
let total = 0, px = 0, py = 0, pz = 0, weight = 0, last = i;
|
|
2158
|
+
for (; last < count && contacts[last].a === a && contacts[last].b === b; last++) {
|
|
2159
|
+
const c = contacts[last], share = c.total + 1e-9;
|
|
2160
|
+
total += c.total;
|
|
2161
|
+
weight += share;
|
|
2162
|
+
px += (a.position[0] + c.ra[0]) * share;
|
|
2163
|
+
py += (a.position[1] + c.ra[1]) * share;
|
|
2164
|
+
pz += (a.position[2] + c.ra[2]) * share;
|
|
2165
|
+
}
|
|
2166
|
+
i = last;
|
|
2167
|
+
report.a = a;
|
|
2168
|
+
report.b = b;
|
|
2169
|
+
report.impulse = total;
|
|
2170
|
+
report.began = !this.#previousTouching.has(this.pairKey(a, b));
|
|
2171
|
+
point[0] = px / weight;
|
|
2172
|
+
point[1] = py / weight;
|
|
2173
|
+
point[2] = pz / weight;
|
|
2174
|
+
normal[0] = first.nx;
|
|
2175
|
+
normal[1] = first.ny;
|
|
2176
|
+
normal[2] = first.nz;
|
|
2177
|
+
this.onContact(report);
|
|
2178
|
+
}
|
|
2179
|
+
}
|
|
2180
|
+
// Bodies joined by contacts or joints rest and wake together: a pile sleeps only once all of it is still.
|
|
2181
|
+
const parent = this.#islands.length >= bodies.length ? this.#islands : (this.#islands = new Int32Array(bodies.length * 2));
|
|
2182
|
+
const rest = this.#islandRest.length >= bodies.length
|
|
2183
|
+
? this.#islandRest
|
|
2184
|
+
: (this.#islandRest = new Float64Array(bodies.length * 2));
|
|
2185
|
+
for (let i = 0; i < bodies.length; i++) {
|
|
2186
|
+
bodies[i].slot = i;
|
|
2187
|
+
parent[i] = i;
|
|
2188
|
+
}
|
|
2189
|
+
const root = (index) => {
|
|
2190
|
+
while (parent[index] !== index) {
|
|
2191
|
+
parent[index] = parent[parent[index]];
|
|
2192
|
+
index = parent[index];
|
|
2193
|
+
}
|
|
2194
|
+
return index;
|
|
2195
|
+
};
|
|
2196
|
+
const join = (a, b) => {
|
|
2197
|
+
if (!b || a.type !== 'dynamic' || b.type !== 'dynamic' || a.isSleeping || b.isSleeping)
|
|
2198
|
+
return;
|
|
2199
|
+
const ra = root(a.slot), rb = root(b.slot);
|
|
2200
|
+
if (ra !== rb)
|
|
2201
|
+
parent[ra] = rb;
|
|
2202
|
+
};
|
|
2203
|
+
if (this.allowSleep) {
|
|
2204
|
+
for (let i = 0; i < count; i++)
|
|
2205
|
+
join(contacts[i].a, contacts[i].b);
|
|
2206
|
+
for (const joint of joints)
|
|
2207
|
+
if (joint.enabled)
|
|
2208
|
+
join(joint.bodyA, joint.bodyB);
|
|
2209
|
+
}
|
|
2210
|
+
for (let i = 0; i < bodies.length; i++)
|
|
2211
|
+
rest[i] = Infinity;
|
|
2212
|
+
for (const body of bodies) {
|
|
2213
|
+
body.force.fill(0);
|
|
2214
|
+
body.torque.fill(0);
|
|
2215
|
+
if (body.type !== 'dynamic' || body.isSleeping || !this.allowSleep)
|
|
2216
|
+
continue;
|
|
2217
|
+
const v = body.linearVelocity, w = body.angularVelocity;
|
|
2218
|
+
const slow = v[0] * v[0] + v[1] * v[1] + v[2] * v[2] < SLEEP_LINEAR * SLEEP_LINEAR &&
|
|
2219
|
+
w[0] * w[0] + w[1] * w[1] + w[2] * w[2] < SLEEP_ANGULAR * SLEEP_ANGULAR;
|
|
2220
|
+
body.sleepTime = slow ? body.sleepTime + dt : 0;
|
|
2221
|
+
const island = root(body.slot);
|
|
2222
|
+
if (body.sleepTime < rest[island])
|
|
2223
|
+
rest[island] = body.sleepTime;
|
|
2224
|
+
}
|
|
2225
|
+
if (this.allowSleep)
|
|
2226
|
+
for (const body of bodies) {
|
|
2227
|
+
if (body.type !== 'dynamic' || body.isSleeping || rest[root(body.slot)] < SLEEP_DELAY)
|
|
2228
|
+
continue;
|
|
2229
|
+
body.isSleeping = true;
|
|
2230
|
+
body.linearVelocity.fill(0);
|
|
2231
|
+
body.angularVelocity.fill(0);
|
|
2232
|
+
}
|
|
2233
|
+
// Contact events compare this step's touching pairs with the previous step's.
|
|
2234
|
+
const now = this.#touching, before = this.#previousTouching;
|
|
2235
|
+
if (this.onContactBegin)
|
|
2236
|
+
for (const [key, pair] of now)
|
|
2237
|
+
if (!before.has(key))
|
|
2238
|
+
this.onContactBegin(pair[0], pair[1]);
|
|
2239
|
+
if (this.onContactEnd)
|
|
2240
|
+
for (const [key, pair] of before)
|
|
2241
|
+
if (!now.has(key) && pair[0].world === this && pair[1].world === this)
|
|
2242
|
+
this.onContactEnd(pair[0], pair[1]);
|
|
2243
|
+
this.#previousTouching = now;
|
|
2244
|
+
this.#touching = before;
|
|
2245
|
+
before.clear();
|
|
2246
|
+
}
|
|
2247
|
+
/**
|
|
2248
|
+
* Share of this step's travel a fast body may make before its shape meets
|
|
2249
|
+
* a surface it is not already resting on. It stops just touching, so the
|
|
2250
|
+
* next step finds the contact and answers it with the body's full speed.
|
|
2251
|
+
*/
|
|
2252
|
+
sweep(body, dt) {
|
|
2253
|
+
const v = body.linearVelocity, speed = Math.sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]), travel = speed * dt, inner = thinnestHalf(body.shape);
|
|
2254
|
+
// Slow enough that the ordinary overlap test cannot miss anything.
|
|
2255
|
+
if (travel <= inner * 0.5 || travel === 0)
|
|
2256
|
+
return 1;
|
|
2257
|
+
const p = body.position, ox = p[0], oy = p[1], oz = p[2];
|
|
2258
|
+
const distance = this.cast(body, v[0] / speed, v[1] / speed, v[2] / speed, travel, inner, other => other !== body &&
|
|
2259
|
+
(body.layer & other.mask) !== 0 &&
|
|
2260
|
+
(other.layer & body.mask) !== 0 &&
|
|
2261
|
+
!(this.#jointPairs.size > 0 && this.#jointPairs.has(this.pairKey(body, other))), true);
|
|
2262
|
+
p[0] = ox;
|
|
2263
|
+
p[1] = oy;
|
|
2264
|
+
p[2] = oz;
|
|
2265
|
+
body.refresh();
|
|
2266
|
+
return distance < 0 ? 1 : distance / travel;
|
|
2267
|
+
}
|
|
2268
|
+
#castNear = [];
|
|
2269
|
+
#castBody;
|
|
2270
|
+
/**
|
|
2271
|
+
* Walk `probe` along a unit direction and return how far it gets before touching one of
|
|
2272
|
+
* the accepted bodies, or -1. The probe is left where it touched and the body it met in
|
|
2273
|
+
* #castBody. With `skipTouching`, whatever the probe already rests on is not counted.
|
|
2274
|
+
*/
|
|
2275
|
+
cast(probe, dx, dy, dz, reach, thin, accept, skipTouching) {
|
|
2276
|
+
const m = this.#manifold, ox = probe.position[0], oy = probe.position[1], oz = probe.position[2], near = this.#castNear;
|
|
2277
|
+
near.length = 0;
|
|
2278
|
+
this.#castBody = undefined;
|
|
2279
|
+
// Only bodies whose bounds meet the swept bounds of the shape can be touched.
|
|
2280
|
+
const from = probe.aabb;
|
|
2281
|
+
this.near(from[0] + Math.min(dx * reach, 0), from[1] + Math.min(dy * reach, 0), from[2] + Math.min(dz * reach, 0), from[3] + Math.max(dx * reach, 0), from[4] + Math.max(dy * reach, 0), from[5] + Math.max(dz * reach, 0), body => {
|
|
2282
|
+
if (!body.isTrigger && accept(body))
|
|
2283
|
+
near.push(body);
|
|
2284
|
+
});
|
|
2285
|
+
if (near.length === 0)
|
|
2286
|
+
return -1;
|
|
2287
|
+
const pressing = (body) => {
|
|
2288
|
+
if (!collide(m, probe, body))
|
|
2289
|
+
return false;
|
|
2290
|
+
for (let i = 0; i < m.count; i++)
|
|
2291
|
+
if (m.contacts[i].depth > (skipTouching ? SLOP : 0))
|
|
2292
|
+
return true;
|
|
2293
|
+
return false;
|
|
2294
|
+
};
|
|
2295
|
+
// Whatever the body already touches, however lightly, is the solver's business, not the sweep's.
|
|
2296
|
+
if (skipTouching)
|
|
2297
|
+
for (let i = near.length - 1; i >= 0; i--)
|
|
2298
|
+
if (collide(m, probe, near[i]))
|
|
2299
|
+
near.splice(i, 1);
|
|
2300
|
+
const touches = (t) => {
|
|
2301
|
+
probe.position[0] = ox + dx * t;
|
|
2302
|
+
probe.position[1] = oy + dy * t;
|
|
2303
|
+
probe.position[2] = oz + dz * t;
|
|
2304
|
+
probe.refresh();
|
|
2305
|
+
for (const body of near)
|
|
2306
|
+
if (pressing(body)) {
|
|
2307
|
+
this.#castBody = body;
|
|
2308
|
+
return true;
|
|
2309
|
+
}
|
|
2310
|
+
return false;
|
|
2311
|
+
};
|
|
2312
|
+
let clear = 0, hit = -1;
|
|
2313
|
+
if (touches(0))
|
|
2314
|
+
hit = 0;
|
|
2315
|
+
else
|
|
2316
|
+
for (let t = Math.min(thin, reach);; t = Math.min(t + thin, reach)) {
|
|
2317
|
+
if (touches(t)) {
|
|
2318
|
+
hit = t;
|
|
2319
|
+
break;
|
|
2320
|
+
}
|
|
2321
|
+
clear = t;
|
|
2322
|
+
if (t >= reach)
|
|
2323
|
+
return -1;
|
|
2324
|
+
}
|
|
2325
|
+
// Narrow the first touch down between the last clear place and the first touching one.
|
|
2326
|
+
for (let i = 0; i < 20 && hit - clear > 1e-5; i++) {
|
|
2327
|
+
const middle = (clear + hit) / 2;
|
|
2328
|
+
if (touches(middle))
|
|
2329
|
+
hit = middle;
|
|
2330
|
+
else
|
|
2331
|
+
clear = middle;
|
|
2332
|
+
}
|
|
2333
|
+
touches(hit);
|
|
2334
|
+
return hit;
|
|
2335
|
+
}
|
|
2336
|
+
findContacts(dt) {
|
|
2337
|
+
this.#contactCount = 0;
|
|
2338
|
+
const sorted = this.#sorted, planes = this.#planes;
|
|
2339
|
+
// Insertion sort keeps the nearly sorted list cheap from step to step.
|
|
2340
|
+
for (let i = 1; i < sorted.length; i++) {
|
|
2341
|
+
const body = sorted[i], key = body.aabb[0];
|
|
2342
|
+
let j = i - 1;
|
|
2343
|
+
while (j >= 0 && sorted[j].aabb[0] > key) {
|
|
2344
|
+
sorted[j + 1] = sorted[j];
|
|
2345
|
+
j--;
|
|
2346
|
+
}
|
|
2347
|
+
sorted[j + 1] = body;
|
|
2348
|
+
}
|
|
2349
|
+
this.#pairStep = dt;
|
|
2350
|
+
for (let i = 0; i < sorted.length; i++) {
|
|
2351
|
+
const a = sorted[i], box = a.aabb, maxX = box[3];
|
|
2352
|
+
this.#pairBody = a;
|
|
2353
|
+
for (let j = i + 1; j < sorted.length; j++) {
|
|
2354
|
+
const b = sorted[j], other = b.aabb;
|
|
2355
|
+
if (other[0] > maxX)
|
|
2356
|
+
break;
|
|
2357
|
+
if (box[1] > other[4] || box[4] < other[1] || box[2] > other[5] || box[5] < other[2])
|
|
2358
|
+
continue;
|
|
2359
|
+
this.testPair(a, b, dt);
|
|
2360
|
+
}
|
|
2361
|
+
for (const plane of planes)
|
|
2362
|
+
this.testPair(a, plane, dt);
|
|
2363
|
+
this.#statics.query(box[0], box[1], box[2], box[3], box[4], box[5], this.#againstStatic);
|
|
2364
|
+
}
|
|
2365
|
+
}
|
|
2366
|
+
#relaxing = false;
|
|
2367
|
+
#islands = new Int32Array(0);
|
|
2368
|
+
#islandRest = new Float64Array(0);
|
|
2369
|
+
#pairBody;
|
|
2370
|
+
#pairStep = 0;
|
|
2371
|
+
#againstStatic = (other) => this.testPair(this.#pairBody, other, this.#pairStep);
|
|
2372
|
+
testPair(a, b, dt) {
|
|
2373
|
+
if ((a.layer & b.mask) === 0 || (b.layer & a.mask) === 0)
|
|
2374
|
+
return;
|
|
2375
|
+
if (this.#jointPairs.size > 0 && this.#jointPairs.has(this.pairKey(a, b)))
|
|
2376
|
+
return;
|
|
2377
|
+
const movingA = a.type === 'dynamic' && !a.isSleeping, movingB = b.type === 'dynamic' && !b.isSleeping;
|
|
2378
|
+
// A pair of resting or immovable bodies needs no work, except to keep reporting triggers.
|
|
2379
|
+
if (!movingA && !movingB && a.type !== 'kinematic' && b.type !== 'kinematic') {
|
|
2380
|
+
if (a.isSleeping || b.isSleeping) {
|
|
2381
|
+
const key = this.pairKey(a, b), kept = this.#previousTouching.get(key);
|
|
2382
|
+
if (kept)
|
|
2383
|
+
this.#touching.set(key, kept);
|
|
2384
|
+
}
|
|
2385
|
+
return;
|
|
2386
|
+
}
|
|
2387
|
+
if (a.type !== 'dynamic' && b.type !== 'dynamic' && !a.isTrigger && !b.isTrigger)
|
|
2388
|
+
return;
|
|
2389
|
+
const m = this.#manifold;
|
|
2390
|
+
if (!collide(m, a, b))
|
|
2391
|
+
return;
|
|
2392
|
+
const key = this.pairKey(a, b);
|
|
2393
|
+
this.#touching.set(key, this.#previousTouching.get(key) ?? [a, b]);
|
|
2394
|
+
if (a.isTrigger || b.isTrigger)
|
|
2395
|
+
return;
|
|
2396
|
+
// A moving body wakes a sleeping partner.
|
|
2397
|
+
// Only a partner that is really moving wakes a sleeper; a merely awake neighbour
|
|
2398
|
+
// would keep a whole resting pile from ever settling.
|
|
2399
|
+
if (a.isSleeping && isMoving(b))
|
|
2400
|
+
a.wake();
|
|
2401
|
+
if (b.isSleeping && isMoving(a))
|
|
2402
|
+
b.wake();
|
|
2403
|
+
if (a.isSleeping || b.isSleeping) {
|
|
2404
|
+
// The sleeper acts as immovable this step.
|
|
2405
|
+
if (a.isSleeping && b.isSleeping)
|
|
2406
|
+
return;
|
|
2407
|
+
}
|
|
2408
|
+
const restitution = Math.max(a.restitution, b.restitution), friction = Math.sqrt(a.friction * b.friction);
|
|
2409
|
+
let warm = this.#warm.get(key), previousCount = 0;
|
|
2410
|
+
const previousFeatures = this.#warmFeatures, previousImpulses = this.#warmImpulses;
|
|
2411
|
+
if (warm && warm.stamp === this.#stamp - 1) {
|
|
2412
|
+
previousCount = warm.count;
|
|
2413
|
+
previousFeatures.set(warm.features);
|
|
2414
|
+
previousImpulses.set(warm.impulses);
|
|
2415
|
+
}
|
|
2416
|
+
else if (!warm)
|
|
2417
|
+
this.#warm.set(key, (warm = { stamp: 0, count: 0, features: new Int16Array(8), impulses: new Float64Array(24) }));
|
|
2418
|
+
warm.stamp = this.#stamp;
|
|
2419
|
+
warm.count = m.count;
|
|
2420
|
+
for (let i = 0; i < m.count; i++) {
|
|
2421
|
+
const source = m.contacts[i];
|
|
2422
|
+
let c = this.#contacts[this.#contactCount];
|
|
2423
|
+
if (!c)
|
|
2424
|
+
c = this.#contacts[this.#contactCount] = {
|
|
2425
|
+
a,
|
|
2426
|
+
b,
|
|
2427
|
+
nx: 0,
|
|
2428
|
+
ny: 0,
|
|
2429
|
+
nz: 0,
|
|
2430
|
+
t1: v3(),
|
|
2431
|
+
t2: v3(),
|
|
2432
|
+
ra: v3(),
|
|
2433
|
+
rb: v3(),
|
|
2434
|
+
normalMass: 0,
|
|
2435
|
+
tangentMass1: 0,
|
|
2436
|
+
tangentMass2: 0,
|
|
2437
|
+
bias: 0,
|
|
2438
|
+
friction: 0,
|
|
2439
|
+
depth: 0,
|
|
2440
|
+
total: 0,
|
|
2441
|
+
bounce: false,
|
|
2442
|
+
normalImpulse: 0,
|
|
2443
|
+
tangentImpulse1: 0,
|
|
2444
|
+
tangentImpulse2: 0,
|
|
2445
|
+
warm,
|
|
2446
|
+
slot: 0,
|
|
2447
|
+
ima: 0,
|
|
2448
|
+
imb: 0,
|
|
2449
|
+
rows: new Float64Array(45)
|
|
2450
|
+
};
|
|
2451
|
+
this.#contactCount++;
|
|
2452
|
+
c.a = a;
|
|
2453
|
+
c.b = b;
|
|
2454
|
+
c.nx = source.nx;
|
|
2455
|
+
c.ny = source.ny;
|
|
2456
|
+
c.nz = source.nz;
|
|
2457
|
+
c.friction = friction;
|
|
2458
|
+
c.normalImpulse = c.tangentImpulse1 = c.tangentImpulse2 = 0;
|
|
2459
|
+
c.warm = warm;
|
|
2460
|
+
c.slot = i;
|
|
2461
|
+
warm.features[i] = source.feature;
|
|
2462
|
+
for (let j = 0; j < previousCount; j++) {
|
|
2463
|
+
if (previousFeatures[j] !== source.feature)
|
|
2464
|
+
continue;
|
|
2465
|
+
c.normalImpulse = previousImpulses[j * 3];
|
|
2466
|
+
c.tangentImpulse1 = previousImpulses[j * 3 + 1];
|
|
2467
|
+
c.tangentImpulse2 = previousImpulses[j * 3 + 2];
|
|
2468
|
+
break;
|
|
2469
|
+
}
|
|
2470
|
+
c.ra[0] = source.px - a.position[0];
|
|
2471
|
+
c.ra[1] = source.py - a.position[1];
|
|
2472
|
+
c.ra[2] = source.pz - a.position[2];
|
|
2473
|
+
c.rb[0] = source.px - b.position[0];
|
|
2474
|
+
c.rb[1] = source.py - b.position[1];
|
|
2475
|
+
c.rb[2] = source.pz - b.position[2];
|
|
2476
|
+
const n = [c.nx, c.ny, c.nz];
|
|
2477
|
+
// Any vector not parallel to the normal seeds the tangent basis.
|
|
2478
|
+
cross(c.t1, n, Math.abs(c.nx) < 0.57 ? [1, 0, 0] : [0, 1, 0]);
|
|
2479
|
+
const length = Math.sqrt(c.t1[0] * c.t1[0] + c.t1[1] * c.t1[1] + c.t1[2] * c.t1[2]);
|
|
2480
|
+
c.t1[0] /= length;
|
|
2481
|
+
c.t1[1] /= length;
|
|
2482
|
+
c.t1[2] /= length;
|
|
2483
|
+
cross(c.t2, n, c.t1);
|
|
2484
|
+
c.ima = a.type === 'dynamic' && !a.isSleeping ? a.inverseMass : 0;
|
|
2485
|
+
c.imb = b.type === 'dynamic' && !b.isSleeping ? b.inverseMass : 0;
|
|
2486
|
+
c.normalMass = this.prepareContactRow(c, 0, n);
|
|
2487
|
+
c.tangentMass1 = this.prepareContactRow(c, 15, c.t1);
|
|
2488
|
+
c.tangentMass2 = this.prepareContactRow(c, 30, c.t2);
|
|
2489
|
+
const approach = this.rowVelocity(c, 0);
|
|
2490
|
+
// Push out of penetration, and bounce only on real impacts so resting contacts stay quiet.
|
|
2491
|
+
// A real impact bounces; a resting or slow contact is pushed out of penetration instead.
|
|
2492
|
+
// Doing both in one step would add the correction speed to the bounce.
|
|
2493
|
+
c.bias =
|
|
2494
|
+
source.depth < 0
|
|
2495
|
+
? source.depth / dt
|
|
2496
|
+
: approach < -1 && restitution > 0
|
|
2497
|
+
? -restitution * approach
|
|
2498
|
+
: (BAUMGARTE / dt) * Math.max(source.depth - SLOP, 0);
|
|
2499
|
+
c.depth = source.depth;
|
|
2500
|
+
c.total = 0;
|
|
2501
|
+
c.bounce = source.depth >= 0 && approach < -1 && restitution > 0;
|
|
2502
|
+
}
|
|
2503
|
+
}
|
|
2504
|
+
pairKey(a, b) {
|
|
2505
|
+
return a.id < b.id ? a.id * 4194304 + b.id : b.id * 4194304 + a.id;
|
|
2506
|
+
}
|
|
2507
|
+
/**
|
|
2508
|
+
* Work out everything one solver direction needs, so the iterations that follow are plain
|
|
2509
|
+
* sums and products. Returns the effective mass along that direction.
|
|
2510
|
+
*/
|
|
2511
|
+
prepareContactRow(c, at, axis) {
|
|
2512
|
+
const row = c.rows, ra = c.ra, rb = c.rb, x = axis[0], y = axis[1], z = axis[2];
|
|
2513
|
+
row[at] = x;
|
|
2514
|
+
row[at + 1] = y;
|
|
2515
|
+
row[at + 2] = z;
|
|
2516
|
+
const ax = ra[1] * z - ra[2] * y, ay = ra[2] * x - ra[0] * z, az = ra[0] * y - ra[1] * x;
|
|
2517
|
+
const bx = rb[1] * z - rb[2] * y, by = rb[2] * x - rb[0] * z, bz = rb[0] * y - rb[1] * x;
|
|
2518
|
+
row[at + 3] = ax;
|
|
2519
|
+
row[at + 4] = ay;
|
|
2520
|
+
row[at + 5] = az;
|
|
2521
|
+
row[at + 6] = bx;
|
|
2522
|
+
row[at + 7] = by;
|
|
2523
|
+
row[at + 8] = bz;
|
|
2524
|
+
let k = c.ima + c.imb;
|
|
2525
|
+
if (c.ima > 0) {
|
|
2526
|
+
const m = c.a.inverseInertiaWorld, sx = m[0] * ax + m[1] * ay + m[2] * az, sy = m[3] * ax + m[4] * ay + m[5] * az, sz = m[6] * ax + m[7] * ay + m[8] * az;
|
|
2527
|
+
row[at + 9] = sx;
|
|
2528
|
+
row[at + 10] = sy;
|
|
2529
|
+
row[at + 11] = sz;
|
|
2530
|
+
k += ax * sx + ay * sy + az * sz;
|
|
2531
|
+
}
|
|
2532
|
+
else
|
|
2533
|
+
row[at + 9] = row[at + 10] = row[at + 11] = 0;
|
|
2534
|
+
if (c.imb > 0) {
|
|
2535
|
+
const m = c.b.inverseInertiaWorld, sx = m[0] * bx + m[1] * by + m[2] * bz, sy = m[3] * bx + m[4] * by + m[5] * bz, sz = m[6] * bx + m[7] * by + m[8] * bz;
|
|
2536
|
+
row[at + 12] = sx;
|
|
2537
|
+
row[at + 13] = sy;
|
|
2538
|
+
row[at + 14] = sz;
|
|
2539
|
+
k += bx * sx + by * sy + bz * sz;
|
|
2540
|
+
}
|
|
2541
|
+
else
|
|
2542
|
+
row[at + 12] = row[at + 13] = row[at + 14] = 0;
|
|
2543
|
+
return k > 0 ? 1 / k : 0;
|
|
2544
|
+
}
|
|
2545
|
+
/** Velocity of b relative to a at the contact, along the direction stored at `at`. */
|
|
2546
|
+
rowVelocity(c, at) {
|
|
2547
|
+
const row = c.rows, va = c.a.linearVelocity, vb = c.b.linearVelocity, wa = c.a.angularVelocity, wb = c.b.angularVelocity;
|
|
2548
|
+
return ((vb[0] - va[0]) * row[at] +
|
|
2549
|
+
(vb[1] - va[1]) * row[at + 1] +
|
|
2550
|
+
(vb[2] - va[2]) * row[at + 2] +
|
|
2551
|
+
wb[0] * row[at + 6] +
|
|
2552
|
+
wb[1] * row[at + 7] +
|
|
2553
|
+
wb[2] * row[at + 8] -
|
|
2554
|
+
wa[0] * row[at + 3] -
|
|
2555
|
+
wa[1] * row[at + 4] -
|
|
2556
|
+
wa[2] * row[at + 5]);
|
|
2557
|
+
}
|
|
2558
|
+
applyRow(c, at, magnitude) {
|
|
2559
|
+
const row = c.rows, x = row[at] * magnitude, y = row[at + 1] * magnitude, z = row[at + 2] * magnitude;
|
|
2560
|
+
if (c.ima > 0) {
|
|
2561
|
+
const v = c.a.linearVelocity, w = c.a.angularVelocity, k = c.ima;
|
|
2562
|
+
v[0] -= x * k;
|
|
2563
|
+
v[1] -= y * k;
|
|
2564
|
+
v[2] -= z * k;
|
|
2565
|
+
w[0] -= row[at + 9] * magnitude;
|
|
2566
|
+
w[1] -= row[at + 10] * magnitude;
|
|
2567
|
+
w[2] -= row[at + 11] * magnitude;
|
|
2568
|
+
}
|
|
2569
|
+
if (c.imb > 0) {
|
|
2570
|
+
const v = c.b.linearVelocity, w = c.b.angularVelocity, k = c.imb;
|
|
2571
|
+
v[0] += x * k;
|
|
2572
|
+
v[1] += y * k;
|
|
2573
|
+
v[2] += z * k;
|
|
2574
|
+
w[0] += row[at + 12] * magnitude;
|
|
2575
|
+
w[1] += row[at + 13] * magnitude;
|
|
2576
|
+
w[2] += row[at + 14] * magnitude;
|
|
2577
|
+
}
|
|
2578
|
+
}
|
|
2579
|
+
solve(c) {
|
|
2580
|
+
// Normal: accumulate and clamp so contacts only ever push.
|
|
2581
|
+
const bias = this.#relaxing && !c.bounce && c.bias > 0 ? 0 : c.bias;
|
|
2582
|
+
let delta = (bias - this.rowVelocity(c, 0)) * c.normalMass;
|
|
2583
|
+
const total = Math.max(c.normalImpulse + delta, 0);
|
|
2584
|
+
delta = total - c.normalImpulse;
|
|
2585
|
+
c.normalImpulse = total;
|
|
2586
|
+
if (delta !== 0)
|
|
2587
|
+
this.applyRow(c, 0, delta);
|
|
2588
|
+
// Friction: bounded by the normal impulse (Coulomb cone, per tangent).
|
|
2589
|
+
const limit = c.friction * c.normalImpulse;
|
|
2590
|
+
let tangent = -this.rowVelocity(c, 15) * c.tangentMass1, clamped = clamp(c.tangentImpulse1 + tangent, -limit, limit);
|
|
2591
|
+
tangent = clamped - c.tangentImpulse1;
|
|
2592
|
+
c.tangentImpulse1 = clamped;
|
|
2593
|
+
if (tangent !== 0)
|
|
2594
|
+
this.applyRow(c, 15, tangent);
|
|
2595
|
+
tangent = -this.rowVelocity(c, 30) * c.tangentMass2;
|
|
2596
|
+
clamped = clamp(c.tangentImpulse2 + tangent, -limit, limit);
|
|
2597
|
+
tangent = clamped - c.tangentImpulse2;
|
|
2598
|
+
c.tangentImpulse2 = clamped;
|
|
2599
|
+
if (tangent !== 0)
|
|
2600
|
+
this.applyRow(c, 30, tangent);
|
|
2601
|
+
}
|
|
2602
|
+
// ---- queries ----------------------------------------------------------------
|
|
2603
|
+
/** Nearest solid body hit by a ray; triggers are skipped. `direction` need not be normalized. */
|
|
2604
|
+
raycast(origin, direction, maxDistance = Infinity, mask = -1) {
|
|
2605
|
+
return this[raycastExcludingBody3D](origin, direction, maxDistance, mask);
|
|
2606
|
+
}
|
|
2607
|
+
/** @internal Skip one body before selecting the nearest hit. */
|
|
2608
|
+
[raycastExcludingBody3D](origin, direction, maxDistance, mask, excluded) {
|
|
2609
|
+
const length = Math.sqrt(direction[0] * direction[0] + direction[1] * direction[1] + direction[2] * direction[2]);
|
|
2610
|
+
if (!(length > 0))
|
|
2611
|
+
throw new World3DError('WORLD3D_INPUT_INVALID', 'ray direction');
|
|
2612
|
+
const d = [direction[0] / length, direction[1] / length, direction[2] / length], o = [origin[0], origin[1], origin[2]];
|
|
2613
|
+
let best, limit = maxDistance;
|
|
2614
|
+
const normal = [0, 0, 0];
|
|
2615
|
+
const test = (body, within) => {
|
|
2616
|
+
if (body === excluded || (body.layer & mask) === 0 || body.isTrigger)
|
|
2617
|
+
return within;
|
|
2618
|
+
const distance = rayShape(body, o, d, within, normal, meshNormal);
|
|
2619
|
+
if (distance < 0 || distance > within)
|
|
2620
|
+
return within;
|
|
2621
|
+
best = {
|
|
2622
|
+
body,
|
|
2623
|
+
distance,
|
|
2624
|
+
point: [o[0] + d[0] * distance, o[1] + d[1] * distance, o[2] + d[2] * distance],
|
|
2625
|
+
normal: [normal[0], normal[1], normal[2]]
|
|
2626
|
+
};
|
|
2627
|
+
return distance;
|
|
2628
|
+
};
|
|
2629
|
+
for (const body of this.#planes)
|
|
2630
|
+
limit = test(body, limit);
|
|
2631
|
+
for (const body of this.#sorted)
|
|
2632
|
+
limit = test(body, limit);
|
|
2633
|
+
this.#statics.ray(o, d, limit, test);
|
|
2634
|
+
return best;
|
|
2635
|
+
}
|
|
2636
|
+
/** Solid bodies whose shape touches a sphere; triggers are skipped. */
|
|
2637
|
+
overlapSphere(center, radius, mask = -1) {
|
|
2638
|
+
const probe = probeBody(center, { type: 'sphere', radius: requirePositive3D(radius, 'radius') }), found = [];
|
|
2639
|
+
const box = probe.aabb;
|
|
2640
|
+
this.near(box[0], box[1], box[2], box[3], box[4], box[5], body => {
|
|
2641
|
+
if ((body.layer & mask) !== 0 && !body.isTrigger && collide(this.#manifold, probe, body))
|
|
2642
|
+
found.push(body);
|
|
2643
|
+
});
|
|
2644
|
+
return found;
|
|
2645
|
+
}
|
|
2646
|
+
/**
|
|
2647
|
+
* Move a shape along a unit direction and report the first body it
|
|
2648
|
+
* touches: the distance travelled, the point of contact and the surface
|
|
2649
|
+
* normal facing the shape. The path is walked in steps no longer than the
|
|
2650
|
+
* shape's thinnest half and the touch refined between two steps, so
|
|
2651
|
+
* anything thinner than that step may be passed over. Triggers are ignored.
|
|
2652
|
+
*/
|
|
2653
|
+
shapeCast(shape, origin, direction, maxDistance, mask = -1, quaternion) {
|
|
2654
|
+
const dx = requireFinite3D(direction[0], 'direction'), dy = requireFinite3D(direction[1], 'direction'), dz = requireFinite3D(direction[2], 'direction');
|
|
2655
|
+
if (Math.abs(Math.sqrt(dx * dx + dy * dy + dz * dz) - 1) > 1e-3)
|
|
2656
|
+
throw new World3DError('WORLD3D_INPUT_INVALID', 'direction must have unit length');
|
|
2657
|
+
const reach = requireFinite3D(maxDistance, 'maxDistance');
|
|
2658
|
+
if (!(reach >= 0))
|
|
2659
|
+
throw new World3DError('WORLD3D_INPUT_INVALID', 'maxDistance');
|
|
2660
|
+
if (shape?.type === 'plane' || shape?.type === 'mesh')
|
|
2661
|
+
throw new World3DError('WORLD3D_INPUT_INVALID', 'only solid shapes can be cast');
|
|
2662
|
+
const probe = probeBody(origin, shape);
|
|
2663
|
+
if (quaternion)
|
|
2664
|
+
probe.setQuaternion(quaternion[0], quaternion[1], quaternion[2], quaternion[3]);
|
|
2665
|
+
const m = this.#manifold, hit = this.cast(probe, dx, dy, dz, reach, Math.max(thinnestHalf(shape), 0.02), body => (body.layer & mask) !== 0, false);
|
|
2666
|
+
if (hit < 0)
|
|
2667
|
+
return undefined;
|
|
2668
|
+
const body = this.#castBody;
|
|
2669
|
+
collide(m, probe, body);
|
|
2670
|
+
let best = m.contacts[0];
|
|
2671
|
+
for (let i = 1; i < m.count; i++)
|
|
2672
|
+
if (m.contacts[i].depth > best.depth)
|
|
2673
|
+
best = m.contacts[i];
|
|
2674
|
+
// Whichever way the pair was ordered, report the normal that faces back along the cast.
|
|
2675
|
+
const facing = best.nx * dx + best.ny * dy + best.nz * dz > 0 ? -1 : 1;
|
|
2676
|
+
return {
|
|
2677
|
+
body,
|
|
2678
|
+
distance: hit,
|
|
2679
|
+
point: [best.px, best.py, best.pz],
|
|
2680
|
+
normal: [best.nx * facing, best.ny * facing, best.nz * facing]
|
|
2681
|
+
};
|
|
2682
|
+
}
|
|
2683
|
+
/** @internal Contacts of a free-standing shape against the world, for the character controller. */
|
|
2684
|
+
probe(body, mask, visit) {
|
|
2685
|
+
const m = this.#manifold, box = body.aabb;
|
|
2686
|
+
this.near(box[0], box[1], box[2], box[3], box[4], box[5], other => {
|
|
2687
|
+
if (other === body || other.isTrigger || (other.layer & mask) === 0 || !collide(m, body, other))
|
|
2688
|
+
return;
|
|
2689
|
+
for (let i = 0; i < m.count; i++) {
|
|
2690
|
+
const c = m.contacts[i];
|
|
2691
|
+
if (c.depth > 0)
|
|
2692
|
+
visit(c.nx, c.ny, c.nz, c.depth, other);
|
|
2693
|
+
}
|
|
2694
|
+
});
|
|
2695
|
+
}
|
|
2696
|
+
}
|
|
2697
|
+
/** Half the thinnest extent of a solid shape; zero for planes and meshes. */
|
|
2698
|
+
function thinnestHalf(shape) {
|
|
2699
|
+
if (shape.type === 'sphere' || shape.type === 'capsule')
|
|
2700
|
+
return shape.radius;
|
|
2701
|
+
if (shape.type === 'box')
|
|
2702
|
+
return Math.min(shape.halfExtents[0], shape.halfExtents[1], shape.halfExtents[2]);
|
|
2703
|
+
if (shape.type === 'convex')
|
|
2704
|
+
return shape.hull.innerRadius;
|
|
2705
|
+
if (shape.type === 'compound') {
|
|
2706
|
+
let least = Infinity;
|
|
2707
|
+
for (const child of shape.children)
|
|
2708
|
+
least = Math.min(least, thinnestHalf(child.shape));
|
|
2709
|
+
return least;
|
|
2710
|
+
}
|
|
2711
|
+
return 0;
|
|
2712
|
+
}
|
|
2713
|
+
function probeBody(position, shape) {
|
|
2714
|
+
return new RigidBody3D({ type: 'kinematic', shape, position: [position[0], position[1], position[2]] });
|
|
2715
|
+
}
|
|
2716
|
+
/**
|
|
2717
|
+
* Kinematic capsule that slides along the world instead of being simulated.
|
|
2718
|
+
* Call move() with the displacement you want; it resolves penetration and
|
|
2719
|
+
* reports whether the capsule stands on walkable ground.
|
|
2720
|
+
*/
|
|
2721
|
+
export class CharacterController3D {
|
|
2722
|
+
world;
|
|
2723
|
+
position = v3();
|
|
2724
|
+
radius;
|
|
2725
|
+
height;
|
|
2726
|
+
slopeLimit;
|
|
2727
|
+
skin;
|
|
2728
|
+
mask;
|
|
2729
|
+
node;
|
|
2730
|
+
/** True when the last move ended on a walkable surface. */
|
|
2731
|
+
isGrounded = false;
|
|
2732
|
+
/** Normal of the ground under the capsule after the last move. */
|
|
2733
|
+
groundNormal = v3(0, 1, 0);
|
|
2734
|
+
/** Body standing under the capsule, when grounded. */
|
|
2735
|
+
groundBody;
|
|
2736
|
+
/** True when the last move was stopped by something overhead. */
|
|
2737
|
+
hitCeiling = false;
|
|
2738
|
+
/** Called for every obstacle the capsule is pushed out of during a move; the normal points from the capsule into the obstacle. */
|
|
2739
|
+
onContact;
|
|
2740
|
+
#probe;
|
|
2741
|
+
constructor(world, options = {}) {
|
|
2742
|
+
this.world = world;
|
|
2743
|
+
this.radius = requirePositive3D(options.radius ?? 0.35, 'radius');
|
|
2744
|
+
this.height = Math.max(requirePositive3D(options.height ?? 1.8, 'height'), this.radius * 2);
|
|
2745
|
+
this.slopeLimit = options.slopeLimit ?? Math.PI / 4;
|
|
2746
|
+
this.skin = options.skin ?? 0.01;
|
|
2747
|
+
this.mask = options.mask ?? -1;
|
|
2748
|
+
this.node = options.node;
|
|
2749
|
+
if (options.position)
|
|
2750
|
+
this.position.set(options.position);
|
|
2751
|
+
this.#probe = new RigidBody3D({
|
|
2752
|
+
type: 'kinematic',
|
|
2753
|
+
shape: { type: 'capsule', radius: this.radius, halfHeight: this.height / 2 - this.radius }
|
|
2754
|
+
});
|
|
2755
|
+
}
|
|
2756
|
+
/** Move by a world-space displacement, sliding along anything in the way. */
|
|
2757
|
+
move(dx, dy, dz) {
|
|
2758
|
+
requireFinite3D(dx + dy + dz, 'displacement');
|
|
2759
|
+
// Split long moves so one step never exceeds the radius and tunnels through thin walls.
|
|
2760
|
+
const distance = Math.sqrt(dx * dx + dy * dy + dz * dz), steps = Math.min(Math.max(Math.ceil(distance / (this.radius * 0.9)), 1), 32);
|
|
2761
|
+
const p = this.position, probe = this.#probe, walkable = Math.cos(this.slopeLimit);
|
|
2762
|
+
this.isGrounded = false;
|
|
2763
|
+
this.groundBody = undefined;
|
|
2764
|
+
this.hitCeiling = false;
|
|
2765
|
+
for (let step = 0; step < steps; step++) {
|
|
2766
|
+
p[0] += dx / steps;
|
|
2767
|
+
p[1] += dy / steps;
|
|
2768
|
+
p[2] += dz / steps;
|
|
2769
|
+
for (let iteration = 0; iteration < 4; iteration++) {
|
|
2770
|
+
probe.position.set(p);
|
|
2771
|
+
probe.refresh();
|
|
2772
|
+
const hit = {
|
|
2773
|
+
depth: 0,
|
|
2774
|
+
nx: 0,
|
|
2775
|
+
ny: 0,
|
|
2776
|
+
nz: 0,
|
|
2777
|
+
body: undefined
|
|
2778
|
+
};
|
|
2779
|
+
this.world.probe(probe, this.mask, (x, y, z, depth, body) => {
|
|
2780
|
+
// Resolve the deepest contact first; the normal points from the capsule into the obstacle.
|
|
2781
|
+
if (depth > hit.depth) {
|
|
2782
|
+
hit.depth = depth;
|
|
2783
|
+
hit.nx = x;
|
|
2784
|
+
hit.ny = y;
|
|
2785
|
+
hit.nz = z;
|
|
2786
|
+
hit.body = body;
|
|
2787
|
+
}
|
|
2788
|
+
});
|
|
2789
|
+
if (!hit.body)
|
|
2790
|
+
break;
|
|
2791
|
+
const push = hit.depth + this.skin;
|
|
2792
|
+
p[0] -= hit.nx * push;
|
|
2793
|
+
p[1] -= hit.ny * push;
|
|
2794
|
+
p[2] -= hit.nz * push;
|
|
2795
|
+
this.onContact?.(hit.body, hit.nx, hit.ny, hit.nz, hit.depth);
|
|
2796
|
+
if (hit.ny > 0.7)
|
|
2797
|
+
this.hitCeiling = true;
|
|
2798
|
+
if (-hit.ny >= walkable) {
|
|
2799
|
+
this.isGrounded = true;
|
|
2800
|
+
this.groundBody = hit.body;
|
|
2801
|
+
this.groundNormal[0] = -hit.nx;
|
|
2802
|
+
this.groundNormal[1] = -hit.ny;
|
|
2803
|
+
this.groundNormal[2] = -hit.nz;
|
|
2804
|
+
}
|
|
2805
|
+
}
|
|
2806
|
+
}
|
|
2807
|
+
if (!this.isGrounded) {
|
|
2808
|
+
// A resting capsule hovers one skin above the floor; look just below the feet.
|
|
2809
|
+
probe.position.set(p);
|
|
2810
|
+
probe.position[1] -= this.skin * 3;
|
|
2811
|
+
probe.refresh();
|
|
2812
|
+
this.world.probe(probe, this.mask, (x, y, z, _depth, body) => {
|
|
2813
|
+
if (-y >= walkable) {
|
|
2814
|
+
this.isGrounded = true;
|
|
2815
|
+
this.groundBody = body;
|
|
2816
|
+
this.groundNormal[0] = -x;
|
|
2817
|
+
this.groundNormal[1] = -y;
|
|
2818
|
+
this.groundNormal[2] = -z;
|
|
2819
|
+
}
|
|
2820
|
+
});
|
|
2821
|
+
}
|
|
2822
|
+
const node = this.node;
|
|
2823
|
+
if (node) {
|
|
2824
|
+
node.position.set(p);
|
|
2825
|
+
node.markTransformDirty();
|
|
2826
|
+
}
|
|
2827
|
+
}
|
|
2828
|
+
}
|
|
2829
|
+
export function createPhysicsWorld3D(options = {}) {
|
|
2830
|
+
return new PhysicsWorld3D(options);
|
|
2831
|
+
}
|
|
2832
|
+
export function createCharacterController3D(world, options = {}) {
|
|
2833
|
+
return new CharacterController3D(world, options);
|
|
2834
|
+
}
|
|
2835
|
+
//# sourceMappingURL=Physics3D.js.map
|