This commit is contained in:
Orion Reed 2024-12-15 15:20:37 -05:00
parent 9c5e2150c8
commit 7a88628512
5 changed files with 230 additions and 2 deletions

43
demo/physics.html Normal file
View File

@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Distance Field Demo</title>
<style>
html {
height: 100%;
}
body {
min-height: 100%;
position: relative;
margin: 0;
overscroll-behavior: none;
}
folk-shape {
position: absolute;
background-color: rgba(100, 100, 100, 0.5);
border: 2px solid rgba(0, 0, 0, 0.5);
}
</style>
</head>
<body>
<folk-physics>
<folk-shape x="250" y="50" width="120" height="30" rotation="30"></folk-shape>
<folk-shape x="450" y="350" width="40" height="120" rotation="45"></folk-shape>
<folk-shape x="150" y="150" width="70" height="70" rotation="15"></folk-shape>
<folk-shape x="350" y="300" width="90" height="45" rotation="-20"></folk-shape>
<folk-shape x="550" y="200" width="35" height="85" rotation="60"></folk-shape>
<folk-shape x="180" y="250" width="65" height="55" rotation="-40"></folk-shape>
<folk-shape x="420" y="150" width="110" height="25" rotation="10"></folk-shape>
<folk-shape x="280" y="380" width="75" height="95" rotation="-50"></folk-shape>
</folk-physics>
<script type="module">
import '../src/standalone/folk-shape.ts';
import '../src/standalone/folk-physics.ts';
</script>
</body>
</html>

View File

