PhysicsBody.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright (C) 2009-2020, 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/physics/PhysicsBody.h>
  6. #include <anki/physics/PhysicsWorld.h>
  7. #include <anki/physics/PhysicsCollisionShape.h>
  8. namespace anki
  9. {
  10. PhysicsBody::PhysicsBody(PhysicsWorld* world, const PhysicsBodyInitInfo& init)
  11. : PhysicsFilteredObject(CLASS_TYPE, world)
  12. {
  13. const Bool dynamic = init.m_mass > 0.0f;
  14. m_shape = init.m_shape;
  15. m_mass = init.m_mass;
  16. // Create motion state
  17. m_motionState.m_body = this;
  18. // Compute inertia
  19. btCollisionShape* shape = m_shape->getBtShape(dynamic);
  20. btVector3 localInertia(0, 0, 0);
  21. if(dynamic)
  22. {
  23. shape->calculateLocalInertia(init.m_mass, localInertia);
  24. }
  25. // Create body
  26. btRigidBody::btRigidBodyConstructionInfo cInfo(init.m_mass, &m_motionState, shape, localInertia);
  27. cInfo.m_friction = init.m_friction;
  28. m_body.init(cInfo);
  29. // User pointer
  30. m_body->setUserPointer(static_cast<PhysicsObject*>(this));
  31. // Other
  32. setMaterialGroup((dynamic) ? PhysicsMaterialBit::DYNAMIC_GEOMETRY : PhysicsMaterialBit::STATIC_GEOMETRY);
  33. PhysicsMaterialBit collidesWith = PhysicsMaterialBit::ALL;
  34. if(!dynamic)
  35. {
  36. collidesWith &= ~PhysicsMaterialBit::STATIC_GEOMETRY;
  37. }
  38. setMaterialMask(collidesWith);
  39. setTransform(init.m_transform);
  40. // Add to world
  41. auto lock = getWorld().lockBtWorld();
  42. getWorld().getBtWorld()->addRigidBody(m_body.get());
  43. }
  44. PhysicsBody::~PhysicsBody()
  45. {
  46. auto lock = getWorld().lockBtWorld();
  47. getWorld().getBtWorld()->removeRigidBody(m_body.get());
  48. }
  49. void PhysicsBody::setMass(F32 mass)
  50. {
  51. ANKI_ASSERT(m_mass > 0.0f && "Only relevant for dynamic bodies");
  52. ANKI_ASSERT(mass > 0.0f);
  53. btVector3 inertia;
  54. m_shape->getBtShape(true)->calculateLocalInertia(mass, inertia);
  55. m_body->setMassProps(mass, inertia);
  56. m_mass = mass;
  57. }
  58. } // end namespace anki