UNPKG

constraint-solver-js

Version:

2D rigid body constraint solver written in typescript.

31 lines (30 loc) 1.19 kB
import { relativeToGlobalPos } from "./cord-sys-utils.js"; import { substractVectors } from "./vector-utils.js"; export class RotConstraint { /** * Rotational constraint constructor * @param bodyA constraint's body A * @param anchorA position (relative to body A coordinate system) where * it is constrained. * @param bodyB constraint's body B * @param anchorB position (relative to body B coordinate system) where * it is constrained. */ constructor(bodyA, anchorA, bodyB, anchorB) { if (bodyA.id === bodyB.id) { throw new Error(`A body cannot be constraint to itself. Body id: ${bodyA.id}`); } this.bodyA = bodyA; this.anchorA = anchorA; this.bodyB = bodyB; this.anchorB = anchorB; } f(xa, ya, thetaA, xb, yb, thetaB) { const cmA = { x: xa, y: ya }; const cmB = { x: xb, y: yb }; const anchAglobal = relativeToGlobalPos(this.anchorA, cmA, thetaA); const anchBglobal = relativeToGlobalPos(this.anchorB, cmB, thetaB); const resultVec = substractVectors(anchAglobal, anchBglobal); return [resultVec.x, resultVec.y]; } }