forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPointerLockControls.js
155 lines (100 loc) · 2.75 KB
/
PointerLockControls.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
var PointerLockControls = function ( camera ) {
var scope = this;
var pitchObject = new THREE.Object3D();
pitchObject.add( camera );
var yawObject = new THREE.Object3D();
yawObject.position.y = 10;
yawObject.add( pitchObject );
var moveForward = false;
var moveBackward = false;
var moveLeft = false;
var moveRight = false;
var isOnObject = false;
var canJump = false;
var velocity = new THREE.Vector3();
var PI_2 = Math.PI / 2;
var onMouseMove = function ( event ) {
if ( scope.enabled === false ) return;
var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
var movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
yawObject.rotation.y -= movementX * 0.002;
pitchObject.rotation.x -= movementY * 0.002;
pitchObject.rotation.x = Math.max( - PI_2, Math.min( PI_2, pitchObject.rotation.x ) );
};
var onKeyDown = function ( event ) {
switch ( event.keyCode ) {
case 38: // up
case 87: // w
moveForward = true;
break;
case 37: // left
case 65: // a
moveLeft = true; break;
case 40: // down
case 83: // s
moveBackward = true;
break;
case 39: // right
case 68: // d
moveRight = true;
break;
case 32: // space
if ( canJump === true ) velocity.y += 10;
canJump = false;
break;
}
};
var onKeyUp = function ( event ) {
switch( event.keyCode ) {
case 38: // up
case 87: // w
moveForward = false;
break;
case 37: // left
case 65: // a
moveLeft = false;
break;
case 40: // down
case 83: // a
moveBackward = false;
break;
case 39: // right
case 68: // d
moveRight = false;
break;
}
};
document.addEventListener( 'mousemove', onMouseMove, false );
document.addEventListener( 'keydown', onKeyDown, false );
document.addEventListener( 'keyup', onKeyUp, false );
this.enabled = false;
this.getObject = function () {
return yawObject;
};
this.isOnObject = function ( boolean ) {
isOnObject = boolean;
canJump = boolean;
};
this.update = function ( delta ) {
if ( scope.enabled === false ) return;
delta *= 0.1;
velocity.x += ( - velocity.x ) * 0.08 * delta;
velocity.z += ( - velocity.z ) * 0.08 * delta;
velocity.y -= 0.25 * delta;
if ( moveForward ) velocity.z -= 0.12 * delta;
if ( moveBackward ) velocity.z += 0.12 * delta;
if ( moveLeft ) velocity.x -= 0.12 * delta;
if ( moveRight ) velocity.x += 0.12 * delta;
if ( isOnObject === true ) {
velocity.y = Math.max( 0, velocity.y );
}
yawObject.translateX( velocity.x );
yawObject.translateY( velocity.y );
yawObject.translateZ( velocity.z );
if ( yawObject.position.y < 10 ) {
velocity.y = 0;
yawObject.position.y = 10;
canJump = true;
}
};
};