Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/flo-bit/flo-bit.github.io. my personal website, w/ astro, svelte, tailwind, typescript, threlte flo-bit.dev
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110import * as PIXI from "pixi.js";import { AnglesMath } from "./Vector";
export class Chain { joints: PIXI.Point[]; linkSize: number; angles: number[]; angleConstraint: number;
tempPoint: PIXI.Point = new PIXI.Point();
constructor( origin: PIXI.Point, jointCount: number, linkSize: number, angleConstraint: number = Math.PI * 2, ) { this.linkSize = linkSize; this.angleConstraint = angleConstraint; this.joints = []; this.angles = [];
this.joints.push(new PIXI.Point(origin.x, origin.y)); this.angles.push(0);
for (let i = 1; i < jointCount; i++) { this.joints.push( new PIXI.Point( this.joints[i - 1].x, this.joints[i - 1].y + this.linkSize, ), ); this.angles.push(0); } }
applyScale(scale: number) { this.linkSize *= scale;
for (let i = 1; i < this.joints.length; i++) { this.joints[i].x *= scale; this.joints[i].y *= scale; } }
resolve(pos: PIXI.Point): void { pos.subtract(this.joints[0], this.tempPoint);
this.angles[0] = this.tempPoint.angle();
this.joints[0].x = pos.x; this.joints[0].y = pos.y;
for (let i = 1; i < this.joints.length; i++) { this.joints[i - 1].subtract(this.joints[i], this.tempPoint);
const curAngle = this.tempPoint.angle();
this.angles[i] = AnglesMath.constrainAngle( curAngle, this.angles[i - 1], this.angleConstraint, );
this.tempPoint.fromAngle(this.angles[i], this.tempPoint); this.tempPoint.multiplyScalar(this.linkSize, this.tempPoint);
this.joints[i - 1].subtract(this.tempPoint, this.joints[i]); } }
fabrikResolve(pos: PIXI.Point, anchor: PIXI.Point): void { this.joints[0] = new PIXI.Point(pos.x, pos.y); for (let i = 1; i < this.joints.length; i++) { AnglesMath.constrainDistance( this.joints[i], this.joints[i - 1], this.linkSize, this.joints[i], ); }
// Backward pass this.joints[this.joints.length - 1] = new PIXI.Point(anchor.x, anchor.y); for (let i = this.joints.length - 2; i >= 0; i--) { AnglesMath.constrainDistance( this.joints[i], this.joints[i + 1], this.linkSize, this.joints[i], ); } }
debugDisplay(graphics: PIXI.Graphics, sizes?: number[]): void { for (let i = 0; i < this.joints.length - 1; i++) { let startJoint = this.joints[i]; let endJoint = this.joints[i + 1]; graphics.moveTo(startJoint.x, startJoint.y); graphics.lineTo(endJoint.x, endJoint.y); }
for (let i = 0; i < this.joints.length; i++) { let joint = this.joints[i]; let size = sizes && sizes.length > i ? sizes[i] : 16; graphics.circle(joint.x, joint.y, size); } }}