-
Notifications
You must be signed in to change notification settings - Fork 33
/
vehicle-wheel-animation-test.js
56 lines (48 loc) · 1.48 KB
/
vehicle-wheel-animation-test.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
/* global AFRAME */
// Vehicle wheel Animation
AFRAME.registerComponent('wheel', {
schema: {
speed: { type: 'number', default: 1 },
wheelDiameter: { type: 'number', default: 1 }
},
init: function () {
const el = this.el;
const self = this;
el.addEventListener('model-loaded', (e) => {
const vehicle = el.getObject3D('mesh');
if (!vehicle) {
return;
}
self.wheel_F_L = vehicle.getObjectByName('wheel_F_L');
self.wheel_F_R = vehicle.getObjectByName('wheel_F_R');
self.wheel_B_L = vehicle.getObjectByName('wheel_B_L');
self.wheel_B_R = vehicle.getObjectByName('wheel_B_R');
// For Truck extra Wheels
self.wheel_B_L_2 = vehicle.getObjectByName('wheel_B_L_2');
self.wheel_B_R_2 = vehicle.getObjectByName('wheel_B_R_2');
});
},
tick: function (t, dt) {
const speed = this.data.speed / 1000; // speed per millisecond
const wheelDiameter = this.data.wheelDiameter;
const rateOfRotation = 2 * (speed / wheelDiameter);
if (this.wheel_F_L) {
this.wheel_F_L.rotateY(rateOfRotation);
}
if (this.wheel_F_R) {
this.wheel_F_R.rotateY(rateOfRotation);
}
if (this.wheel_B_L) {
this.wheel_B_L.rotateY(rateOfRotation);
}
if (this.wheel_B_L_2) {
this.wheel_B_L_2.rotateY(rateOfRotation);
}
if (this.wheel_B_R_2) {
this.wheel_B_R_2.rotateY(rateOfRotation);
}
if (this.wheel_B_R) {
this.wheel_B_R.rotateY(rateOfRotation);
}
}
});