API Docs for: 0.6.1
Show:

File: src/objects/RigidVehicle.js

  1. var Body = require('./Body');
  2. var Sphere = require('../shapes/Sphere');
  3. var Box = require('../shapes/Box');
  4. var Vec3 = require('../math/Vec3');
  5. var HingeConstraint = require('../constraints/HingeConstraint');
  6.  
  7. module.exports = RigidVehicle;
  8.  
  9. /**
  10. * Simple vehicle helper class with spherical rigid body wheels.
  11. * @class RigidVehicle
  12. * @constructor
  13. * @param {Body} [options.chassisBody]
  14. */
  15. function RigidVehicle(options){
  16. this.wheelBodies = [];
  17.  
  18. /**
  19. * @property coordinateSystem
  20. * @type {Vec3}
  21. */
  22. this.coordinateSystem = typeof(options.coordinateSystem)==='undefined' ? new Vec3(1, 2, 3) : options.coordinateSystem.clone();
  23.  
  24. /**
  25. * @property {Body} chassisBody
  26. */
  27. this.chassisBody = options.chassisBody;
  28.  
  29. if(!this.chassisBody){
  30. // No chassis body given. Create it!
  31. var chassisShape = new Box(new Vec3(5, 2, 0.5));
  32. this.chassisBody = new Body(1, chassisShape);
  33. }
  34.  
  35. /**
  36. * @property constraints
  37. * @type {Array}
  38. */
  39. this.constraints = [];
  40.  
  41. this.wheelAxes = [];
  42. this.wheelForces = [];
  43. }
  44.  
  45. /**
  46. * Add a wheel
  47. * @method addWheel
  48. * @param {object} options
  49. * @param {boolean} [options.isFrontWheel]
  50. * @param {Vec3} [options.position] Position of the wheel, locally in the chassis body.
  51. * @param {Vec3} [options.direction] Slide direction of the wheel along the suspension.
  52. * @param {Vec3} [options.axis] Axis of rotation of the wheel, locally defined in the chassis.
  53. * @param {Body} [options.body] The wheel body.
  54. */
  55. RigidVehicle.prototype.addWheel = function(options){
  56. options = options || {};
  57. var wheelBody = options.body;
  58. if(!wheelBody){
  59. wheelBody = new Body(1, new Sphere(1.2));
  60. }
  61. this.wheelBodies.push(wheelBody);
  62. this.wheelForces.push(0);
  63.  
  64. // Position constrain wheels
  65. var zero = new Vec3();
  66. var position = typeof(options.position) !== 'undefined' ? options.position.clone() : new Vec3();
  67.  
  68. // Set position locally to the chassis
  69. var worldPosition = new Vec3();
  70. this.chassisBody.pointToWorldFrame(position, worldPosition);
  71. wheelBody.position.set(worldPosition.x, worldPosition.y, worldPosition.z);
  72.  
  73. // Constrain wheel
  74. var axis = typeof(options.axis) !== 'undefined' ? options.axis.clone() : new Vec3(0, 1, 0);
  75. this.wheelAxes.push(axis);
  76.  
  77. var hingeConstraint = new HingeConstraint(this.chassisBody, wheelBody, {
  78. pivotA: position,
  79. axisA: axis,
  80. pivotB: Vec3.ZERO,
  81. axisB: axis,
  82. collideConnected: false
  83. });
  84. this.constraints.push(hingeConstraint);
  85.  
  86. return this.wheelBodies.length - 1;
  87. };
  88.  
  89. /**
  90. * Set the steering value of a wheel.
  91. * @method setSteeringValue
  92. * @param {number} value
  93. * @param {integer} wheelIndex
  94. * @todo check coordinateSystem
  95. */
  96. RigidVehicle.prototype.setSteeringValue = function(value, wheelIndex){
  97. // Set angle of the hinge axis
  98. var axis = this.wheelAxes[wheelIndex];
  99.  
  100. var c = Math.cos(value),
  101. s = Math.sin(value),
  102. x = axis.x,
  103. y = axis.y;
  104. this.constraints[wheelIndex].axisA.set(
  105. c*x -s*y,
  106. s*x +c*y,
  107. 0
  108. );
  109. };
  110.  
  111. /**
  112. * Set the target rotational speed of the hinge constraint.
  113. * @method setMotorSpeed
  114. * @param {number} value
  115. * @param {integer} wheelIndex
  116. */
  117. RigidVehicle.prototype.setMotorSpeed = function(value, wheelIndex){
  118. var hingeConstraint = this.constraints[wheelIndex];
  119. hingeConstraint.enableMotor();
  120. hingeConstraint.motorTargetVelocity = value;
  121. };
  122.  
  123. /**
  124. * Set the target rotational speed of the hinge constraint.
  125. * @method disableMotor
  126. * @param {number} value
  127. * @param {integer} wheelIndex
  128. */
  129. RigidVehicle.prototype.disableMotor = function(wheelIndex){
  130. var hingeConstraint = this.constraints[wheelIndex];
  131. hingeConstraint.disableMotor();
  132. };
  133.  
  134. var torque = new Vec3();
  135.  
  136. /**
  137. * Set the wheel force to apply on one of the wheels each time step
  138. * @method setWheelForce
  139. * @param {number} value
  140. * @param {integer} wheelIndex
  141. */
  142. RigidVehicle.prototype.setWheelForce = function(value, wheelIndex){
  143. this.wheelForces[wheelIndex] = value;
  144. };
  145.  
  146. /**
  147. * Apply a torque on one of the wheels.
  148. * @method applyWheelForce
  149. * @param {number} value
  150. * @param {integer} wheelIndex
  151. */
  152. RigidVehicle.prototype.applyWheelForce = function(value, wheelIndex){
  153. var axis = this.wheelAxes[wheelIndex];
  154. var wheelBody = this.wheelBodies[wheelIndex];
  155. var bodyTorque = wheelBody.torque;
  156.  
  157. axis.scale(value, torque);
  158. wheelBody.vectorToWorldFrame(torque, torque);
  159. bodyTorque.vadd(torque, bodyTorque);
  160. };
  161.  
  162. /**
  163. * Add the vehicle including its constraints to the world.
  164. * @method addToWorld
  165. * @param {World} world
  166. */
  167. RigidVehicle.prototype.addToWorld = function(world){
  168. var constraints = this.constraints;
  169. var bodies = this.wheelBodies.concat([this.chassisBody]);
  170.  
  171. for (var i = 0; i < bodies.length; i++) {
  172. world.add(bodies[i]);
  173. }
  174.  
  175. for (var i = 0; i < constraints.length; i++) {
  176. world.addConstraint(constraints[i]);
  177. }
  178.  
  179. world.addEventListener('preStep', this._update.bind(this));
  180. };
  181.  
  182. RigidVehicle.prototype._update = function(){
  183. var wheelForces = this.wheelForces;
  184. for (var i = 0; i < wheelForces.length; i++) {
  185. this.applyWheelForce(wheelForces[i], i);
  186. }
  187. };
  188.  
  189. /**
  190. * Remove the vehicle including its constraints from the world.
  191. * @method removeFromWorld
  192. * @param {World} world
  193. */
  194. RigidVehicle.prototype.removeFromWorld = function(world){
  195. var constraints = this.constraints;
  196. var bodies = this.wheelBodies.concat([this.chassisBody]);
  197.  
  198. for (var i = 0; i < bodies.length; i++) {
  199. world.remove(bodies[i]);
  200. }
  201.  
  202. for (var i = 0; i < constraints.length; i++) {
  203. world.removeConstraint(constraints[i]);
  204. }
  205. };
  206.  
  207. var worldAxis = new Vec3();
  208.  
  209. /**
  210. * Get current rotational velocity of a wheel
  211. * @method getWheelSpeed
  212. * @param {integer} wheelIndex
  213. */
  214. RigidVehicle.prototype.getWheelSpeed = function(wheelIndex){
  215. var axis = this.wheelAxes[wheelIndex];
  216. var wheelBody = this.wheelBodies[wheelIndex];
  217. var w = wheelBody.angularVelocity;
  218. this.chassisBody.vectorToWorldFrame(axis, worldAxis);
  219. return w.dot(worldAxis);
  220. };
  221.