godot_ray_world_algorithm.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**************************************************************************/
  2. /* godot_ray_world_algorithm.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "godot_ray_world_algorithm.h"
  31. #include "btRayShape.h"
  32. #include "collision_object_bullet.h"
  33. #include <BulletDynamics/Dynamics/btDiscreteDynamicsWorld.h>
  34. /**
  35. @author AndreaCatania
  36. */
  37. // Epsilon to account for floating point inaccuracies
  38. #define RAY_PENETRATION_DEPTH_EPSILON 0.01
  39. GodotRayWorldAlgorithm::CreateFunc::CreateFunc(const btDiscreteDynamicsWorld *world) :
  40. m_world(world) {}
  41. GodotRayWorldAlgorithm::SwappedCreateFunc::SwappedCreateFunc(const btDiscreteDynamicsWorld *world) :
  42. m_world(world) {}
  43. GodotRayWorldAlgorithm::GodotRayWorldAlgorithm(const btDiscreteDynamicsWorld *world, btPersistentManifold *mf, const btCollisionAlgorithmConstructionInfo &ci, const btCollisionObjectWrapper *body0Wrap, const btCollisionObjectWrapper *body1Wrap, bool isSwapped) :
  44. btActivatingCollisionAlgorithm(ci, body0Wrap, body1Wrap),
  45. m_world(world),
  46. m_manifoldPtr(mf),
  47. m_ownManifold(false),
  48. m_isSwapped(isSwapped) {}
  49. GodotRayWorldAlgorithm::~GodotRayWorldAlgorithm() {
  50. if (m_ownManifold && m_manifoldPtr) {
  51. m_dispatcher->releaseManifold(m_manifoldPtr);
  52. }
  53. }
  54. void GodotRayWorldAlgorithm::processCollision(const btCollisionObjectWrapper *body0Wrap, const btCollisionObjectWrapper *body1Wrap, const btDispatcherInfo &dispatchInfo, btManifoldResult *resultOut) {
  55. if (!m_manifoldPtr) {
  56. if (m_isSwapped) {
  57. m_manifoldPtr = m_dispatcher->getNewManifold(body1Wrap->getCollisionObject(), body0Wrap->getCollisionObject());
  58. } else {
  59. m_manifoldPtr = m_dispatcher->getNewManifold(body0Wrap->getCollisionObject(), body1Wrap->getCollisionObject());
  60. }
  61. m_ownManifold = true;
  62. }
  63. m_manifoldPtr->clearManifold();
  64. resultOut->setPersistentManifold(m_manifoldPtr);
  65. const btRayShape *ray_shape;
  66. btTransform ray_transform;
  67. const btCollisionObjectWrapper *other_co_wrapper;
  68. if (m_isSwapped) {
  69. ray_shape = static_cast<const btRayShape *>(body1Wrap->getCollisionShape());
  70. ray_transform = body1Wrap->getWorldTransform();
  71. other_co_wrapper = body0Wrap;
  72. } else {
  73. ray_shape = static_cast<const btRayShape *>(body0Wrap->getCollisionShape());
  74. ray_transform = body0Wrap->getWorldTransform();
  75. other_co_wrapper = body1Wrap;
  76. }
  77. btTransform to(ray_transform * ray_shape->getSupportPoint());
  78. btCollisionWorld::ClosestRayResultCallback btResult(ray_transform.getOrigin(), to.getOrigin());
  79. m_world->rayTestSingleInternal(ray_transform, to, other_co_wrapper, btResult);
  80. if (btResult.hasHit()) {
  81. btScalar depth(ray_shape->getScaledLength() * (btResult.m_closestHitFraction - 1));
  82. if (depth > -RAY_PENETRATION_DEPTH_EPSILON) {
  83. depth = 0.0;
  84. }
  85. if (ray_shape->getSlipsOnSlope()) {
  86. resultOut->addContactPoint(btResult.m_hitNormalWorld, btResult.m_hitPointWorld, depth);
  87. } else {
  88. resultOut->addContactPoint((ray_transform.getOrigin() - to.getOrigin()).normalize(), btResult.m_hitPointWorld, depth);
  89. }
  90. }
  91. }
  92. btScalar GodotRayWorldAlgorithm::calculateTimeOfImpact(btCollisionObject *body0, btCollisionObject *body1, const btDispatcherInfo &dispatchInfo, btManifoldResult *resultOut) {
  93. return 1;
  94. }