/src/away3d/cameras/SpringCam.as

http://github.com/away3d/away3d-core-fp11 · ActionScript · 132 lines · 69 code · 28 blank · 35 comment · 4 complexity · 64ef20d47808d9d3bf836eba0169522a MD5 · raw file

  1. package away3d.cameras
  2. {
  3. import away3d.cameras.lenses.LensBase;
  4. import away3d.core.base.Object3D;
  5. import flash.geom.Matrix3D;
  6. import flash.geom.Vector3D;
  7. /**
  8. * v1 - 2009-01-21 b at turbulent dot ca - http://agit8.turbulent.ca
  9. * v2 - 2011-03-06 Ringo - http://www.ringo.nl/en/
  10. **/
  11. /**
  12. * A 1st and 3d person camera(depending on positionOffset!), hooked on a physical spring on an optional target.
  13. */
  14. public class SpringCam extends Camera3D
  15. {
  16. /**
  17. * [optional] Target object3d that camera should follow. If target is null, camera behaves just like a normal Camera3D.
  18. */
  19. public var target:Object3D;
  20. //spring stiffness
  21. /**
  22. * Stiffness of the spring, how hard is it to extend. The higher it is, the more "fixed" the cam will be.
  23. * A number between 1 and 20 is recommended.
  24. */
  25. public var stiffness:Number = 1;
  26. /**
  27. * Damping is the spring internal friction, or how much it resists the "boinggggg" effect. Too high and you'll lose it!
  28. * A number between 1 and 20 is recommended.
  29. */
  30. public var damping:Number = 4;
  31. /**
  32. * Mass of the camera, if over 120 and it'll be very heavy to move.
  33. */
  34. public var mass:Number = 40;
  35. /**
  36. * Offset of spring center from target in target object space, ie: Where the camera should ideally be in the target object space.
  37. */
  38. public var positionOffset:Vector3D = new Vector3D(0,5,-50);
  39. /**
  40. * offset of facing in target object space, ie: where in the target object space should the camera look.
  41. */
  42. public var lookOffset:Vector3D = new Vector3D(0,2,10);
  43. //zrot to apply to the cam
  44. private var _zrot:Number = 0;
  45. //private physics members
  46. private var _velocity:Vector3D = new Vector3D();
  47. private var _dv:Vector3D = new Vector3D();
  48. private var _stretch:Vector3D = new Vector3D();
  49. private var _force:Vector3D = new Vector3D();
  50. private var _acceleration:Vector3D = new Vector3D();
  51. //private target members
  52. private var _desiredPosition:Vector3D = new Vector3D();
  53. private var _lookAtPosition:Vector3D = new Vector3D();
  54. //private transformed members
  55. private var _xPositionOffset:Vector3D = new Vector3D();
  56. private var _xLookOffset:Vector3D = new Vector3D();
  57. private var _xPosition:Vector3D = new Vector3D();
  58. private var _viewProjection : Matrix3D = new Matrix3D();
  59. public function SpringCam(lens : LensBase = null)
  60. {
  61. super(lens);
  62. }
  63. /**
  64. * Rotation in degrees along the camera Z vector to apply to the camera after it turns towards the target .
  65. */
  66. public function set zrot(n:Number):void
  67. {
  68. _zrot = n;
  69. if(_zrot < 0.001) n = 0;
  70. }
  71. public function get zrot():Number
  72. {
  73. return _zrot;
  74. }
  75. public override function get viewProjection():Matrix3D
  76. {
  77. if(target != null)
  78. {
  79. _xPositionOffset = target.transform.deltaTransformVector(positionOffset);
  80. _xLookOffset = target.transform.deltaTransformVector(lookOffset);
  81. _desiredPosition = target.position.add(_xPositionOffset);
  82. _lookAtPosition = target.position.add(_xLookOffset);
  83. _stretch = this.position.subtract(_desiredPosition);
  84. _stretch.scaleBy(-stiffness);
  85. _dv = _velocity.clone();
  86. _dv.scaleBy(damping);
  87. _force = _stretch.subtract(_dv);
  88. _acceleration = _force.clone();
  89. _acceleration.scaleBy(1/mass);
  90. _velocity = _velocity.add(_acceleration);
  91. _xPosition = position.add(_velocity);
  92. x = _xPosition.x;
  93. y = _xPosition.y;
  94. z = _xPosition.z;
  95. lookAt(_lookAtPosition);
  96. if(Math.abs(_zrot) > 0)
  97. rotate(Vector3D.Z_AXIS, _zrot);
  98. _viewProjection.copyFrom(inverseSceneTransform);
  99. _viewProjection.append(this.lens.matrix);
  100. }
  101. return this._viewProjection;
  102. }
  103. }
  104. }