mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2026-02-25 11:11:24 +08:00
On second thought, the math optimizations are OK.
(reverted from commit 547ea760d82c3c7e1dc825704eb2046a218317b1) If inaccuracy becomes a problem at runtime (which is unlikely), users can decide to remove the optimizations.
This commit is contained in:
parent
547ea760d8
commit
86fa7aef11
@ -94,8 +94,7 @@ public class Bone implements Updatable {
|
|||||||
appliedScaleX = scaleX;
|
appliedScaleX = scaleX;
|
||||||
appliedScaleY = scaleY;
|
appliedScaleY = scaleY;
|
||||||
|
|
||||||
rotation *= MathUtils.degRad;
|
float cos = MathUtils.cosDeg(rotation), sin = MathUtils.sinDeg(rotation);
|
||||||
float cos = (float)Math.cos(rotation), sin = (float)Math.sin(rotation);
|
|
||||||
float la = cos * scaleX, lb = -sin * scaleY, lc = sin * scaleX, ld = cos * scaleY;
|
float la = cos * scaleX, lb = -sin * scaleY, lc = sin * scaleX, ld = cos * scaleY;
|
||||||
Bone parent = this.parent;
|
Bone parent = this.parent;
|
||||||
if (parent == null) { // Root bone.
|
if (parent == null) { // Root bone.
|
||||||
@ -139,9 +138,8 @@ public class Bone implements Updatable {
|
|||||||
pc = 0;
|
pc = 0;
|
||||||
pd = 1;
|
pd = 1;
|
||||||
do {
|
do {
|
||||||
rotation = parent.appliedRotation * MathUtils.degRad;
|
cos = MathUtils.cosDeg(parent.appliedRotation);
|
||||||
cos = (float)Math.cos(rotation);
|
sin = MathUtils.sinDeg(parent.appliedRotation);
|
||||||
sin = (float)Math.sin(rotation);
|
|
||||||
float temp = pa * cos + pb * sin;
|
float temp = pa * cos + pb * sin;
|
||||||
pb = pa * -sin + pb * cos;
|
pb = pa * -sin + pb * cos;
|
||||||
pa = temp;
|
pa = temp;
|
||||||
@ -162,9 +160,9 @@ public class Bone implements Updatable {
|
|||||||
pc = 0;
|
pc = 0;
|
||||||
pd = 1;
|
pd = 1;
|
||||||
do {
|
do {
|
||||||
rotation = parent.appliedRotation * MathUtils.degRad;
|
float r = parent.appliedRotation;
|
||||||
cos = (float)Math.cos(rotation);
|
cos = MathUtils.cosDeg(r);
|
||||||
sin = (float)Math.sin(rotation);
|
sin = MathUtils.sinDeg(r);
|
||||||
float psx = parent.appliedScaleX, psy = parent.appliedScaleY;
|
float psx = parent.appliedScaleX, psy = parent.appliedScaleY;
|
||||||
float za = cos * psx, zb = -sin * psy, zc = sin * psx, zd = cos * psy;
|
float za = cos * psx, zb = -sin * psy, zc = sin * psx, zd = cos * psy;
|
||||||
float temp = pa * za + pb * zc;
|
float temp = pa * za + pb * zc;
|
||||||
@ -174,9 +172,9 @@ public class Bone implements Updatable {
|
|||||||
pd = pc * zb + pd * zd;
|
pd = pc * zb + pd * zd;
|
||||||
pc = temp;
|
pc = temp;
|
||||||
|
|
||||||
if (psx < 0) rotation = -rotation;
|
if (psx < 0) r = -r;
|
||||||
cos = (float)Math.cos(-rotation);
|
cos = MathUtils.cosDeg(-r);
|
||||||
sin = (float)Math.sin(-rotation);
|
sin = MathUtils.sinDeg(-r);
|
||||||
temp = pa * cos + pb * sin;
|
temp = pa * cos + pb * sin;
|
||||||
pb = pa * -sin + pb * cos;
|
pb = pa * -sin + pb * cos;
|
||||||
pa = temp;
|
pa = temp;
|
||||||
@ -318,11 +316,11 @@ public class Bone implements Updatable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public float getWorldRotationX () {
|
public float getWorldRotationX () {
|
||||||
return (float)Math.atan2(c, a) * MathUtils.radDeg;
|
return MathUtils.atan2(c, a) * MathUtils.radDeg;
|
||||||
}
|
}
|
||||||
|
|
||||||
public float getWorldRotationY () {
|
public float getWorldRotationY () {
|
||||||
return (float)Math.atan2(d, b) * MathUtils.radDeg;
|
return MathUtils.atan2(d, b) * MathUtils.radDeg;
|
||||||
}
|
}
|
||||||
|
|
||||||
public float getWorldScaleX () {
|
public float getWorldScaleX () {
|
||||||
|
|||||||
@ -124,7 +124,7 @@ public class IkConstraint implements Updatable {
|
|||||||
static public void apply (Bone bone, float targetX, float targetY, float alpha) {
|
static public void apply (Bone bone, float targetX, float targetY, float alpha) {
|
||||||
float parentRotation = bone.parent == null ? 0 : bone.parent.getWorldRotationX();
|
float parentRotation = bone.parent == null ? 0 : bone.parent.getWorldRotationX();
|
||||||
float rotation = bone.rotation;
|
float rotation = bone.rotation;
|
||||||
float rotationIK = (float)Math.atan2(targetY - bone.worldY, targetX - bone.worldX) * radDeg - parentRotation;
|
float rotationIK = atan2(targetY - bone.worldY, targetX - bone.worldX) * radDeg - parentRotation;
|
||||||
if ((bone.worldSignX != bone.worldSignY) != (bone.skeleton.flipX != bone.skeleton.flipY)) rotationIK = 360 - rotationIK;
|
if ((bone.worldSignX != bone.worldSignY) != (bone.skeleton.flipX != bone.skeleton.flipY)) rotationIK = 360 - rotationIK;
|
||||||
if (rotationIK > 180)
|
if (rotationIK > 180)
|
||||||
rotationIK -= 360;
|
rotationIK -= 360;
|
||||||
@ -190,10 +190,10 @@ public class IkConstraint implements Updatable {
|
|||||||
cos = -1;
|
cos = -1;
|
||||||
else if (cos > 1) cos = 1;
|
else if (cos > 1) cos = 1;
|
||||||
a2 = (float)Math.acos(cos) * bendDir;
|
a2 = (float)Math.acos(cos) * bendDir;
|
||||||
float a = l1 + l2 * cos, o = l2 * (float)Math.sin(a2);
|
float a = l1 + l2 * cos, o = l2 * sin(a2);
|
||||||
a1 = (float)Math.atan2(ty * a - tx * o, tx * a + ty * o);
|
a1 = atan2(ty * a - tx * o, tx * a + ty * o);
|
||||||
} else {
|
} else {
|
||||||
float a = psx * l2, b = psy * l2, ta = (float)Math.atan2(ty, tx);
|
float a = psx * l2, b = psy * l2, ta = atan2(ty, tx);
|
||||||
float aa = a * a, bb = b * b, ll = l1 * l1, dd = tx * tx + ty * ty;
|
float aa = a * a, bb = b * b, ll = l1 * l1, dd = tx * tx + ty * ty;
|
||||||
float c0 = bb * ll + aa * dd - aa * bb, c1 = -2 * bb * l1, c2 = bb - aa;
|
float c0 = bb * ll + aa * dd - aa * bb, c1 = -2 * bb * l1, c2 = bb - aa;
|
||||||
float d = c1 * c1 - 4 * c2 * c0;
|
float d = c1 * c1 - 4 * c2 * c0;
|
||||||
@ -205,8 +205,8 @@ public class IkConstraint implements Updatable {
|
|||||||
float r = Math.abs(r0) < Math.abs(r1) ? r0 : r1;
|
float r = Math.abs(r0) < Math.abs(r1) ? r0 : r1;
|
||||||
if (r * r <= dd) {
|
if (r * r <= dd) {
|
||||||
float y = (float)Math.sqrt(dd - r * r) * bendDir;
|
float y = (float)Math.sqrt(dd - r * r) * bendDir;
|
||||||
a1 = ta - (float)Math.atan2(y, r);
|
a1 = ta - atan2(y, r);
|
||||||
a2 = (float)Math.atan2(y / psy, (r - l1) / psx);
|
a2 = atan2(y / psy, (r - l1) / psx);
|
||||||
break outer;
|
break outer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -226,8 +226,8 @@ public class IkConstraint implements Updatable {
|
|||||||
minX = x;
|
minX = x;
|
||||||
}
|
}
|
||||||
float angle = (float)Math.acos(-a * l1 / (aa - bb));
|
float angle = (float)Math.acos(-a * l1 / (aa - bb));
|
||||||
x = a * (float)Math.cos(angle) + l1;
|
x = a * cos(angle) + l1;
|
||||||
float y = b * (float)Math.sin(angle);
|
float y = b * sin(angle);
|
||||||
dist = x * x + y * y;
|
dist = x * x + y * y;
|
||||||
if (dist < minDist) {
|
if (dist < minDist) {
|
||||||
minAngle = angle;
|
minAngle = angle;
|
||||||
@ -242,14 +242,14 @@ public class IkConstraint implements Updatable {
|
|||||||
maxY = y;
|
maxY = y;
|
||||||
}
|
}
|
||||||
if (dd <= (minDist + maxDist) / 2) {
|
if (dd <= (minDist + maxDist) / 2) {
|
||||||
a1 = ta - (float)Math.atan2(minY * bendDir, minX);
|
a1 = ta - atan2(minY * bendDir, minX);
|
||||||
a2 = minAngle * bendDir;
|
a2 = minAngle * bendDir;
|
||||||
} else {
|
} else {
|
||||||
a1 = ta - (float)Math.atan2(maxY * bendDir, maxX);
|
a1 = ta - atan2(maxY * bendDir, maxX);
|
||||||
a2 = maxAngle * bendDir;
|
a2 = maxAngle * bendDir;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
float os = (float)Math.atan2(cy, cx) * s2;
|
float os = atan2(cy, cx) * s2;
|
||||||
a1 = (a1 - os) * radDeg + os1;
|
a1 = (a1 - os) * radDeg + os1;
|
||||||
a2 = (a2 + os) * radDeg * s2 + os2;
|
a2 = (a2 + os) * radDeg * s2 + os2;
|
||||||
if (a1 > 180)
|
if (a1 > 180)
|
||||||
|
|||||||
@ -92,9 +92,9 @@ public class RegionAttachment extends Attachment {
|
|||||||
localY *= scaleY;
|
localY *= scaleY;
|
||||||
localX2 *= scaleX;
|
localX2 *= scaleX;
|
||||||
localY2 *= scaleY;
|
localY2 *= scaleY;
|
||||||
float rotation = getRotation() * MathUtils.degRad;
|
float rotation = getRotation();
|
||||||
float cos = (float)Math.cos(rotation);
|
float cos = MathUtils.cosDeg(rotation);
|
||||||
float sin = (float)Math.sin(rotation);
|
float sin = MathUtils.sinDeg(rotation);
|
||||||
float x = getX();
|
float x = getX();
|
||||||
float y = getY();
|
float y = getY();
|
||||||
float localXCos = localX * cos + x;
|
float localXCos = localX * cos + x;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user