godot_area_pair_2d.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*************************************************************************/
  2. /* godot_area_pair_2d.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_2d.h"
  31. #include "godot_collision_solver_2d.h"
  32. bool GodotAreaPair2D::setup(real_t p_step) {
  33. bool result = false;
  34. if (area->collides_with(body) && GodotCollisionSolver2D::solve(body->get_shape(body_shape), body->get_transform() * body->get_shape_transform(body_shape), Vector2(), area->get_shape(area_shape), area->get_transform() * area->get_shape_transform(area_shape), Vector2(), nullptr, this)) {
  35. result = true;
  36. }
  37. process_collision = false;
  38. if (result != colliding) {
  39. if (area->get_space_override_mode() != PhysicsServer2D::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 GodotAreaPair2D::pre_solve(real_t p_step) {
  49. if (!process_collision) {
  50. return false;
  51. }
  52. if (colliding) {
  53. if (area->get_space_override_mode() != PhysicsServer2D::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() != PhysicsServer2D::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 GodotAreaPair2D::solve(real_t p_step) {
  70. // Nothing to do.
  71. }
  72. GodotAreaPair2D::GodotAreaPair2D(GodotBody2D *p_body, int p_body_shape, GodotArea2D *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() == PhysicsServer2D::BODY_MODE_KINEMATIC) { //need to be active to process pair
  80. p_body->set_active(true);
  81. }
  82. }
  83. GodotAreaPair2D::~GodotAreaPair2D() {
  84. if (colliding) {
  85. if (area->get_space_override_mode() != PhysicsServer2D::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, 0);
  93. area->remove_constraint(this);
  94. }
  95. //////////////////////////////////
  96. bool GodotArea2Pair2D::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) && !GodotCollisionSolver2D::solve(area_a->get_shape(shape_a), area_a->get_transform() * area_a->get_shape_transform(shape_a), Vector2(), area_b->get_shape(shape_b), area_b->get_transform() * area_b->get_shape_transform(shape_b), Vector2(), 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 GodotArea2Pair2D::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 GodotArea2Pair2D::solve(real_t p_step) {
  140. // Nothing to do.
  141. }
  142. GodotArea2Pair2D::GodotArea2Pair2D(GodotArea2D *p_area_a, int p_shape_a, GodotArea2D *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. GodotArea2Pair2D::~GodotArea2Pair2D() {
  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. }