Skip to content

Commit c15c2d6

Browse files
committed
Added .moveForward(), .moveRight() methods
1 parent 61ad2ba commit c15c2d6

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
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 & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -299,27 +299,23 @@
299299
var time = performance.now();
300300
var delta = ( time - prevTime ) / 1000;
301301

302-
velocity.x -= velocity.x * 10.0 * delta;
303-
velocity.z -= velocity.z * 10.0 * delta;
304-
305302
velocity.y -= 9.8 * 100.0 * delta; // 100.0 = mass
306303

307304
direction.z = Number( moveForward ) - Number( moveBackward );
308-
direction.x = Number( moveLeft ) - Number( moveRight );
305+
direction.x = Number( moveRight ) - Number( moveLeft );
309306
direction.normalize(); // this ensures consistent movements in all directions
310307

311-
if ( moveForward || moveBackward ) velocity.z -= direction.z * 400.0 * delta;
312-
if ( moveLeft || moveRight ) velocity.x -= direction.x * 400.0 * delta;
313-
314308
if ( onObject === true ) {
315309

316310
velocity.y = Math.max( 0, velocity.y );
317311
canJump = true;
318312

319313
}
320-
controls.getObject().translateX( velocity.x * delta );
314+
315+
controls.moveRight( direction.x * 400.0 * delta );
316+
controls.moveForward( direction.z * 400.0 * delta );
317+
321318
controls.getObject().position.y += ( velocity.y * delta ); // new behavior
322-
controls.getObject().translateZ( velocity.z * delta );
323319

324320
if ( controls.getObject().position.y < 10 ) {
325321

0 commit comments

Comments
 (0)