VelocityNode.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. THREE.VelocityNode = function ( target, params ) {
  5. THREE.Vector3Node.call( this );
  6. this.requestUpdate = true;
  7. this.target = target;
  8. this.params = params || {};
  9. this.position = this.target.position.clone();
  10. this.velocity = new THREE.Vector3();
  11. switch ( this.params.type ) {
  12. case "elastic":
  13. this.moment = new THREE.Vector3();
  14. this.speed = new THREE.Vector3();
  15. this.springVelocity = new THREE.Vector3();
  16. this.lastVelocity = new THREE.Vector3();
  17. break;
  18. }
  19. };
  20. THREE.VelocityNode.prototype = Object.create( THREE.Vector3Node.prototype );
  21. THREE.VelocityNode.prototype.constructor = THREE.VelocityNode;
  22. THREE.VelocityNode.prototype.updateFrame = function ( delta ) {
  23. this.velocity.subVectors( this.target.position, this.position );
  24. this.position.copy( this.target.position );
  25. switch ( this.params.type ) {
  26. case "elastic":
  27. // convert to real scale: 0 at 1 values
  28. var deltaFps = delta * (this.params.fps || 60);
  29. var spring = Math.pow( this.params.spring, deltaFps ),
  30. damping = Math.pow( this.params.damping, deltaFps );
  31. // fix relative frame-rate
  32. this.velocity.multiplyScalar( Math.exp( -this.params.damping * deltaFps ) );
  33. // elastic
  34. this.velocity.add( this.springVelocity );
  35. this.velocity.add( this.speed.multiplyScalar( damping ).multiplyScalar( 1 - spring ) );
  36. // speed
  37. this.speed.subVectors( this.velocity, this.lastVelocity );
  38. // spring velocity
  39. this.springVelocity.add( this.speed );
  40. this.springVelocity.multiplyScalar( spring );
  41. // moment
  42. this.moment.add( this.springVelocity );
  43. // damping
  44. this.moment.multiplyScalar( damping );
  45. this.lastVelocity.copy( this.velocity );
  46. this.value.copy( this.moment );
  47. break;
  48. default:
  49. this.value.copy( this.velocity );
  50. }
  51. };