Bone.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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/Bone.h>
  31. #include <spine/extension.h>
  32. #include <stdio.h>
  33. static int yDown;
  34. void spBone_setYDown (int value) {
  35. yDown = value;
  36. }
  37. int spBone_isYDown () {
  38. return yDown;
  39. }
  40. spBone* spBone_create (spBoneData* data, spSkeleton* skeleton, spBone* parent) {
  41. spBone* self = NEW(spBone);
  42. CONST_CAST(spBoneData*, self->data) = data;
  43. CONST_CAST(spSkeleton*, self->skeleton) = skeleton;
  44. CONST_CAST(spBone*, self->parent) = parent;
  45. spBone_setToSetupPose(self);
  46. return self;
  47. }
  48. void spBone_dispose (spBone* self) {
  49. FREE(self->children);
  50. FREE(self);
  51. }
  52. void spBone_updateWorldTransform (spBone* self) {
  53. spBone_updateWorldTransformWith(self, self->x, self->y, self->rotation, self->scaleX, self->scaleY, self->shearX, self->shearY);
  54. }
  55. void spBone_updateWorldTransformWith (spBone* self, float x, float y, float rotation, float scaleX, float scaleY, float shearX, float shearY) {
  56. float cosine, sine;
  57. float pa, pb, pc, pd;
  58. spBone* parent = self->parent;
  59. self->ax = x;
  60. self->ay = y;
  61. self->arotation = rotation;
  62. self->ascaleX = scaleX;
  63. self->ascaleY = scaleY;
  64. self->ashearX = shearX;
  65. self->ashearY = shearY;
  66. self->appliedValid = 1;
  67. if (!parent) { /* Root bone. */
  68. float rotationY = rotation + 90 + shearY;
  69. float la = COS_DEG(rotation + shearX) * scaleX;
  70. float lb = COS_DEG(rotationY) * scaleY;
  71. float lc = SIN_DEG(rotation + shearX) * scaleX;
  72. float ld = SIN_DEG(rotationY) * scaleY;
  73. if (self->skeleton->flipX) {
  74. x = -x;
  75. la = -la;
  76. lb = -lb;
  77. }
  78. if (self->skeleton->flipY != yDown) {
  79. y = -y;
  80. lc = -lc;
  81. ld = -ld;
  82. }
  83. CONST_CAST(float, self->a) = la;
  84. CONST_CAST(float, self->b) = lb;
  85. CONST_CAST(float, self->c) = lc;
  86. CONST_CAST(float, self->d) = ld;
  87. CONST_CAST(float, self->worldX) = x + self->skeleton->x;
  88. CONST_CAST(float, self->worldY) = y + self->skeleton->y;
  89. return;
  90. }
  91. pa = parent->a;
  92. pb = parent->b;
  93. pc = parent->c;
  94. pd = parent->d;
  95. CONST_CAST(float, self->worldX) = pa * x + pb * y + parent->worldX;
  96. CONST_CAST(float, self->worldY) = pc * x + pd * y + parent->worldY;
  97. switch (self->data->transformMode) {
  98. case SP_TRANSFORMMODE_NORMAL: {
  99. float rotationY = rotation + 90 + shearY;
  100. float la = COS_DEG(rotation + shearX) * scaleX;
  101. float lb = COS_DEG(rotationY) * scaleY;
  102. float lc = SIN_DEG(rotation + shearX) * scaleX;
  103. float ld = SIN_DEG(rotationY) * scaleY;
  104. CONST_CAST(float, self->a) = pa * la + pb * lc;
  105. CONST_CAST(float, self->b) = pa * lb + pb * ld;
  106. CONST_CAST(float, self->c) = pc * la + pd * lc;
  107. CONST_CAST(float, self->d) = pc * lb + pd * ld;
  108. return;
  109. }
  110. case SP_TRANSFORMMODE_ONLYTRANSLATION: {
  111. float rotationY = rotation + 90 + shearY;
  112. CONST_CAST(float, self->a) = COS_DEG(rotation + shearX) * scaleX;
  113. CONST_CAST(float, self->b) = COS_DEG(rotationY) * scaleY;
  114. CONST_CAST(float, self->c) = SIN_DEG(rotation + shearX) * scaleX;
  115. CONST_CAST(float, self->d) = SIN_DEG(rotationY) * scaleY;
  116. break;
  117. }
  118. case SP_TRANSFORMMODE_NOROTATIONORREFLECTION: {
  119. float s = pa * pa + pc * pc;
  120. float prx, rx, ry, la, lb, lc, ld;
  121. if (s > 0.0001f) {
  122. s = ABS(pa * pd - pb * pc) / s;
  123. pb = pc * s;
  124. pd = pa * s;
  125. prx = ATAN2(pc, pa) * RAD_DEG;
  126. } else {
  127. pa = 0;
  128. pc = 0;
  129. prx = 90 - ATAN2(pd, pb) * RAD_DEG;
  130. }
  131. rx = rotation + shearX - prx;
  132. ry = rotation + shearY - prx + 90;
  133. la = COS_DEG(rx) * scaleX;
  134. lb = COS_DEG(ry) * scaleY;
  135. lc = SIN_DEG(rx) * scaleX;
  136. ld = SIN_DEG(ry) * scaleY;
  137. CONST_CAST(float, self->a) = pa * la - pb * lc;
  138. CONST_CAST(float, self->b) = pa * lb - pb * ld;
  139. CONST_CAST(float, self->c) = pc * la + pd * lc;
  140. CONST_CAST(float, self->d) = pc * lb + pd * ld;
  141. break;
  142. }
  143. case SP_TRANSFORMMODE_NOSCALE:
  144. case SP_TRANSFORMMODE_NOSCALEORREFLECTION: {
  145. float za, zc, s;
  146. float r, zb, zd, la, lb, lc, ld;
  147. cosine = COS_DEG(rotation); sine = SIN_DEG(rotation);
  148. za = pa * cosine + pb * sine;
  149. zc = pc * cosine + pd * sine;
  150. s = SQRT(za * za + zc * zc);
  151. if (s > 0.00001f) s = 1 / s;
  152. za *= s;
  153. zc *= s;
  154. s = SQRT(za * za + zc * zc);
  155. r = PI / 2 + atan2(zc, za);
  156. zb = COS(r) * s;
  157. zd = SIN(r) * s;
  158. la = COS_DEG(shearX) * scaleX;
  159. lb = COS_DEG(90 + shearY) * scaleY;
  160. lc = SIN_DEG(shearX) * scaleX;
  161. ld = SIN_DEG(90 + shearY) * scaleY;
  162. CONST_CAST(float, self->a) = za * la + zb * lc;
  163. CONST_CAST(float, self->b) = za * lb + zb * ld;
  164. CONST_CAST(float, self->c) = zc * la + zd * lc;
  165. CONST_CAST(float, self->d) = zc * lb + zd * ld;
  166. if (self->data->transformMode != SP_TRANSFORMMODE_NOSCALEORREFLECTION ? pa * pd - pb * pc < 0 : self->skeleton->flipX != self->skeleton->flipY) {
  167. CONST_CAST(float, self->b) = -self->b;
  168. CONST_CAST(float, self->d) = -self->d;
  169. }
  170. return;
  171. }
  172. }
  173. if (self->skeleton->flipX) {
  174. CONST_CAST(float, self->a) = -self->a;
  175. CONST_CAST(float, self->b) = -self->b;
  176. }
  177. if (self->skeleton->flipY != yDown) {
  178. CONST_CAST(float, self->c) = -self->c;
  179. CONST_CAST(float, self->d) = -self->d;
  180. }
  181. }
  182. void spBone_setToSetupPose (spBone* self) {
  183. self->x = self->data->x;
  184. self->y = self->data->y;
  185. self->rotation = self->data->rotation;
  186. self->scaleX = self->data->scaleX;
  187. self->scaleY = self->data->scaleY;
  188. self->shearX = self->data->shearX;
  189. self->shearY = self->data->shearY;
  190. }
  191. float spBone_getWorldRotationX (spBone* self) {
  192. return ATAN2(self->c, self->a) * RAD_DEG;
  193. }
  194. float spBone_getWorldRotationY (spBone* self) {
  195. return ATAN2(self->d, self->b) * RAD_DEG;
  196. }
  197. float spBone_getWorldScaleX (spBone* self) {
  198. return SQRT(self->a * self->a + self->c * self->c);
  199. }
  200. float spBone_getWorldScaleY (spBone* self) {
  201. return SQRT(self->b * self->b + self->d * self->d);
  202. }
  203. float spBone_worldToLocalRotationX (spBone* self) {
  204. spBone* parent = self->parent;
  205. if (!parent) return self->arotation;
  206. return ATAN2(parent->a * self->c - parent->c * self->a, parent->d * self->a - parent->b * self->c) * RAD_DEG;
  207. }
  208. float spBone_worldToLocalRotationY (spBone* self) {
  209. spBone* parent = self->parent;
  210. if (!parent) return self->arotation;
  211. return ATAN2(parent->a * self->d - parent->c * self->b, parent->d * self->b - parent->b * self->d) * RAD_DEG;
  212. }
  213. void spBone_rotateWorld (spBone* self, float degrees) {
  214. float a = self->a, b = self->b, c = self->c, d = self->d;
  215. float cosine = COS_DEG(degrees), sine = SIN_DEG(degrees);
  216. CONST_CAST(float, self->a) = cosine * a - sine * c;
  217. CONST_CAST(float, self->b) = cosine * b - sine * d;
  218. CONST_CAST(float, self->c) = sine * a + cosine * c;
  219. CONST_CAST(float, self->d) = sine * b + cosine * d;
  220. CONST_CAST(int, self->appliedValid) = 1;
  221. }
  222. /** Computes the individual applied transform values from the world transform. This can be useful to perform processing using
  223. * the applied transform after the world transform has been modified directly (eg, by a constraint).
  224. * <p>
  225. * Some information is ambiguous in the world transform, such as -1,-1 scale versus 180 rotation. */
  226. void spBone_updateAppliedTransform (spBone* self) {
  227. spBone* parent = self->parent;
  228. self->appliedValid = 1;
  229. if (!parent) {
  230. self->ax = self->worldX;
  231. self->ay = self->worldY;
  232. self->arotation = ATAN2(self->c, self->a) * RAD_DEG;
  233. self->ascaleX = SQRT(self->a * self->a + self->c * self->c);
  234. self->ascaleY = SQRT(self->b * self->b + self->d * self->d);
  235. self->ashearX = 0;
  236. self->ashearY = ATAN2(self->a * self->b + self->c * self->d, self->a * self->d - self->b * self->c) * RAD_DEG;
  237. } else {
  238. float pa = parent->a, pb = parent->b, pc = parent->c, pd = parent->d;
  239. float pid = 1 / (pa * pd - pb * pc);
  240. float dx = self->worldX - parent->worldX, dy = self->worldY - parent->worldY;
  241. float ia = pid * pd;
  242. float id = pid * pa;
  243. float ib = pid * pb;
  244. float ic = pid * pc;
  245. float ra = ia * self->a - ib * self->c;
  246. float rb = ia * self->b - ib * self->d;
  247. float rc = id * self->c - ic * self->a;
  248. float rd = id * self->d - ic * self->b;
  249. self->ax = (dx * pd * pid - dy * pb * pid);
  250. self->ay = (dy * pa * pid - dx * pc * pid);
  251. self->ashearX = 0;
  252. self->ascaleX = SQRT(ra * ra + rc * rc);
  253. if (self->ascaleX > 0.0001f) {
  254. float det = ra * rd - rb * rc;
  255. self->ascaleY = det / self->ascaleX;
  256. self->ashearY = ATAN2(ra * rb + rc * rd, det) * RAD_DEG;
  257. self->arotation = ATAN2(rc, ra) * RAD_DEG;
  258. } else {
  259. self->ascaleX = 0;
  260. self->ascaleY = SQRT(rb * rb + rd * rd);
  261. self->ashearY = 0;
  262. self->arotation = 90 - ATAN2(rd, rb) * RAD_DEG;
  263. }
  264. }
  265. }
  266. void spBone_worldToLocal (spBone* self, float worldX, float worldY, float* localX, float* localY) {
  267. float a = self->a, b = self->b, c = self->c, d = self->d;
  268. float invDet = 1 / (a * d - b * c);
  269. float x = worldX - self->worldX, y = worldY - self->worldY;
  270. *localX = (x * d * invDet - y * b * invDet);
  271. *localY = (y * a * invDet - x * c * invDet);
  272. }
  273. void spBone_localToWorld (spBone* self, float localX, float localY, float* worldX, float* worldY) {
  274. float x = localX, y = localY;
  275. *worldX = x * self->a + y * self->b + self->worldX;
  276. *worldY = x * self->c + y * self->d + self->worldY;
  277. }