babylon.boneIKController.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. module BABYLON {
  2. /**
  3. * Class used to apply inverse kinematics to bones
  4. * @see http://doc.babylonjs.com/how_to/how_to_use_bones_and_skeletons#boneikcontroller
  5. */
  6. export class BoneIKController {
  7. private static _tmpVecs: Vector3[] = [Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero()];
  8. private static _tmpQuat = Quaternion.Identity();
  9. private static _tmpMats: Matrix[] = [Matrix.Identity(), Matrix.Identity()];
  10. /**
  11. * Gets or sets the target mesh
  12. */
  13. public targetMesh: AbstractMesh;
  14. /** Gets or sets the mesh used as pole */
  15. public poleTargetMesh: AbstractMesh;
  16. /**
  17. * Gets or sets the bone used as pole
  18. */
  19. public poleTargetBone: Nullable<Bone>;
  20. /**
  21. * Gets or sets the target position
  22. */
  23. public targetPosition = Vector3.Zero();
  24. /**
  25. * Gets or sets the pole target position
  26. */
  27. public poleTargetPosition = Vector3.Zero();
  28. /**
  29. * Gets or sets the pole target local offset
  30. */
  31. public poleTargetLocalOffset = Vector3.Zero();
  32. /**
  33. * Gets or sets the pole angle
  34. */
  35. public poleAngle = 0;
  36. /**
  37. * Gets or sets the mesh associated with the controller
  38. */
  39. public mesh: AbstractMesh;
  40. /**
  41. * The amount to slerp (spherical linear interpolation) to the target. Set this to a value between 0 and 1 (a value of 1 disables slerp)
  42. */
  43. public slerpAmount = 1;
  44. private _bone1Quat = Quaternion.Identity();
  45. private _bone1Mat = Matrix.Identity();
  46. private _bone2Ang = Math.PI;
  47. private _bone1: Nullable<Bone>;
  48. private _bone2: Bone;
  49. private _bone1Length: number;
  50. private _bone2Length: number;
  51. private _maxAngle = Math.PI;
  52. private _maxReach: number;
  53. private _rightHandedSystem = false;
  54. private _bendAxis = Vector3.Right();
  55. private _slerping = false;
  56. private _adjustRoll = 0;
  57. /**
  58. * Gets or sets maximum allowed angle
  59. */
  60. public get maxAngle(): number {
  61. return this._maxAngle;
  62. }
  63. public set maxAngle(value: number) {
  64. this._setMaxAngle(value);
  65. }
  66. /**
  67. * Creates a new BoneIKController
  68. * @param mesh defines the mesh to control
  69. * @param bone defines the bone to control
  70. * @param options defines options to set up the controller
  71. */
  72. constructor(mesh: AbstractMesh,
  73. bone: Bone,
  74. options?: {
  75. targetMesh?: AbstractMesh,
  76. poleTargetMesh?: AbstractMesh,
  77. poleTargetBone?: Bone,
  78. poleTargetLocalOffset?: Vector3,
  79. poleAngle?: number,
  80. bendAxis?: Vector3,
  81. maxAngle?: number,
  82. slerpAmount?: number
  83. }) {
  84. this._bone2 = bone;
  85. this._bone1 = bone.getParent();
  86. if (!this._bone1) {
  87. return;
  88. }
  89. this.mesh = mesh;
  90. var bonePos = bone.getPosition();
  91. if (bone.getAbsoluteTransform().determinant() > 0) {
  92. this._rightHandedSystem = true;
  93. this._bendAxis.x = 0;
  94. this._bendAxis.y = 0;
  95. this._bendAxis.z = -1;
  96. if (bonePos.x > bonePos.y && bonePos.x > bonePos.z) {
  97. this._adjustRoll = Math.PI * .5;
  98. this._bendAxis.z = 1;
  99. }
  100. }
  101. if (this._bone1.length) {
  102. var boneScale1 = this._bone1.getScale();
  103. var boneScale2 = this._bone2.getScale();
  104. this._bone1Length = this._bone1.length * boneScale1.y * this.mesh.scaling.y;
  105. this._bone2Length = this._bone2.length * boneScale2.y * this.mesh.scaling.y;
  106. } else if (this._bone1.children[0]) {
  107. mesh.computeWorldMatrix(true);
  108. var pos1 = this._bone2.children[0].getAbsolutePosition(mesh);
  109. var pos2 = this._bone2.getAbsolutePosition(mesh);
  110. var pos3 = this._bone1.getAbsolutePosition(mesh);
  111. this._bone1Length = Vector3.Distance(pos1, pos2);
  112. this._bone2Length = Vector3.Distance(pos2, pos3);
  113. }
  114. this._bone1.getRotationMatrixToRef(Space.WORLD, mesh, this._bone1Mat);
  115. this.maxAngle = Math.PI;
  116. if (options) {
  117. if (options.targetMesh) {
  118. this.targetMesh = options.targetMesh;
  119. this.targetMesh.computeWorldMatrix(true);
  120. }
  121. if (options.poleTargetMesh) {
  122. this.poleTargetMesh = options.poleTargetMesh;
  123. this.poleTargetMesh.computeWorldMatrix(true);
  124. } else if (options.poleTargetBone) {
  125. this.poleTargetBone = options.poleTargetBone;
  126. } else if (this._bone1.getParent()) {
  127. this.poleTargetBone = this._bone1.getParent();
  128. }
  129. if (options.poleTargetLocalOffset) {
  130. this.poleTargetLocalOffset.copyFrom(options.poleTargetLocalOffset);
  131. }
  132. if (options.poleAngle) {
  133. this.poleAngle = options.poleAngle;
  134. }
  135. if (options.bendAxis) {
  136. this._bendAxis.copyFrom(options.bendAxis);
  137. }
  138. if (options.maxAngle) {
  139. this.maxAngle = options.maxAngle;
  140. }
  141. if (options.slerpAmount) {
  142. this.slerpAmount = options.slerpAmount;
  143. }
  144. }
  145. }
  146. private _setMaxAngle(ang: number): void {
  147. if (ang < 0) {
  148. ang = 0;
  149. }
  150. if (ang > Math.PI || ang == undefined) {
  151. ang = Math.PI;
  152. }
  153. this._maxAngle = ang;
  154. var a = this._bone1Length;
  155. var b = this._bone2Length;
  156. this._maxReach = Math.sqrt(a * a + b * b - 2 * a * b * Math.cos(ang));
  157. }
  158. /**
  159. * Force the controller to update the bones
  160. */
  161. public update(): void {
  162. var bone1 = this._bone1;
  163. if (!bone1) {
  164. return;
  165. }
  166. var target = this.targetPosition;
  167. var poleTarget = this.poleTargetPosition;
  168. var mat1 = BoneIKController._tmpMats[0];
  169. var mat2 = BoneIKController._tmpMats[1];
  170. if (this.targetMesh) {
  171. target.copyFrom(this.targetMesh.getAbsolutePosition());
  172. }
  173. if (this.poleTargetBone) {
  174. this.poleTargetBone.getAbsolutePositionFromLocalToRef(this.poleTargetLocalOffset, this.mesh, poleTarget);
  175. }else if (this.poleTargetMesh) {
  176. Vector3.TransformCoordinatesToRef(this.poleTargetLocalOffset, this.poleTargetMesh.getWorldMatrix(), poleTarget);
  177. }
  178. var bonePos = BoneIKController._tmpVecs[0];
  179. var zaxis = BoneIKController._tmpVecs[1];
  180. var xaxis = BoneIKController._tmpVecs[2];
  181. var yaxis = BoneIKController._tmpVecs[3];
  182. var upAxis = BoneIKController._tmpVecs[4];
  183. var _tmpQuat = BoneIKController._tmpQuat;
  184. bone1.getAbsolutePositionToRef(this.mesh, bonePos);
  185. poleTarget.subtractToRef(bonePos, upAxis);
  186. if (upAxis.x == 0 && upAxis.y == 0 && upAxis.z == 0) {
  187. upAxis.y = 1;
  188. } else {
  189. upAxis.normalize();
  190. }
  191. target.subtractToRef(bonePos, yaxis);
  192. yaxis.normalize();
  193. Vector3.CrossToRef(yaxis, upAxis, zaxis);
  194. zaxis.normalize();
  195. Vector3.CrossToRef(yaxis, zaxis, xaxis);
  196. xaxis.normalize();
  197. Matrix.FromXYZAxesToRef(xaxis, yaxis, zaxis, mat1);
  198. var a = this._bone1Length;
  199. var b = this._bone2Length;
  200. var c = Vector3.Distance(bonePos, target);
  201. if (this._maxReach > 0) {
  202. c = Math.min(this._maxReach, c);
  203. }
  204. var acosa = (b * b + c * c - a * a) / (2 * b * c);
  205. var acosb = (c * c + a * a - b * b) / (2 * c * a);
  206. if (acosa > 1) {
  207. acosa = 1;
  208. }
  209. if (acosb > 1) {
  210. acosb = 1;
  211. }
  212. if (acosa < -1) {
  213. acosa = -1;
  214. }
  215. if (acosb < -1) {
  216. acosb = -1;
  217. }
  218. var angA = Math.acos(acosa);
  219. var angB = Math.acos(acosb);
  220. var angC = -angA - angB;
  221. if (this._rightHandedSystem) {
  222. Matrix.RotationYawPitchRollToRef(0, 0, this._adjustRoll, mat2);
  223. mat2.multiplyToRef(mat1, mat1);
  224. Matrix.RotationAxisToRef(this._bendAxis, angB, mat2);
  225. mat2.multiplyToRef(mat1, mat1);
  226. } else {
  227. var _tmpVec = BoneIKController._tmpVecs[5];
  228. _tmpVec.copyFrom(this._bendAxis);
  229. _tmpVec.x *= -1;
  230. Matrix.RotationAxisToRef(_tmpVec, -angB, mat2);
  231. mat2.multiplyToRef(mat1, mat1);
  232. }
  233. if (this.poleAngle) {
  234. Matrix.RotationAxisToRef(yaxis, this.poleAngle, mat2);
  235. mat1.multiplyToRef(mat2, mat1);
  236. }
  237. if (this._bone1) {
  238. if (this.slerpAmount < 1) {
  239. if (!this._slerping) {
  240. Quaternion.FromRotationMatrixToRef(this._bone1Mat, this._bone1Quat);
  241. }
  242. Quaternion.FromRotationMatrixToRef(mat1, _tmpQuat);
  243. Quaternion.SlerpToRef(this._bone1Quat, _tmpQuat, this.slerpAmount, this._bone1Quat);
  244. angC = this._bone2Ang * (1.0 - this.slerpAmount) + angC * this.slerpAmount;
  245. this._bone1.setRotationQuaternion(this._bone1Quat, Space.WORLD, this.mesh);
  246. this._slerping = true;
  247. } else {
  248. this._bone1.setRotationMatrix(mat1, Space.WORLD, this.mesh);
  249. this._bone1Mat.copyFrom(mat1);
  250. this._slerping = false;
  251. }
  252. }
  253. this._bone2.setAxisAngle(this._bendAxis, angC, Space.LOCAL);
  254. this._bone2Ang = angC;
  255. }
  256. }
  257. }