Spaces:
Running
Running
File size: 7,834 Bytes
6cd9596 |
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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
/**
* @author alteredq / http://alteredqualia.com/
* @author Lewy Blue https://github.com/looeee
*
* The model is expected to follow real world car proportions. You can try unusual car types
* but your results may be unexpected. Scaled models are also not supported.
*
* Defaults are rough estimates for a real world scale car model
*
*/
THREE.Car = ( function ( ) {
// private variables
var steeringWheelSpeed = 1.5;
var maxSteeringRotation = 0.6;
var acceleration = 0;
var maxSpeedReverse, accelerationReverse, deceleration;
var controlKeys = { LEFT: 37, UP: 38, RIGHT: 39, DOWN: 40, BRAKE: 32 };
var wheelOrientation = 0;
var carOrientation = 0;
var root = null;
var frontLeftWheelRoot = null;
var frontRightWheelRoot = null;
var frontLeftWheel = new THREE.Group();
var frontRightWheel = new THREE.Group();
var backLeftWheel = null;
var backRightWheel = null;
var steeringWheel = null;
var wheelDiameter = 1;
var length = 1;
var loaded = false;
var controls = {
brake: false,
moveForward: false,
moveBackward: false,
moveLeft: false,
moveRight: false
};
function Car( maxSpeed, acceleration, brakePower, turningRadius, keys ) {
this.enabled = true;
this.elemNames = {
flWheel: 'wheel_fl',
frWheel: 'wheel_fr',
rlWheel: 'wheel_rl',
rrWheel: 'wheel_rr',
steeringWheel: 'steering_wheel', // set to null to disable
};
// km/hr
this.maxSpeed = maxSpeed || 180;
maxSpeedReverse = - this.maxSpeed * 0.25;
// m/s
this.acceleration = acceleration || 10;
accelerationReverse = this.acceleration * 0.5;
// metres
this.turningRadius = turningRadius || 6;
// m/s
deceleration = this.acceleration * 2;
// multiplied with deceleration, so breaking deceleration = ( acceleration * 2 * brakePower ) m/s
this.brakePower = brakePower || 10;
// exposed so that a user can use this for various effect, e.g blur
this.speed = 0;
// keys used to control car - by default the arrow keys and space to brake
controlKeys = keys || controlKeys;
// local axes of rotation - these are likely to vary between models
this.wheelRotationAxis = 'x';
this.wheelTurnAxis = 'z';
this.steeringWheelTurnAxis = 'y';
document.addEventListener( 'keydown', this.onKeyDown, false );
document.addEventListener( 'keyup', this.onKeyUp, false );
}
Car.prototype = {
constructor: Car,
onKeyDown: function ( event ) {
switch ( event.keyCode ) {
case controlKeys.BRAKE:
controls.brake = true;
controls.moveForward = false;
controls.moveBackward = false;
break;
case controlKeys.UP: controls.moveForward = true; break;
case controlKeys.DOWN: controls.moveBackward = true; break;
case controlKeys.LEFT: controls.moveLeft = true; break;
case controlKeys.RIGHT: controls.moveRight = true; break;
}
},
onKeyUp: function ( event ) {
switch ( event.keyCode ) {
case controlKeys.BRAKE: controls.brake = false; break;
case controlKeys.UP: controls.moveForward = false; break;
case controlKeys.DOWN: controls.moveBackward = false; break;
case controlKeys.LEFT: controls.moveLeft = false; break;
case controlKeys.RIGHT: controls.moveRight = false; break;
}
},
dispose: function () {
document.removeEventListener( 'keydown', this.onKeyDown, false );
document.removeEventListener( 'keyup', this.onKeyUp, false );
},
update: function ( delta ) {
if ( ! loaded || ! this.enabled ) return;
var brakingDeceleration = 1;
if ( controls.brake ) brakingDeceleration = this.brakePower;
if ( controls.moveForward ) {
this.speed = THREE.Math.clamp( this.speed + delta * this.acceleration, maxSpeedReverse, this.maxSpeed );
acceleration = THREE.Math.clamp( acceleration + delta, - 1, 1 );
}
if ( controls.moveBackward ) {
this.speed = THREE.Math.clamp( this.speed - delta * accelerationReverse, maxSpeedReverse, this.maxSpeed );
acceleration = THREE.Math.clamp( acceleration - delta, - 1, 1 );
}
if ( controls.moveLeft ) {
wheelOrientation = THREE.Math.clamp( wheelOrientation + delta * steeringWheelSpeed, - maxSteeringRotation, maxSteeringRotation );
}
if ( controls.moveRight ) {
wheelOrientation = THREE.Math.clamp( wheelOrientation - delta * steeringWheelSpeed, - maxSteeringRotation, maxSteeringRotation );
}
// this.speed decay
if ( ! ( controls.moveForward || controls.moveBackward ) ) {
if ( this.speed > 0 ) {
var k = exponentialEaseOut( this.speed / this.maxSpeed );
this.speed = THREE.Math.clamp( this.speed - k * delta * deceleration * brakingDeceleration, 0, this.maxSpeed );
acceleration = THREE.Math.clamp( acceleration - k * delta, 0, 1 );
} else {
var k = exponentialEaseOut( this.speed / maxSpeedReverse );
this.speed = THREE.Math.clamp( this.speed + k * delta * accelerationReverse * brakingDeceleration, maxSpeedReverse, 0 );
acceleration = THREE.Math.clamp( acceleration + k * delta, - 1, 0 );
}
}
// steering decay
if ( ! ( controls.moveLeft || controls.moveRight ) ) {
if ( wheelOrientation > 0 ) {
wheelOrientation = THREE.Math.clamp( wheelOrientation - delta * steeringWheelSpeed, 0, maxSteeringRotation );
} else {
wheelOrientation = THREE.Math.clamp( wheelOrientation + delta * steeringWheelSpeed, - maxSteeringRotation, 0 );
}
}
var forwardDelta = - this.speed * delta;
carOrientation -= ( forwardDelta * this.turningRadius * 0.02 ) * wheelOrientation;
// movement of car
root.position.x += Math.sin( carOrientation ) * forwardDelta * length;
root.position.z += Math.cos( carOrientation ) * forwardDelta * length;
// angle of car
root.rotation.y = carOrientation;
// wheels rolling
var angularSpeedRatio = - 2 / wheelDiameter;
var wheelDelta = forwardDelta * angularSpeedRatio * length;
frontLeftWheel.rotation[ this.wheelRotationAxis ] -= wheelDelta;
frontRightWheel.rotation[ this.wheelRotationAxis ] -= wheelDelta;
backLeftWheel.rotation[ this.wheelRotationAxis ] -= wheelDelta;
backRightWheel.rotation[ this.wheelRotationAxis ] -= wheelDelta;
// rotation while steering
frontLeftWheelRoot.rotation[ this.wheelTurnAxis ] = wheelOrientation;
frontRightWheelRoot.rotation[ this.wheelTurnAxis ] = wheelOrientation;
steeringWheel.rotation[ this.steeringWheelTurnAxis ] = -wheelOrientation * 6;
},
setModel: function ( model, elemNames ) {
if ( elemNames ) this.elemNames = elemNames;
root = model;
this.setupWheels();
this.computeDimensions();
loaded = true;
},
setupWheels: function () {
frontLeftWheelRoot = root.getObjectByName( this.elemNames.flWheel );
frontRightWheelRoot = root.getObjectByName( this.elemNames.frWheel );
backLeftWheel = root.getObjectByName( this.elemNames.rlWheel );
backRightWheel = root.getObjectByName( this.elemNames.rrWheel );
if ( this.elemNames.steeringWheel !== null ) steeringWheel = root.getObjectByName( this.elemNames.steeringWheel );
while ( frontLeftWheelRoot.children.length > 0 ) frontLeftWheel.add( frontLeftWheelRoot.children[ 0 ] );
while ( frontRightWheelRoot.children.length > 0 ) frontRightWheel.add( frontRightWheelRoot.children[ 0 ] );
frontLeftWheelRoot.add( frontLeftWheel );
frontRightWheelRoot.add( frontRightWheel );
},
computeDimensions: function () {
var bb = new THREE.Box3().setFromObject( frontLeftWheelRoot );
var size = new THREE.Vector3();
bb.getSize( size );
wheelDiameter = Math.max( size.x, size.y, size.z );
bb.setFromObject( root );
size = bb.getSize( size );
length = Math.max( size.x, size.y, size.z );
}
};
function exponentialEaseOut( k ) {
return k === 1 ? 1 : - Math.pow( 2, - 10 * k ) + 1;
}
return Car;
} )();
|