CmGameObject.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #include "CmGameObject.h"
  2. #include "CmComponent.h"
  3. #include "CmSceneManager.h"
  4. #include "CmException.h"
  5. #include "CmDebug.h"
  6. namespace CamelotEngine
  7. {
  8. GameObject::GameObject(const String& name)
  9. :mName(name), mPosition(Vector3::ZERO), mRotation(Quaternion::IDENTITY), mScale(Vector3::UNIT_SCALE),
  10. mWorldPosition(Vector3::ZERO), mWorldRotation(Quaternion::IDENTITY), mWorldScale(Vector3::UNIT_SCALE),
  11. mCachedLocalTfrm(Matrix4::IDENTITY), mIsCachedLocalTfrmUpToDate(false),
  12. mCachedWorldTfrm(Matrix4::IDENTITY), mIsCachedWorldTfrmUpToDate(false),
  13. mCustomWorldTfrm(Matrix4::IDENTITY), mIsCustomTfrmModeActive(false),
  14. mIsDestroyed(false)
  15. { }
  16. GameObject::~GameObject()
  17. {
  18. if(!mIsDestroyed)
  19. destroy();
  20. }
  21. GameObjectPtr GameObject::create(const String& name)
  22. {
  23. GameObjectPtr newObject = createInternal(name);
  24. gSceneManager().registerNewGO(newObject);
  25. return newObject;
  26. }
  27. GameObjectPtr GameObject::createInternal(const String& name)
  28. {
  29. GameObjectPtr newObject = GameObjectPtr(new GameObject(name));
  30. newObject->mThis = newObject;
  31. return newObject;
  32. }
  33. void GameObject::destroy()
  34. {
  35. mIsDestroyed = true;
  36. for(auto iter = mChildren.begin(); iter != mChildren.end(); ++iter)
  37. (*iter)->destroy();
  38. mChildren.clear();
  39. for(auto iter = mComponents.begin(); iter != mComponents.end(); ++iter)
  40. (*iter)->destroy();
  41. mComponents.clear();
  42. // Parent is our owner, so when his reference to us is removed, delete might be called.
  43. // So make sure this is the last thing we do.
  44. if(!mParent.expired())
  45. {
  46. GameObjectPtr parentPtr = mParent.lock();
  47. if(!parentPtr->isDestroyed())
  48. parentPtr->removeChild(mThis.lock());
  49. }
  50. }
  51. /************************************************************************/
  52. /* Transform */
  53. /************************************************************************/
  54. void GameObject::setPosition(const Vector3& position)
  55. {
  56. mPosition = position;
  57. markTfrmDirty();
  58. }
  59. void GameObject::setRotation(const Quaternion& rotation)
  60. {
  61. mRotation = rotation;
  62. markTfrmDirty();
  63. }
  64. void GameObject::setScale(const Vector3& scale)
  65. {
  66. mScale = scale;
  67. markTfrmDirty();
  68. }
  69. const Vector3& GameObject::getWorldPosition() const
  70. {
  71. if(!mIsCachedWorldTfrmUpToDate)
  72. updateWorldTfrm();
  73. return mWorldPosition;
  74. }
  75. const Quaternion& GameObject::getWorldRotation() const
  76. {
  77. if(!mIsCachedWorldTfrmUpToDate)
  78. updateWorldTfrm();
  79. return mWorldRotation;
  80. }
  81. const Vector3& GameObject::getWorldScale() const
  82. {
  83. if(!mIsCachedWorldTfrmUpToDate)
  84. updateWorldTfrm();
  85. return mWorldScale;
  86. }
  87. void GameObject::lookAt(const Vector3& location, const Vector3& up)
  88. {
  89. Vector3 forward = location - mPosition;
  90. forward.normalise();
  91. Vector3 upCopy = up;
  92. upCopy.normalise();
  93. Vector3 right = forward.crossProduct(up);
  94. right.normalise();
  95. Quaternion newRotation;
  96. newRotation.FromAxes(right, upCopy, forward);
  97. setRotation(newRotation);
  98. }
  99. const Matrix4& GameObject::getWorldTfrm() const
  100. {
  101. if(!mIsCachedWorldTfrmUpToDate)
  102. updateWorldTfrm();
  103. return mCachedWorldTfrm;
  104. }
  105. const Matrix4& GameObject::getLocalTfrm() const
  106. {
  107. if(!mIsCachedLocalTfrmUpToDate)
  108. updateLocalTfrm();
  109. return mCachedLocalTfrm;
  110. }
  111. void GameObject::markTfrmDirty() const
  112. {
  113. mIsCachedLocalTfrmUpToDate = false;
  114. if(mIsCachedWorldTfrmUpToDate) // If it's already marked as dirty, no point is recursing the hierarchy again
  115. {
  116. mIsCachedWorldTfrmUpToDate = false;
  117. for(auto iter = mChildren.begin(); iter != mChildren.end(); ++iter)
  118. {
  119. (*iter)->markTfrmDirty();
  120. }
  121. }
  122. }
  123. void GameObject::updateWorldTfrm() const
  124. {
  125. if(!mParent.expired())
  126. {
  127. GameObjectPtr tempParentPtr = mParent.lock();
  128. mCachedWorldTfrm = getLocalTfrm() * tempParentPtr->getWorldTfrm();
  129. // Update orientation
  130. const Quaternion& parentOrientation = tempParentPtr->getWorldRotation();
  131. mWorldRotation = parentOrientation * mRotation;
  132. // Update scale
  133. const Vector3& parentScale = tempParentPtr->getWorldScale();
  134. // Scale own position by parent scale, NB just combine
  135. // as equivalent axes, no shearing
  136. mWorldScale = parentScale * mScale;
  137. // Change position vector based on parent's orientation & scale
  138. mWorldPosition = parentOrientation * (parentScale * mPosition);
  139. // Add altered position vector to parents
  140. mWorldPosition += tempParentPtr->getWorldPosition();
  141. }
  142. else
  143. {
  144. mCachedWorldTfrm = getLocalTfrm();
  145. mWorldRotation = mRotation;
  146. mWorldPosition = mPosition;
  147. mWorldScale = mScale;
  148. }
  149. mIsCachedWorldTfrmUpToDate = true;
  150. }
  151. void GameObject::updateLocalTfrm() const
  152. {
  153. mCachedLocalTfrm.makeTransform(mPosition, mScale, mRotation);
  154. mIsCachedLocalTfrmUpToDate = true;
  155. }
  156. /************************************************************************/
  157. /* Hierarchy */
  158. /************************************************************************/
  159. void GameObject::setParent(GameObjectPtr parent)
  160. {
  161. if(parent == nullptr || parent->isDestroyed())
  162. {
  163. CM_EXCEPT(InternalErrorException,
  164. "Parent is not allowed to be NULL or destroyed.");
  165. }
  166. if(mParent.expired() || mParent.lock() != parent)
  167. {
  168. if(!mParent.expired())
  169. mParent.lock()->removeChild(mThis.lock());
  170. if(parent != nullptr)
  171. parent->addChild(mThis.lock());
  172. mParent = parent;
  173. markTfrmDirty();
  174. }
  175. }
  176. GameObjectPtr GameObject::getChild(unsigned int idx) const
  177. {
  178. if(idx < 0 || idx >= mChildren.size())
  179. {
  180. CM_EXCEPT(InternalErrorException,
  181. "Child index out of range.");
  182. }
  183. return mChildren[idx];
  184. }
  185. int GameObject::indexOfChild(const GameObjectPtr child) const
  186. {
  187. for(int i = 0; i < (int)mChildren.size(); i++)
  188. {
  189. if(mChildren[i] == child)
  190. return i;
  191. }
  192. return -1;
  193. }
  194. void GameObject::addChild(GameObjectPtr object)
  195. {
  196. mChildren.push_back(object);
  197. }
  198. void GameObject::removeChild(GameObjectPtr object)
  199. {
  200. auto result = find(mChildren.begin(), mChildren.end(), object);
  201. if(result != mChildren.end())
  202. mChildren.erase(result);
  203. else
  204. {
  205. CM_EXCEPT(InternalErrorException,
  206. "Trying to remove a child but it's not a child of the transform.");
  207. }
  208. }
  209. void GameObject::destroyComponent(ComponentPtr component)
  210. {
  211. if(component == nullptr)
  212. {
  213. LOGDBG("Trying to remove a null component");
  214. return;
  215. }
  216. auto iter = std::find(mComponents.begin(), mComponents.end(), component);
  217. if(iter != mComponents.end())
  218. {
  219. (*iter)->destroy();
  220. mComponents.erase(iter);
  221. gSceneManager().notifyComponentRemoved((*iter));
  222. }
  223. else
  224. LOGDBG("Trying to remove a component that doesn't exist on this GameObject.");
  225. }
  226. }