boneIKController.ts 11 KB

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