VelocityNode.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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.position = this.target.position.clone();
  9. this.velocity = new THREE.Vector3();
  10. this.moment = new THREE.Vector3();
  11. this.params = params || {};
  12. };
  13. THREE.VelocityNode.prototype = Object.create( THREE.Vector3Node.prototype );
  14. THREE.VelocityNode.prototype.constructor = THREE.VelocityNode;
  15. THREE.VelocityNode.prototype.updateFrame = function( delta ) {
  16. this.velocity.subVectors( this.target.position, this.position );
  17. this.position.copy( this.target.position );
  18. switch ( this.params.type ) {
  19. case "elastic":
  20. delta *= this.params.fps || 60;
  21. var spring = Math.pow( this.params.spring, delta );
  22. var friction = Math.pow( this.params.friction, delta );
  23. // spring
  24. this.moment.x += this.velocity.x * spring;
  25. this.moment.y += this.velocity.y * spring;
  26. this.moment.z += this.velocity.z * spring;
  27. // friction
  28. this.moment.x *= friction;
  29. this.moment.y *= friction;
  30. this.moment.z *= friction;
  31. this.value.copy( this.moment );
  32. break;
  33. default:
  34. this.value.copy( this.velocity );
  35. break;
  36. }
  37. };