godot_ray_world_algorithm.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*************************************************************************/
  2. /* godot_ray_world_algorithm.cpp */
  3. /* Author: AndreaCatania */
  4. /*************************************************************************/
  5. /* This file is part of: */
  6. /* GODOT ENGINE */
  7. /* http://www.godotengine.org */
  8. /*************************************************************************/
  9. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  10. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  11. /* */
  12. /* Permission is hereby granted, free of charge, to any person obtaining */
  13. /* a copy of this software and associated documentation files (the */
  14. /* "Software"), to deal in the Software without restriction, including */
  15. /* without limitation the rights to use, copy, modify, merge, publish, */
  16. /* distribute, sublicense, and/or sell copies of the Software, and to */
  17. /* permit persons to whom the Software is furnished to do so, subject to */
  18. /* the following conditions: */
  19. /* */
  20. /* The above copyright notice and this permission notice shall be */
  21. /* included in all copies or substantial portions of the Software. */
  22. /* */
  23. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  24. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  25. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  26. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  27. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  28. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  29. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  30. /*************************************************************************/
  31. #include "godot_ray_world_algorithm.h"
  32. #include "BulletDynamics/Dynamics/btDiscreteDynamicsWorld.h"
  33. #include "btRayShape.h"
  34. #include "collision_object_bullet.h"
  35. GodotRayWorldAlgorithm::CreateFunc::CreateFunc(const btDiscreteDynamicsWorld *world)
  36. : m_world(world) {}
  37. GodotRayWorldAlgorithm::SwappedCreateFunc::SwappedCreateFunc(const btDiscreteDynamicsWorld *world)
  38. : m_world(world) {}
  39. GodotRayWorldAlgorithm::GodotRayWorldAlgorithm(const btDiscreteDynamicsWorld *world, btPersistentManifold *mf, const btCollisionAlgorithmConstructionInfo &ci, const btCollisionObjectWrapper *body0Wrap, const btCollisionObjectWrapper *body1Wrap, bool isSwapped)
  40. : btActivatingCollisionAlgorithm(ci, body0Wrap, body1Wrap),
  41. m_manifoldPtr(mf),
  42. m_ownManifold(false),
  43. m_world(world),
  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. btVector3 ray_normal(to.getOrigin() - ray_transform.getOrigin());
  78. ray_normal.normalize();
  79. ray_normal *= -1;
  80. resultOut->addContactPoint(ray_normal, btResult.m_hitPointWorld, ray_shape->getScaledLength() * (btResult.m_closestHitFraction - 1));
  81. }
  82. }
  83. btScalar GodotRayWorldAlgorithm::calculateTimeOfImpact(btCollisionObject *body0, btCollisionObject *body1, const btDispatcherInfo &dispatchInfo, btManifoldResult *resultOut) {
  84. return 1;
  85. }