-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovement-controls.js
216 lines (178 loc) · 5.85 KB
/
movement-controls.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/**
* Movement Controls
*
* @author Don McCurdy <dm@donmccurdy.com>
*/
const COMPONENT_SUFFIX = '-controls',
MAX_DELTA = 0.2, // ms
EPS = 10e-6;
module.exports = AFRAME.registerComponent('movement-controls', {
/*******************************************************************
* Schema
*/
dependencies: ['rotation'],
schema: {
enabled: { default: true },
controls: { default: ['gamepad', 'trackpad', 'keyboard', 'touch'] },
speed: { default: 0.3, min: 0 },
fly: { default: false },
constrainToNavMesh: { default: false },
camera: { default: '[camera]', type: 'selector' }
},
/*******************************************************************
* Lifecycle
*/
init: function () {
const el = this.el;
this.velocityCtrl = null;
this.velocity = new THREE.Vector3();
this.heading = new THREE.Quaternion();
// Navigation
this.navGroup = null;
this.navNode = null;
if (el.sceneEl.hasLoaded) {
this.injectControls();
} else {
el.sceneEl.addEventListener('loaded', this.injectControls.bind(this));
}
},
update: function (prevData) {
const el = this.el;
const data = this.data;
if (el.sceneEl.hasLoaded) {
this.injectControls();
}
if (data.constrainToNavMesh !== prevData.constrainToNavMesh) {
const nav = el.sceneEl.systems.nav;
data.constrainToNavMesh
? nav.addAgent(this)
: nav.removeAgent(this);
}
},
injectControls: function () {
const data = this.data;
var name;
for (let i = 0; i < data.controls.length; i++) {
name = data.controls[i] + COMPONENT_SUFFIX;
if (!this.el.components[name]) {
this.el.setAttribute(name, '');
}
}
},
updateNavLocation: function () {
this.navGroup = null;
this.navNode = null;
},
/*******************************************************************
* Tick
*/
tick: (function () {
const start = new THREE.Vector3();
const end = new THREE.Vector3();
const clampedEnd = new THREE.Vector3();
return function (t, dt) {
if (!dt) return;
const el = this.el;
const data = this.data;
if (!data.enabled) return;
this.updateVelocityCtrl();
const velocityCtrl = this.velocityCtrl;
const velocity = this.velocity;
if (!velocityCtrl) return;
// Update velocity. If FPS is too low, reset.
if (dt / 1000 > MAX_DELTA) {
velocity.set(0, 0, 0);
} else {
this.updateVelocity(dt);
}
if (data.constrainToNavMesh
&& velocityCtrl.isNavMeshConstrained !== false) {
if (velocity.lengthSq() < EPS) return;
start.copy(el.object3D.position);
end
.copy(velocity)
.multiplyScalar(dt / 1000)
.add(start);
const nav = el.sceneEl.systems.nav;
this.navGroup = this.navGroup === null ? nav.getGroup(start) : this.navGroup;
this.navNode = this.navNode || nav.getNode(start, this.navGroup);
this.navNode = nav.clampStep(start, end, this.navGroup, this.navNode, clampedEnd);
el.object3D.position.copy(clampedEnd);
} else if (el.hasAttribute('velocity')) {
el.setAttribute('velocity', velocity);
} else {
el.object3D.position.x += velocity.x * dt / 1000;
el.object3D.position.y += velocity.y * dt / 1000;
el.object3D.position.z += velocity.z * dt / 1000;
}
};
}()),
/*******************************************************************
* Movement
*/
updateVelocityCtrl: function () {
const data = this.data;
if (data.enabled) {
for (let i = 0, l = data.controls.length; i < l; i++) {
const control = this.el.components[data.controls[i] + COMPONENT_SUFFIX];
if (control && control.isVelocityActive()) {
this.velocityCtrl = control;
return;
}
}
this.velocityCtrl = null;
}
},
updateVelocity: (function () {
const vector2 = new THREE.Vector2();
// var matrix = new THREE.Matrix4();
// var matrix2 = new THREE.Matrix4();
// var position = new THREE.Vector3();
// var quaternion = new THREE.Quaternion();
// var scale = new THREE.Vector3();
return function (dt) {
let dVelocity;
const el = this.el;
const control = this.velocityCtrl;
const velocity = this.velocity;
const data = this.data;
if (control) {
if (control.getVelocityDelta) {
dVelocity = control.getVelocityDelta(dt);
} else if (control.getVelocity) {
velocity.copy(control.getVelocity());
return;
} else if (control.getPositionDelta) {
velocity.copy(control.getPositionDelta(dt).multiplyScalar(1000 / dt));
return;
} else {
throw new Error('Incompatible movement controls: ', control);
}
}
if (el.hasAttribute('velocity') && !data.constrainToNavMesh) {
velocity.copy(this.el.getAttribute('velocity'));
}
if (dVelocity && data.enabled) {
// TODO: Handle rotated rig.
const cameraEl = data.camera;
// matrix.copy(cameraEl.object3D.matrixWorld);
// matrix2.getInverse(el.object3D.matrixWorld);
// matrix.multiply(matrix2);
// matrix.decompose(position, quaternion, scale);
// dVelocity.applyQuaternion(quaternion);
// Rotate to heading
dVelocity.applyQuaternion(cameraEl.object3D.quaternion);
const factor = dVelocity.length();
if (data.fly) {
velocity.copy(dVelocity);
velocity.multiplyScalar(this.data.speed * dt);
} else {
vector2.set(dVelocity.x, dVelocity.z);
vector2.setLength(factor * this.data.speed * dt);
velocity.x = vector2.x;
velocity.z = vector2.y;
}
}
};
}())
});