Browse Source

[csharp] Fixed differences in TransformConstraint between csharp and ref-impl, potential bug fixed. See #1294.

Harald Csaszar 6 years ago
parent
commit
7d646316a7
1 changed files with 47 additions and 19 deletions
  1. 47 19
      spine-csharp/src/TransformConstraint.cs

+ 47 - 19
spine-csharp/src/TransformConstraint.cs

@@ -31,21 +31,19 @@
 using System;
 using System;
 
 
 namespace Spine {
 namespace Spine {
+	/// <summary>
+	/// <para>
+	/// Stores the current pose for a transform constraint. A transform constraint adjusts the world transform of the constrained
+	/// bones to match that of the target bone.</para>
+	/// <para>
+	/// See <a href="http://esotericsoftware.com/spine-transform-constraints">Transform constraints</a> in the Spine User Guide.</para>
+	/// </summary>
 	public class TransformConstraint : IConstraint {
 	public class TransformConstraint : IConstraint {
 		internal TransformConstraintData data;
 		internal TransformConstraintData data;
 		internal ExposedList<Bone> bones;
 		internal ExposedList<Bone> bones;
 		internal Bone target;
 		internal Bone target;
 		internal float rotateMix, translateMix, scaleMix, shearMix;
 		internal float rotateMix, translateMix, scaleMix, shearMix;
 
 
-		public TransformConstraintData Data { get { return data; } }
-		public int Order { get { return data.order; } }
-		public ExposedList<Bone> Bones { get { return bones; } }
-		public Bone Target { get { return target; } set { target = value; } }
-		public float RotateMix { get { return rotateMix; } set { rotateMix = value; } }
-		public float TranslateMix { get { return translateMix; } set { translateMix = value; } }
-		public float ScaleMix { get { return scaleMix; } set { scaleMix = value; } }
-		public float ShearMix { get { return shearMix; } set { shearMix = value; } }
-
 		public TransformConstraint (TransformConstraintData data, Skeleton skeleton) {
 		public TransformConstraint (TransformConstraintData data, Skeleton skeleton) {
 			if (data == null) throw new ArgumentNullException("data", "data cannot be null.");
 			if (data == null) throw new ArgumentNullException("data", "data cannot be null.");
 			if (skeleton == null) throw new ArgumentNullException("skeleton", "skeleton cannot be null.");
 			if (skeleton == null) throw new ArgumentNullException("skeleton", "skeleton cannot be null.");
@@ -57,11 +55,27 @@ namespace Spine {
 
 
 			bones = new ExposedList<Bone>();
 			bones = new ExposedList<Bone>();
 			foreach (BoneData boneData in data.bones)
 			foreach (BoneData boneData in data.bones)
-				bones.Add (skeleton.FindBone (boneData.name));
+				bones.Add (skeleton.FindBone(boneData.name));
 
 
 			target = skeleton.FindBone(data.target.name);
 			target = skeleton.FindBone(data.target.name);
 		}
 		}
 
 
+		/// <summary>Copy constructor.</summary>
+		public TransformConstraint (TransformConstraint constraint, Skeleton skeleton) {
+			if (constraint == null) throw new ArgumentNullException("constraint cannot be null.");
+			if (skeleton == null) throw new ArgumentNullException("skeleton cannot be null.");
+			data = constraint.data;
+			bones = new ExposedList<Bone>(constraint.Bones.Count);
+			foreach (Bone bone in constraint.Bones)
+				bones.Add(skeleton.Bones.Items[bone.data.index]);
+			target = skeleton.Bones.Items[constraint.target.data.index];
+			rotateMix = constraint.rotateMix;
+			translateMix = constraint.translateMix;
+			scaleMix = constraint.scaleMix;
+			shearMix = constraint.shearMix;
+		}
+
+		/// <summary>Applies the constraint to the constrained bones.</summary>
 		public void Apply () {
 		public void Apply () {
 			Update();
 			Update();
 		}
 		}
@@ -116,13 +130,11 @@ namespace Spine {
 
 
 				if (scaleMix > 0) {
 				if (scaleMix > 0) {
 					float s = (float)Math.Sqrt(bone.a * bone.a + bone.c * bone.c);
 					float s = (float)Math.Sqrt(bone.a * bone.a + bone.c * bone.c);
-					//float ts = (float)Math.sqrt(ta * ta + tc * tc);
-					if (s > 0.00001f) s = (s + ((float)Math.Sqrt(ta * ta + tc * tc) - s + data.offsetScaleX) * scaleMix) / s;
+					if (s != 0) s = (s + ((float)Math.Sqrt(ta * ta + tc * tc) - s + data.offsetScaleX) * scaleMix) / s;
 					bone.a *= s;
 					bone.a *= s;
 					bone.c *= s;
 					bone.c *= s;
 					s = (float)Math.Sqrt(bone.b * bone.b + bone.d * bone.d);
 					s = (float)Math.Sqrt(bone.b * bone.b + bone.d * bone.d);
-					//ts = (float)Math.Sqrt(tb * tb + td * td);
-					if (s > 0.00001f) s = (s + ((float)Math.Sqrt(tb * tb + td * td) - s + data.offsetScaleY) * scaleMix) / s;
+					if (s != 0) s = (s + ((float)Math.Sqrt(tb * tb + td * td) - s + data.offsetScaleY) * scaleMix) / s;
 					bone.b *= s;
 					bone.b *= s;
 					bone.d *= s;
 					bone.d *= s;
 					modified = true;
 					modified = true;
@@ -231,15 +243,15 @@ namespace Spine {
 
 
 				float scaleX = bone.ascaleX, scaleY = bone.ascaleY;
 				float scaleX = bone.ascaleX, scaleY = bone.ascaleY;
 				if (scaleMix != 0) {
 				if (scaleMix != 0) {
-					if (scaleX > 0.00001f) scaleX = (scaleX + (target.ascaleX - scaleX + data.offsetScaleX) * scaleMix) / scaleX;
-					if (scaleY > 0.00001f) scaleY = (scaleY + (target.ascaleY - scaleY + data.offsetScaleY) * scaleMix) / scaleY;
+					if (scaleX != 0) scaleX = (scaleX + (target.ascaleX - scaleX + data.offsetScaleX) * scaleMix) / scaleX;
+					if (scaleY != 0) scaleY = (scaleY + (target.ascaleY - scaleY + data.offsetScaleY) * scaleMix) / scaleY;
 				}
 				}
 
 
 				float shearY = bone.ashearY;
 				float shearY = bone.ashearY;
 				if (shearMix != 0) {
 				if (shearMix != 0) {
 					float r = target.ashearY - shearY + data.offsetShearY;
 					float r = target.ashearY - shearY + data.offsetShearY;
 					r -= (16384 - (int)(16384.499999999996 - r / 360)) * 360;
 					r -= (16384 - (int)(16384.499999999996 - r / 360)) * 360;
-					bone.shearY += r * shearMix;
+					shearY += r * shearMix;
 				}
 				}
 
 
 				bone.UpdateWorldTransform(x, y, rotation, scaleX, scaleY, bone.ashearX, shearY);
 				bone.UpdateWorldTransform(x, y, rotation, scaleX, scaleY, bone.ashearX, shearY);
@@ -266,8 +278,8 @@ namespace Spine {
 
 
 				float scaleX = bone.ascaleX, scaleY = bone.ascaleY;
 				float scaleX = bone.ascaleX, scaleY = bone.ascaleY;
 				if (scaleMix != 0) {
 				if (scaleMix != 0) {
-					if (scaleX > 0.00001f) scaleX *= ((target.ascaleX - 1 + data.offsetScaleX) * scaleMix) + 1;
-					if (scaleY > 0.00001f) scaleY *= ((target.ascaleY - 1 + data.offsetScaleY) * scaleMix) + 1;
+					scaleX *= ((target.ascaleX - 1 + data.offsetScaleX) * scaleMix) + 1;
+					scaleY *= ((target.ascaleY - 1 + data.offsetScaleY) * scaleMix) + 1;
 				}
 				}
 
 
 				float shearY = bone.ashearY;
 				float shearY = bone.ashearY;
@@ -277,6 +289,22 @@ namespace Spine {
 			}
 			}
 		}
 		}
 
 
+		public int Order { get { return data.order; } }
+		/// <summary>The bones that will be modified by this transform constraint.</summary>
+		public ExposedList<Bone> Bones { get { return bones; } }
+		/// <summary>The target bone whose world transform will be copied to the constrained bones.</summary>
+		public Bone Target { get { return target; } set { target = value; } }
+		/// <summary>A percentage (0-1) that controls the mix between the constrained and unconstrained rotations.</summary>
+		public float RotateMix { get { return rotateMix; } set { rotateMix = value; } }
+		/// <summary>A percentage (0-1) that controls the mix between the constrained and unconstrained translations.</summary>
+		public float TranslateMix { get { return translateMix; } set { translateMix = value; } }
+		/// <summary>A percentage (0-1) that controls the mix between the constrained and unconstrained scales.</summary>
+		public float ScaleMix { get { return scaleMix; } set { scaleMix = value; } }
+		/// <summary>A percentage (0-1) that controls the mix between the constrained and unconstrained scales.</summary>
+		public float ShearMix { get { return shearMix; } set { shearMix = value; } }
+		/// <summary>The transform constraint's setup pose data.</summary>
+		public TransformConstraintData Data { get { return data; } }
+
 		override public string ToString () {
 		override public string ToString () {
 			return data.name;
 			return data.name;
 		}
 		}