Bone.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated July 28, 2023. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2023, 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 or
  13. * 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 THE
  27. * SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. using System;
  30. namespace Spine {
  31. using Physics = Skeleton.Physics;
  32. /// <summary>
  33. /// Stores a bone's current pose.
  34. /// <para>
  35. /// A bone has a local transform which is used to compute its world transform. A bone also has an applied transform, which is a
  36. /// local transform that can be applied to compute the world transform. The local transform and applied transform may differ if a
  37. /// constraint or application code modifies the world transform after it was computed from the local transform.
  38. /// </para>
  39. /// </summary>
  40. public class Bone : IUpdatable {
  41. static public bool yDown;
  42. internal BoneData data;
  43. internal Skeleton skeleton;
  44. internal Bone parent;
  45. internal ExposedList<Bone> children = new ExposedList<Bone>();
  46. internal float x, y, rotation, scaleX, scaleY, shearX, shearY;
  47. internal float ax, ay, arotation, ascaleX, ascaleY, ashearX, ashearY;
  48. internal float a, b, worldX;
  49. internal float c, d, worldY;
  50. internal Inherit inherit;
  51. internal bool sorted, active;
  52. public BoneData Data { get { return data; } }
  53. public Skeleton Skeleton { get { return skeleton; } }
  54. public Bone Parent { get { return parent; } }
  55. public ExposedList<Bone> Children { get { return children; } }
  56. public bool Active { get { return active; } }
  57. /// <summary>The local X translation.</summary>
  58. public float X { get { return x; } set { x = value; } }
  59. /// <summary>The local Y translation.</summary>
  60. public float Y { get { return y; } set { y = value; } }
  61. /// <summary>The local rotation.</summary>
  62. public float Rotation { get { return rotation; } set { rotation = value; } }
  63. /// <summary>The local scaleX.</summary>
  64. public float ScaleX { get { return scaleX; } set { scaleX = value; } }
  65. /// <summary>The local scaleY.</summary>
  66. public float ScaleY { get { return scaleY; } set { scaleY = value; } }
  67. /// <summary>The local shearX.</summary>
  68. public float ShearX { get { return shearX; } set { shearX = value; } }
  69. /// <summary>The local shearY.</summary>
  70. public float ShearY { get { return shearY; } set { shearY = value; } }
  71. /// <summary>Controls how parent world transforms affect this bone.</summary>
  72. public Inherit Inherit { get { return inherit; } set { inherit = value; } }
  73. /// <summary>The rotation, as calculated by any constraints.</summary>
  74. public float AppliedRotation { get { return arotation; } set { arotation = value; } }
  75. /// <summary>The applied local x translation.</summary>
  76. public float AX { get { return ax; } set { ax = value; } }
  77. /// <summary>The applied local y translation.</summary>
  78. public float AY { get { return ay; } set { ay = value; } }
  79. /// <summary>The applied local scaleX.</summary>
  80. public float AScaleX { get { return ascaleX; } set { ascaleX = value; } }
  81. /// <summary>The applied local scaleY.</summary>
  82. public float AScaleY { get { return ascaleY; } set { ascaleY = value; } }
  83. /// <summary>The applied local shearX.</summary>
  84. public float AShearX { get { return ashearX; } set { ashearX = value; } }
  85. /// <summary>The applied local shearY.</summary>
  86. public float AShearY { get { return ashearY; } set { ashearY = value; } }
  87. /// <summary>Part of the world transform matrix for the X axis. If changed, <see cref="UpdateAppliedTransform()"/> should be called.</summary>
  88. public float A { get { return a; } set { a = value; } }
  89. /// <summary>Part of the world transform matrix for the Y axis. If changed, <see cref="UpdateAppliedTransform()"/> should be called.</summary>
  90. public float B { get { return b; } set { b = value; } }
  91. /// <summary>Part of the world transform matrix for the X axis. If changed, <see cref="UpdateAppliedTransform()"/> should be called.</summary>
  92. public float C { get { return c; } set { c = value; } }
  93. /// <summary>Part of the world transform matrix for the Y axis. If changed, <see cref="UpdateAppliedTransform()"/> should be called.</summary>
  94. public float D { get { return d; } set { d = value; } }
  95. /// <summary>The world X position. If changed, <see cref="UpdateAppliedTransform()"/> should be called.</summary>
  96. public float WorldX { get { return worldX; } set { worldX = value; } }
  97. /// <summary>The world Y position. If changed, <see cref="UpdateAppliedTransform()"/> should be called.</summary>
  98. public float WorldY { get { return worldY; } set { worldY = value; } }
  99. /// <summary>The world rotation for the X axis, calculated using <see cref="a"/> and <see cref="c"/>.</summary>
  100. public float WorldRotationX { get { return MathUtils.Atan2Deg(c, a); } }
  101. /// <summary>The world rotation for the Y axis, calculated using <see cref="b"/> and <see cref="d"/>.</summary>
  102. public float WorldRotationY { get { return MathUtils.Atan2Deg(d, b); } }
  103. /// <summary>Returns the magnitide (always positive) of the world scale X.</summary>
  104. public float WorldScaleX { get { return (float)Math.Sqrt(a * a + c * c); } }
  105. /// <summary>Returns the magnitide (always positive) of the world scale Y.</summary>
  106. public float WorldScaleY { get { return (float)Math.Sqrt(b * b + d * d); } }
  107. public Bone (BoneData data, Skeleton skeleton, Bone parent) {
  108. if (data == null) throw new ArgumentNullException("data", "data cannot be null.");
  109. if (skeleton == null) throw new ArgumentNullException("skeleton", "skeleton cannot be null.");
  110. this.data = data;
  111. this.skeleton = skeleton;
  112. this.parent = parent;
  113. SetToSetupPose();
  114. }
  115. /// <summary>Copy constructor. Does not copy the <see cref="Children"/> bones.</summary>
  116. /// <param name="parent">May be null.</param>
  117. public Bone (Bone bone, Skeleton skeleton, Bone parent) {
  118. if (bone == null) throw new ArgumentNullException("bone", "bone cannot be null.");
  119. if (skeleton == null) throw new ArgumentNullException("skeleton", "skeleton cannot be null.");
  120. this.skeleton = skeleton;
  121. this.parent = parent;
  122. data = bone.data;
  123. x = bone.x;
  124. y = bone.y;
  125. rotation = bone.rotation;
  126. scaleX = bone.scaleX;
  127. scaleY = bone.scaleY;
  128. shearX = bone.shearX;
  129. shearY = bone.shearY;
  130. inherit = bone.inherit;
  131. }
  132. /// <summary>Computes the world transform using the parent bone and this bone's local applied transform.</summary>
  133. public void Update (Physics physics) {
  134. UpdateWorldTransform(ax, ay, arotation, ascaleX, ascaleY, ashearX, ashearY);
  135. }
  136. /// <summary>Computes the world transform using the parent bone and this bone's local transform.</summary>
  137. public void UpdateWorldTransform () {
  138. UpdateWorldTransform(x, y, rotation, scaleX, scaleY, shearX, shearY);
  139. }
  140. /// <summary>Computes the world transform using the parent bone and the specified local transform. The applied transform is set to the
  141. /// specified local transform. Child bones are not updated.
  142. /// <para>
  143. /// See <a href="http://esotericsoftware.com/spine-runtime-skeletons#World-transforms">World transforms</a> in the Spine
  144. /// Runtimes Guide.</para></summary>
  145. public void UpdateWorldTransform (float x, float y, float rotation, float scaleX, float scaleY, float shearX, float shearY) {
  146. ax = x;
  147. ay = y;
  148. arotation = rotation;
  149. ascaleX = scaleX;
  150. ascaleY = scaleY;
  151. ashearX = shearX;
  152. ashearY = shearY;
  153. Bone parent = this.parent;
  154. if (parent == null) { // Root bone.
  155. Skeleton skeleton = this.skeleton;
  156. float sx = skeleton.scaleX, sy = skeleton.ScaleY;
  157. float rx = (rotation + shearX) * MathUtils.DegRad;
  158. float ry = (rotation + 90 + shearY) * MathUtils.DegRad;
  159. a = (float)Math.Cos(rx) * scaleX * sx;
  160. b = (float)Math.Cos(ry) * scaleY * sx;
  161. c = (float)Math.Sin(rx) * scaleX * sy;
  162. d = (float)Math.Sin(ry) * scaleY * sy;
  163. worldX = x * sx + skeleton.x;
  164. worldY = y * sy + skeleton.y;
  165. return;
  166. }
  167. float pa = parent.a, pb = parent.b, pc = parent.c, pd = parent.d;
  168. worldX = pa * x + pb * y + parent.worldX;
  169. worldY = pc * x + pd * y + parent.worldY;
  170. switch (inherit) {
  171. case Inherit.Normal: {
  172. float rx = (rotation + shearX) * MathUtils.DegRad;
  173. float ry = (rotation + 90 + shearY) * MathUtils.DegRad;
  174. float la = (float)Math.Cos(rx) * scaleX;
  175. float lb = (float)Math.Cos(ry) * scaleY;
  176. float lc = (float)Math.Sin(rx) * scaleX;
  177. float ld = (float)Math.Sin(ry) * scaleY;
  178. a = pa * la + pb * lc;
  179. b = pa * lb + pb * ld;
  180. c = pc * la + pd * lc;
  181. d = pc * lb + pd * ld;
  182. return;
  183. }
  184. case Inherit.OnlyTranslation: {
  185. float rx = (rotation + shearX) * MathUtils.DegRad;
  186. float ry = (rotation + 90 + shearY) * MathUtils.DegRad;
  187. a = (float)Math.Cos(rx) * scaleX;
  188. b = (float)Math.Cos(ry) * scaleY;
  189. c = (float)Math.Sin(rx) * scaleX;
  190. d = (float)Math.Sin(ry) * scaleY;
  191. break;
  192. }
  193. case Inherit.NoRotationOrReflection: {
  194. float s = pa * pa + pc * pc, prx;
  195. if (s > 0.0001f) {
  196. s = Math.Abs(pa * pd - pb * pc) / s;
  197. pa /= skeleton.scaleX;
  198. pc /= skeleton.ScaleY;
  199. pb = pc * s;
  200. pd = pa * s;
  201. prx = MathUtils.Atan2Deg(pc, pa);
  202. } else {
  203. pa = 0;
  204. pc = 0;
  205. prx = 90 - MathUtils.Atan2Deg(pd, pb);
  206. }
  207. float rx = (rotation + shearX - prx) * MathUtils.DegRad;
  208. float ry = (rotation + shearY - prx + 90) * MathUtils.DegRad;
  209. float la = (float)Math.Cos(rx) * scaleX;
  210. float lb = (float)Math.Cos(ry) * scaleY;
  211. float lc = (float)Math.Sin(rx) * scaleX;
  212. float ld = (float)Math.Sin(ry) * scaleY;
  213. a = pa * la - pb * lc;
  214. b = pa * lb - pb * ld;
  215. c = pc * la + pd * lc;
  216. d = pc * lb + pd * ld;
  217. break;
  218. }
  219. case Inherit.NoScale:
  220. case Inherit.NoScaleOrReflection: {
  221. rotation *= MathUtils.DegRad;
  222. float cos = (float)Math.Cos(rotation), sin = (float)Math.Sin(rotation);
  223. float za = (pa * cos + pb * sin) / skeleton.scaleX;
  224. float zc = (pc * cos + pd * sin) / skeleton.ScaleY;
  225. float s = (float)Math.Sqrt(za * za + zc * zc);
  226. if (s > 0.00001f) s = 1 / s;
  227. za *= s;
  228. zc *= s;
  229. s = (float)Math.Sqrt(za * za + zc * zc);
  230. if (inherit == Inherit.NoScale && (pa * pd - pb * pc < 0) != (skeleton.scaleX < 0 != skeleton.ScaleY < 0)) s = -s;
  231. rotation = MathUtils.PI / 2 + MathUtils.Atan2(zc, za);
  232. float zb = (float)Math.Cos(rotation) * s;
  233. float zd = (float)Math.Sin(rotation) * s;
  234. shearX *= MathUtils.DegRad;
  235. shearY = (90 + shearY) * MathUtils.DegRad;
  236. float la = (float)Math.Cos(shearX) * scaleX;
  237. float lb = (float)Math.Cos(shearY) * scaleY;
  238. float lc = (float)Math.Sin(shearX) * scaleX;
  239. float ld = (float)Math.Sin(shearY) * scaleY;
  240. a = za * la + zb * lc;
  241. b = za * lb + zb * ld;
  242. c = zc * la + zd * lc;
  243. d = zc * lb + zd * ld;
  244. break;
  245. }
  246. }
  247. a *= skeleton.scaleX;
  248. b *= skeleton.scaleX;
  249. c *= skeleton.ScaleY;
  250. d *= skeleton.ScaleY;
  251. }
  252. /// <summary>Sets this bone's local transform to the setup pose.</summary>
  253. public void SetToSetupPose () {
  254. BoneData data = this.data;
  255. x = data.x;
  256. y = data.y;
  257. rotation = data.rotation;
  258. scaleX = data.scaleX;
  259. scaleY = data.ScaleY;
  260. shearX = data.shearX;
  261. shearY = data.shearY;
  262. inherit = data.inherit;
  263. }
  264. /// <summary>
  265. /// Computes the applied transform values from the world transform.
  266. /// <para>
  267. /// If the world transform is modified (by a constraint, <see cref="RotateWorld(float)"/>, etc) then this method should be called so
  268. /// the applied transform matches the world transform. The applied transform may be needed by other code (eg to apply another
  269. /// constraint).
  270. /// </para><para>
  271. /// Some information is ambiguous in the world transform, such as -1,-1 scale versus 180 rotation. The applied transform after
  272. /// calling this method is equivalent to the local transform used to compute the world transform, but may not be identical.
  273. /// </para></summary>
  274. public void UpdateAppliedTransform () {
  275. Bone parent = this.parent;
  276. if (parent == null) {
  277. ax = worldX - skeleton.x;
  278. ay = worldY - skeleton.y;
  279. float a = this.a, b = this.b, c = this.c, d = this.d;
  280. arotation = MathUtils.Atan2Deg(c, a);
  281. ascaleX = (float)Math.Sqrt(a * a + c * c);
  282. ascaleY = (float)Math.Sqrt(b * b + d * d);
  283. ashearX = 0;
  284. ashearY = MathUtils.Atan2Deg(a * b + c * d, a * d - b * c);
  285. return;
  286. }
  287. float pa = parent.a, pb = parent.b, pc = parent.c, pd = parent.d;
  288. float pid = 1 / (pa * pd - pb * pc);
  289. float ia = pd * pid, ib = pb * pid, ic = pc * pid, id = pa * pid;
  290. float dx = worldX - parent.worldX, dy = worldY - parent.worldY;
  291. ax = (dx * ia - dy * ib);
  292. ay = (dy * id - dx * ic);
  293. float ra, rb, rc, rd;
  294. if (inherit == Inherit.OnlyTranslation) {
  295. ra = a;
  296. rb = b;
  297. rc = c;
  298. rd = d;
  299. } else {
  300. switch (inherit) {
  301. case Inherit.NoRotationOrReflection: {
  302. float s = Math.Abs(pa * pd - pb * pc) / (pa * pa + pc * pc);
  303. float sa = pa / skeleton.scaleX;
  304. float sc = pc / skeleton.ScaleY;
  305. pb = -sc * s * skeleton.scaleX;
  306. pd = sa * s * skeleton.ScaleY;
  307. pid = 1 / (pa * pd - pb * pc);
  308. ia = pd * pid;
  309. ib = pb * pid;
  310. break;
  311. }
  312. case Inherit.NoScale:
  313. case Inherit.NoScaleOrReflection: {
  314. float r = rotation * MathUtils.DegRad, cos = (float)Math.Cos(r), sin = (float)Math.Sin(r);
  315. pa = (pa * cos + pb * sin) / skeleton.scaleX;
  316. pc = (pc * cos + pd * sin) / skeleton.ScaleY;
  317. float s = (float)Math.Sqrt(pa * pa + pc * pc);
  318. if (s > 0.00001f) s = 1 / s;
  319. pa *= s;
  320. pc *= s;
  321. s = (float)Math.Sqrt(pa * pa + pc * pc);
  322. if (inherit == Inherit.NoScale && pid < 0 != (skeleton.scaleX < 0 != skeleton.ScaleY < 0)) s = -s;
  323. r = MathUtils.PI / 2 + MathUtils.Atan2(pc, pa);
  324. pb = (float)Math.Cos(r) * s;
  325. pd = (float)Math.Sin(r) * s;
  326. pid = 1 / (pa * pd - pb * pc);
  327. ia = pd * pid;
  328. ib = pb * pid;
  329. ic = pc * pid;
  330. id = pa * pid;
  331. break;
  332. }
  333. }
  334. ra = ia * a - ib * c;
  335. rb = ia * b - ib * d;
  336. rc = id * c - ic * a;
  337. rd = id * d - ic * b;
  338. }
  339. ashearX = 0;
  340. ascaleX = (float)Math.Sqrt(ra * ra + rc * rc);
  341. if (ascaleX > 0.0001f) {
  342. float det = ra * rd - rb * rc;
  343. ascaleY = det / ascaleX;
  344. ashearY = -MathUtils.Atan2Deg(ra * rb + rc * rd, det);
  345. arotation = MathUtils.Atan2Deg(rc, ra);
  346. } else {
  347. ascaleX = 0;
  348. ascaleY = (float)Math.Sqrt(rb * rb + rd * rd);
  349. ashearY = 0;
  350. arotation = 90 - MathUtils.Atan2Deg(rd, rb);
  351. }
  352. }
  353. /// <summary>Transforms a point from world coordinates to the bone's local coordinates.</summary>
  354. public void WorldToLocal (float worldX, float worldY, out float localX, out float localY) {
  355. float a = this.a, b = this.b, c = this.c, d = this.d;
  356. float det = a * d - b * c;
  357. float x = worldX - this.worldX, y = worldY - this.worldY;
  358. localX = (x * d - y * b) / det;
  359. localY = (y * a - x * c) / det;
  360. }
  361. /// <summary>Transforms a point from the bone's local coordinates to world coordinates.</summary>
  362. public void LocalToWorld (float localX, float localY, out float worldX, out float worldY) {
  363. worldX = localX * a + localY * b + this.worldX;
  364. worldY = localX * c + localY * d + this.worldY;
  365. }
  366. /// <summary>Transforms a point from world coordinates to the parent bone's local coordinates.</summary>
  367. public void WorldToParent (float worldX, float worldY, out float parentX, out float parentY) {
  368. if (parent == null) {
  369. parentX = worldX;
  370. parentY = worldY;
  371. } else {
  372. parent.WorldToLocal(worldX, worldY, out parentX, out parentY);
  373. }
  374. }
  375. /// <summary>Transforms a point from the parent bone's coordinates to world coordinates.</summary>
  376. public void ParentToWorld (float parentX, float parentY, out float worldX, out float worldY) {
  377. if (parent == null) {
  378. worldX = parentX;
  379. worldY = parentY;
  380. } else {
  381. parent.LocalToWorld(parentX, parentY, out worldX, out worldY);
  382. }
  383. }
  384. /// <summary>Transforms a world rotation to a local rotation.</summary>
  385. public float WorldToLocalRotation (float worldRotation) {
  386. worldRotation *= MathUtils.DegRad;
  387. float sin = (float)Math.Sin(worldRotation), cos = (float)Math.Cos(worldRotation);
  388. return MathUtils.Atan2Deg(a * sin - c * cos, d * cos - b * sin) + rotation - shearX;
  389. }
  390. /// <summary>Transforms a local rotation to a world rotation.</summary>
  391. public float LocalToWorldRotation (float localRotation) {
  392. localRotation = (localRotation - rotation - shearX) * MathUtils.DegRad;
  393. float sin = (float)Math.Sin(localRotation), cos = (float)Math.Cos(localRotation);
  394. return MathUtils.Atan2Deg(cos * c + sin * d, cos * a + sin * b);
  395. }
  396. /// <summary>
  397. /// Rotates the world transform the specified amount.
  398. /// <para>
  399. /// After changes are made to the world transform, <see cref="UpdateAppliedTransform()"/> should be called and
  400. /// <see cref="Update(Skeleton.Physics)"/> will need to be called on any child bones, recursively.
  401. /// </para></summary>
  402. public void RotateWorld (float degrees) {
  403. degrees *= MathUtils.DegRad;
  404. float sin = (float)Math.Sin(degrees), cos = (float)Math.Cos(degrees);
  405. float ra = a, rb = b;
  406. a = cos * ra - sin * c;
  407. b = cos * rb - sin * d;
  408. c = sin * ra + cos * c;
  409. d = sin * rb + cos * d;
  410. }
  411. override public string ToString () {
  412. return data.name;
  413. }
  414. }
  415. }