Răsfoiți Sursa

[cpp] Bone length fix for stretchy IK.

badlogic 7 ani în urmă
părinte
comite
b4d71f68c2
1 a modificat fișierele cu 3 adăugiri și 3 ștergeri
  1. 3 3
      spine-cpp/spine-cpp/src/spine/IkConstraint.cpp

+ 3 - 3
spine-cpp/spine-cpp/src/spine/IkConstraint.cpp

@@ -54,8 +54,8 @@ void IkConstraint::apply(Bone &bone, float targetX, float targetY, bool stretch,
 	else if (rotationIK < -180) rotationIK += 360;
 	float sx = bone._ascaleX;
 	if (stretch) {
-		float dd = MathUtil::sqrt(tx * tx + ty * ty);
-		if (dd > bone._data.getLength() * sx) sx *= (dd / (bone._data.getLength() * sx) - 1) * alpha + 1;
+		float b = bone._data.getLength() * sx, dd = MathUtil::sqrt(tx * tx + ty * ty);
+		if (dd > b && b > 0.0001f) sx *= (dd / b - 1) * alpha + 1;
 	}
 	bone.updateWorldTransform(bone._ax, bone._ay, bone._arotation + rotationIK * alpha, sx,
 							  bone._ascaleY, bone._ashearX, bone._ashearY);
@@ -128,7 +128,7 @@ void IkConstraint::apply(Bone &parent, Bone &child, float targetX, float targetY
 		if (cosine < -1) cosine = -1;
 		else if (cosine > 1) {
 			cosine = 1;
-			if (stretch) sx *= (MathUtil::sqrt(dd) / (l1 + l2) - 1) * alpha + 1;
+			if (stretch && l1 + l2 > 0.0001f) sx *= (MathUtil::sqrt(dd) / (l1 + l2) - 1) * alpha + 1;
 		}
 		a2 = MathUtil::acos(cosine) * bendDir;
 		a = l1 + l2 * cosine;