瀏覽代碼

Merge pull request #17241 from WestLangley/dev_pointer_lock

PointerLockControls: added .moveForward(), .moveRight() methods
Michael Herzog 6 年之前
父節點
當前提交
01d8fec095

+ 23 - 0
examples/js/controls/PointerLockControls.js

@@ -22,6 +22,8 @@ THREE.PointerLockControls = function ( camera, domElement ) {
 
 	var PI_2 = Math.PI / 2;
 
+	var vec = new Vector3();
+
 	function onMouseMove( event ) {
 
 		if ( scope.isLocked === false ) return;
@@ -106,6 +108,27 @@ THREE.PointerLockControls = function ( camera, domElement ) {
 
 	}();
 
+	this.moveForward = function ( distance ) {
+
+		// move forward parallel to the xz-plane
+		// assumes camera.up is y-up
+
+		vec.setFromMatrixColumn( camera.matrix, 0 );
+
+		vec.crossVectors( camera.up, vec );
+
+		camera.position.addScaledVector( vec, distance );
+
+	};
+
+	this.moveRight = function ( distance ) {
+
+		vec.setFromMatrixColumn( camera.matrix, 0 );
+
+		camera.position.addScaledVector( vec, distance );
+
+	};
+
 	this.lock = function () {
 
 		this.domElement.requestPointerLock();

+ 23 - 0
examples/jsm/controls/PointerLockControls.js

@@ -28,6 +28,8 @@ var PointerLockControls = function ( camera, domElement ) {
 
 	var PI_2 = Math.PI / 2;
 
+	var vec = new Vector3();
+
 	function onMouseMove( event ) {
 
 		if ( scope.isLocked === false ) return;
@@ -112,6 +114,27 @@ var PointerLockControls = function ( camera, domElement ) {
 
 	}();
 
+	this.moveForward = function ( distance ) {
+
+		// move forward parallel to the xz-plane
+		// assumes camera.up is y-up
+
+		vec.setFromMatrixColumn( camera.matrix, 0 );
+
+		vec.crossVectors( camera.up, vec );
+
+		camera.position.addScaledVector( vec, distance );
+
+	};
+
+	this.moveRight = function ( distance ) {
+
+		vec.setFromMatrixColumn( camera.matrix, 0 );
+
+		camera.position.addScaledVector( vec, distance );
+
+	};
+
 	this.lock = function () {
 
 		this.domElement.requestPointerLock();

+ 5 - 3
examples/misc_controls_pointerlock.html

@@ -305,7 +305,7 @@
 					velocity.y -= 9.8 * 100.0 * delta; // 100.0 = mass
 
 					direction.z = Number( moveForward ) - Number( moveBackward );
-					direction.x = Number( moveLeft ) - Number( moveRight );
+					direction.x = Number( moveRight ) - Number( moveLeft );
 					direction.normalize(); // this ensures consistent movements in all directions
 
 					if ( moveForward || moveBackward ) velocity.z -= direction.z * 400.0 * delta;
@@ -317,9 +317,11 @@
 						canJump = true;
 
 					}
-					controls.getObject().translateX( velocity.x * delta );
+
+					controls.moveRight( - velocity.x * delta );
+					controls.moveForward( - velocity.z * delta );
+
 					controls.getObject().position.y += ( velocity.y * delta ); // new behavior
-					controls.getObject().translateZ( velocity.z * delta );
 
 					if ( controls.getObject().position.y < 10 ) {