forked from schteppe/cannon.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPointerLockControls.js
177 lines (132 loc) · 4.87 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* @author mrdoob / http://mrdoob.com/
* @author schteppe / https://github.com/schteppe
*/
var PointerLockControls = function ( camera, cannonBody ) {
var eyeYPos = 2; // eyes are 2 meters above the ground
var velocityFactor = 0.2;
var jumpVelocity = 20;
var scope = this;
var pitchObject = new THREE.Object3D();
pitchObject.add( camera );
var yawObject = new THREE.Object3D();
yawObject.position.y = 2;
yawObject.add( pitchObject );
var quat = new THREE.Quaternion();
var moveForward = false;
var moveBackward = false;
var moveLeft = false;
var moveRight = false;
var canJump = false;
var contactNormal = new CANNON.Vec3(); // Normal in the contact, pointing *out* of whatever the player touched
var upAxis = new CANNON.Vec3(0,1,0);
cannonBody.addEventListener("collide",function(e){
var contact = e.contact;
// contact.bi and contact.bj are the colliding bodies, and contact.ni is the collision normal.
// We do not yet know which one is which! Let's check.
if(contact.bi.id == cannonBody.id) // bi is the player body, flip the contact normal
contact.ni.negate(contactNormal);
else
contactNormal.copy(contact.ni); // bi is something else. Keep the normal as it is
// If contactNormal.dot(upAxis) is between 0 and 1, we know that the contact normal is somewhat in the up direction.
if(contactNormal.dot(upAxis) > 0.5) // Use a "good" threshold value between 0 and 1 here!
canJump = true;
});
var velocity = cannonBody.velocity;
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 = jumpVelocity;
}
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.getDirection = function(targetVec){
targetVec.set(0,0,-1);
quat.multiplyVector3(targetVec);
}
// Moves the camera to the Cannon.js object position and adds velocity to the object if the run key is down
var inputVelocity = new THREE.Vector3();
var euler = new THREE.Euler();
this.update = function ( delta ) {
if ( scope.enabled === false ) return;
delta *= 0.1;
inputVelocity.set(0,0,0);
if ( moveForward ){
inputVelocity.z = -velocityFactor * delta;
}
if ( moveBackward ){
inputVelocity.z = velocityFactor * delta;
}
if ( moveLeft ){
inputVelocity.x = -velocityFactor * delta;
}
if ( moveRight ){
inputVelocity.x = velocityFactor * delta;
}
// Convert velocity to world coordinates
euler.x = pitchObject.rotation.x;
euler.y = yawObject.rotation.y;
euler.order = "XYZ";
quat.setFromEuler(euler);
inputVelocity.applyQuaternion(quat);
//quat.multiplyVector3(inputVelocity);
// Add to the object
velocity.x += inputVelocity.x;
velocity.z += inputVelocity.z;
yawObject.position.copy(cannonBody.position);
};
};