Bone.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. #ifdef SPINE_UE4
  30. #include "SpinePluginPrivatePCH.h"
  31. #endif
  32. #include <spine/Bone.h>
  33. #include <spine/BoneData.h>
  34. #include <spine/Skeleton.h>
  35. using namespace spine;
  36. RTTI_IMPL(Bone, Updatable)
  37. bool Bone::yDown = false;
  38. void Bone::setYDown(bool inValue) {
  39. yDown = inValue;
  40. }
  41. bool Bone::isYDown() {
  42. return yDown;
  43. }
  44. Bone::Bone(BoneData &data, Skeleton &skeleton, Bone *parent) : Updatable(),
  45. _data(data),
  46. _skeleton(skeleton),
  47. _parent(parent),
  48. _x(0),
  49. _y(0),
  50. _rotation(0),
  51. _scaleX(0),
  52. _scaleY(0),
  53. _shearX(0),
  54. _shearY(0),
  55. _ax(0),
  56. _ay(0),
  57. _arotation(0),
  58. _ascaleX(0),
  59. _ascaleY(0),
  60. _ashearX(0),
  61. _ashearY(0),
  62. _appliedValid(false),
  63. _a(1),
  64. _b(0),
  65. _worldX(0),
  66. _c(0),
  67. _d(1),
  68. _worldY(0),
  69. _sorted(false),
  70. _active(false)
  71. {
  72. setToSetupPose();
  73. }
  74. void Bone::update() {
  75. updateWorldTransform(_x, _y, _rotation, _scaleX, _scaleY, _shearX, _shearY);
  76. }
  77. void Bone::updateWorldTransform() {
  78. updateWorldTransform(_x, _y, _rotation, _scaleX, _scaleY, _shearX, _shearY);
  79. }
  80. void Bone::updateWorldTransform(float x, float y, float rotation, float scaleX, float scaleY, float shearX, float shearY) {
  81. float cosine, sine;
  82. float pa, pb, pc, pd;
  83. Bone *parent = _parent;
  84. _ax = x;
  85. _ay = y;
  86. _arotation = rotation;
  87. _ascaleX = scaleX;
  88. _ascaleY = scaleY;
  89. _ashearX = shearX;
  90. _ashearY = shearY;
  91. _appliedValid = true;
  92. if (!parent) { /* Root bone. */
  93. float rotationY = rotation + 90 + shearY;
  94. float sx = _skeleton.getScaleX();
  95. float sy = _skeleton.getScaleY();
  96. _a = MathUtil::cosDeg(rotation + shearX) * scaleX * sx;
  97. _b = MathUtil::cosDeg(rotationY) * scaleY * sx;
  98. _c = MathUtil::sinDeg(rotation + shearX) * scaleX * sy;
  99. _d = MathUtil::sinDeg(rotationY) * scaleY * sy;
  100. _worldX = x * sx + _skeleton.getX();
  101. _worldY = y * sy + _skeleton.getY();
  102. return;
  103. }
  104. pa = parent->_a;
  105. pb = parent->_b;
  106. pc = parent->_c;
  107. pd = parent->_d;
  108. _worldX = pa * x + pb * y + parent->_worldX;
  109. _worldY = pc * x + pd * y + parent->_worldY;
  110. switch (_data.getTransformMode()) {
  111. case TransformMode_Normal: {
  112. float rotationY = rotation + 90 + shearY;
  113. float la = MathUtil::cosDeg(rotation + shearX) * scaleX;
  114. float lb = MathUtil::cosDeg(rotationY) * scaleY;
  115. float lc = MathUtil::sinDeg(rotation + shearX) * scaleX;
  116. float ld = MathUtil::sinDeg(rotationY) * scaleY;
  117. _a = pa * la + pb * lc;
  118. _b = pa * lb + pb * ld;
  119. _c = pc * la + pd * lc;
  120. _d = pc * lb + pd * ld;
  121. return;
  122. }
  123. case TransformMode_OnlyTranslation: {
  124. float rotationY = rotation + 90 + shearY;
  125. _a = MathUtil::cosDeg(rotation + shearX) * scaleX;
  126. _b = MathUtil::cosDeg(rotationY) * scaleY;
  127. _c = MathUtil::sinDeg(rotation + shearX) * scaleX;
  128. _d = MathUtil::sinDeg(rotationY) * scaleY;
  129. break;
  130. }
  131. case TransformMode_NoRotationOrReflection: {
  132. float s = pa * pa + pc * pc;
  133. float prx, rx, ry, la, lb, lc, ld;
  134. if (s > 0.0001f) {
  135. s = MathUtil::abs(pa * pd - pb * pc) / s;
  136. pb = pc * s;
  137. pd = pa * s;
  138. prx = MathUtil::atan2(pc, pa) * MathUtil::Rad_Deg;
  139. } else {
  140. pa = 0;
  141. pc = 0;
  142. prx = 90 - MathUtil::atan2(pd, pb) * MathUtil::Rad_Deg;
  143. }
  144. rx = rotation + shearX - prx;
  145. ry = rotation + shearY - prx + 90;
  146. la = MathUtil::cosDeg(rx) * scaleX;
  147. lb = MathUtil::cosDeg(ry) * scaleY;
  148. lc = MathUtil::sinDeg(rx) * scaleX;
  149. ld = MathUtil::sinDeg(ry) * scaleY;
  150. _a = pa * la - pb * lc;
  151. _b = pa * lb - pb * ld;
  152. _c = pc * la + pd * lc;
  153. _d = pc * lb + pd * ld;
  154. break;
  155. }
  156. case TransformMode_NoScale:
  157. case TransformMode_NoScaleOrReflection: {
  158. float za, zc, s;
  159. float r, zb, zd, la, lb, lc, ld;
  160. cosine = MathUtil::cosDeg(rotation);
  161. sine = MathUtil::sinDeg(rotation);
  162. za = (pa * cosine + pb * sine) / _skeleton.getScaleX();
  163. zc = (pc * cosine + pd * sine) / _skeleton.getScaleY();
  164. s = MathUtil::sqrt(za * za + zc * zc);
  165. if (s > 0.00001f) s = 1 / s;
  166. za *= s;
  167. zc *= s;
  168. s = MathUtil::sqrt(za * za + zc * zc);
  169. if (_data.getTransformMode() == TransformMode_NoScale
  170. && (pa * pd - pb * pc < 0) != (_skeleton.getScaleX() < 0 != _skeleton.getScaleY() < 0))
  171. s = -s;
  172. r = MathUtil::Pi / 2 + MathUtil::atan2(zc, za);
  173. zb = MathUtil::cos(r) * s;
  174. zd = MathUtil::sin(r) * s;
  175. la = MathUtil::cosDeg(shearX) * scaleX;
  176. lb = MathUtil::cosDeg(90 + shearY) * scaleY;
  177. lc = MathUtil::sinDeg(shearX) * scaleX;
  178. ld = MathUtil::sinDeg(90 + shearY) * scaleY;
  179. _a = za * la + zb * lc;
  180. _b = za * lb + zb * ld;
  181. _c = zc * la + zd * lc;
  182. _d = zc * lb + zd * ld;
  183. break;
  184. }
  185. }
  186. _a *= _skeleton.getScaleX();
  187. _b *= _skeleton.getScaleX();
  188. _c *= _skeleton.getScaleY();
  189. _d *= _skeleton.getScaleY();
  190. }
  191. void Bone::setToSetupPose() {
  192. BoneData &data = _data;
  193. _x = data.getX();
  194. _y = data.getY();
  195. _rotation = data.getRotation();
  196. _scaleX = data.getScaleX();
  197. _scaleY = data.getScaleY();
  198. _shearX = data.getShearX();
  199. _shearY = data.getShearY();
  200. }
  201. void Bone::worldToLocal(float worldX, float worldY, float &outLocalX, float &outLocalY) {
  202. float a = _a;
  203. float b = _b;
  204. float c = _c;
  205. float d = _d;
  206. float invDet = 1 / (a * d - b * c);
  207. float x = worldX - _worldX;
  208. float y = worldY - _worldY;
  209. outLocalX = (x * d * invDet - y * b * invDet);
  210. outLocalY = (y * a * invDet - x * c * invDet);
  211. }
  212. void Bone::localToWorld(float localX, float localY, float &outWorldX, float &outWorldY) {
  213. outWorldX = localX * _a + localY * _b + _worldX;
  214. outWorldY = localX * _c + localY * _d + _worldY;
  215. }
  216. float Bone::worldToLocalRotation(float worldRotation) {
  217. float sin = MathUtil::sinDeg(worldRotation);
  218. float cos = MathUtil::cosDeg(worldRotation);
  219. return MathUtil::atan2(_a * sin - _c * cos, _d * cos - _b * sin) * MathUtil::Rad_Deg + this->_rotation - this->_shearX;
  220. }
  221. float Bone::localToWorldRotation(float localRotation) {
  222. localRotation -= this->_rotation - this->_shearX;
  223. float sin = MathUtil::sinDeg(localRotation);
  224. float cos = MathUtil::cosDeg(localRotation);
  225. return MathUtil::atan2(cos * _c + sin * _d, cos * _a + sin * _b) * MathUtil::Rad_Deg;
  226. }
  227. void Bone::rotateWorld(float degrees) {
  228. float a = _a;
  229. float b = _b;
  230. float c = _c;
  231. float d = _d;
  232. float cos = MathUtil::cosDeg(degrees);
  233. float sin = MathUtil::sinDeg(degrees);
  234. _a = cos * a - sin * c;
  235. _b = cos * b - sin * d;
  236. _c = sin * a + cos * c;
  237. _d = sin * b + cos * d;
  238. _appliedValid = false;
  239. }
  240. float Bone::getWorldToLocalRotationX() {
  241. Bone *parent = _parent;
  242. if (!parent) {
  243. return _arotation;
  244. }
  245. float pa = parent->_a;
  246. float pb = parent->_b;
  247. float pc = parent->_c;
  248. float pd = parent->_d;
  249. float a = _a;
  250. float c = _c;
  251. return MathUtil::atan2(pa * c - pc * a, pd * a - pb * c) * MathUtil::Rad_Deg;
  252. }
  253. float Bone::getWorldToLocalRotationY() {
  254. Bone *parent = _parent;
  255. if (!parent) {
  256. return _arotation;
  257. }
  258. float pa = parent->_a;
  259. float pb = parent->_b;
  260. float pc = parent->_c;
  261. float pd = parent->_d;
  262. float b = _b;
  263. float d = _d;
  264. return MathUtil::atan2(pa * d - pc * b, pd * b - pb * d) * MathUtil::Rad_Deg;
  265. }
  266. BoneData &Bone::getData() {
  267. return _data;
  268. }
  269. Skeleton &Bone::getSkeleton() {
  270. return _skeleton;
  271. }
  272. Bone *Bone::getParent() {
  273. return _parent;
  274. }
  275. Vector<Bone *> &Bone::getChildren() {
  276. return _children;
  277. }
  278. float Bone::getX() {
  279. return _x;
  280. }
  281. void Bone::setX(float inValue) {
  282. _x = inValue;
  283. }
  284. float Bone::getY() {
  285. return _y;
  286. }
  287. void Bone::setY(float inValue) {
  288. _y = inValue;
  289. }
  290. float Bone::getRotation() {
  291. return _rotation;
  292. }
  293. void Bone::setRotation(float inValue) {
  294. _rotation = inValue;
  295. }
  296. float Bone::getScaleX() {
  297. return _scaleX;
  298. }
  299. void Bone::setScaleX(float inValue) {
  300. _scaleX = inValue;
  301. }
  302. float Bone::getScaleY() {
  303. return _scaleY;
  304. }
  305. void Bone::setScaleY(float inValue) {
  306. _scaleY = inValue;
  307. }
  308. float Bone::getShearX() {
  309. return _shearX;
  310. }
  311. void Bone::setShearX(float inValue) {
  312. _shearX = inValue;
  313. }
  314. float Bone::getShearY() {
  315. return _shearY;
  316. }
  317. void Bone::setShearY(float inValue) {
  318. _shearY = inValue;
  319. }
  320. float Bone::getAppliedRotation() {
  321. return _arotation;
  322. }
  323. void Bone::setAppliedRotation(float inValue) {
  324. _arotation = inValue;
  325. }
  326. float Bone::getAX() {
  327. return _ax;
  328. }
  329. void Bone::setAX(float inValue) {
  330. _ax = inValue;
  331. }
  332. float Bone::getAY() {
  333. return _ay;
  334. }
  335. void Bone::setAY(float inValue) {
  336. _ay = inValue;
  337. }
  338. float Bone::getAScaleX() {
  339. return _ascaleX;
  340. }
  341. void Bone::setAScaleX(float inValue) {
  342. _ascaleX = inValue;
  343. }
  344. float Bone::getAScaleY() {
  345. return _ascaleY;
  346. }
  347. void Bone::setAScaleY(float inValue) {
  348. _ascaleY = inValue;
  349. }
  350. float Bone::getAShearX() {
  351. return _ashearX;
  352. }
  353. void Bone::setAShearX(float inValue) {
  354. _ashearX = inValue;
  355. }
  356. float Bone::getAShearY() {
  357. return _ashearY;
  358. }
  359. void Bone::setAShearY(float inValue) {
  360. _ashearY = inValue;
  361. }
  362. float Bone::getA() {
  363. return _a;
  364. }
  365. void Bone::setA(float inValue) {
  366. _a = inValue;
  367. }
  368. float Bone::getB() {
  369. return _b;
  370. }
  371. void Bone::setB(float inValue) {
  372. _b = inValue;
  373. }
  374. float Bone::getC() {
  375. return _c;
  376. }
  377. void Bone::setC(float inValue) {
  378. _c = inValue;
  379. }
  380. float Bone::getD() {
  381. return _d;
  382. }
  383. void Bone::setD(float inValue) {
  384. _d = inValue;
  385. }
  386. float Bone::getWorldX() {
  387. return _worldX;
  388. }
  389. void Bone::setWorldX(float inValue) {
  390. _worldX = inValue;
  391. }
  392. float Bone::getWorldY() {
  393. return _worldY;
  394. }
  395. void Bone::setWorldY(float inValue) {
  396. _worldY = inValue;
  397. }
  398. float Bone::getWorldRotationX() {
  399. return MathUtil::atan2(_c, _a) * MathUtil::MathUtil::Rad_Deg;
  400. }
  401. float Bone::getWorldRotationY() {
  402. return MathUtil::atan2(_d, _b) * MathUtil::Rad_Deg;
  403. }
  404. float Bone::getWorldScaleX() {
  405. return MathUtil::sqrt(_a * _a + _c * _c);
  406. }
  407. float Bone::getWorldScaleY() {
  408. return MathUtil::sqrt(_b * _b + _d * _d);
  409. }
  410. bool Bone::isAppliedValid() {
  411. return _appliedValid;
  412. }
  413. void Bone::setAppliedValid(bool valid) {
  414. _appliedValid = valid;
  415. }
  416. void Bone::updateAppliedTransform() {
  417. Bone *parent = _parent;
  418. _appliedValid = 1;
  419. if (!parent) {
  420. _ax = _worldX;
  421. _ay = _worldY;
  422. _arotation = MathUtil::atan2(_c, _a) * MathUtil::Rad_Deg;
  423. _ascaleX = MathUtil::sqrt(_a * _a + _c * _c);
  424. _ascaleY = MathUtil::sqrt(_b * _b + _d * _d);
  425. _ashearX = 0;
  426. _ashearY = MathUtil::atan2(_a * _b + _c * _d, _a * _d - _b * _c) * MathUtil::Rad_Deg;
  427. } else {
  428. float pa = parent->_a, pb = parent->_b, pc = parent->_c, pd = parent->_d;
  429. float pid = 1 / (pa * pd - pb * pc);
  430. float dx = _worldX - parent->_worldX, dy = _worldY - parent->_worldY;
  431. float ia = pid * pd;
  432. float id = pid * pa;
  433. float ib = pid * pb;
  434. float ic = pid * pc;
  435. float ra = ia * _a - ib * _c;
  436. float rb = ia * _b - ib * _d;
  437. float rc = id * _c - ic * _a;
  438. float rd = id * _d - ic * _b;
  439. _ax = (dx * pd * pid - dy * pb * pid);
  440. _ay = (dy * pa * pid - dx * pc * pid);
  441. _ashearX = 0;
  442. _ascaleX = MathUtil::sqrt(ra * ra + rc * rc);
  443. if (_ascaleX > 0.0001f) {
  444. float det = ra * rd - rb * rc;
  445. _ascaleY = det / _ascaleX;
  446. _ashearY = MathUtil::atan2(ra * rb + rc * rd, det) * MathUtil::Rad_Deg;
  447. _arotation = MathUtil::atan2(rc, ra) * MathUtil::Rad_Deg;
  448. } else {
  449. _ascaleX = 0;
  450. _ascaleY = MathUtil::sqrt(rb * rb + rd * rd);
  451. _ashearY = 0;
  452. _arotation = 90 - MathUtil::atan2(rd, rb) * MathUtil::Rad_Deg;
  453. }
  454. }
  455. }
  456. bool Bone::isActive() {
  457. return _active;
  458. }
  459. void Bone::setActive(bool inValue) {
  460. _active = inValue;
  461. }