import type { Creature } from "./creature"; import Rand, { PRNG } from "rand-seed"; const RAPIER = await import("@dimforge/rapier2d"); export interface SimulationResult { distance: number; } export function simulate( seed: string, creature: Creature, frames: number, ): SimulationResult { let rng = new Rand(seed); let gravity = { x: 0.0, y: -9.81 }; let world = new RAPIER.World(gravity); const nodes = creature.nodes.map(({ pos }) => { let nodeDesc = RAPIER.RigidBodyDesc.dynamic().setTranslation(pos.x, pos.y); let node = world.createRigidBody(nodeDesc); return node; }); let groundDesc = RAPIER.RigidBodyDesc.fixed().setTranslation(0.0, -10.0); let ground = world.createRigidBody(groundDesc); // let lastPos = rigidBody.translation(); // while (true) { // world.step(); // const pos = rigidBody.translation(); // if (pos.x === lastPos.x && pos.y === lastPos.y) break; // lastPos = pos; // } for (let frame = 0; frame < frames; ++frame) { world.step(); } const xs = nodes.map((node) => node.translation().x); return { distance: (Math.max(...xs) - Math.min(...xs)) / xs.length, }; }