From 734f89ce159b32c9c09c2542d55ba124dea3113a Mon Sep 17 00:00:00 2001 From: Antonio De Lucreziis Date: Fri, 10 Mar 2023 17:27:48 +0100 Subject: [PATCH] minor fixes --- frontend/src/components/KnotLayer.jsx | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/frontend/src/components/KnotLayer.jsx b/frontend/src/components/KnotLayer.jsx index e80fc67..f435f2a 100644 --- a/frontend/src/components/KnotLayer.jsx +++ b/frontend/src/components/KnotLayer.jsx @@ -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