minor fixes

main
Antonio De Lucreziis 2 years ago
parent 29efe05010
commit 734f89ce15

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

Loading…
Cancel
Save