IkConstraint.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. #include <spine/IkConstraint.h>
  31. #include <spine/Skeleton.h>
  32. #include <spine/extension.h>
  33. #include <float.h>
  34. spIkConstraint *spIkConstraint_create(spIkConstraintData *data, const spSkeleton *skeleton) {
  35. int i;
  36. spIkConstraint *self = NEW(spIkConstraint);
  37. CONST_CAST(spIkConstraintData*, self->data) = data;
  38. self->bendDirection = data->bendDirection;
  39. self->mix = data->mix;
  40. self->bonesCount = self->data->bonesCount;
  41. self->bones = MALLOC(spBone*, self->bonesCount);
  42. for (i = 0; i < self->bonesCount; ++i)
  43. self->bones[i] = spSkeleton_findBone(skeleton, self->data->bones[i]->name);
  44. self->target = spSkeleton_findBone(skeleton, self->data->target->name);
  45. return self;
  46. }
  47. void spIkConstraint_dispose(spIkConstraint *self) {
  48. FREE(self->bones);
  49. FREE(self);
  50. }
  51. void spIkConstraint_apply(spIkConstraint *self) {
  52. switch (self->bonesCount) {
  53. case 1:
  54. spIkConstraint_apply1(self->bones[0], self->target->worldX, self->target->worldY, self->mix);
  55. break;
  56. case 2:
  57. spIkConstraint_apply2(self->bones[0], self->bones[1], self->target->worldX, self->target->worldY, self->bendDirection, self->mix);
  58. break;
  59. }
  60. }
  61. void spIkConstraint_apply1 (spBone* bone, float targetX, float targetY, float alpha) {
  62. spBone* p = bone->parent;
  63. float id, x, y, tx, ty, rotationIK;
  64. if (!bone->appliedValid) spBone_updateAppliedTransform(bone);
  65. id = 1 / (p->a * p->d - p->b * p->c);
  66. x = targetX - p->worldX, y = targetY - p->worldY;
  67. tx = (x * p->d - y * p->b) * id - bone->ax; ty = (y * p->a - x * p->c) * id - bone->ay;
  68. rotationIK = ATAN2(ty, tx) * RAD_DEG - bone->ashearX - bone->arotation;
  69. if (bone->ascaleX < 0) rotationIK += 180;
  70. if (rotationIK > 180) rotationIK -= 360;
  71. else if (rotationIK < -180) rotationIK += 360;
  72. spBone_updateWorldTransformWith(bone, bone->ax, bone->ay, bone->arotation + rotationIK * alpha, bone->ascaleX,
  73. bone->ascaleY, bone->ashearX, bone->ashearY);
  74. }
  75. void spIkConstraint_apply2 (spBone* parent, spBone* child, float targetX, float targetY, int bendDir, float alpha) {
  76. float px, py, psx, psy;
  77. float cx, cy, csx, cwx, cwy;
  78. int o1, o2, s2, u;
  79. spBone* pp = parent->parent;
  80. float tx, ty, dx, dy, l1, l2, a1, a2, r;
  81. float id, x, y;
  82. if (alpha == 0) {
  83. spBone_updateWorldTransform(child);
  84. return;
  85. }
  86. if (!parent->appliedValid) spBone_updateAppliedTransform(parent);
  87. if (!child->appliedValid) spBone_updateAppliedTransform(child);
  88. px = parent->ax; py = parent->ay; psx = parent->ascaleX; psy = parent->ascaleY; csx = child->ascaleX;
  89. if (psx < 0) {
  90. psx = -psx;
  91. o1 = 180;
  92. s2 = -1;
  93. } else {
  94. o1 = 0;
  95. s2 = 1;
  96. }
  97. if (psy < 0) {
  98. psy = -psy;
  99. s2 = -s2;
  100. }
  101. if (csx < 0) {
  102. csx = -csx;
  103. o2 = 180;
  104. } else
  105. o2 = 0;
  106. r = psx - psy;
  107. cx = child->ax;
  108. u = (r < 0 ? -r : r) <= 0.0001f;
  109. if (!u) {
  110. cy = 0;
  111. cwx = parent->a * cx + parent->worldX;
  112. cwy = parent->c * cx + parent->worldY;
  113. } else {
  114. cy = child->ay;
  115. cwx = parent->a * cx + parent->b * cy + parent->worldX;
  116. cwy = parent->c * cx + parent->d * cy + parent->worldY;
  117. }
  118. id = 1 / (pp->a * pp->d - pp->b * pp->c);
  119. x = targetX - pp->worldX;
  120. y = targetY - pp->worldY;
  121. tx = (x * pp->d - y * pp->b) * id - px;
  122. ty = (y * pp->a - x * pp->c) * id - py;
  123. x = cwx - pp->worldX;
  124. y = cwy - pp->worldY;
  125. dx = (x * pp->d - y * pp->b) * id - px;
  126. dy = (y * pp->a - x * pp->c) * id - py;
  127. l1 = SQRT(dx * dx + dy * dy);
  128. l2 = child->data->length * csx;
  129. if (u) {
  130. float cosine, a, b;
  131. l2 *= psx;
  132. cosine = (tx * tx + ty * ty - l1 * l1 - l2 * l2) / (2 * l1 * l2);
  133. if (cosine < -1) cosine = -1;
  134. else if (cosine > 1) cosine = 1;
  135. a2 = ACOS(cosine) * bendDir;
  136. a = l1 + l2 * cosine;
  137. b = l2 * SIN(a2);
  138. a1 = ATAN2(ty * a - tx * b, tx * a + ty * b);
  139. } else {
  140. float a = psx * l2, b = psy * l2;
  141. float aa = a * a, bb = b * b, ll = l1 * l1, dd = tx * tx + ty * ty, ta = ATAN2(ty, tx);
  142. float c0 = bb * ll + aa * dd - aa * bb, c1 = -2 * bb * l1, c2 = bb - aa;
  143. float d = c1 * c1 - 4 * c2 * c0;
  144. float minAngle = 0, minDist = FLT_MAX, minX = 0, minY = 0;
  145. float maxAngle = 0, maxDist = 0, maxX = 0, maxY = 0;
  146. float x = l1 + a, dist = x * x, angle, y;
  147. if (d >= 0) {
  148. float q = SQRT(d), r0, r1;
  149. if (c1 < 0) q = -q;
  150. q = -(c1 + q) / 2;
  151. r0 = q / c2; r1 = c0 / q;
  152. r = ABS(r0) < ABS(r1) ? r0 : r1;
  153. if (r * r <= dd) {
  154. y = SQRT(dd - r * r) * bendDir;
  155. a1 = ta - ATAN2(y, r);
  156. a2 = ATAN2(y / psy, (r - l1) / psx);
  157. goto outer;
  158. }
  159. }
  160. if (dist > maxDist) {
  161. maxAngle = 0;
  162. maxDist = dist;
  163. maxX = x;
  164. }
  165. x = l1 - a;
  166. dist = x * x;
  167. if (dist < minDist) {
  168. minAngle = PI;
  169. minDist = dist;
  170. minX = x;
  171. }
  172. angle = ACOS(-a * l1 / (aa - bb));
  173. x = a * COS(angle) + l1;
  174. y = b * SIN(angle);
  175. dist = x * x + y * y;
  176. if (dist < minDist) {
  177. minAngle = angle;
  178. minDist = dist;
  179. minX = x;
  180. minY = y;
  181. }
  182. if (dist > maxDist) {
  183. maxAngle = angle;
  184. maxDist = dist;
  185. maxX = x;
  186. maxY = y;
  187. }
  188. if (dd <= (minDist + maxDist) / 2) {
  189. a1 = ta - ATAN2(minY * bendDir, minX);
  190. a2 = minAngle * bendDir;
  191. } else {
  192. a1 = ta - ATAN2(maxY * bendDir, maxX);
  193. a2 = maxAngle * bendDir;
  194. }
  195. }
  196. outer: {
  197. float os = ATAN2(cy, cx) * s2;
  198. a1 = (a1 - os) * RAD_DEG + o1 - parent->arotation;
  199. if (a1 > 180) a1 -= 360;
  200. else if (a1 < -180) a1 += 360;
  201. spBone_updateWorldTransformWith(parent, px, py, parent->rotation + a1 * alpha, parent->ascaleX, parent->ascaleY, 0, 0);
  202. a2 = ((a2 + os) * RAD_DEG - child->ashearX) * s2 + o2 - child->arotation;
  203. if (a2 > 180) a2 -= 360;
  204. else if (a2 < -180) a2 += 360;
  205. spBone_updateWorldTransformWith(child, cx, cy, child->arotation + a2 * alpha, child->ascaleX, child->ascaleY, child->ashearX, child->ashearY);
  206. }
  207. }