-
Notifications
You must be signed in to change notification settings - Fork 6
/
LeapControls.js
125 lines (81 loc) · 3.32 KB
/
LeapControls.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
/**
* Hard work done by:
* @author James Baicoianu / http://www.baicoianu.com/
*
* Leap work done by:
* @author Cabbibo / http://www.cabbibo.com/
*
*/
THREE.LeapFlyControls = function ( object, controller, domElement ) {
this.object = object;
this.controller = controller;
this.domElement = ( domElement !== undefined ) ? domElement : document;
//if ( domElement ) this.domElement.setAttribute( 'tabindex', -1 );
// API
this.rollSpeed = .005;
this.lookSpeed = .008;
this.movementSpeed = .10;
this.directionFactor = .01;
this.positionFactor = .01;
this.weakDampening = .99;
this.strongDampening = .90;
this.dampening = this.weakDampening;
this.speed = 0;
this.acceleration = 0;
this.tmpQuaternion = new THREE.Quaternion();
this.rotationVelocity = new THREE.Vector3();
this.rotationVector = new THREE.Vector3();
this.update = function( delta ) {
var frame = this.controller.frame();
if( frame.hands[0] ){
var hand = frame.hands[0];
// If there are fingers, Glide
// if the hand is a fist, slow to a stop!
if( hand.fingers[0] ){
this.dampening = this.weakDampening;
this.acceleration = hand.palmPosition[2] * this.movementSpeed;
this.speed += this.acceleration;
}else{
this.dampening = this.strongDampening;
}
this.speed *= this.dampening;
// First Off reduce the rotation every frame.
// This is used to limit the rotation, as well as
// make sure rotation always slows to a stop if we
// are in the neutral position
this.rotationVelocity.multiplyScalar( this.dampening );
// Pitch
var centerY = frame.interactionBox.center[1];
var xP = ( hand.palmPosition[1] - centerY ) * this.positionFactor ;
var xD = hand.direction[1] * this.directionFactor ;
this.rotationVelocity.x += this.lookSpeed * ( xP + xD );
// Yaw
var yP = hand.palmPosition[0] * this.positionFactor ;
var yD = hand.direction[0] * this.directionFactor ;
this.rotationVelocity.y -= this.lookSpeed * ( yP + yD );
// Roll
this.rotationVelocity.z += this.rollSpeed * hand.palmNormal[0];
// Set them equal every frame, as long as a hand is present
this.rotationVector.x = this.rotationVelocity.x;
this.rotationVector.y = this.rotationVelocity.y;
this.rotationVector.z = this.rotationVelocity.z;
}else{
// if there is no hand, slow down!
this.dampening = this.strongDampening;
this.rotationVector.multiplyScalar( this.dampening );
this.speed *= ( this.dampening );
}
//this.object.translateX( this.moveVector.x * delta );
//this.object.translateY( this.moveVector.y * delta );
this.object.translateZ( this.speed * delta );
this.tmpQuaternion.set(
this.rotationVector.x * delta,
this.rotationVector.y * delta,
this.rotationVector.z * delta,
1
).normalize();
this.object.quaternion.multiply( this.tmpQuaternion );
// expose the rotation vector for convenience
this.object.rotation.setFromQuaternion( this.object.quaternion, this.object.rotation.order );
};
};