瀏覽代碼

ArcRotateCamera can now check collisions

David Catuhe 11 年之前
父節點
當前提交
54de3e21ba

+ 31 - 0
Babylon/Cameras/babylon.arcRotateCamera.js

@@ -34,6 +34,12 @@ var BABYLON;
             this.zoomOnFactor = 1;
             this._keys = [];
             this._viewMatrix = new BABYLON.Matrix();
+            this.checkCollisions = false;
+            this.collisionRadius = new BABYLON.Vector3(0.5, 0.5, 0.5);
+            this._collider = new BABYLON.Collider();
+            this._previousPosition = BABYLON.Vector3.Zero();
+            this._collisionVelocity = BABYLON.Vector3.Zero();
+            this._newPosition = BABYLON.Vector3.Zero();
 
             this.getViewMatrix();
         }
@@ -365,8 +371,33 @@ var BABYLON;
             var target = this._getTargetPosition();
 
             target.addToRef(new BABYLON.Vector3(this.radius * cosa * sinb, this.radius * cosb, this.radius * sina * sinb), this.position);
+
+            if (this.checkCollisions) {
+                this._collider.radius = this.collisionRadius;
+                this.position.subtractToRef(this._previousPosition, this._collisionVelocity);
+
+                this.getScene()._getNewPosition(this._previousPosition, this._collisionVelocity, this._collider, 3, this._newPosition);
+
+                if (!this._newPosition.equalsWithEpsilon(this.position)) {
+                    this.position.copyFrom(this._previousPosition);
+
+                    this.alpha = this._previousAlpha;
+                    this.beta = this._previousBeta;
+                    this.radius = this._previousRadius;
+
+                    if (this.onCollide) {
+                        this.onCollide(this._collider.collidedMesh);
+                    }
+                }
+            }
+
             BABYLON.Matrix.LookAtLHToRef(this.position, target, this.upVector, this._viewMatrix);
 
+            this._previousAlpha = this.alpha;
+            this._previousBeta = this.beta;
+            this._previousRadius = this.radius;
+            this._previousPosition.copyFrom(this.position);
+
             return this._viewMatrix;
         };
 

+ 37 - 0
Babylon/Cameras/babylon.arcRotateCamera.ts

@@ -36,6 +36,18 @@
         private _onGesture: (e: MSGestureEvent) => void;
         private _MSGestureHandler: MSGesture;
 
+        // Collisions
+        public onCollide: (collidedMesh: AbstractMesh) => void;
+        public checkCollisions = false;
+        public collisionRadius = new Vector3(0.5, 0.5, 0.5);
+        private _collider = new Collider();
+        private _previousPosition = Vector3.Zero();
+        private _collisionVelocity = Vector3.Zero();
+        private _newPosition = Vector3.Zero();
+        private _previousAlpha: number;
+        private _previousBeta: number;
+        private _previousRadius: number;
+
         constructor(name: string, public alpha: number, public beta: number, public radius: number, public target: any, scene: Scene) {
             super(name, BABYLON.Vector3.Zero(), scene);
 
@@ -383,8 +395,33 @@
             var target = this._getTargetPosition();
 
             target.addToRef(new BABYLON.Vector3(this.radius * cosa * sinb, this.radius * cosb, this.radius * sina * sinb), this.position);
+
+            if (this.checkCollisions) {
+                this._collider.radius = this.collisionRadius;
+                this.position.subtractToRef(this._previousPosition, this._collisionVelocity);
+
+                this.getScene()._getNewPosition(this._previousPosition, this._collisionVelocity, this._collider, 3, this._newPosition);
+
+                if (!this._newPosition.equalsWithEpsilon(this.position)) {
+                    this.position.copyFrom(this._previousPosition);
+
+                    this.alpha = this._previousAlpha;
+                    this.beta = this._previousBeta;
+                    this.radius = this._previousRadius;
+
+                    if (this.onCollide) {
+                        this.onCollide(this._collider.collidedMesh);
+                    }
+                }
+            }
+
             Matrix.LookAtLHToRef(this.position, target, this.upVector, this._viewMatrix);
 
+            this._previousAlpha = this.alpha;
+            this._previousBeta = this.beta;
+            this._previousRadius = this.radius;
+            this._previousPosition.copyFrom(this.position);
+
             return this._viewMatrix;
         }
 

+ 0 - 1
Babylon/Cameras/babylon.freeCamera.js

@@ -24,7 +24,6 @@ var BABYLON;
             this.noRotationConstraint = false;
             this.angularSensibility = 2000.0;
             this.lockedTarget = null;
-            this.onCollide = null;
             this._keys = [];
             this._collider = new BABYLON.Collider();
             this._needMoveForGravity = true;

+ 1 - 1
Babylon/Cameras/babylon.freeCamera.ts

@@ -14,7 +14,7 @@
         public noRotationConstraint = false;
         public angularSensibility = 2000.0;
         public lockedTarget = null;
-        public onCollide = null;
+        public onCollide: (collidedMesh: AbstractMesh) => void;
 
         private _keys = [];
         private _collider = new Collider();

+ 4 - 0
Babylon/Math/babylon.math.js

@@ -515,6 +515,10 @@
             return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z;
         };
 
+        Vector3.prototype.equalsWithEpsilon = function (otherVector) {
+            return Math.abs(this.x - otherVector.x) < BABYLON.Engine.Epsilon && Math.abs(this.y - otherVector.y) < BABYLON.Engine.Epsilon && Math.abs(this.z - otherVector.z) < BABYLON.Engine.Epsilon;
+        };
+
         Vector3.prototype.equalsToFloats = function (x, y, z) {
             return this.x === x && this.y === y && this.z === z;
         };

+ 6 - 0
Babylon/Math/babylon.math.ts

@@ -483,6 +483,12 @@
             return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z;
         }
 
+        public equalsWithEpsilon(otherVector: Vector3): boolean {
+            return  Math.abs(this.x - otherVector.x) < Engine.Epsilon &&
+                Math.abs(this.y - otherVector.y) < Engine.Epsilon &&
+                Math.abs(this.z - otherVector.z) < Engine.Epsilon;
+        }
+
         public equalsToFloats(x: number, y: number, z: number): boolean {
             return this.x === x && this.y === y && this.z === z;
         }

文件差異過大導致無法顯示
+ 1 - 1
babylon.1.13-beta-debug.js


文件差異過大導致無法顯示
+ 7 - 7
babylon.1.13-beta.js