godot_ray_world_algorithm.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. // Epsilon to account for floating point inaccuracies
  35. #define RAY_PENETRATION_DEPTH_EPSILON 0.01
  36. GodotRayWorldAlgorithm::CreateFunc::CreateFunc(const btDiscreteDynamicsWorld *world) :
  37. m_world(world) {}
  38. GodotRayWorldAlgorithm::SwappedCreateFunc::SwappedCreateFunc(const btDiscreteDynamicsWorld *world) :
  39. m_world(world) {}
  40. GodotRayWorldAlgorithm::GodotRayWorldAlgorithm(const btDiscreteDynamicsWorld *world, btPersistentManifold *mf, const btCollisionAlgorithmConstructionInfo &ci, const btCollisionObjectWrapper *body0Wrap, const btCollisionObjectWrapper *body1Wrap, bool isSwapped) :
  41. btActivatingCollisionAlgorithm(ci, body0Wrap, body1Wrap),
  42. m_world(world),
  43. m_manifoldPtr(mf),
  44. m_isSwapped(isSwapped) {}
  45. GodotRayWorldAlgorithm::~GodotRayWorldAlgorithm() {
  46. if (m_ownManifold && m_manifoldPtr) {
  47. m_dispatcher->releaseManifold(m_manifoldPtr);
  48. }
  49. }
  50. void GodotRayWorldAlgorithm::processCollision(const btCollisionObjectWrapper *body0Wrap, const btCollisionObjectWrapper *body1Wrap, const btDispatcherInfo &dispatchInfo, btManifoldResult *resultOut) {
  51. if (!m_manifoldPtr) {
  52. if (m_isSwapped) {
  53. m_manifoldPtr = m_dispatcher->getNewManifold(body1Wrap->getCollisionObject(), body0Wrap->getCollisionObject());
  54. } else {
  55. m_manifoldPtr = m_dispatcher->getNewManifold(body0Wrap->getCollisionObject(), body1Wrap->getCollisionObject());
  56. }
  57. m_ownManifold = true;
  58. }
  59. m_manifoldPtr->clearManifold();
  60. resultOut->setPersistentManifold(m_manifoldPtr);
  61. const btRayShape *ray_shape;
  62. btTransform ray_transform;
  63. const btCollisionObjectWrapper *other_co_wrapper;
  64. if (m_isSwapped) {
  65. ray_shape = static_cast<const btRayShape *>(body1Wrap->getCollisionShape());
  66. ray_transform = body1Wrap->getWorldTransform();
  67. other_co_wrapper = body0Wrap;
  68. } else {
  69. ray_shape = static_cast<const btRayShape *>(body0Wrap->getCollisionShape());
  70. ray_transform = body0Wrap->getWorldTransform();
  71. other_co_wrapper = body1Wrap;
  72. }
  73. btTransform to(ray_transform * ray_shape->getSupportPoint());
  74. btCollisionWorld::ClosestRayResultCallback btResult(ray_transform.getOrigin(), to.getOrigin());
  75. m_world->rayTestSingleInternal(ray_transform, to, other_co_wrapper, btResult);
  76. if (btResult.hasHit()) {
  77. btScalar depth(ray_shape->getScaledLength() * (btResult.m_closestHitFraction - 1));
  78. if (depth > -RAY_PENETRATION_DEPTH_EPSILON) {
  79. depth = 0.0;
  80. }
  81. if (ray_shape->getSlipsOnSlope()) {
  82. resultOut->addContactPoint(btResult.m_hitNormalWorld, btResult.m_hitPointWorld, depth);
  83. } else {
  84. resultOut->addContactPoint((ray_transform.getOrigin() - to.getOrigin()).normalize(), btResult.m_hitPointWorld, depth);
  85. }
  86. }
  87. }
  88. btScalar GodotRayWorldAlgorithm::calculateTimeOfImpact(btCollisionObject *body0, btCollisionObject *body1, const btDispatcherInfo &dispatchInfo, btManifoldResult *resultOut) {
  89. return 1;
  90. }