Beispiel #1
0
        /// <summary>Adjusts the parent and child bone rotations so the tip of the child is as close to the target position as
        /// possible. The target is specified in the world coordinate system.</summary>
        /// <param name="child">Any descendant bone of the parent.</param>
        static public void apply(Bone parent, Bone child, float targetX, float targetY, int bendDirection, float alpha)
        {
            float childRotation = child.rotation, parentRotation = parent.rotation;

            if (alpha == 0)
            {
                child.rotationIK  = childRotation;
                parent.rotationIK = parentRotation;
                return;
            }
            float positionX, positionY;
            Bone  parentParent = parent.parent;

            if (parentParent != null)
            {
                parentParent.worldToLocal(targetX, targetY, out positionX, out positionY);
                targetX = (positionX - parent.x) * parentParent.worldScaleX;
                targetY = (positionY - parent.y) * parentParent.worldScaleY;
            }
            else
            {
                targetX -= parent.x;
                targetY -= parent.y;
            }
            if (child.parent == parent)
            {
                positionX = child.x;
                positionY = child.y;
            }
            else
            {
                child.parent.localToWorld(child.x, child.y, out positionX, out positionY);
                parent.worldToLocal(positionX, positionY, out positionX, out positionY);
            }
            float childX = positionX * parent.worldScaleX, childY = positionY * parent.worldScaleY;
            float offset = (float)Math.Atan2(childY, childX);
            float len1 = (float)Math.Sqrt(childX * childX + childY * childY), len2 = child.data.length * child.worldScaleX;
            // Based on code by Ryan Juckett with permission: Copyright (c) 2008-2009 Ryan Juckett, http://www.ryanjuckett.com/
            float cosDenom = 2 * len1 * len2;

            if (cosDenom < 0.0001f)
            {
                child.rotationIK = childRotation + ((float)Math.Atan2(targetY, targetX) * radDeg - parentRotation - childRotation)
                                   * alpha;
                return;
            }
            float cos = (targetX * targetX + targetY * targetY - len1 * len1 - len2 * len2) / cosDenom;

            if (cos < -1)
            {
                cos = -1;
            }
            else if (cos > 1)
            {
                cos = 1;
            }
            float childAngle = (float)Math.Acos(cos) * bendDirection;
            float adjacent = len1 + len2 * cos, opposite = len2 * (float)Math.Sin(childAngle);
            float parentAngle = (float)Math.Atan2(targetY * adjacent - targetX * opposite, targetX * adjacent + targetY * opposite);
            float rotation    = (parentAngle - offset) * radDeg - parentRotation;

            if (rotation > 180)
            {
                rotation -= 360;
            }
            else if (rotation < -180)             //
            {
                rotation += 360;
            }
            parent.rotationIK = parentRotation + rotation * alpha;
            rotation          = (childAngle + offset) * radDeg - childRotation;
            if (rotation > 180)
            {
                rotation -= 360;
            }
            else if (rotation < -180)             //
            {
                rotation += 360;
            }
            child.rotationIK = childRotation + (rotation + parent.worldRotation - child.parent.worldRotation) * alpha;
        }
