Car.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. */
  4. THREE.Car = function () {
  5. var scope = this;
  6. // car geometry manual parameters
  7. this.modelScale = 1;
  8. this.backWheelOffset = 2;
  9. this.autoWheelGeometry = true;
  10. // car geometry parameters automatically set from wheel mesh
  11. // - assumes wheel mesh is front left wheel in proper global
  12. // position with respect to body mesh
  13. // - other wheels are mirrored against car root
  14. // - if necessary back wheels can be offset manually
  15. this.wheelOffset = new THREE.Vector3();
  16. this.wheelDiameter = 1;
  17. // car "feel" parameters
  18. this.MAX_SPEED = 2200;
  19. this.MAX_REVERSE_SPEED = -1500;
  20. this.MAX_WHEEL_ROTATION = 0.6;
  21. this.FRONT_ACCELERATION = 1250;
  22. this.BACK_ACCELERATION = 1500;
  23. this.WHEEL_ANGULAR_ACCELERATION = 1.5;
  24. this.FRONT_DECCELERATION = 750;
  25. this.WHEEL_ANGULAR_DECCELERATION = 1.0;
  26. this.STEERING_RADIUS_RATIO = 0.0023;
  27. this.MAX_TILT_SIDES = 0.05;
  28. this.MAX_TILT_FRONTBACK = 0.015;
  29. // internal control variables
  30. this.speed = 0;
  31. this.acceleration = 0;
  32. this.wheelOrientation = 0;
  33. this.carOrientation = 0;
  34. // car rigging
  35. this.root = new THREE.Object3D();
  36. this.frontLeftWheelRoot = new THREE.Object3D();
  37. this.frontRightWheelRoot = new THREE.Object3D();
  38. this.bodyMesh = null;
  39. this.frontLeftWheelMesh = null;
  40. this.frontRightWheelMesh = null;
  41. this.backLeftWheelMesh = null;
  42. this.backRightWheelMesh = null;
  43. this.bodyGeometry = null;
  44. this.wheelGeometry = null;
  45. // internal helper variables
  46. this.loaded = false;
  47. this.meshes = [];
  48. // API
  49. this.enableShadows = function ( enable ) {
  50. for ( var i = 0; i < this.meshes.length; i ++ ) {
  51. this.meshes[ i ].castShadow = enable;
  52. this.meshes[ i ].receiveShadow = enable;
  53. }
  54. };
  55. this.setVisible = function ( enable ) {
  56. for ( var i = 0; i < this.meshes.length; i ++ ) {
  57. this.meshes[ i ].visible = enable;
  58. this.meshes[ i ].visible = enable;
  59. }
  60. };
  61. this.loadPartsJSON = function ( bodyURL, wheelURL ) {
  62. var loader = new THREE.JSONLoader();
  63. loader.load( bodyURL, function( geometry ) { createBody( geometry ) } );
  64. loader.load( wheelURL, function( geometry ) { createWheels( geometry ) } );
  65. };
  66. this.loadPartsBinary = function ( bodyURL, wheelURL ) {
  67. var loader = new THREE.BinaryLoader();
  68. loader.load( bodyURL, function( geometry ) { createBody( geometry ) } );
  69. loader.load( wheelURL, function( geometry ) { createWheels( geometry ) } );
  70. };
  71. this.updateCarModel = function ( delta, controls ) {
  72. // speed and wheels based on controls
  73. if ( controls.moveForward ) {
  74. this.speed = THREE.Math.clamp( this.speed + delta * this.FRONT_ACCELERATION, this.MAX_REVERSE_SPEED, this.MAX_SPEED );
  75. this.acceleration = THREE.Math.clamp( this.acceleration + delta, -1, 1 );
  76. }
  77. if ( controls.moveBackward ) {
  78. this.speed = THREE.Math.clamp( this.speed - delta * this.BACK_ACCELERATION, this.MAX_REVERSE_SPEED, this.MAX_SPEED );
  79. this.acceleration = THREE.Math.clamp( this.acceleration - delta, -1, 1 );
  80. }
  81. if ( controls.moveLeft ) {
  82. this.wheelOrientation = THREE.Math.clamp( this.wheelOrientation + delta * this.WHEEL_ANGULAR_ACCELERATION, - this.MAX_WHEEL_ROTATION, this.MAX_WHEEL_ROTATION );
  83. }
  84. if ( controls.moveRight ) {
  85. this.wheelOrientation = THREE.Math.clamp( this.wheelOrientation - delta * this.WHEEL_ANGULAR_ACCELERATION, - this.MAX_WHEEL_ROTATION, this.MAX_WHEEL_ROTATION );
  86. }
  87. // speed decay
  88. if ( ! ( controls.moveForward || controls.moveBackward ) ) {
  89. if ( this.speed > 0 ) {
  90. var k = exponentialEaseOut( this.speed / this.MAX_SPEED );
  91. this.speed = THREE.Math.clamp( this.speed - k * delta * this.FRONT_DECCELERATION, 0, this.MAX_SPEED );
  92. this.acceleration = THREE.Math.clamp( this.acceleration - k * delta, 0, 1 );
  93. } else {
  94. var k = exponentialEaseOut( this.speed / this.MAX_REVERSE_SPEED );
  95. this.speed = THREE.Math.clamp( this.speed + k * delta * this.BACK_ACCELERATION, this.MAX_REVERSE_SPEED, 0 );
  96. this.acceleration = THREE.Math.clamp( this.acceleration + k * delta, -1, 0 );
  97. }
  98. }
  99. // steering decay
  100. if ( ! ( controls.moveLeft || controls.moveRight ) ) {
  101. if ( this.wheelOrientation > 0 ) {
  102. this.wheelOrientation = THREE.Math.clamp( this.wheelOrientation - delta * this.WHEEL_ANGULAR_DECCELERATION, 0, this.MAX_WHEEL_ROTATION );
  103. } else {
  104. this.wheelOrientation = THREE.Math.clamp( this.wheelOrientation + delta * this.WHEEL_ANGULAR_DECCELERATION, - this.MAX_WHEEL_ROTATION, 0 );
  105. }
  106. }
  107. // car update
  108. var forwardDelta = this.speed * delta;
  109. this.carOrientation += ( forwardDelta * this.STEERING_RADIUS_RATIO )* this.wheelOrientation;
  110. // displacement
  111. this.root.position.x += Math.sin( this.carOrientation ) * forwardDelta;
  112. this.root.position.z += Math.cos( this.carOrientation ) * forwardDelta;
  113. // steering
  114. this.root.rotation.y = this.carOrientation;
  115. // tilt
  116. if ( this.loaded ) {
  117. this.bodyMesh.rotation.z = this.MAX_TILT_SIDES * this.wheelOrientation * ( this.speed / this.MAX_SPEED );
  118. this.bodyMesh.rotation.x = - this.MAX_TILT_FRONTBACK * this.acceleration;
  119. }
  120. // wheels rolling
  121. var angularSpeedRatio = 1 / ( this.modelScale * ( this.wheelDiameter / 2 ) );
  122. var wheelDelta = forwardDelta * angularSpeedRatio;
  123. if ( this.loaded ) {
  124. this.frontLeftWheelMesh.rotation.x += wheelDelta;
  125. this.frontRightWheelMesh.rotation.x += wheelDelta;
  126. this.backLeftWheelMesh.rotation.x += wheelDelta;
  127. this.backRightWheelMesh.rotation.x += wheelDelta;
  128. }
  129. // front wheels steering
  130. this.frontLeftWheelRoot.rotation.y = this.wheelOrientation;
  131. this.frontRightWheelRoot.rotation.y = this.wheelOrientation;
  132. };
  133. // internal helper methods
  134. function createBody ( geometry ) {
  135. scope.bodyGeometry = geometry;
  136. createCar();
  137. };
  138. function createWheels ( geometry ) {
  139. scope.wheelGeometry = geometry;
  140. createCar();
  141. };
  142. function createCar () {
  143. if ( scope.bodyGeometry && scope.wheelGeometry ) {
  144. // compute wheel geometry parameters
  145. if ( scope.autoWheelGeometry ) {
  146. scope.wheelGeometry.computeBoundingBox();
  147. var bb = scope.wheelGeometry.boundingBox;
  148. scope.wheelOffset.add( bb.min, bb.max );
  149. scope.wheelOffset.multiplyScalar( 0.5 );
  150. scope.wheelDiameter = bb.max.y - bb.min.y;
  151. THREE.GeometryUtils.center( scope.wheelGeometry );
  152. }
  153. // rig the car
  154. var s = scope.modelScale,
  155. delta = new THREE.Vector3(),
  156. faceMaterial = new THREE.MeshFaceMaterial();
  157. // body
  158. scope.bodyMesh = new THREE.Mesh( scope.bodyGeometry, faceMaterial );
  159. scope.bodyMesh.scale.set( s, s, s );
  160. scope.root.add( scope.bodyMesh );
  161. // front left wheel
  162. delta.multiply( scope.wheelOffset, new THREE.Vector3( s, s, s ) );
  163. scope.frontLeftWheelRoot.position.addSelf( delta );
  164. scope.frontLeftWheelMesh = new THREE.Mesh( scope.wheelGeometry, faceMaterial );
  165. scope.frontLeftWheelMesh.scale.set( s, s, s );
  166. scope.frontLeftWheelRoot.add( scope.frontLeftWheelMesh );
  167. scope.root.add( scope.frontLeftWheelRoot );
  168. // front right wheel
  169. delta.multiply( scope.wheelOffset, new THREE.Vector3( -s, s, s ) );
  170. scope.frontRightWheelRoot.position.addSelf( delta );
  171. scope.frontRightWheelMesh = new THREE.Mesh( scope.wheelGeometry, faceMaterial );
  172. scope.frontRightWheelMesh.scale.set( s, s, s );
  173. scope.frontRightWheelMesh.rotation.z = Math.PI;
  174. scope.frontRightWheelRoot.add( scope.frontRightWheelMesh );
  175. scope.root.add( scope.frontRightWheelRoot );
  176. // back left wheel
  177. delta.multiply( scope.wheelOffset, new THREE.Vector3( s, s, -s ) );
  178. delta.z -= scope.backWheelOffset;
  179. scope.backLeftWheelMesh = new THREE.Mesh( scope.wheelGeometry, faceMaterial );
  180. scope.backLeftWheelMesh.position.addSelf( delta );
  181. scope.backLeftWheelMesh.scale.set( s, s, s );
  182. scope.root.add( scope.backLeftWheelMesh );
  183. // back right wheel
  184. delta.multiply( scope.wheelOffset, new THREE.Vector3( -s, s, -s ) );
  185. delta.z -= scope.backWheelOffset;
  186. scope.backRightWheelMesh = new THREE.Mesh( scope.wheelGeometry, faceMaterial );
  187. scope.backRightWheelMesh.position.addSelf( delta );
  188. scope.backRightWheelMesh.scale.set( s, s, s );
  189. scope.backRightWheelMesh.rotation.z = Math.PI;
  190. scope.root.add( scope.backRightWheelMesh );
  191. // cache meshes
  192. scope.meshes = [ scope.bodyMesh, scope.frontLeftWheelMesh, scope.frontRightWheelMesh, scope.backLeftWheelMesh, scope.backRightWheelMesh ];
  193. // callback
  194. scope.loaded = true;
  195. if ( scope.callback ) {
  196. scope.callback( scope );
  197. }
  198. }
  199. };
  200. function quadraticEaseOut( k ) { return - k * ( k - 2 ); }
  201. function cubicEaseOut( k ) { return --k * k * k + 1; }
  202. function circularEaseOut( k ) { return Math.sqrt( 1 - --k * k ); }
  203. function sinusoidalEaseOut( k ) { return Math.sin( k * Math.PI / 2 ); }
  204. function exponentialEaseOut( k ) { return k === 1 ? 1 : - Math.pow( 2, - 10 * k ) + 1; }
  205. };