Browse Source

adopt Spherical in editor.

Ben Houston 9 years ago
parent
commit
8c424ae663
2 changed files with 14 additions and 11 deletions
  1. 5 10
      examples/js/controls/EditorControls.js
  2. 9 1
      src/math/Spherical.js

+ 5 - 10
examples/js/controls/EditorControls.js

@@ -86,21 +86,16 @@ THREE.EditorControls = function ( object, domElement ) {
 
 
 		vector.copy( object.position ).sub( center );
 		vector.copy( object.position ).sub( center );
 
 
-		var theta = Math.atan2( vector.x, vector.z );
-		var phi = Math.atan2( Math.sqrt( vector.x * vector.x + vector.z * vector.z ), vector.y );
+		var spherical = new THREE.Spherical().fromDirection( vector );
 
 
-		theta += delta.x;
-		phi += delta.y;
+		spherical.theta += delta.x;
+		spherical.phi += delta.y;
 
 
-		var EPS = 0.000001;
-
-		phi = Math.max( EPS, Math.min( Math.PI - EPS, phi ) );
+		spherical.makeSafe();
 
 
 		var radius = vector.length();
 		var radius = vector.length();
 
 
-		vector.x = radius * Math.sin( phi ) * Math.sin( theta );
-		vector.y = radius * Math.cos( phi );
-		vector.z = radius * Math.sin( phi ) * Math.cos( theta );
+		vector = spherical.direction().multiplyScalar( radius );
 
 
 		object.position.copy( center ).add( vector );
 		object.position.copy( center ).add( vector );
 
 

+ 9 - 1
src/math/Spherical.js

@@ -9,7 +9,7 @@ THREE.Spherical = function ( phi, theta ) {
 	this.theta = ( theta !== undefined ) ? theta : 0; // around the equator of the sphere
 	this.theta = ( theta !== undefined ) ? theta : 0; // around the equator of the sphere
 
 
 	return this;
 	return this;
-	
+
 };
 };
 
 
 THREE.Spherical.prototype = {
 THREE.Spherical.prototype = {
@@ -55,6 +55,14 @@ THREE.Spherical.prototype = {
     return this;
     return this;
   },
   },
 
 
+	// restrict phi to be betwee EPS and PI-EPS
+	makeSafe: function() {
+
+		var EPS = 0.000001;
+		this.phi = Math.max( EPS, Math.min( Math.PI - EPS, this.phi ) );
+
+	},
+
   fromDirection: function( direction ) {
   fromDirection: function( direction ) {
 
 
     this.theta = Math.atan2( direction.x, direction.z ); // equator angle around y-up axis
     this.theta = Math.atan2( direction.x, direction.z ); // equator angle around y-up axis