Bone.lua 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. -------------------------------------------------------------------------------
  2. -- Spine Runtimes Software License
  3. -- Version 2.1
  4. --
  5. -- Copyright (c) 2013, Esoteric Software
  6. -- All rights reserved.
  7. --
  8. -- You are granted a perpetual, non-exclusive, non-sublicensable and
  9. -- non-transferable license to install, execute and perform the Spine Runtimes
  10. -- Software (the "Software") solely for internal use. Without the written
  11. -- permission of Esoteric Software (typically granted by licensing Spine), you
  12. -- may not (a) modify, translate, adapt or otherwise create derivative works,
  13. -- improvements of the Software or develop new applications using the Software
  14. -- or (b) remove, delete, alter or obscure any trademarks or any copyright,
  15. -- trademark, patent or other intellectual property or proprietary rights
  16. -- notices on or in the Software, including any copy thereof. Redistributions
  17. -- in binary or source 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 SOFTARE 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; LOSS OF USE, DATA, OR PROFITS;
  25. -- OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. -- WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27. -- OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  28. -- ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. -------------------------------------------------------------------------------
  30. local Bone = {}
  31. function Bone.new (data, skeleton, parent)
  32. if not data then error("data cannot be nil", 2) end
  33. if not skeleton then error("skeleton cannot be nil", 2) end
  34. local self = {
  35. data = data,
  36. skeleton = skeleton,
  37. parent = parent,
  38. x = 0, y = 0,
  39. rotation = 0, rotationIK = 0,
  40. scaleX = 1, scaleY = 1,
  41. flipX = false, flipY = false,
  42. m00 = 0, m01 = 0, worldX = 0, -- a b x
  43. m10 = 0, m11 = 0, worldY = 0, -- c d y
  44. worldRotation = 0,
  45. worldScaleX = 1, worldScaleY = 1,
  46. worldFlipX = false, worldFlipY = false,
  47. }
  48. function self:updateWorldTransform (flipX, flipY)
  49. local parent = self.parent
  50. if parent then
  51. self.worldX = self.x * parent.m00 + self.y * parent.m01 + parent.worldX
  52. self.worldY = self.x * parent.m10 + self.y * parent.m11 + parent.worldY
  53. if (self.data.inheritScale) then
  54. self.worldScaleX = parent.worldScaleX * self.scaleX
  55. self.worldScaleY = parent.worldScaleY * self.scaleY
  56. else
  57. self.worldScaleX = self.scaleX
  58. self.worldScaleY = self.scaleY
  59. end
  60. if (self.data.inheritRotation) then
  61. self.worldRotation = parent.worldRotation + self.rotationIK
  62. else
  63. self.worldRotation = self.rotationIK
  64. end
  65. self.worldFlipX = parent.worldFlipX ~= self.flipX
  66. self.worldFlipY = parent.worldFlipY ~= self.flipY
  67. else
  68. local skeletonFlipX, skeletonFlipY = self.skeleton.flipX, self.skeleton.flipY
  69. if skeletonFlipX then
  70. self.worldX = -self.x
  71. else
  72. self.worldX = self.x
  73. end
  74. if skeletonFlipY then
  75. self.worldY = -self.y
  76. else
  77. self.worldY = self.y
  78. end
  79. self.worldScaleX = self.scaleX
  80. self.worldScaleY = self.scaleY
  81. self.worldRotation = self.rotationIK
  82. self.worldFlipX = skeletonFlipX ~= self.flipX
  83. self.worldFlipY = skeletonFlipY ~= self.flipY
  84. end
  85. local radians = math.rad(self.worldRotation)
  86. local cos = math.cos(radians)
  87. local sin = math.sin(radians)
  88. if self.worldFlipX then
  89. self.m00 = -cos * self.worldScaleX
  90. self.m01 = sin * self.worldScaleY
  91. else
  92. self.m00 = cos * self.worldScaleX
  93. self.m01 = -sin * self.worldScaleY
  94. end
  95. if self.worldFlipY then
  96. self.m10 = -sin * self.worldScaleX
  97. self.m11 = -cos * self.worldScaleY
  98. else
  99. self.m10 = sin * self.worldScaleX
  100. self.m11 = cos * self.worldScaleY
  101. end
  102. end
  103. function self:setToSetupPose ()
  104. local data = self.data
  105. self.x = data.x
  106. self.y = data.y
  107. self.rotation = data.rotation
  108. self.rotationIK = self.rotation
  109. self.scaleX = data.scaleX
  110. self.scaleY = data.scaleY
  111. self.flipX = data.flipX
  112. self.flipY = data.flipY
  113. end
  114. function self:worldToLocal (worldCoords)
  115. local dx = worldCoords[1] - self.worldX
  116. local dy = worldCoords[2] - self.worldY
  117. local m00 = self.m00
  118. local m10 = self.m10
  119. local m01 = self.m01
  120. local m11 = self.m11
  121. if self.worldFlipX ~= self.worldFlipY then
  122. m00 = -m00
  123. m11 = -m11
  124. end
  125. local invDet = 1 / (m00 * m11 - m01 * m10)
  126. worldCoords[1] = dx * m00 * invDet - dy * m01 * invDet
  127. worldCoords[2] = dy * m11 * invDet - dx * m10 * invDet
  128. end
  129. function self:localToWorld (localCoords)
  130. local localX = localCoords[1]
  131. local localY = localCoords[2]
  132. localCoords[1] = localX * self.m00 + localY * self.m01 + self.worldX
  133. localCoords[2] = localX * self.m10 + localY * self.m11 + self.worldY
  134. end
  135. self:setToSetupPose()
  136. return self
  137. end
  138. return Bone