-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.html
157 lines (150 loc) · 5.94 KB
/
index.html
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
<html>
<head>
<title>A-Frame Test</title>
<script src="https://aframe.io/releases/0.9.2/aframe.min.js"></script>
<script src="//cdn.rawgit.com/donmccurdy/aframe-extras/v6.0.0/dist/aframe-extras.min.js"></script>
<script src="//cdn.rawgit.com/donmccurdy/aframe-physics-system/v4.0.0/dist/aframe-physics-system.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/super-hands.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/aframe-physics-extras.min.js"></script>
<script>
AFRAME.registerComponent('car', {
init: function () {
window.addEventListener('keydown', this.onKeyDown.bind(this))
window.addEventListener('keyup', this.onKeyUp.bind(this))
this.rotating = null
this.speeding = null
this.speed = 0.0
},
onAxisMoveSpeed: function(e) {
if (e.detail.axis[1] == 0) {
this.speeding = null
} else if (e.detail.axis[1] > 0) {
this.speeding = 'down'
} else if (e.detail.axis[1] < 0) {
this.speeding = 'up'
}
},
onAxisMoveAngle: function(e) {
if (e.detail.axis[0] == 0) {
this.rotating = null
} else if (e.detail.axis[0] > 0) {
this.rotating = 'right'
} else if (e.detail.axis[0] < 0) {
this.rotating = 'left'
}
},
onKeyDown: function (e) {
if (e.keyCode == 65) {
this.rotating = 'left'
} else if (e.keyCode == 68) {
this.rotating = 'right'
} else if (e.keyCode == 87) {
this.speeding = 'up'
} else if (e.keyCode == 83) {
this.speeding = 'down'
}
},
onKeyUp: function (e) {
if (e.keyCode == 65 && this.rotating == 'left') {
this.rotating = null
} else if (e.keyCode == 68 && this.rotating == 'right') {
this.rotating = null
} else if (e.keyCode == 87 && this.speeding == 'up') {
this.speeding = null
} else if (e.keyCode == 83 && this.speeding == 'down') {
this.speeding = null
}
},
tick: function () {
if (this.speeding != null) {
const direction = this.speed > 0 ? 1 : -1
if (this.rotating == 'left') {
this.el.object3D.rotateY(direction * Math.PI / 120)
} else if (this.rotating == 'right') {
this.el.object3D.rotateY(direction * -Math.PI / 120)
}
}
if (this.speeding == 'up') {
this.speed = Math.min(this.speed + 0.02, 0.2)
} else if (this.speeding == 'down') {
this.speed = Math.max(this.speed - 0.02, -0.2)
}
const position = this.el.getAttribute('position')
const rotation = this.el.getAttribute('rotation')
const angle = Math.PI * rotation.y / 180
position.x += this.speed * Math.sin(angle)
position.z += this.speed * Math.cos(angle)
this.el.setAttribute('position', position)
if (this.speed > 0) {
this.speed = Math.max(this.speed - 0.01, 0)
}
if (this.speed < 0) {
this.speed = Math.min(this.speed + 0.01, 0)
}
}
})
AFRAME.registerComponent('car-controller-left', {
init: function () {
this.el.addEventListener('axismove', function (e) {
const car = document.querySelector('[car]').components.car
car.onAxisMoveSpeed(e)
})
}
})
AFRAME.registerComponent('car-controller-right', {
init: function () {
this.el.addEventListener('axismove', function (e) {
const car = document.querySelector('[car]').components.car
car.onAxisMoveAngle(e)
})
}
})
</script>
</head>
<body>
<a-scene physics>
<a-assets>
<img id="ground" src="images/block.png">
<img id="bg" src="images/bg.jpg">
<a-asset-item id="car-obj" src="models/car.obj"></a-asset-item>
<a-asset-item id="car-mtl" src="models/car.mtl"></a-asset-item>
<a-mixin id="controller"
super-hands="colliderEvent: collisions;
colliderEventProperty: els;
colliderEndEvent: collisions;
colliderEndEventProperty: clearedEls;
grabStartButtons: gripdown, pointdown, triggerdown;
grabEndButtons: gripup, pointup, triggerup"
static-body="shape: sphere; sphereRadius: 0.11"
oculus-touch-controls="hand: left"
physics-collider
collision-filter = "collidesWith: default, red, green, blue;
collisionForces: false">
</a-assets>
<a-entity>
<a-camera position="0 4 0" wasd-controls-enabled="false"
>
<a-entity cursor="rayOrigin: mouse"
position="0 0 -1"
geometry="primitive: ring; radiusInner: 0.015; radiusOuter: 0.018"
material="color: #888; shader: flat">
</a-camera>
<a-entity hand-controls="left" mixin="controller" car-controller-left></a-entity>
<a-entity hand-controls="right" mixin="controller" car-controller-right></a-entity>
</a-entity>
<a-entity height="100" width="100" shadow="cast:true; receive:true">
<a-box position="-1 0.5 -2" rotation="0 45 0" color="#4CC3D9" dynamic-body grabbable></a-box>
<a-obj-model
src="#car-obj"
mtl="#car-mtl"
position="0 0.01 -10"
rotation="0 0 0"
car
animation-mixer
></a-obj-model>
<a-plane src="#ground" height="100" width="100" rotation="-90 0 0" repeat="100 100" static-body></a-plane>
</a-entity>
<a-sky src="#bg"></a-sky>
</a-scene>
</body>
</html>