Fixed single bone IK with flipping and y-up coordinate systems.

This commit is contained in:
NathanSweet 2015-01-30 17:59:11 +01:00
parent ee849c62eb
commit 028769f54f
5 changed files with 17 additions and 5 deletions

View File

@ -77,7 +77,9 @@ public class IkConstraint {
static public function apply1 (bone:Bone, targetX:Number, targetY:Number, alpha:Number) : void { 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 parentRotation:Number = (!bone._data.inheritRotation || bone._parent == null) ? 0 : bone._parent._worldRotation;
var rotation:Number = bone.rotation; 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; bone.rotationIK = rotation + (rotationIK - rotation) * alpha;
} }

View File

@ -69,7 +69,9 @@ void spIkConstraint_apply (spIkConstraint* self) {
void spIkConstraint_apply1 (spBone* bone, float targetX, float targetY, float alpha) { void spIkConstraint_apply1 (spBone* bone, float targetX, float targetY, float alpha) {
float parentRotation = (!bone->data->inheritRotation || !bone->parent) ? 0 : bone->parent->worldRotation; float parentRotation = (!bone->data->inheritRotation || !bone->parent) ? 0 : bone->parent->worldRotation;
float rotation = bone->rotation; 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; bone->rotationIK = rotation + (rotationIK - rotation) * alpha;
} }

View File

@ -82,7 +82,9 @@ namespace Spine {
static public void apply (Bone bone, float targetX, float targetY, float alpha) { 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 parentRotation = (!bone.data.inheritRotation || bone.parent == null) ? 0 : bone.parent.worldRotation;
float rotation = bone.rotation; 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; bone.rotationIK = rotation + (rotationIK - rotation) * alpha;
} }

View File

@ -226,7 +226,9 @@ spine.IkConstraint.prototype = {
spine.IkConstraint.apply1 = function (bone, targetX, targetY, alpha) { spine.IkConstraint.apply1 = function (bone, targetX, targetY, alpha) {
var parentRotation = (!bone.data.inheritRotation || !bone.parent) ? 0 : bone.parent.worldRotation; var parentRotation = (!bone.data.inheritRotation || !bone.parent) ? 0 : bone.parent.worldRotation;
var rotation = bone.rotation; 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; 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 /** Adjusts the parent and child bone rotations so the tip of the child is as close to the target position as possible. The

View File

@ -72,7 +72,11 @@ function IkConstraint.apply1 (bone, targetX, targetY, alpha)
parentRotation = bone.parent.worldRotation parentRotation = bone.parent.worldRotation
end end
local rotation = bone.rotation 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 bone.rotationIK = rotation + (rotationIK - rotation) * alpha
end end