CmGameObject.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. { }
  15. GameObject::~GameObject()
  16. {
  17. if(!mThisHandle.isDestroyed())
  18. {
  19. LOGWRN("Object is being deleted without being destroyed first?");
  20. destroyInternal();
  21. }
  22. }
  23. HGameObject GameObject::create(const String& name)
  24. {
  25. HGameObject newObject = createInternal(name);
  26. gSceneManager().registerNewGO(newObject);
  27. return newObject;
  28. }
  29. HGameObject GameObject::createInternal(const String& name)
  30. {
  31. HGameObject gameObject = HGameObject(new GameObject(name));
  32. gameObject->mThisHandle = gameObject;
  33. return gameObject;
  34. }
  35. void GameObject::destroy()
  36. {
  37. // Parent is our owner, so when his reference to us is removed, delete might be called.
  38. // So make sure this is the last thing we do.
  39. if(mParent != nullptr)
  40. {
  41. if(!mParent.isDestroyed())
  42. mParent->removeChild(mThisHandle);
  43. }
  44. destroyInternal();
  45. }
  46. void GameObject::destroyInternal()
  47. {
  48. for(auto iter = mChildren.begin(); iter != mChildren.end(); ++iter)
  49. (*iter)->destroyInternal();
  50. mChildren.clear();
  51. for(auto iter = mComponents.begin(); iter != mComponents.end(); ++iter)
  52. (*iter)->destroy();
  53. mComponents.clear();
  54. mThisHandle.releaseHeldObject();
  55. }
  56. /************************************************************************/
  57. /* Transform */
  58. /************************************************************************/
  59. void GameObject::setPosition(const Vector3& position)
  60. {
  61. mPosition = position;
  62. markTfrmDirty();
  63. }
  64. void GameObject::setRotation(const Quaternion& rotation)
  65. {
  66. mRotation = rotation;
  67. markTfrmDirty();
  68. }
  69. void GameObject::setScale(const Vector3& scale)
  70. {
  71. mScale = scale;
  72. markTfrmDirty();
  73. }
  74. const Vector3& GameObject::getWorldPosition() const
  75. {
  76. if(!mIsCachedWorldTfrmUpToDate)
  77. updateWorldTfrm();
  78. return mWorldPosition;
  79. }
  80. const Quaternion& GameObject::getWorldRotation() const
  81. {
  82. if(!mIsCachedWorldTfrmUpToDate)
  83. updateWorldTfrm();
  84. return mWorldRotation;
  85. }
  86. const Vector3& GameObject::getWorldScale() const
  87. {
  88. if(!mIsCachedWorldTfrmUpToDate)
  89. updateWorldTfrm();
  90. return mWorldScale;
  91. }
  92. void GameObject::lookAt(const Vector3& location, const Vector3& up)
  93. {
  94. Vector3 forward = location - mPosition;
  95. forward.normalise();
  96. // TODO - I'm ignoring "up" direction
  97. setForward(forward);
  98. Quaternion upRot = getUp().getRotationTo(up);
  99. setRotation(getRotation() * upRot);
  100. }
  101. const Matrix4& GameObject::getWorldTfrm() const
  102. {
  103. if(!mIsCachedWorldTfrmUpToDate)
  104. updateWorldTfrm();
  105. return mCachedWorldTfrm;
  106. }
  107. const Matrix4& GameObject::getLocalTfrm() const
  108. {
  109. if(!mIsCachedLocalTfrmUpToDate)
  110. updateLocalTfrm();
  111. return mCachedLocalTfrm;
  112. }
  113. void GameObject::move(const Vector3& vec)
  114. {
  115. setPosition(mPosition + vec);
  116. }
  117. void GameObject::moveRelative(const Vector3& vec)
  118. {
  119. // Transform the axes of the relative vector by camera's local axes
  120. Vector3 trans = mRotation * vec;
  121. setPosition(mPosition + trans);
  122. }
  123. void GameObject::rotate(const Vector3& axis, const Radian& angle)
  124. {
  125. Quaternion q;
  126. q.FromAngleAxis(angle,axis);
  127. rotate(q);
  128. }
  129. void GameObject::rotate(const Quaternion& q)
  130. {
  131. // Note the order of the mult, i.e. q comes after
  132. // Normalize the quat to avoid cumulative problems with precision
  133. Quaternion qnorm = q;
  134. qnorm.normalise();
  135. setRotation(qnorm * mRotation);
  136. }
  137. void GameObject::roll(const Radian& angle)
  138. {
  139. // Rotate around local Z axis
  140. Vector3 zAxis = mRotation * Vector3::UNIT_Z;
  141. rotate(zAxis, angle);
  142. }
  143. void GameObject::yaw(const Radian& angle)
  144. {
  145. Vector3 yAxis = mRotation * Vector3::UNIT_Y;
  146. rotate(yAxis, angle);
  147. }
  148. void GameObject::pitch(const Radian& angle)
  149. {
  150. // Rotate around local X axis
  151. Vector3 xAxis = mRotation * Vector3::UNIT_X;
  152. rotate(xAxis, angle);
  153. }
  154. void GameObject::setForward(const Vector3& forwardDir)
  155. {
  156. if (forwardDir == Vector3::ZERO)
  157. return;
  158. Vector3 nrmForwardDir = forwardDir.normalisedCopy();
  159. Vector3 currentForwardDir = getForward();
  160. const Quaternion& currentRotation = getWorldRotation();
  161. Quaternion targetRotation;
  162. if ((nrmForwardDir+currentForwardDir).squaredLength() < 0.00005f)
  163. {
  164. // Oops, a 180 degree turn (infinite possible rotation axes)
  165. // Default to yaw i.e. use current UP
  166. targetRotation = Quaternion(-currentRotation.y, -currentRotation.z, currentRotation.w, currentRotation.x);
  167. }
  168. else
  169. {
  170. // Derive shortest arc to new direction
  171. Quaternion rotQuat = currentForwardDir.getRotationTo(nrmForwardDir);
  172. targetRotation = rotQuat * currentRotation;
  173. }
  174. setRotation(targetRotation);
  175. }
  176. void GameObject::markTfrmDirty() const
  177. {
  178. mIsCachedLocalTfrmUpToDate = false;
  179. if(mIsCachedWorldTfrmUpToDate) // If it's already marked as dirty, no point is recursing the hierarchy again
  180. {
  181. mIsCachedWorldTfrmUpToDate = false;
  182. for(auto iter = mChildren.begin(); iter != mChildren.end(); ++iter)
  183. {
  184. (*iter)->markTfrmDirty();
  185. }
  186. }
  187. }
  188. void GameObject::updateWorldTfrm() const
  189. {
  190. if(mParent != nullptr)
  191. {
  192. mCachedWorldTfrm = getLocalTfrm() * mParent->getWorldTfrm();
  193. // Update orientation
  194. const Quaternion& parentOrientation = mParent->getWorldRotation();
  195. mWorldRotation = parentOrientation * mRotation;
  196. // Update scale
  197. const Vector3& parentScale = mParent->getWorldScale();
  198. // Scale own position by parent scale, NB just combine
  199. // as equivalent axes, no shearing
  200. mWorldScale = parentScale * mScale;
  201. // Change position vector based on parent's orientation & scale
  202. mWorldPosition = parentOrientation * (parentScale * mPosition);
  203. // Add altered position vector to parents
  204. mWorldPosition += mParent->getWorldPosition();
  205. }
  206. else
  207. {
  208. mCachedWorldTfrm = getLocalTfrm();
  209. mWorldRotation = mRotation;
  210. mWorldPosition = mPosition;
  211. mWorldScale = mScale;
  212. }
  213. mIsCachedWorldTfrmUpToDate = true;
  214. }
  215. void GameObject::updateLocalTfrm() const
  216. {
  217. mCachedLocalTfrm.makeTransform(mPosition, mScale, mRotation);
  218. mIsCachedLocalTfrmUpToDate = true;
  219. }
  220. /************************************************************************/
  221. /* Hierarchy */
  222. /************************************************************************/
  223. void GameObject::setParent(const HGameObject& parent)
  224. {
  225. if(parent.isDestroyed())
  226. {
  227. CM_EXCEPT(InternalErrorException,
  228. "Trying to assign a GameObject parent that is destroyed.");
  229. }
  230. if(mParent == nullptr || mParent != parent)
  231. {
  232. if(mParent != nullptr)
  233. mParent->removeChild(mThisHandle);
  234. if(parent != nullptr)
  235. parent->addChild(mThisHandle);
  236. mParent = parent;
  237. markTfrmDirty();
  238. }
  239. }
  240. HGameObject GameObject::getChild(unsigned int idx) const
  241. {
  242. if(idx < 0 || idx >= mChildren.size())
  243. {
  244. CM_EXCEPT(InternalErrorException,
  245. "Child index out of range.");
  246. }
  247. return mChildren[idx];
  248. }
  249. int GameObject::indexOfChild(const HGameObject& child) const
  250. {
  251. for(int i = 0; i < (int)mChildren.size(); i++)
  252. {
  253. if(mChildren[i] == child)
  254. return i;
  255. }
  256. return -1;
  257. }
  258. void GameObject::addChild(const HGameObject& object)
  259. {
  260. mChildren.push_back(object);
  261. }
  262. void GameObject::removeChild(const HGameObject& object)
  263. {
  264. auto result = find(mChildren.begin(), mChildren.end(), object);
  265. if(result != mChildren.end())
  266. mChildren.erase(result);
  267. else
  268. {
  269. CM_EXCEPT(InternalErrorException,
  270. "Trying to remove a child but it's not a child of the transform.");
  271. }
  272. }
  273. ComponentPtr GameObject::getComponent(UINT32 typeId) const
  274. {
  275. for(auto iter = mComponents.begin(); iter != mComponents.end(); ++iter)
  276. {
  277. if((*iter)->getRTTI()->getRTTIId() == typeId)
  278. return *iter;
  279. }
  280. return nullptr;
  281. }
  282. void GameObject::destroyComponent(ComponentPtr component)
  283. {
  284. if(component == nullptr)
  285. {
  286. LOGDBG("Trying to remove a null component");
  287. return;
  288. }
  289. auto iter = std::find(mComponents.begin(), mComponents.end(), component);
  290. if(iter != mComponents.end())
  291. {
  292. (*iter)->destroy();
  293. mComponents.erase(iter);
  294. gSceneManager().notifyComponentRemoved((*iter));
  295. }
  296. else
  297. LOGDBG("Trying to remove a component that doesn't exist on this GameObject.");
  298. }
  299. }