A few fixes and minor improvements.

This commit is contained in:
NathanSweet 2016-02-17 03:52:22 +01:00
parent 11c251615d
commit 0f723b9fa7
9 changed files with 149 additions and 159 deletions

View File

@ -109,22 +109,21 @@ void spBone_updateWorldTransformWith (spBone* self, float x, float y, float rota
CONST_CAST(float, self->c) = pc * la + pd * lc; CONST_CAST(float, self->c) = pc * la + pd * lc;
CONST_CAST(float, self->d) = pc * lb + pd * ld; CONST_CAST(float, self->d) = pc * lb + pd * ld;
} else if (self->data->inheritRotation) { /* No scale inheritance. */ } else if (self->data->inheritRotation) { /* No scale inheritance. */
spBone* p = parent;
pa = 1; pa = 1;
pb = 0; pb = 0;
pc = 0; pc = 0;
pd = 1; pd = 1;
while (p) { do {
cosine = COS(p->appliedRotation * DEG_RAD); cosine = COS(parent->appliedRotation * DEG_RAD);
sine = SIN(p->appliedRotation * DEG_RAD); sine = SIN(parent->appliedRotation * DEG_RAD);
temp = pa * cosine + pb * sine; temp = pa * cosine + pb * sine;
pb = pa * -sine + pb * cosine; pb = pa * -sine + pb * cosine;
pa = temp; pa = temp;
temp = pc * cosine + pd * sine; temp = pc * cosine + pd * sine;
pd = pc * -sine + pd * cosine; pd = pc * -sine + pd * cosine;
pc = temp; pc = temp;
p = p->parent; parent = parent->parent;
} } while (parent);
CONST_CAST(float, self->a) = pa * la + pb * lc; CONST_CAST(float, self->a) = pa * la + pb * lc;
CONST_CAST(float, self->b) = pa * lb + pb * ld; CONST_CAST(float, self->b) = pa * lb + pb * ld;
CONST_CAST(float, self->c) = pc * la + pd * lc; CONST_CAST(float, self->c) = pc * la + pd * lc;
@ -138,15 +137,14 @@ void spBone_updateWorldTransformWith (spBone* self, float x, float y, float rota
CONST_CAST(float, self->d) = -self->d; CONST_CAST(float, self->d) = -self->d;
} }
} else if (self->data->inheritScale) { /* No rotation inheritance. */ } else if (self->data->inheritScale) { /* No rotation inheritance. */
spBone* p = parent;
pa = 1; pa = 1;
pb = 0; pb = 0;
pc = 0; pc = 0;
pd = 1; pd = 1;
while (p) { do {
float za, zb, zc, zd; float za, zb, zc, zd;
float r = p->rotation; float r = parent->rotation;
float psx = p->appliedScaleX, psy = p->appliedScaleY; float psx = parent->appliedScaleX, psy = parent->appliedScaleY;
cosine = COS(r * DEG_RAD); cosine = COS(r * DEG_RAD);
sine = SIN(r * DEG_RAD); sine = SIN(r * DEG_RAD);
za = cosine * psx; za = cosine * psx;
@ -170,8 +168,8 @@ void spBone_updateWorldTransformWith (spBone* self, float x, float y, float rota
pd = pc * -sine + pd * cosine; pd = pc * -sine + pd * cosine;
pc = temp; pc = temp;
p = p->parent; parent = parent->parent;
} } while (parent);
CONST_CAST(float, self->a) = pa * la + pb * lc; CONST_CAST(float, self->a) = pa * la + pb * lc;
CONST_CAST(float, self->b) = pa * lb + pb * ld; CONST_CAST(float, self->b) = pa * lb + pb * ld;
CONST_CAST(float, self->c) = pc * la + pd * lc; CONST_CAST(float, self->c) = pc * la + pd * lc;

View File

@ -527,8 +527,8 @@ spSkeletonData* spSkeletonJson_readSkeletonData (spSkeletonJson* self, const cha
} }
transformConstraintData->translateMix = Json_getFloat(transformMap, "translateMix", 1); transformConstraintData->translateMix = Json_getFloat(transformMap, "translateMix", 1);
transformConstraintData->x = Json_getFloat(transformMap, "x", 0); transformConstraintData->x = Json_getFloat(transformMap, "x", 0) * self->scale;
transformConstraintData->y = Json_getFloat(transformMap, "y", 0); transformConstraintData->y = Json_getFloat(transformMap, "y", 0) * self->scale;
skeletonData->transformConstraints[i] = transformConstraintData; skeletonData->transformConstraints[i] = transformConstraintData;
} }

