Skip to content

Commit

Permalink
[csharp] Use native Math lib for IK accuracy.
Browse files Browse the repository at this point in the history
  • Loading branch information
pharan authored Mar 23, 2017
1 parent a8f1159 commit 8cde9cf
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions spine-csharp/src/IkConstraint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ static public void Apply (Bone bone, float targetX, float targetY, float alpha)
float id = 1 / (p.a * p.d - p.b * p.c);
float x = targetX - p.worldX, y = targetY - p.worldY;
float tx = (x * p.d - y * p.b) * id - bone.ax, ty = (y * p.a - x * p.c) * id - bone.ay;
float rotationIK = MathUtils.Atan2(ty, tx) * MathUtils.RadDeg - bone.ashearX - bone.arotation;
float rotationIK = (float)Math.Atan2(ty, tx) * MathUtils.RadDeg - bone.ashearX - bone.arotation;
if (bone.ascaleX < 0) rotationIK += 180;
if (rotationIK > 180)
rotationIK -= 360;
Expand Down Expand Up @@ -156,12 +156,12 @@ static public void Apply (Bone parent, Bone child, float targetX, float targetY,
else if (cos > 1) cos = 1;
a2 = (float)Math.Acos(cos) * bendDir;
a = l1 + l2 * cos;
b = l2 * MathUtils.Sin(a2);
a1 = MathUtils.Atan2(ty * a - tx * b, tx * a + ty * b);
b = l2 * (float)Math.Sin(a2);
a1 = (float)Math.Atan2(ty * a - tx * b, tx * a + ty * b);
} else {
a = psx * l2;
b = psy * l2;
float aa = a * a, bb = b * b, dd = tx * tx + ty * ty, ta = MathUtils.Atan2(ty, tx);
float aa = a * a, bb = b * b, dd = tx * tx + ty * ty, ta = (float)Math.Atan2(ty, tx);
c = bb * l1 * l1 + aa * dd - aa * bb;
float c1 = -2 * bb * l1, c2 = bb - aa;
d = c1 * c1 - 4 * c2 * c;
Expand All @@ -173,8 +173,8 @@ static public void Apply (Bone parent, Bone child, float targetX, float targetY,
float r = Math.Abs(r0) < Math.Abs(r1) ? r0 : r1;
if (r * r <= dd) {
y = (float)Math.Sqrt(dd - r * r) * bendDir;
a1 = ta - MathUtils.Atan2(y, r);
a2 = MathUtils.Atan2(y / psy, (r - l1) / psx);
a1 = ta - (float)Math.Atan2(y, r);
a2 = (float)Math.Atan2(y / psy, (r - l1) / psx);
goto outer;
}
}
Expand All @@ -190,13 +190,13 @@ static public void Apply (Bone parent, Bone child, float targetX, float targetY,
x = l1 - a;
d = x * x;
if (d < minDist) {
minAngle = MathUtils.PI;
minAngle = (float)Math.PI;
minDist = d;
minX = x;
}
float angle = (float)Math.Acos(-a * l1 / (aa - bb));
x = a * MathUtils.Cos(angle) + l1;
y = b * MathUtils.Sin(angle);
x = a * (float)Math.Cos(angle) + l1;
y = b * (float)Math.Sin(angle);
d = x * x + y * y;
if (d < minDist) {
minAngle = angle;
Expand All @@ -211,15 +211,15 @@ static public void Apply (Bone parent, Bone child, float targetX, float targetY,
maxY = y;
}
if (dd <= (minDist + maxDist) / 2) {
a1 = ta - MathUtils.Atan2(minY * bendDir, minX);
a1 = ta - (float)Math.Atan2(minY * bendDir, minX);
a2 = minAngle * bendDir;
} else {
a1 = ta - MathUtils.Atan2(maxY * bendDir, maxX);
a1 = ta - (float)Math.Atan2(maxY * bendDir, maxX);
a2 = maxAngle * bendDir;
}
}
outer:
float os = MathUtils.Atan2(cy, cx) * s2;
float os = (float)Math.Atan2(cy, cx) * s2;
float rotation = parent.arotation;
a1 = (a1 - os) * MathUtils.RadDeg + os1 - rotation;
if (a1 > 180)
Expand Down

0 comments on commit 8cde9cf

Please sign in to comment.