godot_collision_configuration.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**************************************************************************/
  2. /* godot_collision_configuration.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_collision_configuration.h"
  31. #include "godot_ray_world_algorithm.h"
  32. #include <BulletCollision/BroadphaseCollision/btBroadphaseProxy.h>
  33. #include <BulletDynamics/Dynamics/btDiscreteDynamicsWorld.h>
  34. /**
  35. @author AndreaCatania
  36. */
  37. GodotCollisionConfiguration::GodotCollisionConfiguration(const btDiscreteDynamicsWorld *world, const btDefaultCollisionConstructionInfo &constructionInfo) :
  38. btDefaultCollisionConfiguration(constructionInfo) {
  39. void *mem = nullptr;
  40. mem = btAlignedAlloc(sizeof(GodotRayWorldAlgorithm::CreateFunc), 16);
  41. m_rayWorldCF = new (mem) GodotRayWorldAlgorithm::CreateFunc(world);
  42. mem = btAlignedAlloc(sizeof(GodotRayWorldAlgorithm::SwappedCreateFunc), 16);
  43. m_swappedRayWorldCF = new (mem) GodotRayWorldAlgorithm::SwappedCreateFunc(world);
  44. }
  45. GodotCollisionConfiguration::~GodotCollisionConfiguration() {
  46. m_rayWorldCF->~btCollisionAlgorithmCreateFunc();
  47. btAlignedFree(m_rayWorldCF);
  48. m_swappedRayWorldCF->~btCollisionAlgorithmCreateFunc();
  49. btAlignedFree(m_swappedRayWorldCF);
  50. }
  51. btCollisionAlgorithmCreateFunc *GodotCollisionConfiguration::getCollisionAlgorithmCreateFunc(int proxyType0, int proxyType1) {
  52. if (CUSTOM_CONVEX_SHAPE_TYPE == proxyType0 && CUSTOM_CONVEX_SHAPE_TYPE == proxyType1) {
  53. // This collision is not supported
  54. return m_emptyCreateFunc;
  55. } else if (CUSTOM_CONVEX_SHAPE_TYPE == proxyType0) {
  56. return m_rayWorldCF;
  57. } else if (CUSTOM_CONVEX_SHAPE_TYPE == proxyType1) {
  58. return m_swappedRayWorldCF;
  59. } else {
  60. return btDefaultCollisionConfiguration::getCollisionAlgorithmCreateFunc(proxyType0, proxyType1);
  61. }
  62. }
  63. btCollisionAlgorithmCreateFunc *GodotCollisionConfiguration::getClosestPointsAlgorithmCreateFunc(int proxyType0, int proxyType1) {
  64. if (CUSTOM_CONVEX_SHAPE_TYPE == proxyType0 && CUSTOM_CONVEX_SHAPE_TYPE == proxyType1) {
  65. // This collision is not supported
  66. return m_emptyCreateFunc;
  67. } else if (CUSTOM_CONVEX_SHAPE_TYPE == proxyType0) {
  68. return m_rayWorldCF;
  69. } else if (CUSTOM_CONVEX_SHAPE_TYPE == proxyType1) {
  70. return m_swappedRayWorldCF;
  71. } else {
  72. return btDefaultCollisionConfiguration::getClosestPointsAlgorithmCreateFunc(proxyType0, proxyType1);
  73. }
  74. }
  75. GodotSoftCollisionConfiguration::GodotSoftCollisionConfiguration(const btDiscreteDynamicsWorld *world, const btDefaultCollisionConstructionInfo &constructionInfo) :
  76. btSoftBodyRigidBodyCollisionConfiguration(constructionInfo) {
  77. void *mem = nullptr;
  78. mem = btAlignedAlloc(sizeof(GodotRayWorldAlgorithm::CreateFunc), 16);
  79. m_rayWorldCF = new (mem) GodotRayWorldAlgorithm::CreateFunc(world);
  80. mem = btAlignedAlloc(sizeof(GodotRayWorldAlgorithm::SwappedCreateFunc), 16);
  81. m_swappedRayWorldCF = new (mem) GodotRayWorldAlgorithm::SwappedCreateFunc(world);
  82. }
  83. GodotSoftCollisionConfiguration::~GodotSoftCollisionConfiguration() {
  84. m_rayWorldCF->~btCollisionAlgorithmCreateFunc();
  85. btAlignedFree(m_rayWorldCF);
  86. m_swappedRayWorldCF->~btCollisionAlgorithmCreateFunc();
  87. btAlignedFree(m_swappedRayWorldCF);
  88. }
  89. btCollisionAlgorithmCreateFunc *GodotSoftCollisionConfiguration::getCollisionAlgorithmCreateFunc(int proxyType0, int proxyType1) {
  90. if (CUSTOM_CONVEX_SHAPE_TYPE == proxyType0 && CUSTOM_CONVEX_SHAPE_TYPE == proxyType1) {
  91. // This collision is not supported
  92. return m_emptyCreateFunc;
  93. } else if (CUSTOM_CONVEX_SHAPE_TYPE == proxyType0) {
  94. return m_rayWorldCF;
  95. } else if (CUSTOM_CONVEX_SHAPE_TYPE == proxyType1) {
  96. return m_swappedRayWorldCF;
  97. } else {
  98. return btSoftBodyRigidBodyCollisionConfiguration::getCollisionAlgorithmCreateFunc(proxyType0, proxyType1);
  99. }
  100. }
  101. btCollisionAlgorithmCreateFunc *GodotSoftCollisionConfiguration::getClosestPointsAlgorithmCreateFunc(int proxyType0, int proxyType1) {
  102. if (CUSTOM_CONVEX_SHAPE_TYPE == proxyType0 && CUSTOM_CONVEX_SHAPE_TYPE == proxyType1) {
  103. // This collision is not supported
  104. return m_emptyCreateFunc;
  105. } else if (CUSTOM_CONVEX_SHAPE_TYPE == proxyType0) {
  106. return m_rayWorldCF;
  107. } else if (CUSTOM_CONVEX_SHAPE_TYPE == proxyType1) {
  108. return m_swappedRayWorldCF;
  109. } else {
  110. return btSoftBodyRigidBodyCollisionConfiguration::getClosestPointsAlgorithmCreateFunc(proxyType0, proxyType1);
  111. }
  112. }