/******************************************************************************
* Spine Runtimes License Agreement
* Last updated April 5, 2025. Replaces all prior versions.
*
* Copyright (c) 2013-2026, Esoteric Software LLC
*
* Integration of the Spine Runtimes into software or otherwise creating
* derivative works of the Spine Runtimes is permitted under the terms and
* conditions of Section 2 of the Spine Editor License Agreement:
* http://esotericsoftware.com/spine-editor-license
*
* Otherwise, it is permitted to integrate the Spine Runtimes into software
* or otherwise create derivative works of the Spine Runtimes (collectively,
* "Products"), provided that each user of the Products must obtain their own
* Spine Editor license and redistribution of the Products in any form must
* include this license and copyright notice.
*
* THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
* BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
using System;
namespace Spine {
/// The applied local pose and world transform for a bone. This is the with constraints applied and the
/// world transform computed by and .
///
/// If the world transform is changed, call before using the local transform. The local
/// transform may be needed by other code (eg to apply another constraint).
///
/// After changing the world transform, call on every descendant bone. It may be more
/// convenient to modify the local transform instead, then call to update the world
/// transforms for all bones and apply constraints.
///
public class BonePose : IPose, IUpdate {
public Bone bone;
internal float x, y, rotation, scaleX, scaleY, shearX, shearY;
internal Inherit inherit;
internal float a, b, worldX;
internal float c, d, worldY;
internal int world, local;
public void Set (BonePose pose) {
if (pose == null) throw new ArgumentNullException("pose", "pose cannot be null.");
x = pose.x;
y = pose.y;
rotation = pose.rotation;
scaleX = pose.scaleX;
scaleY = pose.scaleY;
shearX = pose.shearX;
shearY = pose.shearY;
inherit = pose.inherit;
}
/// The local X translation.
public float X { get { return x; } set { x = value; } }
/// The local Y translation.
public float Y { get { return y; } set { y = value; } }
/// Sets local x and y translation.
public void SetPosition (float x, float y) {
this.x = x;
this.y = y;
}
/// The local rotation.
public float Rotation { get { return rotation; } set { rotation = value; } }
/// The local scaleX.
public float ScaleX { get { return scaleX; } set { scaleX = value; } }
/// The local scaleY.
public float ScaleY { get { return scaleY; } set { scaleY = value; } }
/// Sets local scaleX and scaleY.
public void SetScale (float scaleX, float scaleY) {
this.scaleX = scaleX;
this.scaleY = scaleY;
}
/// Sets local scaleX and scaleY to the same value.
public void SetScale (float scale) {
scaleX = scale;
scaleY = scale;
}
/// The local shearX.
public float ShearX { get { return shearX; } set { shearX = value; } }
/// The local shearY.
public float ShearY { get { return shearY; } set { shearY = value; } }
/// Determines how parent world transforms affect this bone.
public Inherit Inherit { get { return inherit; } set { inherit = value; } }
///
/// Called by to compute the world transform, if needed.
///
public void Update (Skeleton skeleton, Physics physics) {
if (world != skeleton.update) UpdateWorldTransform(skeleton);
}
/// Computes the world transform using the parent bone's world transform and this applied local pose. Child bones are not
/// updated.
///
/// See World transforms in the Spine
/// Runtimes Guide.
public void UpdateWorldTransform (Skeleton skeleton) {
if (local == skeleton.update)
UpdateLocalTransform(skeleton);
else
world = skeleton.update;
if (bone.parent == null) { // Root bone.
float sx = skeleton.scaleX, sy = skeleton.ScaleY;
float rx = (rotation + shearX) * MathUtils.DegRad;
float ry = (rotation + 90 + shearY) * MathUtils.DegRad;
a = (float)Math.Cos(rx) * scaleX * sx;
b = (float)Math.Cos(ry) * scaleY * sx;
c = (float)Math.Sin(rx) * scaleX * sy;
d = (float)Math.Sin(ry) * scaleY * sy;
worldX = x * sx + skeleton.x;
worldY = y * sy + skeleton.y;
return;
}
BonePose parent = bone.parent.appliedPose;
float pa = parent.a, pb = parent.b, pc = parent.c, pd = parent.d;
worldX = pa * x + pb * y + parent.worldX;
worldY = pc * x + pd * y + parent.worldY;
switch (inherit) {
case Inherit.Normal: {
float rx = (rotation + shearX) * MathUtils.DegRad;
float ry = (rotation + 90 + shearY) * MathUtils.DegRad;
float la = (float)Math.Cos(rx) * scaleX;
float lb = (float)Math.Cos(ry) * scaleY;
float lc = (float)Math.Sin(rx) * scaleX;
float ld = (float)Math.Sin(ry) * scaleY;
a = pa * la + pb * lc;
b = pa * lb + pb * ld;
c = pc * la + pd * lc;
d = pc * lb + pd * ld;
break;
}
case Inherit.OnlyTranslation: {
float sx = skeleton.scaleX, sy = skeleton.ScaleY;
float rx = (rotation + shearX) * MathUtils.DegRad;
float ry = (rotation + 90 + shearY) * MathUtils.DegRad;
a = (float)Math.Cos(rx) * scaleX * sx;
b = (float)Math.Cos(ry) * scaleY * sx;
c = (float)Math.Sin(rx) * scaleX * sy;
d = (float)Math.Sin(ry) * scaleY * sy;
break;
}
case Inherit.NoRotationOrReflection: {
float sx = skeleton.scaleX, sy = skeleton.ScaleY, sxi = 1 / sx, syi = 1 / sy;
pa *= sxi;
pc *= syi;
float s = pa * pa + pc * pc, r;
if (s > MathUtils.EpsilonSq) {
s = Math.Abs(pa * pd * syi - pb * sxi * pc) / s;
pb = pc * s;
pd = pa * s;
r = rotation - MathUtils.Atan2Deg(pc, pa);
} else {
pa = 0;
pc = 0;
r = rotation - 90 + MathUtils.Atan2Deg(pd, pb);
}
float rx = (r + shearX) * MathUtils.DegRad;
float ry = (r + shearY + 90) * MathUtils.DegRad;
float la = (float)Math.Cos(rx) * scaleX;
float lb = (float)Math.Cos(ry) * scaleY;
float lc = (float)Math.Sin(rx) * scaleX;
float ld = (float)Math.Sin(ry) * scaleY;
a = (pa * la - pb * lc) * sx;
b = (pa * lb - pb * ld) * sx;
c = (pc * la + pd * lc) * sy;
d = (pc * lb + pd * ld) * sy;
break;
}
case Inherit.NoScale:
case Inherit.NoScaleOrReflection: {
float sx = skeleton.scaleX, sy = skeleton.ScaleY, sxi = 1 / sx, syi = 1 / sy;
float r = rotation * MathUtils.DegRad, cos = (float)Math.Cos(r), sin = (float)Math.Sin(r);
float za = (pa * cos + pb * sin) * sxi;
float zc = (pc * cos + pd * sin) * syi;
float s = 1 / (float)Math.Sqrt(za * za + zc * zc);
za *= s;
zc *= s;
float zb = -zc, zd = za;
if (inherit == Inherit.NoScale && pa * pd - pb * pc < 0 != (sx < 0 != sy < 0)) {
zb = -zb;
zd = -zd;
}
float rx = shearX * MathUtils.DegRad;
float ry = (90 + shearY) * MathUtils.DegRad;
float la = (float)Math.Cos(rx) * scaleX;
float lb = (float)Math.Cos(ry) * scaleY;
float lc = (float)Math.Sin(rx) * scaleX;
float ld = (float)Math.Sin(ry) * scaleY;
a = (za * la + zb * lc) * sx;
b = (za * lb + zb * ld) * sx;
c = (zc * la + zd * lc) * sy;
d = (zc * lb + zd * ld) * sy;
break;
}
}
}
///
/// Computes the local transform values from the world transform.
///
/// Some information is ambiguous in the world transform, such as -1,-1 scale versus 180 rotation. The local transform after
/// calling this method is equivalent to the local transform used to compute the world transform, but may not be identical.
///
public void UpdateLocalTransform (Skeleton skeleton) {
local = 0;
world = skeleton.update;
float sx = skeleton.scaleX, sy = skeleton.ScaleY;
if (bone.parent == null) {
float sxi = 1 / sx, syi = 1 / sy;
x = (worldX - skeleton.x) * sxi;
y = (worldY - skeleton.y) * syi;
Set(a * sxi, b * sxi, c * syi, d * syi, 0);
return;
}
BonePose parent = bone.parent.appliedPose;
float pa = parent.a, pb = parent.b, pc = parent.c, pd = parent.d;
float pad = pa * pd - pb * pc, pid = 1 / pad;
float ia = pd * pid, ib = pb * pid, ic = pc * pid, id = pa * pid;
float dx = worldX - parent.worldX, dy = worldY - parent.worldY;
x = dx * ia - dy * ib;
y = dy * id - dx * ic;
switch (inherit) {
case Inherit.Normal: {
Set(ia * a - ib * c, ia * b - ib * d, id * c - ic * a, id * d - ic * b, 0);
break;
}
case Inherit.OnlyTranslation: {
float sxi = 1 / sx, syi = 1 / sy;
Set(a * sxi, b * sxi, c * syi, d * syi, 0);
break;
}
case Inherit.NoRotationOrReflection: {
float sxi = 1 / sx, syi = 1 / sy;
pa *= sxi;
pc *= syi;
float wa = a * sxi, wb = b * sxi, wc = c * syi, wd = d * syi;
float s = 1 / (pa * pa + pc * pc), det = 1 / Math.Abs(pad * sxi * syi);
Set((pa * wa + pc * wc) * s, (pa * wb + pc * wd) * s, (pa * wc - pc * wa) * det, (pa * wd - pc * wb) * det,
MathUtils.Atan2Deg(pc, pa));
break;
}
case Inherit.NoScale:
case Inherit.NoScaleOrReflection: {
float sxi = 1 / sx, syi = 1 / sy;
float wa = a * sxi, wb = b * sxi, wc = c * syi, wd = d * syi;
float tx = pd * a - pb * c, ty = pa * c - pc * a;
if (pad < 0) {
tx = -tx;
ty = -ty;
}
float r = MathUtils.Atan2Deg(ty, tx);
rotation = r;
r *= MathUtils.DegRad;
float cos = (float)Math.Cos(r), sin = (float)Math.Sin(r);
float za = (pa * cos + pb * sin) * sxi;
float zc = (pc * cos + pd * sin) * syi;
float s = 1 / (float)Math.Sqrt(za * za + zc * zc);
za *= s;
zc *= s;
float si = inherit == Inherit.NoScale && pad < 0 != (sx < 0 != sy < 0) ? -1 : 1;
Set(za * wa + zc * wc, za * wb + zc * wd, (za * wc - zc * wa) * si, (za * wd - zc * wb) * si);
break;
}
}
}
private void Set (float ra, float rb, float rc, float rd) {
float x = ra * ra + rc * rc, y = rb * rb + rd * rd;
if (x > MathUtils.EpsilonSq) {
shearX = MathUtils.Atan2Deg(rc, ra);
scaleX = (float)Math.Sqrt(x);
} else {
shearX = 0;
scaleX = 0;
}
scaleY = (float)Math.Sqrt(y);
if (y > MathUtils.EpsilonSq) {
shearY = MathUtils.Atan2Deg(rd, rb);
if (ra * rd - rb * rc < 0) {
scaleY = -scaleY;
shearY += 90;
} else
shearY -= 90;
if (shearY > 180)
shearY -= 360;
else if (shearY <= -180) //
shearY += 360;
} else
shearY = 0;
}
private void Set (float ra, float rb, float rc, float rd, float ro) {
shearX = 0;
float x = ra * ra + rc * rc, y = rb * rb + rd * rd;
if (x > MathUtils.EpsilonSq) {
float r = MathUtils.Atan2Deg(rc, ra);
rotation = r + ro;
scaleX = (float)Math.Sqrt(x);
scaleY = (float)Math.Sqrt(y);
if (y > MathUtils.EpsilonSq) {
shearY = MathUtils.Atan2Deg(rd, rb);
if (ra * rd - rb * rc < 0) {
scaleY = -scaleY;
shearY += 90 - r;
} else
shearY -= 90 + r;
if (shearY > 180)
shearY -= 360;
else if (shearY <= -180) //
shearY += 360;
} else
shearY = 0;
} else {
scaleX = 0;
scaleY = (float)Math.Sqrt(y);
shearY = 0;
rotation = y > MathUtils.EpsilonSq ? MathUtils.Atan2Deg(rd, rb) - 90 + ro : ro;
}
}
///
/// If the world transform has been modified by constraints and the local transform no longer matches,
/// is called. Call this after before
/// using the applied local transform.
///
public void ValidateLocalTransform (Skeleton skeleton) {
if (local == skeleton.update) UpdateLocalTransform(skeleton);
}
internal void ModifyLocal (Skeleton skeleton) {
if (local == skeleton.update) UpdateLocalTransform(skeleton);
world = 0;
ResetWorld(skeleton.update);
}
internal void ModifyWorld (int update) {
local = update;
world = update;
ResetWorld(update);
}
private void ResetWorld (int update) {
Bone[] children = bone.children.Items;
for (int i = 0, n = bone.children.Count; i < n; i++) {
BonePose child = children[i].appliedPose;
if (child.world == update) {
child.world = 0;
child.local = 0;
child.ResetWorld(update);
}
}
}
/// The world transform [a b][c d] x-axis x component.
public float A { get { return a; } set { a = value; } }
/// The world transform [a b][c d] y-axis x component.
public float B { get { return b; } set { b = value; } }
/// The world transform [a b][c d] x-axis y component.
public float C { get { return c; } set { c = value; } }
/// The world transform [a b][c d] y-axis y component.
public float D { get { return d; } set { d = value; } }
/// The world X position.
public float WorldX { get { return worldX; } set { worldX = value; } }
/// The world Y position.
public float WorldY { get { return worldY; } set { worldY = value; } }
/// The world rotation for the X axis, calculated using and . This is the direction the
/// bone is pointing.
public float WorldRotationX { get { return MathUtils.Atan2Deg(c, a); } }
/// The world rotation for the Y axis, calculated using and .
public float WorldRotationY { get { return MathUtils.Atan2Deg(d, b); } }
/// Returns the magnitude (always positive) of the world scale X, calculated using and .
public float WorldScaleX { get { return (float)Math.Sqrt(a * a + c * c); } }
/// Returns the magnitude (always positive) of the world scale Y, calculated using and .
public float WorldScaleY { get { return (float)Math.Sqrt(b * b + d * d); } }
/// Transforms a point from world coordinates to the bone's local coordinates.
public void WorldToLocal (float worldX, float worldY, out float localX, out float localY) {
float a = this.a, b = this.b, c = this.c, d = this.d;
float det = a * d - b * c;
float x = worldX - this.worldX, y = worldY - this.worldY;
localX = (x * d - y * b) / det;
localY = (y * a - x * c) / det;
}
/// Transforms a point from the bone's local coordinates to world coordinates.
public void LocalToWorld (float localX, float localY, out float worldX, out float worldY) {
worldX = localX * a + localY * b + this.worldX;
worldY = localX * c + localY * d + this.worldY;
}
/// Transforms a point from world coordinates to the parent bone's local coordinates.
public void WorldToParent (float worldX, float worldY, out float parentX, out float parentY) {
if (bone.parent == null) {
parentX = worldX;
parentY = worldY;
} else {
bone.parent.appliedPose.WorldToLocal(worldX, worldY, out parentX, out parentY);
}
}
/// Transforms a point from the parent bone's coordinates to world coordinates.
public void ParentToWorld (float parentX, float parentY, out float worldX, out float worldY) {
if (bone.parent == null) {
worldX = parentX;
worldY = parentY;
} else {
bone.parent.appliedPose.LocalToWorld(parentX, parentY, out worldX, out worldY);
}
}
/// Transforms a world rotation to a local rotation.
public float WorldToLocalRotation (float worldRotation) {
worldRotation *= MathUtils.DegRad;
float sin = (float)Math.Sin(worldRotation), cos = (float)Math.Cos(worldRotation);
return MathUtils.Atan2Deg(a * sin - c * cos, d * cos - b * sin) + rotation - shearX;
}
/// Transforms a local rotation to a world rotation.
public float LocalToWorldRotation (float localRotation) {
localRotation = (localRotation - rotation - shearX) * MathUtils.DegRad;
float sin = (float)Math.Sin(localRotation), cos = (float)Math.Cos(localRotation);
return MathUtils.Atan2Deg(cos * c + sin * d, cos * a + sin * b);
}
/// Rotates the world transform the specified amount.
public void RotateWorld (float degrees) {
degrees *= MathUtils.DegRad;
float sin = (float)Math.Sin(degrees), cos = (float)Math.Cos(degrees);
float ra = a, rb = b;
a = cos * ra - sin * c;
b = cos * rb - sin * d;
c = sin * ra + cos * c;
d = sin * rb + cos * d;
}
override public string ToString () {
return bone.data.name;
}
}
}