|
|
|
@ -2,6 +2,11 @@ import { useEffect, useRef, useState } from 'preact/hooks'
|
|
|
|
|
import { resampleCurve, simplifyCurve } from '../../lib/math/curves.js'
|
|
|
|
|
import { Vec2 } from '../../lib/math/math.js'
|
|
|
|
|
|
|
|
|
|
function mod(i, modulus) {
|
|
|
|
|
const r = i % modulus
|
|
|
|
|
return r < 0 ? r + modulus : r
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function createSimulation2d(positions, velocities, accelerations) {
|
|
|
|
|
return {
|
|
|
|
|
positions,
|
|
|
|
@ -86,18 +91,14 @@ class KnotSimulation {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { positions, velocities } = this.particleSimulation
|
|
|
|
|
const { positions } = this.particleSimulation
|
|
|
|
|
const N = positions.length
|
|
|
|
|
|
|
|
|
|
const newAccelerations = Array.from({ length: N }, () => [0, 0])
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < N; i++) {
|
|
|
|
|
const prev = positions.at((i - 1) % N)
|
|
|
|
|
const curr = positions.at(i)
|
|
|
|
|
const next = positions.at((i + 1) % N)
|
|
|
|
|
|
|
|
|
|
if (this.blackHolePosition) {
|
|
|
|
|
const v = Vec2.sub(this.blackHolePosition, curr)
|
|
|
|
|
const v = Vec2.sub(this.blackHolePosition, positions[i])
|
|
|
|
|
const dir = Vec2.normalize(v)
|
|
|
|
|
const dist = Vec2.norm2(v)
|
|
|
|
|
|
|
|
|
@ -116,11 +117,8 @@ class KnotSimulation {
|
|
|
|
|
|
|
|
|
|
const factor = (d - 10) / d
|
|
|
|
|
|
|
|
|
|
positions[i] = Vec2.add(curr, Vec2.scale(v, 0.5 * factor))
|
|
|
|
|
|
|
|
|
|
const nextPos = Vec2.add(next, Vec2.scale(v, -0.5 * factor))
|
|
|
|
|
next[0] = nextPos[0]
|
|
|
|
|
next[1] = nextPos[1]
|
|
|
|
|
positions[mod(i, N)] = Vec2.add(curr, Vec2.scale(v, 0.5 * factor))
|
|
|
|
|
positions[mod(i + 1, N)] = Vec2.add(next, Vec2.scale(v, -0.5 * factor))
|
|
|
|
|
|
|
|
|
|
const baseForce = Vec2.scale(Vec2.add(Vec2.sub(prev, curr), Vec2.sub(next, curr)), 0.5)
|
|
|
|
|
const jointFactor = 1
|
|
|
|
|