From 028769f54f66fcdabc369f59b94ac18c51801d99 Mon Sep 17 00:00:00 2001 From: NathanSweet Date: Fri, 30 Jan 2015 17:59:11 +0100 Subject: [PATCH] Fixed single bone IK with flipping and y-up coordinate systems. --- spine-as3/spine-as3/src/spine/IkConstraint.as | 4 +++- spine-c/src/spine/IkConstraint.c | 4 +++- spine-csharp/src/IkConstraint.cs | 4 +++- spine-js/spine.js | 4 +++- spine-lua/IkConstraint.lua | 6 +++++- 5 files changed, 17 insertions(+), 5 deletions(-) diff --git a/spine-as3/spine-as3/src/spine/IkConstraint.as b/spine-as3/spine-as3/src/spine/IkConstraint.as index 7e754b6d0..f51070e0c 100644 --- a/spine-as3/spine-as3/src/spine/IkConstraint.as +++ b/spine-as3/spine-as3/src/spine/IkConstraint.as @@ -77,7 +77,9 @@ public class IkConstraint { static public function apply1 (bone:Bone, targetX:Number, targetY:Number, alpha:Number) : void { var parentRotation:Number = (!bone._data.inheritRotation || bone._parent == null) ? 0 : bone._parent._worldRotation; var rotation:Number = bone.rotation; - var rotationIK:Number = Math.atan2(targetY - bone._worldY, targetX - bone._worldX) * radDeg - parentRotation; + var rotationIK:Number = Math.atan2(targetY - bone._worldY, targetX - bone._worldX) * radDeg; + if (bone._worldFlipX != (bone._worldFlipY != Bone.yDown)) rotationIK = -rotationIK; + rotationIK -= parentRotation; bone.rotationIK = rotation + (rotationIK - rotation) * alpha; } diff --git a/spine-c/src/spine/IkConstraint.c b/spine-c/src/spine/IkConstraint.c index d23ada807..106b5ae51 100644 --- a/spine-c/src/spine/IkConstraint.c +++ b/spine-c/src/spine/IkConstraint.c @@ -69,7 +69,9 @@ void spIkConstraint_apply (spIkConstraint* self) { void spIkConstraint_apply1 (spBone* bone, float targetX, float targetY, float alpha) { float parentRotation = (!bone->data->inheritRotation || !bone->parent) ? 0 : bone->parent->worldRotation; float rotation = bone->rotation; - float rotationIK = ATAN2(targetY - bone->worldY, targetX - bone->worldX) * RAD_DEG - parentRotation; + float rotationIK = ATAN2(targetY - bone->worldY, targetX - bone->worldX) * RAD_DEG; + if (bone->worldFlipX != (bone->worldFlipY != yDown)) rotationIK = -rotationIK; + rotationIK -= parentRotation; bone->rotationIK = rotation + (rotationIK - rotation) * alpha; } diff --git a/spine-csharp/src/IkConstraint.cs b/spine-csharp/src/IkConstraint.cs index 6261e083a..a2816e796 100644 --- a/spine-csharp/src/IkConstraint.cs +++ b/spine-csharp/src/IkConstraint.cs @@ -82,7 +82,9 @@ namespace Spine { static public void apply (Bone bone, float targetX, float targetY, float alpha) { float parentRotation = (!bone.data.inheritRotation || bone.parent == null) ? 0 : bone.parent.worldRotation; float rotation = bone.rotation; - float rotationIK = (float)Math.Atan2(targetY - bone.worldY, targetX - bone.worldX) * radDeg - parentRotation; + float rotationIK = (float)Math.Atan2(targetY - bone.worldY, targetX - bone.worldX) * radDeg; + if (bone.worldFlipX != (bone.worldFlipY != Bone.yDown)) rotationIK = -rotationIK; + rotationIK -= parentRotation; bone.rotationIK = rotation + (rotationIK - rotation) * alpha; } diff --git a/spine-js/spine.js b/spine-js/spine.js index 522cc3f72..215b5c89e 100644 --- a/spine-js/spine.js +++ b/spine-js/spine.js @@ -226,7 +226,9 @@ spine.IkConstraint.prototype = { spine.IkConstraint.apply1 = function (bone, targetX, targetY, alpha) { var parentRotation = (!bone.data.inheritRotation || !bone.parent) ? 0 : bone.parent.worldRotation; var rotation = bone.rotation; - var rotationIK = Math.atan2(targetY - bone.worldY, targetX - bone.worldX) * spine.radDeg - parentRotation; + var rotationIK = Math.atan2(targetY - bone.worldY, targetX - bone.worldX) * spine.radDeg; + if (bone.worldFlipX != (bone.worldFlipY != spine.Bone.yDown)) rotationIK = -rotationIK; + rotationIK -= parentRotation; bone.rotationIK = rotation + (rotationIK - rotation) * alpha; }; /** Adjusts the parent and child bone rotations so the tip of the child is as close to the target position as possible. The diff --git a/spine-lua/IkConstraint.lua b/spine-lua/IkConstraint.lua index 7a23187b5..96317bdc8 100644 --- a/spine-lua/IkConstraint.lua +++ b/spine-lua/IkConstraint.lua @@ -72,7 +72,11 @@ function IkConstraint.apply1 (bone, targetX, targetY, alpha) parentRotation = bone.parent.worldRotation end local rotation = bone.rotation - local rotationIK = math.atan2(targetY - bone.worldY, targetX - bone.worldX) * radDeg - parentRotation + local rotationIK = math.atan2(targetY - bone.worldY, targetX - bone.worldX) * radDeg + if bone.worldFlipX ~= bone.worldFlipY then + rotationIK = -rotationIK + end + rotationIK = rotationIK - parentRotation bone.rotationIK = rotation + (rotationIK - rotation) * alpha end