diff --git a/spine-ts/spine-core/src/PhysicsConstraint.ts b/spine-ts/spine-core/src/PhysicsConstraint.ts index a81225f36..db89eea74 100644 --- a/spine-ts/spine-core/src/PhysicsConstraint.ts +++ b/spine-ts/spine-core/src/PhysicsConstraint.ts @@ -267,4 +267,22 @@ export class PhysicsConstraint implements Updatable { } bone.updateAppliedTransform(); } + + translate (x: number, y: number) { + this.ux -= x; + this.uy -= y; + this.cx -= x; + this.cy -= y; + } + + /** Rotates the physics constraint so next {@link #update(Physics)} forces are applied as if the bone rotated around the + * specified point in world space. */ + rotate (x: number, y: number, degrees: number) { + let r = degrees * MathUtils.degRad, cos = Math.cos(r), sin = Math.sin(r); + r = this.tx * cos - this.ty * sin; + this.ty = this.tx * sin + this.ty * cos; + this.tx = r; + const dx = this.cx - x, dy = this.cy - y; + this.translate(dx * cos - dy * sin - dx, dx * sin + dy * cos - dy); + } } diff --git a/spine-ts/spine-core/src/Skeleton.ts b/spine-ts/spine-core/src/Skeleton.ts index e9d7b7b66..ec52de6b7 100644 --- a/spine-ts/spine-core/src/Skeleton.ts +++ b/spine-ts/spine-core/src/Skeleton.ts @@ -646,6 +646,19 @@ export class Skeleton { update (delta: number) { this.time += delta; } + + physicsTranslate (x: number, y: number) { + const physicsConstraints = this.physicsConstraints; + for (let i = 0, n = physicsConstraints.length; i < n; i++) + physicsConstraints[i].translate(x, y); + } + + /** Calls {@link PhysicsConstraint#rotate(float, float, float)} for each physics constraint. */ + physicsRotate (x: number, y: number, degrees: number) { + const physicsConstraints = this.physicsConstraints; + for (let i = 0, n = physicsConstraints.length; i < n; i++) + physicsConstraints[i].rotate(x, y, degrees); + } } /** Determines how physics and other non-deterministic updates are applied. */