View File

@ -86,6 +86,11 @@ namespace Spine {
SetToSetupPose(); SetToSetupPose();
} }
/// <summary>Same as {@link #updateWorldTransform()}. This method exists for Bone to implement {@link Updatable}.</summary>
public void Update () {
UpdateWorldTransform(x, y, rotation, scaleX, scaleY);
}
/// <summary>Computes the world SRT using the parent bone and this bone's local SRT.</summary> /// <summary>Computes the world SRT using the parent bone and this bone's local SRT.</summary>
public void UpdateWorldTransform () { public void UpdateWorldTransform () {
UpdateWorldTransform(x, y, rotation, scaleX, scaleY); UpdateWorldTransform(x, y, rotation, scaleX, scaleY);
@ -101,26 +106,26 @@ namespace Spine {
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.
Skeleton skeleton = this.skeleton; Skeleton skeleton = this.skeleton;
if (skeleton.flipX) { if (skeleton.flipX) {
x = -x; x = -x;
la = -la; la = -la;
lb = -lb; lb = -lb;
} }
if (skeleton.flipY != yDown) { if (skeleton.flipY != yDown) {
y = -y; y = -y;
lc = -lc; lc = -lc;
ld = -ld; ld = -ld;
} }
a = la; a = la;
b = lb; b = lb;
c = lc; c = lc;
d = ld; d = ld;
worldX = x; worldX = x;
worldY = y; worldY = y;
worldSignX = Math.Sign(scaleX); worldSignX = Math.Sign(scaleX);
worldSignY = Math.Sign(scaleY); worldSignY = Math.Sign(scaleY);
return; return;
} }
float pa = parent.a, pb = parent.b, pc = parent.c, pd = parent.d; float pa = parent.a, pb = parent.b, pc = parent.c, pd = parent.d;
@ -130,117 +135,109 @@ namespace Spine {
worldSignY = parent.worldSignY * Math.Sign(scaleY); worldSignY = parent.worldSignY * Math.Sign(scaleY);
if (data.inheritRotation && data.inheritScale) { if (data.inheritRotation && data.inheritScale) {
a = pa * la + pb * lc; a = pa * la + pb * lc;
b = pa * lb + pb * ld; b = pa * lb + pb * ld;
c = pc * la + pd * lc; c = pc * la + pd * lc;
d = pc * lb + pd * ld; d = pc * lb + pd * ld;
} else if (data.inheritRotation) { // No scale inheritance. } else if (data.inheritRotation) { // No scale inheritance.
Bone p = parent; pa = 1;
pa = 1; pb = 0;
pb = 0; pc = 0;
pc = 0; pd = 1;
pd = 1; do {
while (p != null) { cos = MathUtils.CosDeg(parent.appliedRotation);
cos = MathUtils.CosDeg(p.appliedRotation); sin = MathUtils.SinDeg(parent.appliedRotation);
sin = MathUtils.SinDeg(p.appliedRotation); float temp = pa * cos + pb * sin;
float ta = pa * cos + pb * sin; pb = pa * -sin + pb * cos;
float tb = pa * -sin + pb * cos; pa = temp;
float tc = pc * cos + pd * sin; temp = pc * cos + pd * sin;
float td = pc * -sin + pd * cos; pd = pc * -sin + pd * cos;
pa = ta; pc = temp;
pb = tb; parent = parent.parent;
pc = tc; } while (parent != null);
pd = td; a = pa * la + pb * lc;
p = p.parent; b = pa * lb + pb * ld;
} c = pc * la + pd * lc;
a = pa * la + pb * lc; d = pc * lb + pd * ld;
b = pa * lb + pb * ld; if (skeleton.flipX) {
c = pc * la + pd * lc; a = -a;
d = pc * lb + pd * ld; b = -b;
if (skeleton.flipX) { }
a = -a; if (skeleton.flipY != yDown) {
b = -b; c = -c;
} d = -d;
if (skeleton.flipY != yDown) { }
c = -c;
d = -d;
}
} else if (data.inheritScale) { // No rotation inheritance. } else if (data.inheritScale) { // No rotation inheritance.
Bone p = parent; pa = 1;
pa = 1; pb = 0;
pb = 0; pc = 0;
pc = 0; pd = 1;
pd = 1; do {
while (p != null) { float r = parent.rotation;
float r = p.rotation; cos = MathUtils.CosDeg(r);
cos = MathUtils.CosDeg(r); sin = MathUtils.SinDeg(r);
sin = MathUtils.SinDeg(r); float psx = parent.appliedScaleX, psy = parent.appliedScaleY;
float psx = p.appliedScaleX, psy = p.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; pb = pa * zb + pb * zd;
pb = pa * zb + pb * zd; pa = temp;
pa = temp; temp = pc * za + pd * zc;
temp = pc * za + pd * zc; pd = pc * zb + pd * zd;
pd = pc * zb + pd * zd; pc = temp;
pc = temp;
if (psx < 0) r = -r; if (psx < 0) r = -r;
cos = MathUtils.CosDeg(-r); cos = MathUtils.CosDeg(-r);
sin = MathUtils.SinDeg(-r); 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;
temp = pc * cos + pd * sin; temp = pc * cos + pd * sin;
pd = pc * -sin + pd * cos; pd = pc * -sin + pd * cos;
pc = temp; pc = temp;
p = p.parent; parent = parent.parent;
} } while (parent != null);
a = pa * la + pb * lc; a = pa * la + pb * lc;
b = pa * lb + pb * ld; b = pa * lb + pb * ld;
c = pc * la + pd * lc; c = pc * la + pd * lc;
d = pc * lb + pd * ld; d = pc * lb + pd * ld;
if (skeleton.flipX) { if (skeleton.flipX) {
a = -a; a = -a;
b = -b; b = -b;
} }
if (skeleton.flipY != yDown) { if (skeleton.flipY != yDown) {
c = -c; c = -c;
d = -d; d = -d;
} }
} else { } else {
a = la; a = la;
b = lb; b = lb;
c = lc; c = lc;
d = ld; d = ld;
} }
} }
public void Update () {
UpdateWorldTransform(x, y, rotation, scaleX, scaleY);
}
public void SetToSetupPose () { public void SetToSetupPose () {
BoneData data = this.data; BoneData data = this.data;
x = data.x; x = data.x;
y = data.y; y = data.y;
rotation = data.rotation; rotation = data.rotation;
scaleX = data.scaleX; scaleX = data.scaleX;
scaleY = data.scaleY; scaleY = data.scaleY;
} }
public void WorldToLocal (float worldX, float worldY, out float localX, out float localY) { public void WorldToLocal (float worldX, float worldY, out float localX, out float localY) {
float x = worldX - this.worldX, y = worldY - this.worldY; float x = worldX - this.worldX, y = worldY - this.worldY;
float a = this.a, b = this.b, c = this.c, d = this.d; float a = this.a, b = this.b, c = this.c, d = this.d;
float invDet = 1 / (a * d - b * c); float invDet = 1 / (a * d - b * c);
localX = (x * a * invDet - y * b * invDet); localX = (x * a * invDet - y * b * invDet);
localY = (y * d * invDet - x * c * invDet); localY = (y * d * invDet - x * c * invDet);
} }
public void LocalToWorld (float localX, float localY, out float worldX, out float worldY) { public void LocalToWorld (float localX, float localY, out float worldX, out float worldY) {
float x = localX, y = localY; float x = localX, y = localY;
worldX = x * a + y * b + this.worldX; worldX = x * a + y * b + this.worldX;
worldY = x * c + y * d + this.worldY; worldY = x * c + y * d + this.worldY;
} }
override public String ToString () { override public String ToString () {

View File

@ -137,8 +137,7 @@ namespace Spine {
if (Math.Abs(psx - psy) <= 0.0001f) { if (Math.Abs(psx - psy) <= 0.0001f) {
l2 *= psx; l2 *= psx;
float cos = (tx * tx + ty * ty - l1 * l1 - l2 * l2) / (2 * l1 * l2); float cos = (tx * tx + ty * ty - l1 * l1 - l2 * l2) / (2 * l1 * l2);
if (cos < -1) if (cos < -1) 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 * MathUtils.Sin(a2); float a = l1 + l2 * cos, o = l2 * MathUtils.Sin(a2);

View File

@ -151,8 +151,8 @@ namespace Spine {
transformConstraintData.bone = skeletonData.bones.Items[ReadInt(input, true)]; transformConstraintData.bone = skeletonData.bones.Items[ReadInt(input, true)];
transformConstraintData.target = skeletonData.bones.Items[ReadInt(input, true)]; transformConstraintData.target = skeletonData.bones.Items[ReadInt(input, true)];
transformConstraintData.translateMix = ReadFloat(input); transformConstraintData.translateMix = ReadFloat(input);
transformConstraintData.x = ReadFloat(input); transformConstraintData.x = ReadFloat(input) * scale;
transformConstraintData.y = ReadFloat(input); transformConstraintData.y = ReadFloat(input) * scale;
skeletonData.transformConstraints.Add(transformConstraintData); skeletonData.transformConstraints.Add(transformConstraintData);
} }

View File

@ -159,8 +159,8 @@ namespace Spine {
if (transformConstraintData.target == null) throw new Exception("Target bone not found: " + targetName); if (transformConstraintData.target == null) throw new Exception("Target bone not found: " + targetName);
transformConstraintData.translateMix = GetFloat(transformMap, "translateMix", 1); transformConstraintData.translateMix = GetFloat(transformMap, "translateMix", 1);
transformConstraintData.x = GetFloat(transformMap, "x", 0); transformConstraintData.x = GetFloat(transformMap, "x", 0) * scale;
transformConstraintData.y = GetFloat(transformMap, "y", 0); transformConstraintData.y = GetFloat(transformMap, "y", 0) * scale;
skeletonData.transformConstraints.Add(transformConstraintData); skeletonData.transformConstraints.Add(transformConstraintData);
} }

View File

@ -54,10 +54,8 @@ namespace Spine {
x = data.x; x = data.x;
y = data.y; y = data.y;
if (skeleton != null) { bone = skeleton.FindBone(data.bone.name);
bone = skeleton.FindBone(data.bone.name); target = skeleton.FindBone(data.target.name);
target = skeleton.FindBone(data.target.name);
}
} }
public void Update () { public void Update () {

View File

@ -78,6 +78,11 @@ public class Bone implements Updatable {
scaleY = bone.scaleY; scaleY = bone.scaleY;
} }
/** Same as {@link #updateWorldTransform()}. This method exists for Bone to implement {@link Updatable}. */
public void update () {
updateWorldTransform(x, y, rotation, scaleX, scaleY);
}
/** Computes the world SRT using the parent bone and this bone's local SRT. */ /** Computes the world SRT using the parent bone and this bone's local SRT. */
public void updateWorldTransform () { public void updateWorldTransform () {
updateWorldTransform(x, y, rotation, scaleX, scaleY); updateWorldTransform(x, y, rotation, scaleX, scaleY);
@ -127,22 +132,21 @@ public class Bone implements Updatable {
c = pc * la + pd * lc; c = pc * la + pd * lc;
d = pc * lb + pd * ld; d = pc * lb + pd * ld;
} else if (data.inheritRotation) { // No scale inheritance. } else if (data.inheritRotation) { // No scale inheritance.
Bone p = parent;
pa = 1; pa = 1;
pb = 0; pb = 0;
pc = 0; pc = 0;
pd = 1; pd = 1;
while (p != null) { do {
cos = MathUtils.cosDeg(p.appliedRotation); cos = MathUtils.cosDeg(parent.appliedRotation);
sin = MathUtils.sinDeg(p.appliedRotation); sin = MathUtils.sinDeg(parent.appliedRotation);
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;
temp = pc * cos + pd * sin; temp = pc * cos + pd * sin;
pd = pc * -sin + pd * cos; pd = pc * -sin + pd * cos;
pc = temp; pc = temp;
p = p.parent; parent = parent.parent;
} } while (parent != null);
a = pa * la + pb * lc; a = pa * la + pb * lc;
b = pa * lb + pb * ld; b = pa * lb + pb * ld;
c = pc * la + pd * lc; c = pc * la + pd * lc;
@ -156,16 +160,15 @@ public class Bone implements Updatable {
d = -d; d = -d;
} }
} else if (data.inheritScale) { // No rotation inheritance. } else if (data.inheritScale) { // No rotation inheritance.
Bone p = parent;
pa = 1; pa = 1;
pb = 0; pb = 0;
pc = 0; pc = 0;
pd = 1; pd = 1;
while (p != null) { do {
float r = p.rotation; float r = parent.rotation;
cos = MathUtils.cosDeg(r); cos = MathUtils.cosDeg(r);
sin = MathUtils.sinDeg(r); sin = MathUtils.sinDeg(r);
float psx = p.appliedScaleX, psy = p.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;
pb = pa * zb + pb * zd; pb = pa * zb + pb * zd;
@ -184,8 +187,8 @@ public class Bone implements Updatable {
pd = pc * -sin + pd * cos; pd = pc * -sin + pd * cos;
pc = temp; pc = temp;
p = p.parent; parent = parent.parent;
} } while (parent != null);
a = pa * la + pb * lc; a = pa * la + pb * lc;
b = pa * lb + pb * ld; b = pa * lb + pb * ld;
c = pc * la + pd * lc; c = pc * la + pd * lc;
@ -206,11 +209,6 @@ public class Bone implements Updatable {
} }
} }
/** Same as {@link #updateWorldTransform()}. This method exists for Bone to implement {@link Updatable}. */
public void update () {
updateWorldTransform(x, y, rotation, scaleX, scaleY);
}
public void setToSetupPose () { public void setToSetupPose () {
BoneData data = this.data; BoneData data = this.data;
x = data.x; x = data.x;

View File

@ -159,8 +159,8 @@ public class SkeletonJson {
if (transformConstraintData.target == null) throw new SerializationException("Target bone not found: " + targetName); if (transformConstraintData.target == null) throw new SerializationException("Target bone not found: " + targetName);
transformConstraintData.translateMix = transformMap.getFloat("translateMix", 1); transformConstraintData.translateMix = transformMap.getFloat("translateMix", 1);
transformConstraintData.x = transformMap.getFloat("x", 0); transformConstraintData.x = transformMap.getFloat("x", 0) * scale;
transformConstraintData.y = transformMap.getFloat("y", 0); transformConstraintData.y = transformMap.getFloat("y", 0) * scale;
skeletonData.transformConstraints.add(transformConstraintData); skeletonData.transformConstraints.add(transformConstraintData);
} }