godot_area_pair_3d.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*************************************************************************/
  2. /* godot_area_pair_3d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_area_pair_3d.h"
  31. #include "godot_collision_solver_3d.h"
  32. bool GodotAreaPair3D::setup(real_t p_step) {
  33. bool result = false;
  34. if (area->collides_with(body) && GodotCollisionSolver3D::solve_static(body->get_shape(body_shape), body->get_transform() * body->get_shape_transform(body_shape), area->get_shape(area_shape), area->get_transform() * area->get_shape_transform(area_shape), nullptr, this)) {
  35. result = true;
  36. }
  37. process_collision = false;
  38. if (result != colliding) {
  39. if (area->get_space_override_mode() != PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED) {
  40. process_collision = true;
  41. } else if (area->has_monitor_callback()) {
  42. process_collision = true;
  43. }
  44. colliding = result;
  45. }
  46. return process_collision;
  47. }
  48. bool GodotAreaPair3D::pre_solve(real_t p_step) {
  49. if (!process_collision) {
  50. return false;
  51. }
  52. if (colliding) {
  53. if (area->get_space_override_mode() != PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED) {
  54. body->add_area(area);
  55. }
  56. if (area->has_monitor_callback()) {
  57. area->add_body_to_query(body, body_shape, area_shape);
  58. }
  59. } else {
  60. if (area->get_space_override_mode() != PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED) {
  61. body->remove_area(area);
  62. }
  63. if (area->has_monitor_callback()) {
  64. area->remove_body_from_query(body, body_shape, area_shape);
  65. }
  66. }
  67. return false; // Never do any post solving.
  68. }
  69. void GodotAreaPair3D::solve(real_t p_step) {
  70. // Nothing to do.
  71. }
  72. GodotAreaPair3D::GodotAreaPair3D(GodotBody3D *p_body, int p_body_shape, GodotArea3D *p_area, int p_area_shape) {
  73. body = p_body;
  74. area = p_area;
  75. body_shape = p_body_shape;
  76. area_shape = p_area_shape;
  77. body->add_constraint(this, 0);
  78. area->add_constraint(this);
  79. if (p_body->get_mode() == PhysicsServer3D::BODY_MODE_KINEMATIC) {
  80. p_body->set_active(true);
  81. }
  82. }
  83. GodotAreaPair3D::~GodotAreaPair3D() {
  84. if (colliding) {
  85. if (area->get_space_override_mode() != PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED) {
  86. body->remove_area(area);
  87. }
  88. if (area->has_monitor_callback()) {
  89. area->remove_body_from_query(body, body_shape, area_shape);
  90. }
  91. }
  92. body->remove_constraint(this);
  93. area->remove_constraint(this);
  94. }
  95. ////////////////////////////////////////////////////
  96. bool GodotArea2Pair3D::setup(real_t p_step) {
  97. bool result_a = area_a->collides_with(area_b);
  98. bool result_b = area_b->collides_with(area_a);
  99. if ((result_a || result_b) && !GodotCollisionSolver3D::solve_static(area_a->get_shape(shape_a), area_a->get_transform() * area_a->get_shape_transform(shape_a), area_b->get_shape(shape_b), area_b->get_transform() * area_b->get_shape_transform(shape_b), nullptr, this)) {
  100. result_a = false;
  101. result_b = false;
  102. }
  103. bool process_collision = false;
  104. process_collision_a = false;
  105. if (result_a != colliding_a) {
  106. if (area_a->has_area_monitor_callback() && area_b->is_monitorable()) {
  107. process_collision_a = true;
  108. process_collision = true;
  109. }
  110. colliding_a = result_a;
  111. }
  112. process_collision_b = false;
  113. if (result_b != colliding_b) {
  114. if (area_b->has_area_monitor_callback() && area_a->is_monitorable()) {
  115. process_collision_b = true;
  116. process_collision = true;
  117. }
  118. colliding_b = result_b;
  119. }
  120. return process_collision;
  121. }
  122. bool GodotArea2Pair3D::pre_solve(real_t p_step) {
  123. if (process_collision_a) {
  124. if (colliding_a) {
  125. area_a->add_area_to_query(area_b, shape_b, shape_a);
  126. } else {
  127. area_a->remove_area_from_query(area_b, shape_b, shape_a);
  128. }
  129. }
  130. if (process_collision_b) {
  131. if (colliding_b) {
  132. area_b->add_area_to_query(area_a, shape_a, shape_b);
  133. } else {
  134. area_b->remove_area_from_query(area_a, shape_a, shape_b);
  135. }
  136. }
  137. return false; // Never do any post solving.
  138. }
  139. void GodotArea2Pair3D::solve(real_t p_step) {
  140. // Nothing to do.
  141. }
  142. GodotArea2Pair3D::GodotArea2Pair3D(GodotArea3D *p_area_a, int p_shape_a, GodotArea3D *p_area_b, int p_shape_b) {
  143. area_a = p_area_a;
  144. area_b = p_area_b;
  145. shape_a = p_shape_a;
  146. shape_b = p_shape_b;
  147. area_a->add_constraint(this);
  148. area_b->add_constraint(this);
  149. }
  150. GodotArea2Pair3D::~GodotArea2Pair3D() {
  151. if (colliding_a) {
  152. if (area_a->has_area_monitor_callback()) {
  153. area_a->remove_area_from_query(area_b, shape_b, shape_a);
  154. }
  155. }
  156. if (colliding_b) {
  157. if (area_b->has_area_monitor_callback()) {
  158. area_b->remove_area_from_query(area_a, shape_a, shape_b);
  159. }
  160. }
  161. area_a->remove_constraint(this);
  162. area_b->remove_constraint(this);
  163. }
  164. ////////////////////////////////////////////////////
  165. bool GodotAreaSoftBodyPair3D::setup(real_t p_step) {
  166. bool result = false;
  167. if (
  168. area->collides_with(soft_body) &&
  169. GodotCollisionSolver3D::solve_static(
  170. soft_body->get_shape(soft_body_shape),
  171. soft_body->get_transform() * soft_body->get_shape_transform(soft_body_shape),
  172. area->get_shape(area_shape),
  173. area->get_transform() * area->get_shape_transform(area_shape),
  174. nullptr,
  175. this)) {
  176. result = true;
  177. }
  178. process_collision = false;
  179. if (result != colliding) {
  180. if (area->get_space_override_mode() != PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED) {
  181. process_collision = true;
  182. } else if (area->has_monitor_callback()) {
  183. process_collision = true;
  184. }
  185. colliding = result;
  186. }
  187. return process_collision;
  188. }
  189. bool GodotAreaSoftBodyPair3D::pre_solve(real_t p_step) {
  190. if (!process_collision) {
  191. return false;
  192. }
  193. if (colliding) {
  194. if (area->get_space_override_mode() != PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED) {
  195. soft_body->add_area(area);
  196. }
  197. if (area->has_monitor_callback()) {
  198. area->add_soft_body_to_query(soft_body, soft_body_shape, area_shape);
  199. }
  200. } else {
  201. if (area->get_space_override_mode() != PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED) {
  202. soft_body->remove_area(area);
  203. }
  204. if (area->has_monitor_callback()) {
  205. area->remove_soft_body_from_query(soft_body, soft_body_shape, area_shape);
  206. }
  207. }
  208. return false; // Never do any post solving.
  209. }
  210. void GodotAreaSoftBodyPair3D::solve(real_t p_step) {
  211. // Nothing to do.
  212. }
  213. GodotAreaSoftBodyPair3D::GodotAreaSoftBodyPair3D(GodotSoftBody3D *p_soft_body, int p_soft_body_shape, GodotArea3D *p_area, int p_area_shape) {
  214. soft_body = p_soft_body;
  215. area = p_area;
  216. soft_body_shape = p_soft_body_shape;
  217. area_shape = p_area_shape;
  218. soft_body->add_constraint(this);
  219. area->add_constraint(this);
  220. }
  221. GodotAreaSoftBodyPair3D::~GodotAreaSoftBodyPair3D() {
  222. if (colliding) {
  223. if (area->get_space_override_mode() != PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED) {
  224. soft_body->remove_area(area);
  225. }
  226. if (area->has_monitor_callback()) {
  227. area->remove_soft_body_from_query(soft_body, soft_body_shape, area_shape);
  228. }
  229. }
  230. soft_body->remove_constraint(this);
  231. area->remove_constraint(this);
  232. }