Skip to content

Commit 01d8fec

Browse files
authored
Merge pull request #17241 from WestLangley/dev_pointer_lock
PointerLockControls: added .moveForward(), .moveRight() methods
2 parents e627205 + 1f3c444 commit 01d8fec

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

examples/js/controls/PointerLockControls.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ THREE.PointerLockControls = function ( camera, domElement ) {
2222

2323
var PI_2 = Math.PI / 2;
2424

25+
var vec = new Vector3();
26+
2527
function onMouseMove( event ) {
2628

2729
if ( scope.isLocked === false ) return;
@@ -106,6 +108,27 @@ THREE.PointerLockControls = function ( camera, domElement ) {
106108

107109
}();
108110

111+
this.moveForward = function ( distance ) {
112+
113+
// move forward parallel to the xz-plane
114+
// assumes camera.up is y-up
115+
116+
vec.setFromMatrixColumn( camera.matrix, 0 );
117+
118+
vec.crossVectors( camera.up, vec );
119+
120+
camera.position.addScaledVector( vec, distance );
121+
122+
};
123+
124+
this.moveRight = function ( distance ) {
125+
126+
vec.setFromMatrixColumn( camera.matrix, 0 );
127+
128+
camera.position.addScaledVector( vec, distance );
129+
130+
};
131+
109132
this.lock = function () {
110133

111134
this.domElement.requestPointerLock();

examples/jsm/controls/PointerLockControls.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ var PointerLockControls = function ( camera, domElement ) {
2828

2929
var PI_2 = Math.PI / 2;
3030

31+
var vec = new Vector3();
32+
3133
function onMouseMove( event ) {
3234

3335
if ( scope.isLocked === false ) return;
@@ -112,6 +114,27 @@ var PointerLockControls = function ( camera, domElement ) {
112114

113115
}();
114116

117+
this.moveForward = function ( distance ) {
118+
119+
// move forward parallel to the xz-plane
120+
// assumes camera.up is y-up
121+
122+
vec.setFromMatrixColumn( camera.matrix, 0 );
123+
124+
vec.crossVectors( camera.up, vec );
125+
126+
camera.position.addScaledVector( vec, distance );
127+
128+
};
129+
130+
this.moveRight = function ( distance ) {
131+
132+
vec.setFromMatrixColumn( camera.matrix, 0 );
133+
134+
camera.position.addScaledVector( vec, distance );
135+
136+
};
137+
115138
this.lock = function () {
116139

117140
this.domElement.requestPointerLock();

examples/misc_controls_pointerlock.html

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@
305305
velocity.y -= 9.8 * 100.0 * delta; // 100.0 = mass
306306

307307
direction.z = Number( moveForward ) - Number( moveBackward );
308-
direction.x = Number( moveLeft ) - Number( moveRight );
308+
direction.x = Number( moveRight ) - Number( moveLeft );
309309
direction.normalize(); // this ensures consistent movements in all directions
310310

311311
if ( moveForward || moveBackward ) velocity.z -= direction.z * 400.0 * delta;
@@ -317,9 +317,11 @@
317317
canJump = true;
318318

319319
}
320-
controls.getObject().translateX( velocity.x * delta );
320+
321+
controls.moveRight( - velocity.x * delta );
322+
controls.moveForward( - velocity.z * delta );
323+
321324
controls.getObject().position.y += ( velocity.y * delta ); // new behavior
322-
controls.getObject().translateZ( velocity.z * delta );
323325

324326
if ( controls.getObject().position.y < 10 ) {
325327

0 commit comments

Comments
 (0)