CollisionResource.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright (C) 2009-2017, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <anki/resource/CollisionResource.h>
  6. #include <anki/resource/ResourceManager.h>
  7. #include <anki/resource/MeshLoader.h>
  8. #include <anki/physics/PhysicsWorld.h>
  9. #include <anki/misc/Xml.h>
  10. namespace anki
  11. {
  12. Error CollisionResource::load(const ResourceFilename& filename)
  13. {
  14. XmlElement el;
  15. XmlDocument doc;
  16. ANKI_CHECK(openFileParseXml(filename, doc));
  17. XmlElement collEl;
  18. ANKI_CHECK(doc.getChildElement("collisionShape", collEl));
  19. ANKI_CHECK(collEl.getChildElement("type", el));
  20. CString type;
  21. ANKI_CHECK(el.getText(type));
  22. XmlElement valEl;
  23. ANKI_CHECK(collEl.getChildElement("value", valEl));
  24. PhysicsWorld& physics = getManager().getPhysicsWorld();
  25. PhysicsCollisionShapeInitInfo csInit;
  26. if(type == "sphere")
  27. {
  28. F64 tmp;
  29. ANKI_CHECK(valEl.getNumber(tmp));
  30. m_physicsShape = physics.newInstance<PhysicsSphere>(csInit, tmp);
  31. }
  32. else if(type == "box")
  33. {
  34. Vec3 extend;
  35. ANKI_CHECK(valEl.getVec3(extend));
  36. m_physicsShape = physics.newInstance<PhysicsBox>(csInit, extend);
  37. }
  38. else if(type == "staticMesh")
  39. {
  40. CString meshfname;
  41. ANKI_CHECK(valEl.getText(meshfname));
  42. MeshLoader loader(&getManager());
  43. ANKI_CHECK(loader.load(meshfname));
  44. m_physicsShape = physics.newInstance<PhysicsTriangleSoup>(csInit,
  45. reinterpret_cast<const Vec3*>(loader.getVertexData()),
  46. loader.getVertexSize(),
  47. reinterpret_cast<const U16*>(loader.getIndexData()),
  48. loader.getHeader().m_totalIndicesCount);
  49. }
  50. else
  51. {
  52. ANKI_RESOURCE_LOGE("Incorrect collision type");
  53. return ErrorCode::USER_DATA;
  54. }
  55. return ErrorCode::NONE;
  56. }
  57. } // end namespace anki