godot_ray_world_algorithm.cpp 5.0 KB

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