瀏覽代碼

Fix for ArcRotate collision detection

now works much more stable. Is also correctly calculating collisions on
the main thread.
Worker collisions are, for some reason, not correctly calculated. will
inspect fix in a future commit..
Raanan Weber 10 年之前
父節點
當前提交
0db991bd69
共有 2 個文件被更改,包括 35 次插入25 次删除
  1. 10 4
      Babylon/Cameras/babylon.arcRotateCamera.js
  2. 25 21
      Babylon/Cameras/babylon.arcRotateCamera.ts

+ 10 - 4
Babylon/Cameras/babylon.arcRotateCamera.js

@@ -45,7 +45,7 @@ var BABYLON;
             this._newPosition = BABYLON.Vector3.Zero();
             this._onCollisionPositionChange = function (collisionId, newPosition, collidedMesh) {
                 if (collidedMesh === void 0) { collidedMesh = null; }
-                if (_this.getScene().workerCollisions) {
+                if (_this.getScene().workerCollisions && _this.checkCollisions) {
                     newPosition.multiplyInPlace(_this._collider.radius);
                 }
                 if (!newPosition.equalsWithEpsilon(_this.position)) {
@@ -54,6 +54,9 @@ var BABYLON;
                     _this.beta = _this._previousBeta;
                     _this.radius = _this._previousRadius;
                 }
+                BABYLON.Matrix.LookAtLHToRef(_this.position, _this._getTargetPosition(), _this.upVector, _this._viewMatrix);
+                _this._viewMatrix.m[12] += _this.targetScreenOffset.x;
+                _this._viewMatrix.m[13] += _this.targetScreenOffset.y;
                 if (collidedMesh && _this.onCollide) {
                     _this.onCollide(collidedMesh);
                 }
@@ -365,13 +368,16 @@ var BABYLON;
                 this._collisionTriggered = true;
                 this.getScene().collisionCoordinator.getNewPosition(this._previousPosition, this._collisionVelocity, this._collider, 3, null, this._onCollisionPositionChange, this.uniqueId);
             }
-            BABYLON.Matrix.LookAtLHToRef(this.position, target, this.upVector, this._viewMatrix);
+            else {
+                BABYLON.Matrix.LookAtLHToRef(this.position, target, this.upVector, this._viewMatrix);
+                this._viewMatrix.m[12] += this.targetScreenOffset.x;
+                this._viewMatrix.m[13] += this.targetScreenOffset.y;
+            }
+            //TODO the definition "previous" is not entirely true.
             this._previousAlpha = this.alpha;
             this._previousBeta = this.beta;
             this._previousRadius = this.radius;
             this._previousPosition.copyFrom(this.position);
-            this._viewMatrix.m[12] += this.targetScreenOffset.x;
-            this._viewMatrix.m[13] += this.targetScreenOffset.y;
             return this._viewMatrix;
         };
         ArcRotateCamera.prototype.zoomOn = function (meshes) {

+ 25 - 21
Babylon/Cameras/babylon.arcRotateCamera.ts

@@ -431,40 +431,44 @@
 
             target.addToRef(new Vector3(this.radius * cosa * sinb, this.radius * cosb, this.radius * sina * sinb), this.position);
 
-            if (this.checkCollisions) {
+			if (this.checkCollisions) {
                 this._collider.radius = this.collisionRadius;
                 this.position.subtractToRef(this._previousPosition, this._collisionVelocity);
 
                 this._collisionTriggered = true;
                 this.getScene().collisionCoordinator.getNewPosition(this._previousPosition, this._collisionVelocity, this._collider, 3, null, this._onCollisionPositionChange, this.uniqueId);
-            }
-
-            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);
-
-            this._viewMatrix.m[12] += this.targetScreenOffset.x;
-            this._viewMatrix.m[13] += this.targetScreenOffset.y;
+            } else {
+				Matrix.LookAtLHToRef(this.position, target, this.upVector, this._viewMatrix);
+				this._viewMatrix.m[12] += this.targetScreenOffset.x;
+				this._viewMatrix.m[13] += this.targetScreenOffset.y;
+			}
+			
+			//TODO the definition "previous" is not entirely true.
+			this._previousAlpha = this.alpha;
+			this._previousBeta = this.beta;
+			this._previousRadius = this.radius;
+			this._previousPosition.copyFrom(this.position);
 
             return this._viewMatrix;
         }
 
         private _onCollisionPositionChange = (collisionId: number, newPosition: Vector3, collidedMesh: AbstractMesh = null) => {
 
-            if (this.getScene().workerCollisions) {
+            if (this.getScene().workerCollisions && this.checkCollisions) {
                 newPosition.multiplyInPlace(this._collider.radius);
 			}
-
-            if (!newPosition.equalsWithEpsilon(this.position)) {
-                this.position.copyFrom(this._previousPosition);
-
-                this.alpha = this._previousAlpha;
-                this.beta = this._previousBeta;
-                this.radius = this._previousRadius;
-            } 
+			
+			if (!newPosition.equalsWithEpsilon(this.position)) {
+                 this.position.copyFrom(this._previousPosition);	
+ 		 
+                 this.alpha = this._previousAlpha;		      
+                 this.beta = this._previousBeta;
+                 this.radius = this._previousRadius;
+			} 
+			
+			Matrix.LookAtLHToRef(this.position, this._getTargetPosition(), this.upVector, this._viewMatrix);
+			this._viewMatrix.m[12] += this.targetScreenOffset.x;
+			this._viewMatrix.m[13] += this.targetScreenOffset.y;
 			
             if (collidedMesh && this.onCollide) {
                this.onCollide(collidedMesh);