@ -11,10 +11,12 @@
},
"dependencies": {
"@babel/parser": "^7.26.2",
"@dimforge/rapier2d": "^0.14.0",
"@lit/reactive-element": "^2.0.4",
"leaflet": "^1.9.4",
"perfect-arrows": "^0.3.7",
"perfect-freehand": "^1.2.2"
"perfect-freehand": "^1.2.2",
"vite-plugin-wasm": "^3.3.0"
},
"devDependencies": {
"@types/leaflet": "^1.9.14",

177
src/folk-physics.ts Normal file
View File

@ -0,0 +1,177 @@
import { DOMRectTransform } from './common/DOMRectTransform.ts';
import { FolkBaseSet } from './folk-base-set.ts';
import { PropertyValues } from '@lit/reactive-element';
import * as RAPIER from '@dimforge/rapier2d';
import { FolkShape } from './folk-shape';
export class FolkPhysics extends FolkBaseSet {
static override tagName = 'folk-physics';
private world?: RAPIER.World;
private bodies: Map<FolkShape, RAPIER.RigidBody> = new Map();
private elementToRect: Map<FolkShape, DOMRectTransform> = new Map();
private animationFrameId?: number;
private lastTimestamp?: number;
connectedCallback() {
super.connectedCallback();
// Create physics world with gravity
this.world = new RAPIER.World({ x: 0.0, y: 9.81 });
// Add container walls and floor
this.createContainer();
// Start the simulation loop
this.startSimulation();
}
disconnectedCallback() {
super.disconnectedCallback();
// Stop simulation loop
if (this.animationFrameId) {
cancelAnimationFrame(this.animationFrameId);
}
// Cleanup physics resources
this.bodies.clear();
this.elementToRect.clear();
this.world?.free();
}
override update(changedProperties: PropertyValues<this>) {
super.update(changedProperties);
if (!this.world || this.sourcesMap.size !== this.sourceElements.size) return;
// Update physics bodies to match source elements
this.updatePhysicsBodies();
}
private updatePhysicsBodies() {
// Remove bodies for elements that no longer exist
for (const [element, body] of this.bodies) {
if (!this.sourceElements.has(element)) {
this.world!.removeRigidBody(body);
this.bodies.delete(element);
}
}
// Create or update bodies for current elements
for (const element of this.sourceElements) {
if (!(element instanceof FolkShape)) continue;
const rect = this.sourcesMap.get(element);
if (!(rect instanceof DOMRectTransform)) continue;
if (!this.bodies.has(element)) {
// Create new rigid body matching the element's position
const bodyDesc = RAPIER.RigidBodyDesc.dynamic()
.setTranslation(rect.x + rect.width / 2, rect.y + rect.height / 2)
.setRotation(rect.rotation);
const body = this.world!.createRigidBody(bodyDesc);
// Create collider with top-left origin
const colliderDesc = RAPIER.ColliderDesc.cuboid(rect.width / 2, rect.height / 2).setTranslation(0, 0);
this.world!.createCollider(colliderDesc, body);
this.bodies.set(element, body);
// Set element's rotation origin to top-left
rect.transformOrigin = { x: 0, y: 0 };
}
// Update existing body
const body = this.bodies.get(element)!;
if (element === document.activeElement) {
body.setBodyType(RAPIER.RigidBodyType.KinematicPositionBased, true);
body.setTranslation(
{
x: rect.x + rect.width / 2,
y: rect.y + rect.height / 2,
},
true
);
body.setRotation(rect.rotation, true);
} else if (body.bodyType() === RAPIER.RigidBodyType.KinematicPositionBased) {
// Update collider size when switching back to dynamic
const collider = body.collider(0);
if (collider) {
this.world!.removeCollider(collider, true);
const newColliderDesc = RAPIER.ColliderDesc.cuboid(rect.width / 2, rect.height / 2).setTranslation(0, 0);
this.world!.createCollider(newColliderDesc, body);
}
body.setBodyType(RAPIER.RigidBodyType.Dynamic, true);
}
}
}
private startSimulation() {
const step = (timestamp: number) => {
if (!this.lastTimestamp) {
this.lastTimestamp = timestamp;
}
if (this.world) {
this.world.step();
// Update visual elements based on physics
this.bodies.forEach((body, shape) => {
if (shape !== document.activeElement) {
const position = body.translation();
// Adjust position back to top-left
shape.x = position.x - shape.width / 2;
shape.y = position.y - shape.height / 2;
shape.rotation = body.rotation();
}
});
}
this.animationFrameId = requestAnimationFrame(step);
};
this.animationFrameId = requestAnimationFrame(step);
}
private createContainer() {
if (!this.world) return;
// Create a single rigid body for the container
const containerDesc = RAPIER.RigidBodyDesc.fixed();
const container = this.world.createRigidBody(containerDesc);
// Create a single collider with all four walls as vertices
const vertices = [
// Floor: left to right
{ x: -1000, y: this.clientHeight },
{ x: 1000, y: this.clientHeight },
// Right wall: bottom to top
{ x: this.clientWidth, y: this.clientHeight },
{ x: this.clientWidth, y: -1000 },
// Ceiling: right to left
{ x: 1000, y: 0 },
{ x: -1000, y: 0 },
// Left wall: top to bottom
{ x: 0, y: -1000 },
{ x: 0, y: this.clientHeight },
];
const indices = [
0,
1, // Floor
2,
3, // Right wall
4,
5, // Ceiling
6,
7, // Left wall
];
const vertexArray = new Float32Array(vertices.flatMap((v) => [v.x, v.y]));
const indexArray = new Uint32Array(indices);
this.world.createCollider(RAPIER.ColliderDesc.polyline(vertexArray, indexArray), container);
}
}

View File

@ -0,0 +1,5 @@
import { FolkPhysics } from '../folk-physics';
FolkPhysics.define();
export { FolkPhysics };

View File

@ -1,6 +1,7 @@
import { resolve } from 'node:path';
import { readdirSync } from 'node:fs';
import { defineConfig, IndexHtmlTransformContext, Plugin } from 'vite';
import vitePluginWasm from 'vite-plugin-wasm';
const demoDir = resolve(__dirname, 'demo');
@ -65,7 +66,7 @@ const linkGenerator = (): Plugin => {
export default defineConfig({
root: 'demo',
plugins: [linkGenerator()],
plugins: [linkGenerator(), vitePluginWasm()],
build: {
target: 'esnext',
rollupOptions: {