babylon.positionAndRotation.tests.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * Describes the test suite.
  3. */
  4. describe('Babylon position and rotation', () => {
  5. let subject: BABYLON.Engine;
  6. /**
  7. * Loads the dependencies.
  8. */
  9. before(function(done) {
  10. this.timeout(180000);
  11. (BABYLONDEVTOOLS).Loader
  12. .useDist()
  13. .testMode()
  14. .load(function() {
  15. // Force apply promise polyfill for consistent behavior between chrome headless, IE11, and other browsers.
  16. BABYLON.PromisePolyfill.Apply(true);
  17. done();
  18. });
  19. });
  20. /**
  21. * Create a new engine subject before each test.
  22. */
  23. beforeEach(function() {
  24. subject = new BABYLON.NullEngine({
  25. renderHeight: 256,
  26. renderWidth: 256,
  27. textureSize: 256,
  28. deterministicLockstep: false,
  29. lockstepMaxSteps: 1
  30. });
  31. });
  32. describe('#position and rotation:', () => {
  33. it('converts between quaternions/euler', () => {
  34. // Converting between quaternions/euler
  35. var originalRotation = new BABYLON.Vector3(0.1, 0.2, 0.3);
  36. var v = originalRotation.clone()
  37. var q = BABYLON.Quaternion.FromEulerVector(v)
  38. q.toEulerAnglesToRef(v)
  39. expect(v.subtract(originalRotation).length() < 0.00001).to.equal(true)
  40. });
  41. it('reorders vector in place', () => {
  42. var originalRotation = new BABYLON.Vector3(0.1, 0.2, 0.3);
  43. var v = originalRotation.clone()
  44. v.reorderInPlace("ZYX")
  45. expect(v.subtract(new BABYLON.Vector3(0.3, 0.2, 0.1)).length() < 0.00001).to.equal(true)
  46. });
  47. it('handles parenting', () => {
  48. // Parent child positions
  49. const scene = new BABYLON.Scene(subject);
  50. var child = new BABYLON.AbstractMesh("", scene)
  51. var parent = new BABYLON.AbstractMesh("", scene)
  52. parent.position.set(0, 0, 1)
  53. child.position.set(0, 0, -1)
  54. child.parent = parent
  55. child.computeWorldMatrix()
  56. expect(child.absolutePosition.equals(new BABYLON.Vector3(0, 0, 0))).to.equal(true)
  57. //Rotate parent around child
  58. parent.rotationQuaternion = new BABYLON.Quaternion()
  59. var eulerRotation = new BABYLON.Vector3(0, Math.PI / 2, 0)
  60. var rotation = new BABYLON.Quaternion()
  61. BABYLON.Quaternion.RotationYawPitchRollToRef(eulerRotation.y, eulerRotation.x, eulerRotation.z, rotation)
  62. parent.rotationQuaternion.multiplyInPlace(rotation);
  63. parent.position.rotateByQuaternionAroundPointToRef(rotation, child.absolutePosition, parent.position)
  64. expect(parent.position.subtract(new BABYLON.Vector3(1, 0, 0)).length() < 0.00001).to.equal(true)
  65. expect(parent.rotationQuaternion.toEulerAngles().subtract(eulerRotation).length() < 0.00001).to.equal(true)
  66. expect(child.absolutePosition.subtract(new BABYLON.Vector3(0, 0, 0)).length() < 0.00001).to.equal(true)
  67. });
  68. });
  69. });