-
Notifications
You must be signed in to change notification settings - Fork 0
/
GravityController.js
68 lines (59 loc) · 1.84 KB
/
GravityController.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
/**
* @author quinnciccoretti
* @class GravityController
* Control gravity with touchpad
*/
class GravityController extends BasicController{
constructor(id){
super( id, "#555666", "Gravity");
//this little ball shows where the user's thumb is on the trackpad
var MODES = { UPDOWN: 0, MULTI: 1 };
var mode = MODES.UPDOWN;
var modelabel;
var geometry = new THREE.IcosahedronGeometry( 0.1, 2 );
var material = new THREE.MeshBasicMaterial({color:"#ff0000"});
var ball = new THREE.Mesh( geometry, material ); //this shows where the user's thumb is on the trackpad
this.ui.add( ball );
var modelist = ["up/down", "multiaxis"];
this.addEventListener( 'axischanged', this.onAxisChanged );
this.addEventListener( 'gripsdown', this.onGripsDown );
}
/**
* Moves user around the scene based on thumbpad
*/
onAxisChanged( event ) {
ball.position.set(event.axes[ 0 ], event.axes[ 1 ], 0);
//only execute the rest if pressing down on thumbpad
if ( this.getButtonState( 'thumbpad' ) === false ) return;
var x = event.axes[ 0 ] / 2.0;
var y = - event.axes[ 1 ] / 2.0;
var r = 2*Math.sqrt(x*x+y*y);
var grav;
if(mode === MODES.UPDOWN){
grav = new THREE.Vector3(0,1,0).multiplyScalar(y*-20);
}
if(mode === MODES.MULTI){
var dir = new THREE.Vector3(0,0,-1);
dir.applyEuler(this.rotation);
grav = dir.multiplyScalar(y*-20);
}
scene.setGravity(grav);
this.pulse(r/6, 5); //pulse at intensity proportional to movement speed, for very short duration, 5ms.
}
/**
* Change mode
*/
onGripsDown(event){
console.log("gripsdown, mode:"+mode);
if(mode === MODES.UPDOWN){
mode = MODES.MULTI;
}
//will not work without else if
else if(mode === MODES.MULTI){
mode = MODES.UPDOWN;
}
this.ui.remove(modelabel);
modelabel = create_text_mesh(modelist[mode], 2, "#ff0000");
this.ui.add(modelabel);
}
}