-
Notifications
You must be signed in to change notification settings - Fork 0
/
driveablecar.html
285 lines (195 loc) · 8.88 KB
/
driveablecar.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<!DOCTYPE html>
<html>
<head>
<!-- //importing three js and cannon (cannon is handling physics and therefore movement) -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/cannon.js/0.6.2/cannon.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://unpkg.com/three@0.85.0/examples/js/controls/OrbitControls.js"></script>
<meta charset="utf-8">
<title>Car Driving</title>
<style>
body {
margin: 0;
}
</style>
</head>
<body>
<script>
//creating global variables
var world, scene, renderer, camera, timeStep = 1/60, geometry, material,floor, groundBody, chassisShape, chassisBody, options, groundMaterial, wheelMaterial,
wheelGroundContactMaterial, vehicle, wheelBodies, cylinder;
//calling our initialisers
initCannon();
initThree();
animate();
function initCannon(){
//literally creating the cannon world
world = new CANNON.World();
//creating gravity, and other shit
world.gravity.set(0,0,-20);
world.broadphase = new CANNON.NaiveBroadphase();
world.solver.interations = 10;
world.defaultContactMaterial.friction = 0;
//creating ground material interaction with wheel material
groundMaterial = new CANNON.Material("groundMaterial");
wheelMaterial = new CANNON.Material("wheelMaterial");
wheelGroundContactMaterial = new CANNON.ContactMaterial(wheelMaterial, groundMaterial,{
friction: 0.3,
restitution: 0,
contactEquationStiffness: 1000
});
//adding contact material interaction to the world
world.addContactMaterial(wheelGroundContactMaterial);
//creating the chassis of the car
chassisShape = new CANNON.Box(new CANNON.Vec3(2,1,0.5));
chassisBody = new CANNON.Body({mass:150});
chassisBody.addShape(chassisShape);
chassisBody.position.set(0,0,0);
chassisBody.angularVelocity.set(0, 0, 0);
// options for car and how it drives
options = {
radius: 0.5,
directionLocal: new CANNON.Vec3(0, 0, -1),
suspensionStiffness: 30,
suspensionRestLength: 0.3,
frictionSlip: 5,
dampingRelaxation: 2.3,
dampingCompression: 4.4,
maxSuspensionForce: 100000,
rollInfluence: 0.01,
axleLocal: new CANNON.Vec3(0, 1, 0),
chassisConnectionPointLocal: new CANNON.Vec3(1, 1, 0),
maxSuspensionTravel: 0.3,
customSlidingRotationalSpeed: -30,
useCustomSlidingRotationalSpeed: true
};
//creating the vehicle
vehicle = new CANNON.RaycastVehicle({
chassisBody: chassisBody
});
//adding wheels and connecting them to the chassis
options.chassisConnectionPointLocal.set(1,1,0);
vehicle.addWheel(options);
options.chassisConnectionPointLocal.set(1,-1,0);
vehicle.addWheel(options);
options.chassisConnectionPointLocal.set(-1,1,0);
vehicle.addWheel(options);
options.chassisConnectionPointLocal.set(-1,-1,0);
vehicle.addWheel(options);
//adding the vehicle to the world
vehicle.addToWorld(world);
//creating our wheels body and shape for the car
wheelBodies = [];
for (var i=0; i<vehicle.wheelInfos.length; i++){
var wheel = vehicle.wheelInfos[i];
var cylinderShape = new CANNON.Cylinder(wheel.radius, wheel.radius, wheel.radius/2, 20);
var wheelBody = new CANNON.Body({mass: 1});
var q = new CANNON.Quaternion();
q.setFromAxisAngle(new CANNON.Vec3(1, 0, 0), Math.PI / 2);
wheelBody.addShape(cylinderShape, new CANNON.Vec3(),q);
wheelBodies.push(wheelBody);
}
//update wheels
world.addEventListener('postStep', function(){
for (var i = 0; i < vehicle.wheelInfos.length; i++){
vehicle.updateWheelTransform(i);
var t = vehicle.wheelInfos[i].worldTransform;
wheelBodies[i].position.copy(t.position);
wheelBodies[i].quaternion.copy(t.quaternion);
}});
document.onkeydown = handler;
document.onkeyup = handler;
var maxSteerVal = 0.5;
var maxForce = 1000;
var brakeForce = 100000;
//steering and acceleration
function handler(event){
var up = (event.type =='keyup');
if (!up && event.type !== 'keydown'){
return;
}
vehicle.setBrake(0, 0);
vehicle.setBrake(0, 1);
vehicle.setBrake(0, 2);
vehicle.setBrake(0, 3);
switch(event.keyCode){
case 87://forward
vehicle.applyEngineForce(up ? 0 : -maxForce, 2);
vehicle.applyEngineForce(up ? 0 : -maxForce, 3);
break;
case 83: //backward
vehicle.applyEngineForce(up ? 0 : maxForce, 2);
vehicle.applyEngineForce(up ? 0: maxForce, 3);
break;
case 68: //right
vehicle.setSteeringValue(up ? 0 : -maxSteerVal, 0);
vehicle.setSteeringValue(up ? 0 : -maxSteerVal, 1);
break;
case 65: //left
vehicle.setSteeringValue(up ? 0 : maxSteerVal, 0);
vehicle.setSteeringValue(up ? 0 : maxSteerVal, 1);
break;
}
}
var groundShape = new CANNON.Plane(100,100,100);
groundBody = new CANNON.Body({ mass: 0, material: groundMaterial });
groundBody.addShape(groundShape);
groundBody.position.set(0,0,-5);
world.add(groundBody);
}
function initThree(){
scene = new THREE.Scene();
//setting camera perspective
camera = new THREE.PerspectiveCamera (75, window.innerWidth / window.innerHeight, 1, 100);
// setting camera position
camera.position.z = 5;
cylinder = new THREE.CylinderGeometry(.5,.5,.5,5);
material3 = new THREE.MeshBasicMaterial({color: 0x00FFFF});
wheelmesh1 = new THREE.Mesh(cylinder, material3);
wheelmesh2 = new THREE.Mesh(cylinder, material3);
wheelmesh3 = new THREE.Mesh(cylinder, material3);
wheelmesh4 = new THREE.Mesh(cylinder, material3);
cube = new THREE.BoxGeometry(2,1,0.5);
var material2 = new THREE.MeshBasicMaterial({color:0x00FFFF});
geometry = new THREE.PlaneGeometry(100,100,100);
material = new THREE.MeshBasicMaterial({color: 0xFFFFFF});
floor = new THREE.Mesh(geometry, material);
cubemesh = new THREE.Mesh(cube, material2);
scene.add(cubemesh);
scene.add(wheelmesh1);
scene.add(wheelmesh2);
scene.add(wheelmesh3);
scene.add(wheelmesh4);
scene.add(floor);
//Adding camera to the scene
scene.add( camera );
renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight);
document.body.appendChild( renderer.domElement );
//giving us control of the camera
const controls = new THREE.OrbitControls( camera, renderer.domElement );
}
function animate(){
requestAnimationFrame(animate);
updatePhysics();
render();
}
function updatePhysics() {
world.step(timeStep);
floor.position.copy(groundBody.position);
cubemesh.position.copy(chassisBody.position);
cubemesh.quaternion.copy(chassisBody.quaternion);
wheelmesh1.position.copy(wheelBodies[0].position);
wheelmesh2.position.copy(wheelBodies[1].position);
wheelmesh3.position.copy(wheelBodies[2].position);
wheelmesh4.position.copy(wheelBodies[3].position);
wheelmesh1.quaternion.copy(wheelBodies[0].quaternion);
wheelmesh2.quaternion.copy(wheelBodies[1].quaternion);
wheelmesh3.quaternion.copy(wheelBodies[2].quaternion);
wheelmesh4.quaternion.copy(wheelBodies[3].quaternion);
}
function render() {
renderer.render ( scene, camera );
}
</script>
</body>