controller.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "controller.h"
  24. #include "math_utils.h"
  25. #include "physics_resource.h"
  26. #include "scene_graph.h"
  27. #include "vector3.h"
  28. #include "physics_callback.h"
  29. #include "PxCapsuleController.h"
  30. #include "PxPhysicsAPI.h"
  31. using physx::PxCapsuleClimbingMode;
  32. using physx::PxCapsuleController;
  33. using physx::PxCapsuleControllerDesc;
  34. using physx::PxCCTNonWalkableMode;
  35. using physx::PxControllerFilters;
  36. using physx::PxControllerFlag;
  37. using physx::PxExtendedVec3;
  38. using physx::PxVec3;
  39. namespace crown
  40. {
  41. Controller::Controller(const PhysicsResource* pr, SceneGraph& sg, int32_t node, PxPhysics* physics, PxControllerManager* manager)
  42. : m_resource(pr)
  43. , m_scene_graph(sg)
  44. , m_node(node)
  45. , m_manager(manager)
  46. , m_controller(NULL)
  47. {
  48. const PhysicsController* contr = physics_resource::controller(pr);
  49. const Vector3 pos = sg.world_position(m_node);
  50. PxCapsuleControllerDesc desc;
  51. desc.climbingMode = PxCapsuleClimbingMode::eCONSTRAINED;
  52. desc.nonWalkableMode = PxCCTNonWalkableMode::eFORCE_SLIDING;
  53. desc.radius = contr->radius;
  54. desc.height = contr->height;
  55. desc.slopeLimit = cos(contr->slope_limit);
  56. desc.stepOffset = contr->step_offset;
  57. desc.contactOffset = contr->contact_offset;
  58. desc.upDirection = PxVec3(0.0, 1.0, 0.0);
  59. desc.material = physics->createMaterial(0.5f, 0.5f, 0.5f);
  60. desc.position = PxExtendedVec3(pos.x, pos.y, pos.z);
  61. desc.reportCallback = &m_callback;
  62. CE_ASSERT(desc.isValid(), "Capsule is not valid");
  63. m_controller = manager->createController(desc);
  64. CE_ASSERT(m_controller, "Failed to create controller");
  65. }
  66. Controller::~Controller()
  67. {
  68. m_controller->release();
  69. }
  70. void Controller::move(const Vector3& pos)
  71. {
  72. const PxVec3 disp(pos.x, pos.y, pos.z);
  73. m_flags = m_controller->move(disp, 0.001, 1.0 / 60.0, PxControllerFilters());
  74. }
  75. void Controller::set_height(float height)
  76. {
  77. m_controller->resize(height);
  78. }
  79. Vector3 Controller::position() const
  80. {
  81. PxExtendedVec3 pos = m_controller->getPosition();
  82. return Vector3(pos.x, pos.y, pos.z);
  83. }
  84. bool Controller::collides_up() const
  85. {
  86. return (m_flags & PxControllerFlag::eCOLLISION_UP) != 0;
  87. }
  88. bool Controller::collides_down() const
  89. {
  90. return (m_flags & PxControllerFlag::eCOLLISION_DOWN) != 0;
  91. }
  92. bool Controller::collides_sides() const
  93. {
  94. return (m_flags & PxControllerFlag::eCOLLISION_SIDES) != 0;
  95. }
  96. void Controller::update()
  97. {
  98. m_scene_graph.set_world_position(m_node, position());
  99. }
  100. } // namespace crown