Beispiel #2
0
 /// <summary>Adjusts the parent and child bone rotations so the tip of the child is as close to the target position as
 /// possible. The target is specified in the world coordinate system.</summary>
 /// <param name="child">Any descendant bone of the parent.</param>
 public static void apply(Bone parent, Bone child, float targetX, float targetY, int bendDirection, float alpha)
 {
     float childRotation = child.rotation, parentRotation = parent.rotation;
     if (alpha == 0) {
         child.rotationIK = childRotation;
         parent.rotationIK = parentRotation;
         return;
     }
     float positionX, positionY;
     Bone parentParent = parent.parent;
     if (parentParent != null) {
         parentParent.worldToLocal(targetX, targetY, out positionX, out positionY);
         targetX = (positionX - parent.x) * parentParent.worldScaleX;
         targetY = (positionY - parent.y) * parentParent.worldScaleY;
     } else {
         targetX -= parent.x;
         targetY -= parent.y;
     }
     if (child.parent == parent) {
         positionX = child.x;
         positionY = child.y;
     } else {
         child.parent.localToWorld(child.x, child.y, out positionX, out positionY);
         parent.worldToLocal(positionX, positionY, out positionX, out positionY);
     }
     float childX = positionX * parent.worldScaleX, childY = positionY * parent.worldScaleY;
     float offset = (float)Math.Atan2(childY, childX);
     float len1 = (float)Math.Sqrt(childX * childX + childY * childY), len2 = child.data.length * child.worldScaleX;
     // Based on code by Ryan Juckett with permission: Copyright (c) 2008-2009 Ryan Juckett, http://www.ryanjuckett.com/
     float cosDenom = 2 * len1 * len2;
     if (cosDenom < 0.0001f) {
         child.rotationIK = childRotation + ((float)Math.Atan2(targetY, targetX) * radDeg - parentRotation - childRotation)
             * alpha;
         return;
     }
     float cos = (targetX * targetX + targetY * targetY - len1 * len1 - len2 * len2) / cosDenom;
     if (cos < -1)
         cos = -1;
     else if (cos > 1)
         cos = 1;
     float childAngle = (float)Math.Acos(cos) * bendDirection;
     float adjacent = len1 + len2 * cos, opposite = len2 * (float)Math.Sin(childAngle);
     float parentAngle = (float)Math.Atan2(targetY * adjacent - targetX * opposite, targetX * adjacent + targetY * opposite);
     float rotation = (parentAngle - offset) * radDeg - parentRotation;
     if (rotation > 180)
         rotation -= 360;
     else if (rotation < -180) //
         rotation += 360;
     parent.rotationIK = parentRotation + rotation * alpha;
     rotation = (childAngle + offset) * radDeg - childRotation;
     if (rotation > 180)
         rotation -= 360;
     else if (rotation < -180) //
         rotation += 360;
     child.rotationIK = childRotation + (rotation + parent.worldRotation - child.parent.worldRotation) * alpha;
 }
Beispiel #3
0
        public static void apply(Bone parent, Bone child, float targetX, float targetY, int bendDirection, float alpha)
        {
            float rotation  = child.rotation;
            float rotation2 = parent.rotation;

            if (alpha == 0f)
            {
                child.rotationIK  = rotation;
                parent.rotationIK = rotation2;
                return;
            }
            Bone  parent2 = parent.parent;
            float x;
            float y;

            if (parent2 != null)
            {
                parent2.worldToLocal(targetX, targetY, out x, out y);
                targetX = (x - parent.x) * parent2.worldScaleX;
                targetY = (y - parent.y) * parent2.worldScaleY;
            }
            else
            {
                targetX -= parent.x;
                targetY -= parent.y;
            }
            if (child.parent == parent)
            {
                x = child.x;
                y = child.y;
            }
            else
            {
                child.parent.localToWorld(child.x, child.y, out x, out y);
                parent.worldToLocal(x, y, out x, out y);
            }
            float num  = x * parent.worldScaleX;
            float num2 = y * parent.worldScaleY;
            float num3 = (float)Math.Atan2((double)num2, (double)num);
            float num4 = (float)Math.Sqrt((double)(num * num + num2 * num2));
            float num5 = child.data.length * child.worldScaleX;
            float num6 = 2f * num4 * num5;

            if (num6 < 0.0001f)
            {
                child.rotationIK = rotation + ((float)Math.Atan2((double)targetY, (double)targetX) * 57.2957764f - rotation2 - rotation) * alpha;
                return;
            }
            float num7 = (targetX * targetX + targetY * targetY - num4 * num4 - num5 * num5) / num6;

            if (num7 < -1f)
            {
                num7 = -1f;
            }
            else if (num7 > 1f)
            {
                num7 = 1f;
            }
            float num8  = (float)Math.Acos((double)num7) * (float)bendDirection;
            float num9  = num4 + num5 * num7;
            float num10 = num5 * (float)Math.Sin((double)num8);
            float num11 = (float)Math.Atan2((double)(targetY * num9 - targetX * num10), (double)(targetX * num9 + targetY * num10));
            float num12 = (num11 - num3) * 57.2957764f - rotation2;

            if (num12 > 180f)
            {
                num12 -= 360f;
            }
            else if (num12 < -180f)
            {
                num12 += 360f;
            }
            parent.rotationIK = rotation2 + num12 * alpha;
            num12             = (num8 + num3) * 57.2957764f - rotation;
            if (num12 > 180f)
            {
                num12 -= 360f;
            }
            else if (num12 < -180f)
            {
                num12 += 360f;
            }
            child.rotationIK = rotation + (num12 + parent.worldRotation - child.parent.worldRotation) * alpha;
        }