Bone.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. using System;
  30. namespace Spine {
  31. /// <summary>
  32. /// Stores a bone's current pose.
  33. /// <para>
  34. /// A bone has a local transform which is used to compute its world transform. A bone also has an applied transform, which is a
  35. /// local transform that can be applied to compute the world transform. The local transform and applied transform may differ if a
  36. /// constraint or application code modifies the world transform after it was computed from the local transform.
  37. /// </para>
  38. /// </summary>
  39. public class Bone : IUpdatable {
  40. static public bool yDown;
  41. internal BoneData data;
  42. internal Skeleton skeleton;
  43. internal Bone parent;
  44. internal ExposedList<Bone> children = new ExposedList<Bone>();
  45. internal float x, y, rotation, scaleX, scaleY, shearX, shearY;
  46. internal float ax, ay, arotation, ascaleX, ascaleY, ashearX, ashearY;
  47. internal float a, b, worldX;
  48. internal float c, d, worldY;
  49. internal bool sorted, active;
  50. public BoneData Data { get { return data; } }
  51. public Skeleton Skeleton { get { return skeleton; } }
  52. public Bone Parent { get { return parent; } }
  53. public ExposedList<Bone> Children { get { return children; } }
  54. /// <summary>Returns false when the bone has not been computed because <see cref="BoneData.SkinRequired"/> is true and the
  55. /// <see cref="Skeleton.Skin">active skin</see> does not <see cref="Skin.Bones">contain</see> this bone.</summary>
  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>The rotation, as calculated by any constraints.</summary>
  72. public float AppliedRotation { get { return arotation; } set { arotation = value; } }
  73. /// <summary>The applied local x translation.</summary>
  74. public float AX { get { return ax; } set { ax = value; } }
  75. /// <summary>The applied local y translation.</summary>
  76. public float AY { get { return ay; } set { ay = value; } }
  77. /// <summary>The applied local scaleX.</summary>
  78. public float AScaleX { get { return ascaleX; } set { ascaleX = value; } }
  79. /// <summary>The applied local scaleY.</summary>
  80. public float AScaleY { get { return ascaleY; } set { ascaleY = value; } }
  81. /// <summary>The applied local shearX.</summary>
  82. public float AShearX { get { return ashearX; } set { ashearX = value; } }
  83. /// <summary>The applied local shearY.</summary>
  84. public float AShearY { get { return ashearY; } set { ashearY = value; } }
  85. /// <summary>Part of the world transform matrix for the X axis. If changed, <see cref="UpdateAppliedTransform()"/> should be called.</summary>
  86. public float A { get { return a; } set { a = value; } }
  87. /// <summary>Part of the world transform matrix for the Y axis. If changed, <see cref="UpdateAppliedTransform()"/> should be called.</summary>
  88. public float B { get { return b; } set { b = value; } }
  89. /// <summary>Part of the world transform matrix for the X axis. If changed, <see cref="UpdateAppliedTransform()"/> should be called.</summary>
  90. public float C { get { return c; } set { c = value; } }
  91. /// <summary>Part of the world transform matrix for the Y axis. If changed, <see cref="UpdateAppliedTransform()"/> should be called.</summary>
  92. public float D { get { return d; } set { d = value; } }
  93. /// <summary>The world X position. If changed, <see cref="UpdateAppliedTransform()"/> should be called.</summary>
  94. public float WorldX { get { return worldX; } set { worldX = value; } }
  95. /// <summary>The world Y position. If changed, <see cref="UpdateAppliedTransform()"/> should be called.</summary>
  96. public float WorldY { get { return worldY; } set { worldY = value; } }
  97. public float WorldRotationX { get { return MathUtils.Atan2(c, a) * MathUtils.RadDeg; } }
  98. public float WorldRotationY { get { return MathUtils.Atan2(d, b) * MathUtils.RadDeg; } }
  99. /// <summary>Returns the magnitide (always positive) of the world scale X.</summary>
  100. public float WorldScaleX { get { return (float)Math.Sqrt(a * a + c * c); } }
  101. /// <summary>Returns the magnitide (always positive) of the world scale Y.</summary>
  102. public float WorldScaleY { get { return (float)Math.Sqrt(b * b + d * d); } }
  103. /// <summary>Copy constructor. Does not copy the <see cref="Children"/> bones.</summary>
  104. /// <param name="parent">May be null.</param>
  105. public Bone (BoneData data, Skeleton skeleton, Bone parent) {
  106. if (data == null) throw new ArgumentNullException("data", "data cannot be null.");
  107. if (skeleton == null) throw new ArgumentNullException("skeleton", "skeleton cannot be null.");
  108. this.data = data;
  109. this.skeleton = skeleton;
  110. this.parent = parent;
  111. SetToSetupPose();
  112. }
  113. /// <summary>Computes the world transform using the parent bone and this bone's local applied transform.</summary>
  114. public void Update () {
  115. UpdateWorldTransform(ax, ay, arotation, ascaleX, ascaleY, ashearX, ashearY);
  116. }
  117. /// <summary>Computes the world transform using the parent bone and this bone's local transform.</summary>
  118. public void UpdateWorldTransform () {
  119. UpdateWorldTransform(x, y, rotation, scaleX, scaleY, shearX, shearY);
  120. }
  121. /// <summary>Computes the world transform using the parent bone and the specified local transform. The applied transform is set to the
  122. /// specified local transform. Child bones are not updated.
  123. /// <para>
  124. /// See <a href="http://esotericsoftware.com/spine-runtime-skeletons#World-transforms">World transforms</a> in the Spine
  125. /// Runtimes Guide.</para></summary>
  126. public void UpdateWorldTransform (float x, float y, float rotation, float scaleX, float scaleY, float shearX, float shearY) {
  127. ax = x;
  128. ay = y;
  129. arotation = rotation;
  130. ascaleX = scaleX;
  131. ascaleY = scaleY;
  132. ashearX = shearX;
  133. ashearY = shearY;
  134. Bone parent = this.parent;
  135. if (parent == null) { // Root bone.
  136. float rotationY = rotation + 90 + shearY, sx = skeleton.ScaleX, sy = skeleton.ScaleY;
  137. a = MathUtils.CosDeg(rotation + shearX) * scaleX * sx;
  138. b = MathUtils.CosDeg(rotationY) * scaleY * sx;
  139. c = MathUtils.SinDeg(rotation + shearX) * scaleX * sy;
  140. d = MathUtils.SinDeg(rotationY) * scaleY * sy;
  141. worldX = x * sx + skeleton.x;
  142. worldY = y * sy + skeleton.y;
  143. return;
  144. }
  145. float pa = parent.a, pb = parent.b, pc = parent.c, pd = parent.d;
  146. worldX = pa * x + pb * y + parent.worldX;
  147. worldY = pc * x + pd * y + parent.worldY;
  148. switch (data.transformMode) {
  149. case TransformMode.Normal: {
  150. float rotationY = rotation + 90 + shearY;
  151. float la = MathUtils.CosDeg(rotation + shearX) * scaleX;
  152. float lb = MathUtils.CosDeg(rotationY) * scaleY;
  153. float lc = MathUtils.SinDeg(rotation + shearX) * scaleX;
  154. float ld = MathUtils.SinDeg(rotationY) * scaleY;
  155. a = pa * la + pb * lc;
  156. b = pa * lb + pb * ld;
  157. c = pc * la + pd * lc;
  158. d = pc * lb + pd * ld;
  159. return;
  160. }
  161. case TransformMode.OnlyTranslation: {
  162. float rotationY = rotation + 90 + shearY;
  163. a = MathUtils.CosDeg(rotation + shearX) * scaleX;
  164. b = MathUtils.CosDeg(rotationY) * scaleY;
  165. c = MathUtils.SinDeg(rotation + shearX) * scaleX;
  166. d = MathUtils.SinDeg(rotationY) * scaleY;
  167. break;
  168. }
  169. case TransformMode.NoRotationOrReflection: {
  170. float s = pa * pa + pc * pc, prx;
  171. if (s > 0.0001f) {
  172. s = Math.Abs(pa * pd - pb * pc) / s;
  173. pa /= skeleton.ScaleX;
  174. pc /= skeleton.ScaleY;
  175. pb = pc * s;
  176. pd = pa * s;
  177. prx = MathUtils.Atan2(pc, pa) * MathUtils.RadDeg;
  178. } else {
  179. pa = 0;
  180. pc = 0;
  181. prx = 90 - MathUtils.Atan2(pd, pb) * MathUtils.RadDeg;
  182. }
  183. float rx = rotation + shearX - prx;
  184. float ry = rotation + shearY - prx + 90;
  185. float la = MathUtils.CosDeg(rx) * scaleX;
  186. float lb = MathUtils.CosDeg(ry) * scaleY;
  187. float lc = MathUtils.SinDeg(rx) * scaleX;
  188. float ld = MathUtils.SinDeg(ry) * scaleY;
  189. a = pa * la - pb * lc;
  190. b = pa * lb - pb * ld;
  191. c = pc * la + pd * lc;
  192. d = pc * lb + pd * ld;
  193. break;
  194. }
  195. case TransformMode.NoScale:
  196. case TransformMode.NoScaleOrReflection: {
  197. float cos = MathUtils.CosDeg(rotation), sin = MathUtils.SinDeg(rotation);
  198. float za = (pa * cos + pb * sin) / skeleton.ScaleX;
  199. float zc = (pc * cos + pd * sin) / skeleton.ScaleY;
  200. float s = (float)Math.Sqrt(za * za + zc * zc);
  201. if (s > 0.00001f) s = 1 / s;
  202. za *= s;
  203. zc *= s;
  204. s = (float)Math.Sqrt(za * za + zc * zc);
  205. if (data.transformMode == TransformMode.NoScale
  206. && (pa * pd - pb * pc < 0) != (skeleton.ScaleX < 0 != skeleton.ScaleY < 0)) s = -s;
  207. float r = MathUtils.PI / 2 + MathUtils.Atan2(zc, za);
  208. float zb = MathUtils.Cos(r) * s;
  209. float zd = MathUtils.Sin(r) * s;
  210. float la = MathUtils.CosDeg(shearX) * scaleX;
  211. float lb = MathUtils.CosDeg(90 + shearY) * scaleY;
  212. float lc = MathUtils.SinDeg(shearX) * scaleX;
  213. float ld = MathUtils.SinDeg(90 + shearY) * scaleY;
  214. a = za * la + zb * lc;
  215. b = za * lb + zb * ld;
  216. c = zc * la + zd * lc;
  217. d = zc * lb + zd * ld;
  218. break;
  219. }
  220. }
  221. a *= skeleton.ScaleX;
  222. b *= skeleton.ScaleX;
  223. c *= skeleton.ScaleY;
  224. d *= skeleton.ScaleY;
  225. }
  226. public void SetToSetupPose () {
  227. BoneData data = this.data;
  228. x = data.x;
  229. y = data.y;
  230. rotation = data.rotation;
  231. scaleX = data.scaleX;
  232. scaleY = data.scaleY;
  233. shearX = data.shearX;
  234. shearY = data.shearY;
  235. }
  236. /// <summary>
  237. /// Computes the applied transform values from the world transform.
  238. /// <para>
  239. /// If the world transform is modified (by a constraint, <see cref="RotateWorld(float)"/>, etc) then this method should be called so
  240. /// the applied transform matches the world transform. The applied transform may be needed by other code (eg to apply another
  241. /// constraint).
  242. /// </para><para>
  243. /// Some information is ambiguous in the world transform, such as -1,-1 scale versus 180 rotation. The applied transform after
  244. /// calling this method is equivalent to the local transform used to compute the world transform, but may not be identical.
  245. /// </para></summary>
  246. public void UpdateAppliedTransform () {
  247. Bone parent = this.parent;
  248. if (parent == null) {
  249. ax = worldX - skeleton.x;
  250. ay = worldY - skeleton.y;
  251. arotation = MathUtils.Atan2(c, a) * MathUtils.RadDeg;
  252. ascaleX = (float)Math.Sqrt(a * a + c * c);
  253. ascaleY = (float)Math.Sqrt(b * b + d * d);
  254. ashearX = 0;
  255. ashearY = MathUtils.Atan2(a * b + c * d, a * d - b * c) * MathUtils.RadDeg;
  256. return;
  257. }
  258. float pa = parent.a, pb = parent.b, pc = parent.c, pd = parent.d;
  259. float pid = 1 / (pa * pd - pb * pc);
  260. float dx = worldX - parent.worldX, dy = worldY - parent.worldY;
  261. ax = (dx * pd * pid - dy * pb * pid);
  262. ay = (dy * pa * pid - dx * pc * pid);
  263. float ia = pid * pd;
  264. float id = pid * pa;
  265. float ib = pid * pb;
  266. float ic = pid * pc;
  267. float ra = ia * a - ib * c;
  268. float rb = ia * b - ib * d;
  269. float rc = id * c - ic * a;
  270. float rd = id * d - ic * b;
  271. ashearX = 0;
  272. ascaleX = (float)Math.Sqrt(ra * ra + rc * rc);
  273. if (ascaleX > 0.0001f) {
  274. float det = ra * rd - rb * rc;
  275. ascaleY = det / ascaleX;
  276. ashearY = MathUtils.Atan2(ra * rb + rc * rd, det) * MathUtils.RadDeg;
  277. arotation = MathUtils.Atan2(rc, ra) * MathUtils.RadDeg;
  278. } else {
  279. ascaleX = 0;
  280. ascaleY = (float)Math.Sqrt(rb * rb + rd * rd);
  281. ashearY = 0;
  282. arotation = 90 - MathUtils.Atan2(rd, rb) * MathUtils.RadDeg;
  283. }
  284. }
  285. public void WorldToLocal (float worldX, float worldY, out float localX, out float localY) {
  286. float a = this.a, b = this.b, c = this.c, d = this.d;
  287. float det = a * d - b * c;
  288. float x = worldX - this.worldX, y = worldY - this.worldY;
  289. localX = (x * d - y * b) / det;
  290. localY = (y * a - x * c) / det;
  291. }
  292. public void LocalToWorld (float localX, float localY, out float worldX, out float worldY) {
  293. worldX = localX * a + localY * b + this.worldX;
  294. worldY = localX * c + localY * d + this.worldY;
  295. }
  296. public float WorldToLocalRotationX {
  297. get {
  298. Bone parent = this.parent;
  299. if (parent == null) return arotation;
  300. float pa = parent.a, pb = parent.b, pc = parent.c, pd = parent.d, a = this.a, c = this.c;
  301. return MathUtils.Atan2(pa * c - pc * a, pd * a - pb * c) * MathUtils.RadDeg;
  302. }
  303. }
  304. public float WorldToLocalRotationY {
  305. get {
  306. Bone parent = this.parent;
  307. if (parent == null) return arotation;
  308. float pa = parent.a, pb = parent.b, pc = parent.c, pd = parent.d, b = this.b, d = this.d;
  309. return MathUtils.Atan2(pa * d - pc * b, pd * b - pb * d) * MathUtils.RadDeg;
  310. }
  311. }
  312. public float WorldToLocalRotation (float worldRotation) {
  313. float sin = MathUtils.SinDeg(worldRotation), cos = MathUtils.CosDeg(worldRotation);
  314. return MathUtils.Atan2(a * sin - c * cos, d * cos - b * sin) * MathUtils.RadDeg + rotation - shearX;
  315. }
  316. public float LocalToWorldRotation (float localRotation) {
  317. localRotation -= rotation - shearX;
  318. float sin = MathUtils.SinDeg(localRotation), cos = MathUtils.CosDeg(localRotation);
  319. return MathUtils.Atan2(cos * c + sin * d, cos * a + sin * b) * MathUtils.RadDeg;
  320. }
  321. /// <summary>
  322. /// Rotates the world transform the specified amount.
  323. /// <para>
  324. /// After changes are made to the world transform, <see cref="UpdateAppliedTransform()"/> should be called and <see cref="Update()"/> will
  325. /// need to be called on any child bones, recursively.
  326. /// </para></summary>
  327. public void RotateWorld (float degrees) {
  328. float a = this.a, b = this.b, c = this.c, d = this.d;
  329. float cos = MathUtils.CosDeg(degrees), sin = MathUtils.SinDeg(degrees);
  330. this.a = cos * a - sin * c;
  331. this.b = cos * b - sin * d;
  332. this.c = sin * a + cos * c;
  333. this.d = sin * b + cos * d;
  334. }
  335. override public string ToString () {
  336. return data.name;
  337. }
  338. }
  339. }