ソースを参照

Added POV movement & rotation

jeff Palmer 10 年 前
コミット
3009b1f364
1 ファイル変更56 行追加1 行削除
  1. 56 1
      Babylon/Mesh/babylon.abstractMesh.ts

+ 56 - 1
Babylon/Mesh/babylon.abstractMesh.ts

@@ -28,6 +28,7 @@
         }
 
         // Properties
+        public definedFacingForward = true; // orientation for POV movement & rotation
         public position = new Vector3(0, 0, 0);
         public rotation = new Vector3(0, 0, 0);
         public rotationQuaternion: Quaternion;
@@ -252,7 +253,61 @@
                 this.position.z = absolutePositionZ;
             }
         }
-
+        // ================================== Point of View Movement =================================
+        /**
+         * Perform relative position change from the point of view of behind the front of the mesh.
+         * This is performed taking into account the meshes current rotation, so you do not have to care.
+         * Supports definition of mesh facing forward or backward.
+         * @param {number} amountRight
+         * @param {number} amountUp
+         * @param {number} amountForward
+         */
+        public movePOV(amountRight : number, amountUp : number, amountForward : number) : void {
+            this.position.addInPlace(this.calcMovePOV(amountRight, amountUp, amountForward));
+        }
+        
+        /**
+         * Calculate relative position change from the point of view of behind the front of the mesh.
+         * This is performed taking into account the meshes current rotation, so you do not have to care.
+         * Supports definition of mesh facing forward or backward.
+         * @param {number} amountRight
+         * @param {number} amountUp
+         * @param {number} amountForward
+         */
+        public calcMovePOV(amountRight : number, amountUp : number, amountForward : number) : BABYLON.Vector3 {
+            var rotMatrix = new BABYLON.Matrix();
+            var rotQuaternion = (this.rotationQuaternion) ? this.rotationQuaternion : BABYLON.Quaternion.RotationYawPitchRoll(this.rotation.y, this.rotation.x, this.rotation.z);
+            rotQuaternion.toRotationMatrix(rotMatrix);
+            
+            var translationDelta = BABYLON.Vector3.Zero();
+            var defForwardMult = this.definedFacingForward ? -1 : 1;
+            BABYLON.Vector3.TransformCoordinatesFromFloatsToRef(amountRight * defForwardMult, amountUp, amountForward * defForwardMult, rotMatrix, translationDelta);
+            return translationDelta;
+        }
+        // ================================== Point of View Rotation =================================
+        /**
+         * Perform relative rotation change from the point of view of behind the front of the mesh.
+         * Supports definition of mesh facing forward or backward.
+         * @param {number} flipBack
+         * @param {number} twirlClockwise
+         * @param {number} tiltRight
+         */
+        public rotatePOV(flipBack : number, twirlClockwise : number, tiltRight : number) : void {
+            this.rotation.addInPlace(this.calcRotatePOV(flipBack, twirlClockwise, tiltRight));
+        }
+        
+        /**
+         * Calculate relative rotation change from the point of view of behind the front of the mesh.
+         * Supports definition of mesh facing forward or backward.
+         * @param {number} flipBack
+         * @param {number} twirlClockwise
+         * @param {number} tiltRight
+         */
+        public calcRotatePOV(flipBack : number, twirlClockwise : number, tiltRight : number) : BABYLON.Vector3 {
+            var defForwardMult = this.definedFacingForward ? 1 : -1;
+            return new BABYLON.Vector3(flipBack * defForwardMult, twirlClockwise, tiltRight * defForwardMult);
+        }
+        
         public setPivotMatrix(matrix: Matrix): void {
             this._pivotMatrix = matrix;
             this._cache.pivotMatrixUpdated = true;