├── .eslintignore ├── .eslintrc ├── .gitignore ├── .husky ├── pre-commit └── pre-push ├── .prettierignore ├── LICENCE ├── Readme.md ├── package.json ├── src ├── app.html ├── global.d.ts ├── lib │ ├── components │ │ ├── Body.svelte │ │ ├── Constraint.svelte │ │ ├── Debugger.svelte │ │ ├── Material.svelte │ │ ├── Shape.svelte │ │ ├── Spring.svelte │ │ ├── World.svelte │ │ ├── constraints │ │ │ ├── DistanceConstraint.svelte │ │ │ └── LockConstraint.svelte │ │ └── shapes │ │ │ ├── Box.svelte │ │ │ ├── Cylinder.svelte │ │ │ ├── Particle.svelte │ │ │ ├── Plane.svelte │ │ │ └── Sphere.svelte │ ├── context-fns.ts │ ├── conversion-fns.test.ts │ ├── conversion-fns.ts │ ├── debugger.ts │ ├── index.ts │ ├── lifecycle-fns.ts │ ├── store-fns.test.ts │ ├── store-fns.ts │ ├── sync-fns.ts │ └── types.ts ├── routes │ ├── +layout.svelte │ ├── +layout.ts │ ├── +page.svelte │ ├── constraints │ │ ├── bridge │ │ │ └── +page.svelte │ │ └── double-pendulum │ │ │ └── +page.svelte │ ├── debugger │ │ └── +page.svelte │ ├── jumpy-box │ │ └── +page.svelte │ ├── minimal │ │ └── +page.svelte │ ├── pill-eater │ │ └── +page.svelte │ └── spring │ │ └── +page.svelte └── site │ ├── Fab.svelte │ ├── ThreeRenderer.svelte │ └── pill-eater │ ├── Hero.svelte │ ├── Level.svelte │ ├── Pill.svelte │ ├── PowerPill.svelte │ └── Wall.svelte ├── static ├── favicon.ico ├── github.svg ├── robots.txt └── source.svg ├── svelte.config.js ├── tsconfig.eslint.json ├── tsconfig.json ├── vite.config.ts └── yarn.lock /.eslintignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | /build 3 | /package 4 | /.svelte-kit 5 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["triple/svelte"], 3 | "parser": "@typescript-eslint/parser", 4 | "parserOptions": { 5 | "project": "./tsconfig.eslint.json", 6 | "extraFileExtensions": [".cjs", ".svelte"] 7 | }, 8 | "env": { 9 | "browser": true, 10 | "jest": true, 11 | "node": true 12 | }, 13 | "rules": { 14 | "new-cap": "off" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | /.svelte-kit 4 | /build 5 | /package 6 | /functions -------------------------------------------------------------------------------- /.husky/pre-commit: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | . "$(dirname "$0")/_/husky.sh" 3 | 4 | npx lint-staged 5 | -------------------------------------------------------------------------------- /.husky/pre-push: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | . "$(dirname "$0")/_/husky.sh" 3 | 4 | npm run test 5 | -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | /.svelte-kit 2 | /build 3 | /node_modules -------------------------------------------------------------------------------- /LICENCE: -------------------------------------------------------------------------------- 1 | Copyright 2022 Bob Fanger 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # Svelte Cannon 2 | 3 | Use [Svelte](https://svelte.dev/) components to build a [cannon-es](https://pmndrs.github.io/cannon-es/) physics simulation. 4 | 5 | ## Installation 6 | 7 | ```sh 8 | npm install svelte-cannon # or yarn add svelte-cannon 9 | ``` 10 | 11 | ## Usage 12 | 13 | This example uses [svelte-cubed](https://svelte-cubed.vercel.app/) to display the objects, but svelte-cannon is renderer engine agnostic. 14 | 15 | ```svelte 16 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 37 | 38 | ``` 39 | 40 | Using the import `* as PE` is helpfull to avoid naming conflicts, [three](https://threejs.org/docs/#api/en/math/Sphere), [cannon-es](https://pmndrs.github.io/cannon-es/docs/classes/Sphere.html) and [svelte-cannon](./src/lib/components/Sphere.svelte) all export a `Sphere` for example. 41 | _(PE is short for **P**hysics **E**ngine)_ 42 | 43 | Generally you'd want the nest the `SC.Canvas` inside the `PE.World`, because that allows subcomponents to create both the physics engine and the render engine components. 44 | 45 | ## Shorthand notations 46 | 47 | Allowed values for setting a 3D vector are: 48 | 49 | - `[1, 2, 3]` 50 | - `new CANNON.Vec3(1, 2, 3)` 51 | - `new THREE.Vector3(1, 2, 3)` 52 | - `{x: 1, y: 2, z: 3}` 53 | 54 | This allows for reusing existing variables and reduces boilerplate. 55 | 56 | ## Two way binding caveats 57 | 58 | 1. `bind:property=` for 3D vectors only works when an `CANNON.Vec3` object is passed. 59 | 60 | This restriction allows svelte-cannon to detect which properties you're interested in. 61 | Due to the nature of physics engines a lot of properties could change every on frame, recalculating the `velocity` of the ground plane would be wasteful. 62 | 63 | 2. When the body is awake `bind:property=` will trigger updates, but the value might not have changed. 64 | 65 | Syncing position the THREE.Mesh is a fast operation. 66 | `$: mesh.position.copy(position);` ( CANNON.Vec3 is compatible with THREE.Vector3 ) 67 | Creating shadow values, recalculating and checking and for changes would be overhead. 68 | 69 | ## Imprecise stores 70 | 71 | `writableVec3` creates store, but when you're setting the value with a CANNON.Vec3 that has roughly the same value it won't trigger an update. 72 | 73 | ```svelte 74 | 81 | 82 | 83 | 84 | ``` 85 | 86 | This allows you to prevent resending values to the renderer that haven't changed. 87 | 88 | From a usage perspective it acts as a `writable(new Vec3(0, 4, 0))` but also allows shorthand notations: 89 | 90 | - `position.set(1, 2, 3)` 91 | - `position.set(new CANNON.Vec3(1, 2, 3))` 92 | - `$position = new CANNON.Vec3(1, 2, 3)` 93 | - `$position = new THREE.Vector3(1, 2, 3)` 94 | - `$position = {x: 1, y: 2, z: 3}` 95 | - `$position = [1, 2, 3]` 96 | - `$position.x = 1; $position.x = 2; $position.x = 3;` 97 | 98 | ## Forces and Constraints 99 | 100 | As bodies can have multiple constrains and forces can affect multiple bodies it doesn't translate well to a component hierarchy. HTML also has this problem with ``s and `