godot_ray_world_algorithm.cpp 5.0 KB

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