Skip to content

Commit

Permalink
a few bugs with skeleton.scaleY
Browse files Browse the repository at this point in the history
formatting
  • Loading branch information
ivanpopelyshev committed Jun 28, 2022
1 parent c7859f3 commit bc73ed9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 39 deletions.
18 changes: 7 additions & 11 deletions packages/runtime-4.0/src/core/Bone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ export class Bone implements Updatable, IBone {
}

/** The bone's setup pose data. */
data: BoneData;
data: BoneData = null;

/** The skeleton this bone belongs to. */
skeleton: Skeleton;
skeleton: Skeleton = null;

/** The parent bone, or null if this is the root bone. */
parent: Bone;
parent: Bone = null;

/** The immediate children of this bone. */
children = new Array<Bone>();
Expand Down Expand Up @@ -169,8 +169,8 @@ export class Bone implements Updatable, IBone {
let prx = 0;
if (s > 0.0001) {
s = Math.abs(pa * pd - pb * pc) / s;
pa /= this.skeleton.scaleX;
pc /= this.skeleton.scaleY;
pa /= sx;
pc /= sy;
pb = pc * s;
pd = pa * s;
prx = Math.atan2(pc, pa) * MathUtils.radDeg;
Expand Down Expand Up @@ -202,12 +202,8 @@ export class Bone implements Updatable, IBone {
za *= s;
zc *= s;
s = Math.sqrt(za * za + zc * zc);
if (
this.data.transformMode == TransformMode.NoScale
&& (pa * pd - pb * pc < 0) != (settings.yDown?
(this.skeleton.scaleX < 0 != this.skeleton.scaleY > 0) :
(this.skeleton.scaleX < 0 != this.skeleton.scaleY < 0))
) s = -s;
if (this.data.transformMode == TransformMode.NoScale
&& (pa * pd - pb * pc < 0) != (sx < 0 != sy < 0)) s = -s;
let r = Math.PI / 2 + Math.atan2(zc, za);
let zb = Math.cos(r) * s;
let zd = Math.sin(r) * s;
Expand Down
17 changes: 12 additions & 5 deletions packages/runtime-4.0/src/core/IkConstraint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Updatable} from "./Updatable";
import {IkConstraintData} from "./IkConstraintData";
import {Bone} from "./Bone";
import {Skeleton} from "./Skeleton";
import {MathUtils, TransformMode} from "@pixi-spine/base";
import {MathUtils, settings, TransformMode} from "@pixi-spine/base";

/** Stores the current pose for an IK constraint. An IK constraint adjusts the rotation of 1 or 2 constrained bones so the tip of
* the last bone is as close to the target bone as possible.
Expand Down Expand Up @@ -78,17 +78,24 @@ export class IkConstraint implements Updatable {
let pa = p.a, pb = p.c, pc = p.b, pd = p.d;
let rotationIK = -bone.ashearX - bone.arotation, tx = 0, ty = 0;

let skelX = bone.skeleton.scaleX;
let skelY = settings.yDown? -bone.skeleton.scaleY : bone.skeleton.scaleY;

switch(bone.data.transformMode) {
case TransformMode.OnlyTranslation:
tx = targetX - bone.worldX;
ty = targetY - bone.worldY;
//TODO: possible bug in spine-ts runtime!
if (settings.yDown) {
ty = -ty;
}
break;
case TransformMode.NoRotationOrReflection:
let s = Math.abs(pa * pd - pb * pc) / (pa * pa + pc * pc);
let sa = pa / bone.skeleton.scaleX;
let sc = pc / bone.skeleton.scaleY;
pb = -sc * s * bone.skeleton.scaleX;
pd = sa * s * bone.skeleton.scaleY;
let sa = pa / skelX;
let sc = pc / skelY;
pb = -sc * s * skelX;
pd = sa * s * skelY;
rotationIK += Math.atan2(sc, sa) * MathUtils.radDeg;
// Fall through
default:
Expand Down
53 changes: 30 additions & 23 deletions packages/runtime-4.0/src/core/PathConstraint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@ import {MathUtils, PositionMode, RotateMode, Utils} from "@pixi-spine/base";
* @public
* */
export class PathConstraint implements Updatable {
static NONE = -1; static BEFORE = -2; static AFTER = -3;
static NONE = -1;
static BEFORE = -2;
static AFTER = -3;
static epsilon = 0.00001;

/** The path constraint's setup pose data. */
data: PathConstraintData;
data: PathConstraintData = null;

/** The bones that will be modified by this path constraint. */
bones: Array<Bone>;
bones: Array<Bone> = null;

/** The slot whose path attachment will be used to constrained the bones. */
target: Slot;
target: Slot = null;

/** The position along the path. */
position = 0;
Expand All @@ -36,15 +38,18 @@ export class PathConstraint implements Updatable {

mixY = 0;

spaces = new Array<number>(); positions = new Array<number>();
world = new Array<number>(); curves = new Array<number>(); lengths = new Array<number>();
spaces = new Array<number>();
positions = new Array<number>();
world = new Array<number>();
curves = new Array<number>();
lengths = new Array<number>();
segments = new Array<number>();

active = false;

constructor (data: PathConstraintData, skeleton: Skeleton) {
if (data == null) throw new Error("data cannot be null.");
if (skeleton == null) throw new Error("skeleton cannot be null.");
constructor(data: PathConstraintData, skeleton: Skeleton) {
if (!data) throw new Error("data cannot be null.");
if (!skeleton) throw new Error("skeleton cannot be null.");
this.data = data;
this.bones = new Array<Bone>();
for (let i = 0, n = data.bones.length; i < n; i++)
Expand All @@ -57,11 +62,11 @@ export class PathConstraint implements Updatable {
this.mixY = data.mixY;
}

isActive () {
isActive() {
return this.active;
}

update () {
update() {
let attachment = this.target.getAttachment();
if (!(attachment instanceof PathAttachment)) return;

Expand All @@ -71,9 +76,10 @@ export class PathConstraint implements Updatable {
let data = this.data;
let tangents = data.rotateMode == RotateMode.Tangent, scale = data.rotateMode == RotateMode.ChainScale;

let boneCount = this.bones.length, spacesCount = tangents ? boneCount : boneCount + 1;
let bones = this.bones;
let spaces = Utils.setArraySize(this.spaces, spacesCount), lengths: Array<number> = scale ? this.lengths = Utils.setArraySize(this.lengths, boneCount) : null;
let boneCount = bones.length, spacesCount = tangents ? boneCount : boneCount + 1;
let spaces = Utils.setArraySize(this.spaces, spacesCount),
lengths: Array<number> = scale ? this.lengths = Utils.setArraySize(this.lengths, boneCount) : null;
let spacing = this.spacing;

switch (data.spacingMode) {
Expand Down Expand Up @@ -191,10 +197,11 @@ export class PathConstraint implements Updatable {
}
}

computeWorldPositions (path: PathAttachment, spacesCount: number, tangents: boolean) {
computeWorldPositions(path: PathAttachment, spacesCount: number, tangents: boolean) {
let target = this.target;
let position = this.position;
let spaces = this.spaces, out = Utils.setArraySize(this.positions, spacesCount * 3 + 2), world: Array<number> = null;
let spaces = this.spaces, out = Utils.setArraySize(this.positions, spacesCount * 3 + 2),
world: Array<number> = null;
let closed = path.closed;
let verticesLength = path.worldVerticesLength, curveCount = verticesLength / 6, prevCurve = PathConstraint.NONE;

Expand Down Expand Up @@ -242,7 +249,7 @@ export class PathConstraint implements Updatable {
}

// Determine curve containing position.
for (;; curve++) {
for (; ; curve++) {
let length = lengths[curve];
if (p > length) continue;
if (curve == 0)
Expand Down Expand Up @@ -321,7 +328,7 @@ export class PathConstraint implements Updatable {

if (this.data.positionMode == PositionMode.Percent) position *= pathLength;

let multiplier = 0;
let multiplier;
switch (this.data.spacingMode) {
case SpacingMode.Percent:
multiplier = pathLength;
Expand Down Expand Up @@ -353,7 +360,7 @@ export class PathConstraint implements Updatable {
}

// Determine curve containing position.
for (;; curve++) {
for (; ; curve++) {
let length = curves[curve];
if (p > length) continue;
if (curve == 0)
Expand Down Expand Up @@ -408,7 +415,7 @@ export class PathConstraint implements Updatable {

// Weight by segment length.
p *= curveLength;
for (;; segment++) {
for (; ; segment++) {
let length = segments[segment];
if (p > length) continue;
if (segment == 0)
Expand All @@ -424,22 +431,22 @@ export class PathConstraint implements Updatable {
return out;
}

addBeforePosition (p: number, temp: Array<number>, i: number, out: Array<number>, o: number) {
addBeforePosition(p: number, temp: Array<number>, i: number, out: Array<number>, o: number) {
let x1 = temp[i], y1 = temp[i + 1], dx = temp[i + 2] - x1, dy = temp[i + 3] - y1, r = Math.atan2(dy, dx);
out[o] = x1 + p * Math.cos(r);
out[o + 1] = y1 + p * Math.sin(r);
out[o + 2] = r;
}

addAfterPosition (p: number, temp: Array<number>, i: number, out: Array<number>, o: number) {
addAfterPosition(p: number, temp: Array<number>, i: number, out: Array<number>, o: number) {
let x1 = temp[i + 2], y1 = temp[i + 3], dx = x1 - temp[i], dy = y1 - temp[i + 1], r = Math.atan2(dy, dx);
out[o] = x1 + p * Math.cos(r);
out[o + 1] = y1 + p * Math.sin(r);
out[o + 2] = r;
}

addCurvePosition (p: number, x1: number, y1: number, cx1: number, cy1: number, cx2: number, cy2: number, x2: number, y2: number,
out: Array<number>, o: number, tangents: boolean) {
addCurvePosition(p: number, x1: number, y1: number, cx1: number, cy1: number, cx2: number, cy2: number, x2: number, y2: number,
out: Array<number>, o: number, tangents: boolean) {
if (p == 0 || isNaN(p)) {
out[o] = x1;
out[o + 1] = y1;
Expand Down

0 comments on commit bc73ed9

Please sign in to comment.