babylon.inputCollisionFilter.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. "use strict";
  2. var BABYLON = BABYLON || {};
  3. (function () {
  4. BABYLON.inputCollisionFilter = function (scene, target, ellipsoid) {
  5. BABYLON.inputFilter.call(this, scene, target);
  6. this._orientationMatrix = new BABYLON.Matrix();
  7. this._orientationMatrixInvert = new BABYLON.Matrix();
  8. this._transformedDirection = new BABYLON.Vector3();
  9. this._tempNewPosition = new BABYLON.Vector3();
  10. this._tempNewPosition2 = new BABYLON.Vector3();
  11. this._ellipsoid = ellipsoid || new BABYLON.Vector3(.5,.5,.5);
  12. this._collider = new BABYLON.Collider();
  13. this._collidedPosition = new BABYLON.Vector3(0, 0, 0);
  14. this._cameraHeight = 1.7;
  15. this._positionBottom = new BABYLON.Vector3(0, 0, 0);
  16. };
  17. BABYLON.inputCollisionFilter.prototype = Object.create(BABYLON.inputFilter.prototype);
  18. BABYLON.inputCollisionFilter.prototype.moveRelative = function (relativeMovement) {
  19. var rotation = this.getOrientation();
  20. BABYLON.Matrix.RotationYawPitchRollToRef(rotation.yaw, rotation.pitch, rotation.roll, this._orientationMatrix);
  21. BABYLON.Vector3.TransformNormalToRef(relativeMovement, this._orientationMatrix, this._transformedDirection);
  22. this.getPosition().addToRef(this._transformedDirection, this._tempNewPosition);
  23. //this._tempNewPosition.y -= this._ellipsoid.y;
  24. this._collider.radius = this._ellipsoid;
  25. var p = this.getPosition();
  26. this._positionBottom.x = p.x;
  27. this._positionBottom.y = p.y;
  28. this._positionBottom.z = p.z;
  29. this._positionBottom.y += this._ellipsoid.y - this._cameraHeight;
  30. this.scene._getNewPosition(this._positionBottom, this._transformedDirection, this._collider, 3, this._collidedPosition);
  31. this._collidedPosition.subtractToRef(this._positionBottom, this._tempNewPosition2);
  32. if (this._tempNewPosition2.length() > BABYLON.Engine.collisionsEpsilon) {
  33. this._orientationMatrix.invertToRef(this._orientationMatrixInvert);
  34. BABYLON.Vector3.TransformNormalToRef(this._tempNewPosition2, this._orientationMatrixInvert, this._tempNewPosition);
  35. this.target.moveRelative(this._tempNewPosition);
  36. }
  37. };
  38. })();