Эх сурвалжийг харах

[c][cpp] Port of commit e04e7b5. Adjusted fix for IK constraint NaN when parent has zero scale.

Mario Zechner 2 жил өмнө
parent
commit
1fb89b6e89

+ 8 - 6
spine-c/spine-c/src/spine/IkConstraint.c

@@ -94,10 +94,13 @@ void spIkConstraint_apply1(spBone *bone, float targetX, float targetY, int /*boo
 		default: {
 		default: {
 			float x = targetX - p->worldX, y = targetY - p->worldY;
 			float x = targetX - p->worldX, y = targetY - p->worldY;
 			float d = pa * pd - pb * pc;
 			float d = pa * pd - pb * pc;
-            if (d > 0) d = MAX(0.0001f, d);
-            else d = MIN(-0.0001f, d);
-			tx = (x * pd - y * pb) / d - bone->ax;
-			ty = (y * pa - x * pc) / d - bone->ay;
+            if (ABS(d) <= 0.0001f) {
+                tx = 0;
+                ty = 0;
+            } else {
+                tx = (x * pd - y * pb) / d - bone->ax;
+                ty = (y * pa - x * pc) / d - bone->ay;
+            }
 		}
 		}
 	}
 	}
 	rotationIK += ATAN2(ty, tx) * RAD_DEG;
 	rotationIK += ATAN2(ty, tx) * RAD_DEG;
@@ -180,8 +183,7 @@ void spIkConstraint_apply2(spBone *parent, spBone *child, float targetX, float t
 	c = pp->c;
 	c = pp->c;
 	d = pp->d;
 	d = pp->d;
 	id = a * d - b * c;
 	id = a * d - b * c;
-    if (id > 0) id = 1 / MAX(0.0001f, id);
-    else id = 1 / MIN(-0.0001f, id);
+    id = ABS(id) <= 0.0001f ? 0 : 1 / id;
 	x = cwx - pp->worldX;
 	x = cwx - pp->worldX;
 	y = cwy - pp->worldY;
 	y = cwy - pp->worldY;
 	dx = (x * d - y * b) * id - px;
 	dx = (x * d - y * b) * id - px;

+ 8 - 6
spine-cpp/spine-cpp/src/spine/IkConstraint.cpp

@@ -61,10 +61,13 @@ void IkConstraint::apply(Bone &bone, float targetX, float targetY, bool compress
 		default:
 		default:
 			float x = targetX - p->_worldX, y = targetY - p->_worldY;
 			float x = targetX - p->_worldX, y = targetY - p->_worldY;
 			float d = pa * pd - pb * pc;
 			float d = pa * pd - pb * pc;
-            if (d > 0) d = MathUtil::max(0.0001f, d);
-            d = MathUtil::min(-0.0001f, d);
-			tx = (x * pd - y * pb) / d - bone._ax;
-			ty = (y * pa - x * pc) / d - bone._ay;
+            if (MathUtil::abs(d) <= 0.0001f) {
+                tx = 0;
+                ty = 0;
+            } else {
+                tx = (x * pd - y * pb) / d - bone._ax;
+                ty = (y * pa - x * pc) / d - bone._ay;
+            }
 	}
 	}
 	rotationIK += MathUtil::atan2(ty, tx) * MathUtil::Rad_Deg;
 	rotationIK += MathUtil::atan2(ty, tx) * MathUtil::Rad_Deg;
 	if (bone._ascaleX < 0) rotationIK += 180;
 	if (bone._ascaleX < 0) rotationIK += 180;
@@ -143,8 +146,7 @@ void IkConstraint::apply(Bone &parent, Bone &child, float targetX, float targetY
 	c = pp->_c;
 	c = pp->_c;
 	d = pp->_d;
 	d = pp->_d;
 	id = a * d - b * c;
 	id = a * d - b * c;
-    if (id > 0) MathUtil::max(0.0001f, id);
-    else MathUtil::min(-0.0001f, id);
+    id = MathUtil::abs(id) <= 0.0001f ? 0 : 1 / id;
     x = cwx - pp->_worldX;
     x = cwx - pp->_worldX;
 	y = cwy - pp->_worldY;
 	y = cwy - pp->_worldY;
 	dx = (x * d - y * b) * id - px;
 	dx = (x * d - y * b) * id - px;