cameraOrbiterComponent.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "T3D/components/camera/cameraOrbitercomponent.h"
  23. #include "core/util/safeDelete.h"
  24. #include "console/consoleTypes.h"
  25. #include "console/consoleObject.h"
  26. #include "core/stream/bitStream.h"
  27. #include "console/engineAPI.h"
  28. #include "sim/netConnection.h"
  29. #include "math/mathUtils.h"
  30. //////////////////////////////////////////////////////////////////////////
  31. // Constructor/Destructor
  32. //////////////////////////////////////////////////////////////////////////
  33. CameraOrbiterComponent::CameraOrbiterComponent() : Component()
  34. {
  35. mMinOrbitDist = 0.0f;
  36. mMaxOrbitDist = 0.0f;
  37. mCurOrbitDist = 8.0f;
  38. mPosition.set(0.0f, 0.0f, 0.0f);
  39. mMaxPitchAngle = 70;
  40. mMinPitchAngle = -10;
  41. mRotation.set(0, 0, 0);
  42. mCamera = NULL;
  43. }
  44. CameraOrbiterComponent::~CameraOrbiterComponent()
  45. {
  46. }
  47. IMPLEMENT_CO_NETOBJECT_V1(CameraOrbiterComponent);
  48. bool CameraOrbiterComponent::onAdd()
  49. {
  50. if (!Parent::onAdd())
  51. return false;
  52. return true;
  53. }
  54. void CameraOrbiterComponent::onRemove()
  55. {
  56. Parent::onRemove();
  57. }
  58. void CameraOrbiterComponent::initPersistFields()
  59. {
  60. Parent::initPersistFields();
  61. addField("orbitDistance", TypeF32, Offset(mCurOrbitDist, CameraOrbiterComponent), "Object world orientation.");
  62. addField("Rotation", TypeRotationF, Offset(mRotation, CameraOrbiterComponent), "Object world orientation.");
  63. addField("maxPitchAngle", TypeF32, Offset(mMaxPitchAngle, CameraOrbiterComponent), "Object world orientation.");
  64. addField("minPitchAngle", TypeF32, Offset(mMinPitchAngle, CameraOrbiterComponent), "Object world orientation.");
  65. }
  66. //This is mostly a catch for situations where the behavior is re-added to the object and the like and we may need to force an update to the behavior
  67. void CameraOrbiterComponent::onComponentAdd()
  68. {
  69. Parent::onComponentAdd();
  70. CameraComponent *cam = mOwner->getComponent<CameraComponent>();
  71. if (cam)
  72. {
  73. mCamera = cam;
  74. }
  75. }
  76. void CameraOrbiterComponent::onComponentRemove()
  77. {
  78. Parent::onComponentRemove();
  79. }
  80. U32 CameraOrbiterComponent::packUpdate(NetConnection *con, U32 mask, BitStream *stream)
  81. {
  82. U32 retMask = Parent::packUpdate(con, mask, stream);
  83. return retMask;
  84. }
  85. void CameraOrbiterComponent::unpackUpdate(NetConnection *con, BitStream *stream)
  86. {
  87. Parent::unpackUpdate(con, stream);
  88. }
  89. void CameraOrbiterComponent::processTick()
  90. {
  91. Parent::processTick();
  92. if (!mOwner)
  93. return;
  94. if (mCamera)
  95. {
  96. //Clamp our pitch to whatever range we allow, first.
  97. mRotation.x = mClampF(mRotation.x, mDegToRad(mMinPitchAngle), mDegToRad(mMaxPitchAngle));
  98. MatrixF ownerTrans = mOwner->getRenderTransform();
  99. Point3F ownerPos = ownerTrans.getPosition();
  100. Point3F pos;
  101. pos.x = mCurOrbitDist * mSin(mRotation.x + M_HALFPI_F) * mCos(-1.0f * (mRotation.z + M_HALFPI_F));
  102. pos.y = mCurOrbitDist * mSin(mRotation.x + M_HALFPI_F) * mSin(-1.0f * (mRotation.z + M_HALFPI_F));
  103. pos.z = mCurOrbitDist * mSin(mRotation.x);
  104. //orient the camera towards the owner
  105. VectorF ownerVec = ownerPos - pos;
  106. ownerVec.normalize();
  107. MatrixF xRot, zRot, cameraMatrix;
  108. xRot.set(EulerF(mRotation.x, 0.0f, 0.0f));
  109. zRot.set(EulerF(0.0f, 0.0f, mRotation.z));
  110. cameraMatrix.mul(zRot, xRot);
  111. cameraMatrix.getColumn(1, &ownerVec);
  112. cameraMatrix.setColumn(3, pos - ownerVec * pos);
  113. RotationF camRot = RotationF(cameraMatrix);
  114. if (camRot != mCamera->getRotOffset())
  115. mCamera->setRotation(camRot);
  116. if (pos != mCamera->getPosOffset())
  117. mCamera->setPosition(pos);
  118. }
  119. }