Skip to content

Commit

Permalink
Fixed single bone IK with flipping and y-up coordinate systems.
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanSweet committed Jan 30, 2015
1 parent ee849c6 commit 028769f
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 5 deletions.
4 changes: 3 additions & 1 deletion spine-as3/spine-as3/src/spine/IkConstraint.as
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 3 additions & 1 deletion spine-c/src/spine/IkConstraint.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 3 additions & 1 deletion spine-csharp/src/IkConstraint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ override public String ToString () {
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;
}

Expand Down
4 changes: 3 additions & 1 deletion spine-js/spine.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion spine-lua/IkConstraint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 028769f

Please sign in to comment.