IkConstraint.lua 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 IkConstraint = {}
  31. function IkConstraint.new (data, skeleton)
  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. bones = {},
  38. target = nil,
  39. bendDirection = data.bendDirection,
  40. mix = data.mix
  41. }
  42. function self:apply ()
  43. local target = self.target
  44. local bones = self.bones
  45. local boneCount = #bones
  46. if boneCount == 1 then
  47. IkConstraint.apply1(bones[1], target.worldX, target.worldY, self.mix)
  48. elseif boneCount == 2 then
  49. IkConstraint.apply2(bones[1], bones[2], target.worldX, target.worldY, self.bendDirection, self.mix)
  50. end
  51. end
  52. for i,boneData in ipairs(data.bones) do
  53. table.insert(self.bones, skeleton:findBone(boneData.name))
  54. end
  55. self.target = skeleton:findBone(data.target.name)
  56. return self
  57. end
  58. local radDeg = 180 / math.pi
  59. local degRad = math.pi / 180
  60. function IkConstraint.apply1 (bone, targetX, targetY, alpha)
  61. local parentRotation
  62. if not bone.data.inheritRotation or not bone.parent then
  63. parentRotation = 0
  64. else
  65. parentRotation = bone.parent.worldRotation
  66. end
  67. local rotation = bone.rotation
  68. local rotationIK = math.atan2(targetY - bone.worldY, targetX - bone.worldX) * radDeg - parentRotation
  69. bone.rotationIK = rotation + (rotationIK - rotation) * alpha
  70. end
  71. local temp = {}
  72. function IkConstraint.apply2 (parent, child, targetX, targetY, bendDirection, alpha)
  73. local childRotation = child.rotation
  74. local parentRotation = parent.rotation
  75. if not alpha then
  76. child.rotationIK = childRotation
  77. parent.rotationIK = parentRotation
  78. return
  79. end
  80. local positionX, positionY
  81. local tempPosition = temp
  82. local parentParent = parent.parent
  83. if parentParent then
  84. tempPosition[1] = targetX
  85. tempPosition[2] = targetY
  86. parentParent:worldToLocal(tempPosition)
  87. targetX = (tempPosition[1] - parent.x) * parentParent.worldScaleX
  88. targetY = (tempPosition[2] - parent.y) * parentParent.worldScaleY
  89. else
  90. targetX = targetX - parent.x
  91. targetY = targetY - parent.y
  92. end
  93. if child.parent == parent then
  94. positionX = child.x
  95. positionY = child.y
  96. else
  97. tempPosition[1] = child.x
  98. tempPosition[2] = child.y
  99. child.parent:localToWorld(tempPosition)
  100. parent:worldToLocal(tempPosition)
  101. positionX = tempPosition[1]
  102. positionY = tempPosition[2]
  103. end
  104. local childX = positionX * parent.worldScaleX
  105. local childY = positionY * parent.worldScaleY
  106. local offset = math.atan2(childY, childX)
  107. local len1 = math.sqrt(childX * childX + childY * childY)
  108. local len2 = child.data.length * child.worldScaleX
  109. -- Based on code by Ryan Juckett with permission: Copyright (c) 2008-2009 Ryan Juckett, http://www.ryanjuckett.com/
  110. local cosDenom = 2 * len1 * len2
  111. if cosDenom < 0.0001 then
  112. child.rotationIK = childRotation + (math.atan2(targetY, targetX) * radDeg - parentRotation - childRotation) * alpha
  113. return
  114. end
  115. local cos = (targetX * targetX + targetY * targetY - len1 * len1 - len2 * len2) / cosDenom
  116. if cos < -1 then
  117. cos = -1
  118. elseif cos > 1 then
  119. cos = 1
  120. end
  121. local childAngle = math.acos(cos) * bendDirection
  122. local adjacent = len1 + len2 * cos
  123. local opposite = len2 * math.sin(childAngle)
  124. local parentAngle = math.atan2(targetY * adjacent - targetX * opposite, targetX * adjacent + targetY * opposite)
  125. local rotation = (parentAngle - offset) * radDeg - parentRotation
  126. if rotation > 180 then
  127. rotation = rotation - 360
  128. elseif rotation < -180 then
  129. rotation = rotation + 360
  130. end
  131. parent.rotationIK = parentRotation + rotation * alpha
  132. rotation = (childAngle + offset) * radDeg - childRotation
  133. if rotation > 180 then
  134. rotation = rotation - 360
  135. elseif rotation < -180 then
  136. rotation = rotation + 360
  137. end
  138. child.rotationIK = childRotation + (rotation + parent.worldRotation - child.parent.worldRotation) * alpha
  139. end
  140. return IkConstraint