Node.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. #include "Base.h"
  2. #include "Node.h"
  3. #include "Scene.h"
  4. #include "Joint.h"
  5. #include "PhysicsRigidBody.h"
  6. #include "PhysicsGhostObject.h"
  7. #include "PhysicsCharacter.h"
  8. #include "Game.h"
  9. #define NODE_DIRTY_WORLD 1
  10. #define NODE_DIRTY_BOUNDS 2
  11. #define NODE_DIRTY_ALL (NODE_DIRTY_WORLD | NODE_DIRTY_BOUNDS)
  12. namespace gameplay
  13. {
  14. Node::Node(const char* id)
  15. : _scene(NULL), _firstChild(NULL), _nextSibling(NULL), _prevSibling(NULL), _parent(NULL), _childCount(NULL),
  16. _camera(NULL), _light(NULL), _model(NULL), _form(NULL), _audioSource(NULL), _particleEmitter(NULL),
  17. _collisionObject(NULL), _dirtyBits(NODE_DIRTY_ALL), _notifyHierarchyChanged(true)
  18. {
  19. if (id)
  20. {
  21. _id = id;
  22. }
  23. }
  24. Node::~Node()
  25. {
  26. removeAllChildren();
  27. if (_model)
  28. _model->setNode(NULL);
  29. if (_audioSource)
  30. _audioSource->setNode(NULL);
  31. if (_particleEmitter)
  32. _particleEmitter->setNode(NULL);
  33. if (_form)
  34. _form->setNode(NULL);
  35. SAFE_RELEASE(_camera);
  36. SAFE_RELEASE(_light);
  37. SAFE_RELEASE(_model);
  38. SAFE_RELEASE(_audioSource);
  39. SAFE_RELEASE(_particleEmitter);
  40. SAFE_RELEASE(_form);
  41. SAFE_DELETE(_collisionObject);
  42. }
  43. Node* Node::create(const char* id)
  44. {
  45. return new Node(id);
  46. }
  47. const char* Node::getId() const
  48. {
  49. return _id.c_str();
  50. }
  51. void Node::setId(const char* id)
  52. {
  53. if (id)
  54. {
  55. _id = id;
  56. }
  57. }
  58. Node::Type Node::getType() const
  59. {
  60. return Node::NODE;
  61. }
  62. void Node::addChild(Node* child)
  63. {
  64. assert(child);
  65. if (child->_parent == this)
  66. {
  67. // This node is already present in our hierarchy
  68. return;
  69. }
  70. child->addRef();
  71. // If the item belongs to another hierarchy, remove it first.
  72. if (child->_parent)
  73. {
  74. child->_parent->removeChild(child);
  75. }
  76. else if (child->_scene)
  77. {
  78. child->_scene->removeNode(child);
  79. }
  80. // Order is irrelevant, so add to the beginning of the list.
  81. if (_firstChild)
  82. {
  83. _firstChild->_prevSibling = child;
  84. child->_nextSibling = _firstChild;
  85. _firstChild = child;
  86. }
  87. else
  88. {
  89. _firstChild = child;
  90. }
  91. child->_parent = this;
  92. ++_childCount;
  93. if (_notifyHierarchyChanged)
  94. {
  95. hierarchyChanged();
  96. }
  97. }
  98. void Node::removeChild(Node* child)
  99. {
  100. if (child == NULL || child->_parent != this)
  101. {
  102. // The child is not in our hierarchy.
  103. return;
  104. }
  105. // Call remove on the child.
  106. child->remove();
  107. SAFE_RELEASE(child);
  108. }
  109. void Node::removeAllChildren()
  110. {
  111. _notifyHierarchyChanged = false;
  112. while (_firstChild)
  113. {
  114. removeChild(_firstChild);
  115. }
  116. _notifyHierarchyChanged = true;
  117. hierarchyChanged();
  118. }
  119. void Node::remove()
  120. {
  121. // Re-link our neighbours.
  122. if (_prevSibling)
  123. {
  124. _prevSibling->_nextSibling = _nextSibling;
  125. }
  126. if (_nextSibling)
  127. {
  128. _nextSibling->_prevSibling = _prevSibling;
  129. }
  130. // Update our parent.
  131. Node* parent = _parent;
  132. if (parent)
  133. {
  134. if (this == parent->_firstChild)
  135. {
  136. parent->_firstChild = _nextSibling;
  137. }
  138. --parent->_childCount;
  139. }
  140. _nextSibling = NULL;
  141. _prevSibling = NULL;
  142. _parent = NULL;
  143. if (parent && parent->_notifyHierarchyChanged)
  144. {
  145. parent->hierarchyChanged();
  146. }
  147. }
  148. Node* Node::getFirstChild() const
  149. {
  150. return _firstChild;
  151. }
  152. Node* Node::getNextSibling() const
  153. {
  154. return _nextSibling;
  155. }
  156. Node* Node::getPreviousSibling() const
  157. {
  158. return _prevSibling;
  159. }
  160. Node* Node::getParent() const
  161. {
  162. return _parent;
  163. }
  164. unsigned int Node::getChildCount() const
  165. {
  166. return _childCount;
  167. }
  168. Node* Node::findNode(const char* id, bool recursive, bool exactMatch) const
  169. {
  170. assert(id);
  171. // Search immediate children first.
  172. for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
  173. {
  174. // Does this child's ID match?
  175. if ((exactMatch && child->_id == id) || (!exactMatch && child->_id.find(id) == 0))
  176. {
  177. return child;
  178. }
  179. }
  180. // Recurse.
  181. if (recursive)
  182. {
  183. for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
  184. {
  185. Node* match = child->findNode(id, true, exactMatch);
  186. if (match)
  187. {
  188. return match;
  189. }
  190. }
  191. }
  192. return NULL;
  193. }
  194. unsigned int Node::findNodes(const char* id, std::vector<Node*>& nodes, bool recursive, bool exactMatch) const
  195. {
  196. assert(id);
  197. unsigned int count = 0;
  198. // Search immediate children first.
  199. for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
  200. {
  201. // Does this child's ID match?
  202. if ((exactMatch && child->_id == id) || (!exactMatch && child->_id.find(id) == 0))
  203. {
  204. nodes.push_back(child);
  205. ++count;
  206. }
  207. }
  208. // Recurse.
  209. if (recursive)
  210. {
  211. for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
  212. {
  213. count += child->findNodes(id, nodes, true, exactMatch);
  214. }
  215. }
  216. return count;
  217. }
  218. Scene* Node::getScene() const
  219. {
  220. // Search for a scene in our parents.
  221. for (Node* n = const_cast<Node*>(this); n != NULL; n = n->getParent())
  222. {
  223. if (n->_scene)
  224. {
  225. return n->_scene;
  226. }
  227. }
  228. return NULL;
  229. }
  230. Node* Node::getRootNode() const
  231. {
  232. Node* n = const_cast<Node*>(this);
  233. while (n->getParent())
  234. {
  235. n = n->getParent();
  236. }
  237. return n;
  238. }
  239. const Matrix& Node::getWorldMatrix() const
  240. {
  241. if (_dirtyBits & NODE_DIRTY_WORLD)
  242. {
  243. // Clear our dirty flag immediately to prevent this block from being entered if our
  244. // parent calls our getWorldMatrix() method as a result of the following calculations.
  245. _dirtyBits &= ~NODE_DIRTY_WORLD;
  246. // If we have a parent, multiply our parent world transform by our local
  247. // transform to obtain our final resolved world transform.
  248. Node* parent = getParent();
  249. if (parent && (!_collisionObject || _collisionObject->isKinematic()))
  250. {
  251. Matrix::multiply(parent->getWorldMatrix(), getMatrix(), &_world);
  252. }
  253. else
  254. {
  255. _world = getMatrix();
  256. }
  257. // Our world matrix was just updated, so call getWorldMatrix() on all child nodes
  258. // to force their resolved world matrices to be updated.
  259. Node* node = getFirstChild();
  260. while (node)
  261. {
  262. node->getWorldMatrix();
  263. node = node->getNextSibling();
  264. }
  265. }
  266. return _world;
  267. }
  268. const Matrix& Node::getWorldViewMatrix() const
  269. {
  270. static Matrix worldView;
  271. Matrix::multiply(getViewMatrix(), getWorldMatrix(), &worldView);
  272. return worldView;
  273. }
  274. const Matrix& Node::getInverseTransposeWorldViewMatrix() const
  275. {
  276. static Matrix invTransWorldView;
  277. Matrix::multiply(getViewMatrix(), getWorldMatrix(), &invTransWorldView);
  278. invTransWorldView.invert();
  279. invTransWorldView.transpose();
  280. return invTransWorldView;
  281. }
  282. const Matrix& Node::getInverseTransposeWorldMatrix() const
  283. {
  284. static Matrix invTransWorld;
  285. invTransWorld = getWorldMatrix();
  286. invTransWorld.invert();
  287. invTransWorld.transpose();
  288. return invTransWorld;
  289. }
  290. const Matrix& Node::getViewMatrix() const
  291. {
  292. Scene* scene = getScene();
  293. Camera* camera = scene ? scene->getActiveCamera() : NULL;
  294. if (camera)
  295. {
  296. return camera->getViewMatrix();
  297. }
  298. else
  299. {
  300. return Matrix::identity();
  301. }
  302. }
  303. const Matrix& Node::getInverseViewMatrix() const
  304. {
  305. Scene* scene = getScene();
  306. Camera* camera = scene ? scene->getActiveCamera() : NULL;
  307. if (camera)
  308. {
  309. return camera->getInverseViewMatrix();
  310. }
  311. else
  312. {
  313. return Matrix::identity();
  314. }
  315. }
  316. const Matrix& Node::getProjectionMatrix() const
  317. {
  318. Scene* scene = getScene();
  319. Camera* camera = scene ? scene->getActiveCamera() : NULL;
  320. if (camera)
  321. {
  322. return camera->getProjectionMatrix();
  323. }
  324. else
  325. {
  326. return Matrix::identity();
  327. }
  328. }
  329. const Matrix& Node::getViewProjectionMatrix() const
  330. {
  331. Scene* scene = getScene();
  332. Camera* camera = scene ? scene->getActiveCamera() : NULL;
  333. if (camera)
  334. {
  335. return camera->getViewProjectionMatrix();
  336. }
  337. else
  338. {
  339. return Matrix::identity();
  340. }
  341. }
  342. const Matrix& Node::getInverseViewProjectionMatrix() const
  343. {
  344. Scene* scene = getScene();
  345. Camera* camera = scene ? scene->getActiveCamera() : NULL;
  346. if (camera)
  347. {
  348. return camera->getInverseViewProjectionMatrix();
  349. }
  350. return Matrix::identity();
  351. }
  352. const Matrix& Node::getWorldViewProjectionMatrix() const
  353. {
  354. static Matrix worldViewProj;
  355. // Always re-calculate worldViewProjection matrix since it's extremely difficult
  356. // to track whether the camera has changed (it may frequently change every frame).
  357. Matrix::multiply(getViewProjectionMatrix(), getWorldMatrix(), &worldViewProj);
  358. return worldViewProj;
  359. }
  360. Vector3 Node::getTranslationWorld() const
  361. {
  362. Vector3 translation;
  363. getWorldMatrix().getTranslation(&translation);
  364. return translation;
  365. }
  366. Vector3 Node::getTranslationView() const
  367. {
  368. Vector3 translation;
  369. getWorldMatrix().getTranslation(&translation);
  370. getViewMatrix().transformPoint(&translation);
  371. return translation;
  372. }
  373. Vector3 Node::getForwardVectorWorld() const
  374. {
  375. Vector3 vector;
  376. getWorldMatrix().getForwardVector(&vector);
  377. return vector;
  378. }
  379. Vector3 Node::getForwardVectorView() const
  380. {
  381. Vector3 vector;
  382. getWorldMatrix().getForwardVector(&vector);
  383. getViewMatrix().transformVector(&vector);
  384. //getForwardVector(&vector);
  385. //getWorldViewMatrix().transformVector(&vector);
  386. return vector;
  387. }
  388. Vector3 Node::getActiveCameraTranslationWorld() const
  389. {
  390. Scene* scene = getScene();
  391. if (scene)
  392. {
  393. Camera* camera = scene->getActiveCamera();
  394. if (camera)
  395. {
  396. Node* cameraNode = camera->getNode();
  397. if (cameraNode)
  398. {
  399. return cameraNode->getTranslationWorld();
  400. }
  401. }
  402. }
  403. return Vector3::zero();
  404. }
  405. Vector3 Node::getActiveCameraTranslationView() const
  406. {
  407. Scene* scene = getScene();
  408. if (scene)
  409. {
  410. Camera* camera = scene->getActiveCamera();
  411. if (camera)
  412. {
  413. Node* cameraNode = camera->getNode();
  414. if (cameraNode)
  415. {
  416. return cameraNode->getTranslationView();
  417. }
  418. }
  419. }
  420. return Vector3::zero();
  421. }
  422. void Node::hierarchyChanged()
  423. {
  424. // When our hierarchy changes our world transform is affected, so we must dirty it.
  425. transformChanged();
  426. }
  427. void Node::transformChanged()
  428. {
  429. // Our local transform was changed, so mark our world matrices dirty.
  430. _dirtyBits |= NODE_DIRTY_WORLD | NODE_DIRTY_BOUNDS;
  431. // Notify our children that their transform has also changed (since transforms are inherited).
  432. Joint* rootJoint = NULL;
  433. Node* n = getFirstChild();
  434. while (n)
  435. {
  436. n->transformChanged();
  437. n = n->getNextSibling();
  438. }
  439. Transform::transformChanged();
  440. }
  441. void Node::setBoundsDirty()
  442. {
  443. // Mark ourself and our parent nodes as dirty
  444. _dirtyBits |= NODE_DIRTY_BOUNDS;
  445. // Mark our parent bounds as dirty as well
  446. if (_parent)
  447. _parent->setBoundsDirty();
  448. }
  449. Camera* Node::getCamera() const
  450. {
  451. return _camera;
  452. }
  453. void Node::setCamera(Camera* camera)
  454. {
  455. if (_camera != camera)
  456. {
  457. if (_camera)
  458. {
  459. _camera->setNode(NULL);
  460. SAFE_RELEASE(_camera);
  461. }
  462. _camera = camera;
  463. if (_camera)
  464. {
  465. _camera->addRef();
  466. _camera->setNode(this);
  467. }
  468. }
  469. }
  470. Light* Node::getLight() const
  471. {
  472. return _light;
  473. }
  474. void Node::setLight(Light* light)
  475. {
  476. if (_light != light)
  477. {
  478. if (_light)
  479. {
  480. _light->setNode(NULL);
  481. SAFE_RELEASE(_light);
  482. }
  483. _light = light;
  484. if (_light)
  485. {
  486. _light->addRef();
  487. _light->setNode(this);
  488. }
  489. }
  490. }
  491. void Node::setModel(Model* model)
  492. {
  493. if (_model != model)
  494. {
  495. if (_model)
  496. {
  497. _model->setNode(NULL);
  498. SAFE_RELEASE(_model);
  499. }
  500. _model = model;
  501. if (_model)
  502. {
  503. _model->addRef();
  504. _model->setNode(this);
  505. }
  506. }
  507. }
  508. Model* Node::getModel() const
  509. {
  510. return _model;
  511. }
  512. void Node::setForm(Form* form)
  513. {
  514. if (_form != form)
  515. {
  516. if (_form)
  517. {
  518. _form->setNode(NULL);
  519. SAFE_RELEASE(_form);
  520. }
  521. _form = form;
  522. if (_form)
  523. {
  524. _form->addRef();
  525. _form->setNode(this);
  526. }
  527. }
  528. }
  529. Form* Node::getForm() const
  530. {
  531. return _form;
  532. }
  533. const BoundingSphere& Node::getBoundingSphere() const
  534. {
  535. if (_dirtyBits & NODE_DIRTY_BOUNDS)
  536. {
  537. _dirtyBits &= ~NODE_DIRTY_BOUNDS;
  538. const Matrix& worldMatrix = getWorldMatrix();
  539. // Start with our local bounding sphere
  540. // TODO: Incorporate bounds from entities other than mesh (i.e. emitters, audiosource, etc)
  541. bool empty = true;
  542. if (_model && _model->getMesh())
  543. {
  544. _bounds.set(_model->getMesh()->getBoundingSphere());
  545. empty = false;
  546. }
  547. else
  548. {
  549. // Empty bounding sphere, set the world translation with zero radius
  550. worldMatrix.getTranslation(&_bounds.center);
  551. _bounds.radius = 0;
  552. }
  553. // Transform the sphere (if not empty) into world space.
  554. if (!empty)
  555. {
  556. bool applyWorldTransform = true;
  557. if (_model && _model->getSkin())
  558. {
  559. // Special case: If the root joint of our mesh skin is parented by any nodes,
  560. // multiply the world matrix of the root joint's parent by this node's
  561. // world matrix. This computes a final world matrix used for transforming this
  562. // node's bounding volume. This allows us to store a much smaller bounding
  563. // volume approximation than would otherwise be possible for skinned meshes,
  564. // since joint parent nodes that are not in the matrix pallette do not need to
  565. // be considered as directly transforming vertices on the GPU (they can instead
  566. // be applied directly to the bounding volume transformation below).
  567. Node* jointParent = _model->getSkin()->getRootJoint()->getParent();
  568. if (jointParent)
  569. {
  570. // TODO: Should we protect against the case where joints are nested directly
  571. // in the node hierachy of the model (this is normally not the case)?
  572. Matrix boundsMatrix;
  573. Matrix::multiply(getWorldMatrix(), jointParent->getWorldMatrix(), &boundsMatrix);
  574. _bounds.transform(boundsMatrix);
  575. applyWorldTransform = false;
  576. }
  577. }
  578. if (applyWorldTransform)
  579. {
  580. _bounds.transform(getWorldMatrix());
  581. }
  582. }
  583. // Merge this world-space bounding sphere with our childrens' bounding volumes.
  584. for (Node* n = getFirstChild(); n != NULL; n = n->getNextSibling())
  585. {
  586. const BoundingSphere& childSphere = n->getBoundingSphere();
  587. if (!childSphere.isEmpty())
  588. {
  589. if (empty)
  590. {
  591. _bounds.set(childSphere);
  592. empty = false;
  593. }
  594. else
  595. {
  596. _bounds.merge(childSphere);
  597. }
  598. }
  599. }
  600. }
  601. return _bounds;
  602. }
  603. Node* Node::clone() const
  604. {
  605. NodeCloneContext context;
  606. return cloneRecursive(context);
  607. }
  608. Node* Node::cloneSingleNode(NodeCloneContext &context) const
  609. {
  610. Node* copy = Node::create(getId());
  611. context.registerClonedNode(this, copy);
  612. cloneInto(copy, context);
  613. return copy;
  614. }
  615. Node* Node::cloneRecursive(NodeCloneContext &context) const
  616. {
  617. Node* copy = cloneSingleNode(context);
  618. for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
  619. {
  620. Node* childCopy = child->cloneRecursive(context);
  621. copy->addChild(childCopy); // TODO: Does child order matter?
  622. childCopy->release();
  623. }
  624. return copy;
  625. }
  626. void Node::cloneInto(Node* node, NodeCloneContext &context) const
  627. {
  628. Transform::cloneInto(node, context);
  629. // TODO: Clone the rest of the node data.
  630. //node->setCamera(getCamera());
  631. //node->setLight(getLight());
  632. if (Camera* camera = getCamera())
  633. {
  634. Camera* cameraClone = camera->clone(context);
  635. node->setCamera(cameraClone);
  636. cameraClone->release();
  637. }
  638. if (Light* light = getLight())
  639. {
  640. Light* lightClone = light->clone(context);
  641. node->setLight(lightClone);
  642. lightClone->release();
  643. }
  644. if (AudioSource* audio = getAudioSource())
  645. {
  646. AudioSource* audioClone = audio->clone(context);
  647. node->setAudioSource(audioClone);
  648. audioClone->release();
  649. }
  650. if (Model* model = getModel())
  651. {
  652. Model* modelClone = model->clone(context);
  653. node->setModel(modelClone);
  654. modelClone->release();
  655. }
  656. node->_world = _world;
  657. node->_bounds = _bounds;
  658. }
  659. AudioSource* Node::getAudioSource() const
  660. {
  661. return _audioSource;
  662. }
  663. void Node::setAudioSource(AudioSource* audio)
  664. {
  665. if (_audioSource != audio)
  666. {
  667. if (_audioSource)
  668. {
  669. _audioSource->setNode(NULL);
  670. SAFE_RELEASE(_audioSource);
  671. }
  672. _audioSource = audio;
  673. if (_audioSource)
  674. {
  675. _audioSource->addRef();
  676. _audioSource->setNode(this);
  677. }
  678. }
  679. }
  680. ParticleEmitter* Node::getParticleEmitter() const
  681. {
  682. return _particleEmitter;
  683. }
  684. void Node::setParticleEmitter(ParticleEmitter* emitter)
  685. {
  686. if (_particleEmitter != emitter)
  687. {
  688. if (_particleEmitter)
  689. {
  690. _particleEmitter->setNode(NULL);
  691. SAFE_RELEASE(_particleEmitter);
  692. }
  693. _particleEmitter = emitter;
  694. if (_particleEmitter)
  695. {
  696. _particleEmitter->addRef();
  697. _particleEmitter->setNode(this);
  698. }
  699. }
  700. }
  701. PhysicsCollisionObject* Node::getCollisionObject() const
  702. {
  703. return _collisionObject;
  704. }
  705. PhysicsCollisionObject* Node::setCollisionObject(PhysicsCollisionObject::Type type, const PhysicsCollisionShape::Definition& shape, PhysicsRigidBody::Parameters* rigidBodyParameters)
  706. {
  707. SAFE_DELETE(_collisionObject);
  708. switch (type)
  709. {
  710. case PhysicsCollisionObject::RIGID_BODY:
  711. {
  712. _collisionObject = new PhysicsRigidBody(this, shape, rigidBodyParameters ? *rigidBodyParameters : PhysicsRigidBody::Parameters());
  713. }
  714. break;
  715. case PhysicsCollisionObject::GHOST_OBJECT:
  716. {
  717. _collisionObject = new PhysicsGhostObject(this, shape);
  718. }
  719. break;
  720. case PhysicsCollisionObject::CHARACTER:
  721. {
  722. _collisionObject = new PhysicsCharacter(this, shape);
  723. }
  724. break;
  725. }
  726. return _collisionObject;
  727. }
  728. PhysicsCollisionObject* Node::setCollisionObject(const char* filePath)
  729. {
  730. // Load the collision object properties from file.
  731. Properties* properties = Properties::create(filePath);
  732. assert(properties);
  733. if (properties == NULL)
  734. {
  735. WARN_VARG("Failed to load collision object file: %s", filePath);
  736. return NULL;
  737. }
  738. PhysicsCollisionObject* collisionObject = setCollisionObject(properties->getNextNamespace());
  739. SAFE_DELETE(properties);
  740. return collisionObject;
  741. }
  742. PhysicsCollisionObject* Node::setCollisionObject(Properties* properties)
  743. {
  744. SAFE_DELETE(_collisionObject);
  745. // Check if the properties is valid.
  746. if (!properties ||
  747. !(strcmp(properties->getNamespace(), "character") == 0 ||
  748. strcmp(properties->getNamespace(), "ghost") == 0 ||
  749. strcmp(properties->getNamespace(), "rigidbody") == 0))
  750. {
  751. WARN("Failed to load collision object from properties object: must be non-null object and have namespace equal to \'character\', \'ghost\', or \'rigidbody\'.");
  752. return NULL;
  753. }
  754. if (strcmp(properties->getNamespace(), "character") == 0)
  755. {
  756. _collisionObject = PhysicsCharacter::create(this, properties);
  757. }
  758. else if (strcmp(properties->getNamespace(), "ghost") == 0)
  759. {
  760. _collisionObject = PhysicsGhostObject::create(this, properties);
  761. }
  762. else if (strcmp(properties->getNamespace(), "rigidbody") == 0)
  763. {
  764. _collisionObject = PhysicsRigidBody::create(this, properties);
  765. }
  766. return _collisionObject;
  767. }
  768. NodeCloneContext::NodeCloneContext()
  769. {
  770. }
  771. NodeCloneContext::~NodeCloneContext()
  772. {
  773. }
  774. Animation* NodeCloneContext::findClonedAnimation(const Animation* animation)
  775. {
  776. AnimationMap::iterator it = _clonedAnimations.find(animation);
  777. return it != _clonedAnimations.end() ? it->second : NULL;
  778. }
  779. void NodeCloneContext::registerClonedAnimation(const Animation* original, Animation* clone)
  780. {
  781. _clonedAnimations[original] = clone;
  782. }
  783. Node* NodeCloneContext::findClonedNode(const Node* node)
  784. {
  785. NodeMap::iterator it = _clonedNodes.find(node);
  786. return it != _clonedNodes.end() ? it->second : NULL;
  787. }
  788. void NodeCloneContext::registerClonedNode(const Node* original, Node* clone)
  789. {
  790. _clonedNodes[original] = clone;
  791. }
  792. }