源战役客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 line
8.5 KiB

  1. /******************************************************************************
  2. * Spine Runtimes Software License v2.5
  3. *
  4. * Copyright (c) 2013-2016, Esoteric Software
  5. * All rights reserved.
  6. *
  7. * You are granted a perpetual, non-exclusive, non-sublicensable, and
  8. * non-transferable license to use, install, execute, and perform the Spine
  9. * Runtimes software and derivative works solely for personal or internal
  10. * use. Without the written permission of Esoteric Software (see Section 2 of
  11. * the Spine Software License Agreement), you may not (a) modify, translate,
  12. * adapt, or develop new applications using the Spine Runtimes or otherwise
  13. * create derivative works or improvements of the Spine Runtimes or (b) remove,
  14. * delete, alter, or obscure any trademarks or any copyright, trademark, patent,
  15. * or other intellectual property or proprietary rights notices on or in the
  16. * Software, including any copy thereof. Redistributions in binary or source
  17. * form must include this license and terms.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  22. * EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
  25. * USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *****************************************************************************/
  30. using System;
  31. namespace Spine {
  32. public class IkConstraint : IConstraint {
  33. internal IkConstraintData data;
  34. internal ExposedList<Bone> bones = new ExposedList<Bone>();
  35. internal Bone target;
  36. internal float mix;
  37. internal int bendDirection;
  38. public IkConstraintData Data { get { return data; } }
  39. public int Order { get { return data.order; } }
  40. public ExposedList<Bone> Bones { get { return bones; } }
  41. public Bone Target { get { return target; } set { target = value; } }
  42. public int BendDirection { get { return bendDirection; } set { bendDirection = value; } }
  43. public float Mix { get { return mix; } set { mix = value; } }
  44. public IkConstraint (IkConstraintData data, Skeleton skeleton) {
  45. if (data == null) throw new ArgumentNullException("data", "data cannot be null.");
  46. if (skeleton == null) throw new ArgumentNullException("skeleton", "skeleton cannot be null.");
  47. this.data = data;
  48. mix = data.mix;
  49. bendDirection = data.bendDirection;
  50. bones = new ExposedList<Bone>(data.bones.Count);
  51. foreach (BoneData boneData in data.bones)
  52. bones.Add(skeleton.FindBone(boneData.name));
  53. target = skeleton.FindBone(data.target.name);
  54. }
  55. /// <summary>Applies the constraint to the constrained bones.</summary>
  56. public void Apply () {
  57. Update();
  58. }
  59. public void Update () {
  60. Bone target = this.target;
  61. ExposedList<Bone> bones = this.bones;
  62. switch (bones.Count) {
  63. case 1:
  64. Apply(bones.Items[0], target.worldX, target.worldY, mix);
  65. break;
  66. case 2:
  67. Apply(bones.Items[0], bones.Items[1], target.worldX, target.worldY, bendDirection, mix);
  68. break;
  69. }
  70. }
  71. override public string ToString () {
  72. return data.name;
  73. }
  74. /// <summary>Adjusts the bone rotation so the tip is as close to the target position as possible. The target is specified
  75. /// in the world coordinate system.</summary>
  76. static public void Apply (Bone bone, float targetX, float targetY, float alpha) {
  77. if (!bone.appliedValid) bone.UpdateAppliedTransform();
  78. Bone p = bone.parent;
  79. float id = 1 / (p.a * p.d - p.b * p.c);
  80. float x = targetX - p.worldX, y = targetY - p.worldY;
  81. float tx = (x * p.d - y * p.b) * id - bone.ax, ty = (y * p.a - x * p.c) * id - bone.ay;
  82. float rotationIK = (float)Math.Atan2(ty, tx) * MathUtils.RadDeg - bone.ashearX - bone.arotation;
  83. if (bone.ascaleX < 0) rotationIK += 180;
  84. if (rotationIK > 180)
  85. rotationIK -= 360;
  86. else if (rotationIK < -180) rotationIK += 360;
  87. bone.UpdateWorldTransform(bone.ax, bone.ay, bone.arotation + rotationIK * alpha, bone.ascaleX, bone.ascaleY, bone.ashearX,
  88. bone.ashearY);
  89. }
  90. /// <summary>Adjusts the parent and child bone rotations so the tip of the child is as close to the target position as
  91. /// possible. The target is specified in the world coordinate system.</summary>
  92. /// <param name="child">A direct descendant of the parent bone.</param>
  93. static public void Apply (Bone parent, Bone child, float targetX, float targetY, int bendDir, float alpha) {
  94. if (alpha == 0) {
  95. child.UpdateWorldTransform ();
  96. return;
  97. }
  98. //float px = parent.x, py = parent.y, psx = parent.scaleX, psy = parent.scaleY, csx = child.scaleX;
  99. if (!parent.appliedValid) parent.UpdateAppliedTransform();
  100. if (!child.appliedValid) child.UpdateAppliedTransform();
  101. float px = parent.ax, py = parent.ay, psx = parent.ascaleX, psy = parent.ascaleY, csx = child.ascaleX;
  102. int os1, os2, s2;
  103. if (psx < 0) {
  104. psx = -psx;
  105. os1 = 180;
  106. s2 = -1;
  107. } else {
  108. os1 = 0;
  109. s2 = 1;
  110. }
  111. if (psy < 0) {
  112. psy = -psy;
  113. s2 = -s2;
  114. }
  115. if (csx < 0) {
  116. csx = -csx;
  117. os2 = 180;
  118. } else
  119. os2 = 0;
  120. float cx = child.ax, cy, cwx, cwy, a = parent.a, b = parent.b, c = parent.c, d = parent.d;
  121. bool u = Math.Abs(psx - psy) <= 0.0001f;
  122. if (!u) {
  123. cy = 0;
  124. cwx = a * cx + parent.worldX;
  125. cwy = c * cx + parent.worldY;
  126. } else {
  127. cy = child.ay;
  128. cwx = a * cx + b * cy + parent.worldX;
  129. cwy = c * cx + d * cy + parent.worldY;
  130. }
  131. Bone pp = parent.parent;
  132. a = pp.a;
  133. b = pp.b;
  134. c = pp.c;
  135. d = pp.d;
  136. float id = 1 / (a * d - b * c), x = targetX - pp.worldX, y = targetY - pp.worldY;
  137. float tx = (x * d - y * b) * id - px, ty = (y * a - x * c) * id - py;
  138. x = cwx - pp.worldX;
  139. y = cwy - pp.worldY;
  140. float dx = (x * d - y * b) * id - px, dy = (y * a - x * c) * id - py;
  141. float l1 = (float)Math.Sqrt(dx * dx + dy * dy), l2 = child.data.length * csx, a1, a2;
  142. if (u) {
  143. l2 *= psx;
  144. float cos = (tx * tx + ty * ty - l1 * l1 - l2 * l2) / (2 * l1 * l2);
  145. if (cos < -1)
  146. cos = -1;
  147. else if (cos > 1) cos = 1;
  148. a2 = (float)Math.Acos(cos) * bendDir;
  149. a = l1 + l2 * cos;
  150. b = l2 * (float)Math.Sin(a2);
  151. a1 = (float)Math.Atan2(ty * a - tx * b, tx * a + ty * b);
  152. } else {
  153. a = psx * l2;
  154. b = psy * l2;
  155. float aa = a * a, bb = b * b, dd = tx * tx + ty * ty, ta = (float)Math.Atan2(ty, tx);
  156. c = bb * l1 * l1 + aa * dd - aa * bb;
  157. float c1 = -2 * bb * l1, c2 = bb - aa;
  158. d = c1 * c1 - 4 * c2 * c;
  159. if (d >= 0) {
  160. float q = (float)Math.Sqrt(d);
  161. if (c1 < 0) q = -q;
  162. q = -(c1 + q) / 2;
  163. float r0 = q / c2, r1 = c / q;
  164. float r = Math.Abs(r0) < Math.Abs(r1) ? r0 : r1;
  165. if (r * r <= dd) {
  166. y = (float)Math.Sqrt(dd - r * r) * bendDir;
  167. a1 = ta - (float)Math.Atan2(y, r);
  168. a2 = (float)Math.Atan2(y / psy, (r - l1) / psx);
  169. goto break_outer; // break outer;
  170. }
  171. }
  172. float minAngle = MathUtils.PI, minX = l1 - a, minDist = minX * minX, minY = 0;
  173. float maxAngle = 0, maxX = l1 + a, maxDist = maxX * maxX, maxY = 0;
  174. c = -a * l1 / (aa - bb);
  175. if (c >= -1 && c <= 1) {
  176. c = (float)Math.Acos(c);
  177. x = a * (float)Math.Cos(c) + l1;
  178. y = b * (float)Math.Sin(c);
  179. d = x * x + y * y;
  180. if (d < minDist) {
  181. minAngle = c;
  182. minDist = d;
  183. minX = x;
  184. minY = y;
  185. }
  186. if (d > maxDist) {
  187. maxAngle = c;
  188. maxDist = d;
  189. maxX = x;
  190. maxY = y;
  191. }
  192. }
  193. if (dd <= (minDist + maxDist) / 2) {
  194. a1 = ta - (float)Math.Atan2(minY * bendDir, minX);
  195. a2 = minAngle * bendDir;
  196. } else {
  197. a1 = ta - (float)Math.Atan2(maxY * bendDir, maxX);
  198. a2 = maxAngle * bendDir;
  199. }
  200. }
  201. break_outer:
  202. float os = (float)Math.Atan2(cy, cx) * s2;
  203. float rotation = parent.arotation;
  204. a1 = (a1 - os) * MathUtils.RadDeg + os1 - rotation;
  205. if (a1 > 180)
  206. a1 -= 360;
  207. else if (a1 < -180) a1 += 360;
  208. parent.UpdateWorldTransform(px, py, rotation + a1 * alpha, parent.scaleX, parent.ascaleY, 0, 0);
  209. rotation = child.arotation;
  210. a2 = ((a2 + os) * MathUtils.RadDeg - child.ashearX) * s2 + os2 - rotation;
  211. if (a2 > 180)
  212. a2 -= 360;
  213. else if (a2 < -180) a2 += 360;
  214. child.UpdateWorldTransform(cx, cy, rotation + a2 * alpha, child.ascaleX, child.ascaleY, child.ashearX, child.ashearY);
  215. }
  216. }
  217. }