-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasicController.js
140 lines (125 loc) · 3.8 KB
/
BasicController.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
/**
* @author quinnciccoretti
* @class BasicController
* Defines basic methods for getting sensor data and using haptics
*/
class BasicController extends ViveController {
constructor( id, uicolor="#ff00ff", name = "Basic"){
super(id);
console.log("BasicController instantiated with id:"+id);
//UI, appears on the touchpad, with uicolor
var geometry = new THREE.CircleGeometry( 1, 32 );
var material = new THREE.MeshBasicMaterial( { color: uicolor } );
this.ui = new THREE.Mesh( geometry, material );
this.ui.position.set( 0, 0.005, 0.0495 );
this.ui.rotation.x = - 1.45;
this.ui.scale.setScalar( 0.02 );
this.add( this.ui );
/** starts at false */
var made_nameplate = false;
}
/**
* Returns controller id, 1 or 0
*/
get_id() {
return this.id_;
}
/**
* Updates controller position data based on gamepad pose
*/
handle_update() {
this.update(); //refreshes controller data
this.update_phys_objects();
}
/**
* @returns the position of the controller in the scene, not relative to user
*/
get_absolute_position() {
var temp = user.position.clone();
return temp.add(this.position);
}
/**
* @returns the velocity of the controller from the gamepad
*/
get_velocity() {
return new THREE.Vector3().fromArray(this.getGamepad().pose.linearVelocity);
}
/**
* @returns the angular velocity of the controller from the gamepad
*/
get_angular_velocity() {
return new THREE.Vector3().fromArray(this.getGamepad().pose.angularVelocity);
}
/**
* @returns a vector pointing out from the controller,
* intended for shooting things alongs
*/
get_pointing_vector(){
var dir = new THREE.Vector3(0,0,-1);
dir.applyEuler(this.rotation);
return dir;
}
/**
* Updates invisible boxes (phys_obj1 and phys_obj2)
* to follow controllers so they can push things
*/
update_phys_objects(){
var my_pos = this.get_absolute_position();
//get the appropriate physics object, object 1 or 2, from a global list
var phys_obj = phys_obj_list[this.id_];
var rot = this.rotation;
phys_obj.rotation.set(rot.x, rot.y, rot.z);
phys_obj.__dirtyRotation = true;
phys_obj.position.set( my_pos.x, my_pos.y + controller_offset_y, my_pos.z );
phys_obj.__dirtyPosition = true;
//cancel the object's velocity
phys_obj.setLinearVelocity(new THREE.Vector3(0, 0, 0));
phys_obj.setAngularVelocity(new THREE.Vector3(0, 0, 0));
}
/**
* called whenever the menu button is pressed to switch to this mode
* from another mode
*/
on_activate(){
this.userData.points = [ new THREE.Vector3(), new THREE.Vector3() ];
this.userData.matrices = [ new THREE.Matrix4(), new THREE.Matrix4() ];
user.add( this );
//add the model of the controller
this.add(basic_controller_models[this.id_]);
this.make_nameplate();
}
/**
* called whenever the menu button is pressed to switch modes from
* this mode to another
*/
on_deactivate(){
//this removes the model to conserve memory
this.remove(basic_controller_models[this.id_]);
user.remove(this);
}
/**
* Adds a text label to controller and sets .name attribute
*/
make_nameplate(){
this.name = name;
if( !made_nameplate && (typeof uifont !== "undefined") ){
this.nameplate = create_text_mesh(name, 2, uicolor);
this.nameplate.rotation.z = -1.55; //align w/ controller handle
this.ui.add(this.nameplate);
this.nameplate.position.set(-0.5, -0.8, 0.3); //set relative pos
made_nameplate = true;
}
}
/**
* Vibrates haptics if controller has them
* @param intensity a 0-1 value, 1 is highest vibration
* @param duration duration of a pulse in ms
*/
pulse(intensity, duration){
var gp = this.getGamepad();
if( gp.hapticActuators && gp.hapticActuators[ 0 ]){ //Check if it has haptics
gp.hapticActuators[ 0 ].pulse( intensity, duration );
//pulse at 0-1 intensity for (duration)ms
}
}
}