BsSceneObject.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsMatrix4.h"
  4. #include "BsVector3.h"
  5. #include "BsQuaternion.h"
  6. #include "BsRTTIType.h"
  7. #include "BsCoreSceneManager.h"
  8. #include "BsGameObjectManager.h"
  9. #include "BsGameObject.h"
  10. namespace BansheeEngine
  11. {
  12. /**
  13. * @brief SceneObject represents an object in the scene graph. It has a world position,
  14. * place in the hierarchy and optionally a number of attached components.
  15. */
  16. class BS_CORE_EXPORT SceneObject : public GameObject
  17. {
  18. friend class CoreSceneManager;
  19. public:
  20. ~SceneObject();
  21. /**
  22. * @brief Creates a new SceneObject with the specified name. Object will be placed in the top
  23. * of the scene hierarchy.
  24. */
  25. static HSceneObject create(const String& name);
  26. /**
  27. * @brief Destroys this object and any of its held components.
  28. */
  29. void destroy();
  30. private:
  31. SceneObject(const String& name);
  32. static HSceneObject createInternal(const String& name);
  33. void destroyInternal();
  34. private:
  35. HSceneObject mThisHandle;
  36. /************************************************************************/
  37. /* Transform */
  38. /************************************************************************/
  39. public:
  40. /**
  41. * @brief Sets local position of the object.
  42. */
  43. void setPosition(const Vector3& position);
  44. /**
  45. * @brief Gets local position of the object.
  46. */
  47. const Vector3& getPosition() const { return mPosition; }
  48. /**
  49. * @brief Gets world position of the object.
  50. *
  51. * @note Performance warning: This might involve updating the transforms if the transform is dirty.
  52. */
  53. const Vector3& getWorldPosition() const;
  54. /**
  55. * @brief Sets the local rotation of the object.
  56. */
  57. void setRotation(const Quaternion& rotation);
  58. /**
  59. * @brief Gets the local rotation of the object.
  60. */
  61. const Quaternion& getRotation() const { return mRotation; }
  62. /**
  63. * @brief Gets world rotation of the object.
  64. *
  65. * @note Performance warning: This might involve updating the transforms if the transform is dirty.
  66. */
  67. const Quaternion& getWorldRotation() const;
  68. /**
  69. * @brief Sets the local scale of the object.
  70. */
  71. void setScale(const Vector3& scale);
  72. /**
  73. * @brief Gets the local scale of the object.
  74. */
  75. const Vector3& getScale() const { return mScale; }
  76. /**
  77. * @brief Gets world scale of the object.
  78. *
  79. * @note Performance warning: This might involve updating the transforms if the transform is dirty.
  80. */
  81. const Vector3& getWorldScale() const;
  82. /**
  83. * @brief Orients the object so it is looking at the provided "location" (local space)
  84. * where "up" is used for determining the location of the objects Y axis.
  85. *
  86. */
  87. void lookAt(const Vector3& location, const Vector3& up = Vector3::UNIT_Y);
  88. /**
  89. * @brief Gets the objects world transform matrix.
  90. *
  91. * @note Performance warning: This might involve updating the transforms if the transform is dirty.
  92. */
  93. const Matrix4& getWorldTfrm() const;
  94. /**
  95. * @brief Gets the objects local transform matrix.
  96. */
  97. const Matrix4& getLocalTfrm() const;
  98. /**
  99. * @brief Moves the object's position by the vector offset provided along world axes.
  100. */
  101. void move(const Vector3& vec);
  102. /**
  103. * @brief Moves the object's position by the vector offset provided along it's own axes (relative to orientation).
  104. */
  105. void moveRelative(const Vector3& vec);
  106. /**
  107. * @brief Gets the Z (forward) axis of the object, in world space.
  108. *
  109. * @return Forward axis of the object.
  110. */
  111. Vector3 getForward() const { return getWorldRotation().rotate(-Vector3::UNIT_Z); }
  112. /**
  113. * @brief Gets the Y (up) axis of the object, in world space.
  114. *
  115. * @return Up axis of the object.
  116. */
  117. Vector3 getUp() const { return getWorldRotation().rotate(Vector3::UNIT_Y); }
  118. /**
  119. * @brief Gets the X (right) axis of the object, in world space.
  120. *
  121. * @return Right axis of the object.
  122. */
  123. Vector3 getRight() const { return getWorldRotation().rotate(Vector3::UNIT_X); }
  124. /**
  125. * @brief Rotates the game object so it's forward axis faces the provided
  126. * direction.
  127. *
  128. * @note Local forward axis is considered to be negative Z.
  129. *
  130. * @param forwardDir The forward direction to face, in world space.
  131. */
  132. void setForward(const Vector3& forwardDir);
  133. /**
  134. * @brief Rotate the object around an arbitrary axis.
  135. */
  136. void rotate(const Vector3& axis, const Radian& angle);
  137. /**
  138. * @brief Rotate the object around an arbitrary axis using a Quaternion.
  139. */
  140. void rotate(const Quaternion& q);
  141. /**
  142. * @brief Rotates around local Z axis.
  143. *
  144. * @param angle Angle to rotate by.
  145. */
  146. void roll(const Radian& angle);
  147. /**
  148. * @brief Rotates around Y axis.
  149. *
  150. * @param angle Angle to rotate by.
  151. */
  152. void yaw(const Radian& angle);
  153. /**
  154. * @brief Rotates around X axis
  155. *
  156. * @param angle Angle to rotate by.
  157. */
  158. void pitch(const Radian& angle);
  159. private:
  160. Vector3 mPosition;
  161. Quaternion mRotation;
  162. Vector3 mScale;
  163. mutable Vector3 mWorldPosition;
  164. mutable Quaternion mWorldRotation;
  165. mutable Vector3 mWorldScale;
  166. mutable Matrix4 mCachedLocalTfrm;
  167. mutable bool mIsCachedLocalTfrmUpToDate;
  168. mutable Matrix4 mCachedWorldTfrm;
  169. mutable bool mIsCachedWorldTfrmUpToDate;
  170. /**
  171. * @brief Marks the transform as dirty so that we know to update
  172. * it when the transform is requested.
  173. */
  174. void markTfrmDirty() const;
  175. /**
  176. * @brief Updates the local transform. Normally just reconstructs the
  177. * transform matrix from the position/rotation/scale.
  178. */
  179. void updateLocalTfrm() const;
  180. /**
  181. * @brief Updates the world transform. Reconstructs the local transform
  182. * matrix and multiplies it with any parent transforms.
  183. *
  184. * @note If parent transforms are dirty they will be updated.
  185. */
  186. void updateWorldTfrm() const;
  187. /************************************************************************/
  188. /* Hierarchy */
  189. /************************************************************************/
  190. public:
  191. /**
  192. * @brief Changes the parent of this object. Also removes the object from the current parent,
  193. * and assigns it to the new parent.
  194. *
  195. * @param [in] parent New parent.
  196. */
  197. void setParent(const HSceneObject& parent);
  198. /**
  199. * @brief Gets the parent of this object.
  200. *
  201. * @return Parent object, or nullptr if this SceneObject is at root level.
  202. */
  203. HSceneObject getParent() const { return mParent; }
  204. /**
  205. * @brief Gets a child of this item.
  206. *
  207. * @param idx The zero based index of the child.
  208. *
  209. * @return SceneObject of the child.
  210. */
  211. HSceneObject getChild(UINT32 idx) const;
  212. /**
  213. * @brief Find the index of the specified child. Don't persist this value as
  214. * it may change whenever you add/remove children.
  215. *
  216. * @param child The child to look for.
  217. *
  218. * @return The zero-based index of the found child, or -1 if no match was found.
  219. */
  220. int indexOfChild(const HSceneObject& child) const;
  221. /**
  222. * @brief Gets the number of all child GameObjects.
  223. */
  224. UINT32 getNumChildren() const { return (UINT32)mChildren.size(); }
  225. /**
  226. * @brief Makes a deep copy of this object.
  227. */
  228. HSceneObject clone();
  229. private:
  230. HSceneObject mParent;
  231. Vector<HSceneObject> mChildren;
  232. /**
  233. * @brief Adds a child to the child array. This method doesn't check for null or duplicate values.
  234. *
  235. * @param [in] object New child.
  236. */
  237. void addChild(const HSceneObject& object);
  238. /**
  239. * @brief Removes the child from the object.
  240. *
  241. * @param [in] object Child to remove.
  242. *
  243. * @throws INTERNAL_ERROR If the provided child isn't a child of the current object.
  244. */
  245. void removeChild(const HSceneObject& object);
  246. /************************************************************************/
  247. /* Component */
  248. /************************************************************************/
  249. public:
  250. /**
  251. * @brief Constructs a new component of the specified type and adds it to
  252. * the internal component list.
  253. */
  254. template<class T, class... Args>
  255. GameObjectHandle<T> addComponent(Args &&... args)
  256. {
  257. static_assert((std::is_base_of<BansheeEngine::Component, T>::value),
  258. "Specified type is not a valid Component.");
  259. std::shared_ptr<T> gameObject(new (bs_alloc<T, PoolAlloc>()) T(mThisHandle,
  260. std::forward<Args>(args)...),
  261. &bs_delete<PoolAlloc, T>, StdAlloc<PoolAlloc>());
  262. GameObjectHandle<T> newComponent =
  263. GameObjectHandle<T>(GameObjectManager::instance().registerObject(gameObject));
  264. mComponents.push_back(newComponent);
  265. gSceneManager().notifyComponentAdded(newComponent);
  266. return newComponent;
  267. }
  268. /**
  269. * @brief Searches for a component with the specific type and returns the first one
  270. * it finds.
  271. *
  272. * @note Don't call this too often as it is relatively slow. It is more efficient
  273. * to call it once and store the result for further use.
  274. *
  275. * @tparam typename T Type of the component.
  276. *
  277. * @return Component if found, nullptr otherwise.
  278. */
  279. template <typename T>
  280. GameObjectHandle<T> getComponent()
  281. {
  282. static_assert((std::is_base_of<BansheeEngine::Component, T>::value),
  283. "Specified type is not a valid Component.");
  284. return static_object_cast<T>(getComponent(T::getRTTIStatic()->getRTTIId()));
  285. }
  286. /**
  287. * @brief Checks if the current object contains the specified component
  288. *
  289. * @note Don't call this too often as it is relatively slow.
  290. *
  291. * @tparam typename T Type of the component.
  292. *
  293. * @return True if component exists on the object.
  294. */
  295. template <typename T>
  296. bool hasComponent()
  297. {
  298. static_assert((std::is_base_of<BansheeEngine::Component, T>::value),
  299. "Specified type is not a valid Component.");
  300. return hasComponent(T::getRTTIStatic()->getRTTIId());
  301. }
  302. /**
  303. * @brief Searches for a component with the specified type id and returns the first one it
  304. * finds.
  305. *
  306. * @note Don't call this too often as it is relatively slow. It is more efficient to
  307. * call it once and store the result for further use.
  308. *
  309. * @param typeId Identifier for the type.
  310. *
  311. * @return Component if found, nullptr otherwise.
  312. */
  313. HComponent getComponent(UINT32 typeId) const;
  314. /**
  315. * @brief Removes the component from this object, and deallocates it.
  316. *
  317. * @param [in] component The component to destroy.
  318. */
  319. void destroyComponent(const HComponent& component);
  320. /**
  321. * @brief Removes the component from this object, and deallocates it.
  322. *
  323. * @param [in] component The component to destroy.
  324. */
  325. void destroyComponent(Component* component);
  326. /**
  327. * @brief Returns all components on this object.
  328. */
  329. const Vector<HComponent>& getComponents() const { return mComponents; }
  330. private:
  331. /**
  332. * @brief Creates an empty component with the default constructor.
  333. */
  334. template <typename T>
  335. static std::shared_ptr<T> createEmptyComponent()
  336. {
  337. static_assert((std::is_base_of<BansheeEngine::Component, T>::value), "Specified type is not a valid Component.");
  338. std::shared_ptr<T> gameObject(new (bs_alloc<T, PoolAlloc>()) T(), &bs_delete<PoolAlloc, T>, StdAlloc<PoolAlloc>());
  339. GameObjectHandle<T>(GameObjectManager::instance().registerObject(gameObject));
  340. return gameObject;
  341. }
  342. /**
  343. * @brief Adds the component to the internal component array.
  344. */
  345. void addComponentInternal(const std::shared_ptr<Component> component);
  346. Vector<HComponent> mComponents;
  347. /************************************************************************/
  348. /* RTTI */
  349. /************************************************************************/
  350. public:
  351. friend class GameObjectRTTI;
  352. friend class SceneObjectRTTI;
  353. static RTTITypeBase* getRTTIStatic();
  354. virtual RTTITypeBase* getRTTI() const;
  355. };
  356. }