godot_area_pair_2d.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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) 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_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. has_space_override = false;
  39. if (result != colliding) {
  40. if ((int)area->get_param(PhysicsServer2D::AREA_PARAM_GRAVITY_OVERRIDE_MODE) != PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED) {
  41. has_space_override = true;
  42. } else if ((int)area->get_param(PhysicsServer2D::AREA_PARAM_LINEAR_DAMP_OVERRIDE_MODE) != PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED) {
  43. has_space_override = true;
  44. } else if ((int)area->get_param(PhysicsServer2D::AREA_PARAM_ANGULAR_DAMP_OVERRIDE_MODE) != PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED) {
  45. has_space_override = true;
  46. }
  47. process_collision = has_space_override;
  48. if (area->has_monitor_callback()) {
  49. process_collision = true;
  50. }
  51. colliding = result;
  52. }
  53. return process_collision;
  54. }
  55. bool GodotAreaPair2D::pre_solve(real_t p_step) {
  56. if (!process_collision) {
  57. return false;
  58. }
  59. if (colliding) {
  60. if (has_space_override) {
  61. body->add_area(area);
  62. }
  63. if (area->has_monitor_callback()) {
  64. area->add_body_to_query(body, body_shape, area_shape);
  65. }
  66. } else {
  67. if (has_space_override) {
  68. body->remove_area(area);
  69. }
  70. if (area->has_monitor_callback()) {
  71. area->remove_body_from_query(body, body_shape, area_shape);
  72. }
  73. }
  74. return false; // Never do any post solving.
  75. }
  76. void GodotAreaPair2D::solve(real_t p_step) {
  77. // Nothing to do.
  78. }
  79. GodotAreaPair2D::GodotAreaPair2D(GodotBody2D *p_body, int p_body_shape, GodotArea2D *p_area, int p_area_shape) {
  80. body = p_body;
  81. area = p_area;
  82. body_shape = p_body_shape;
  83. area_shape = p_area_shape;
  84. body->add_constraint(this, 0);
  85. area->add_constraint(this);
  86. if (p_body->get_mode() == PhysicsServer2D::BODY_MODE_KINEMATIC) { //need to be active to process pair
  87. p_body->set_active(true);
  88. }
  89. }
  90. GodotAreaPair2D::~GodotAreaPair2D() {
  91. if (colliding) {
  92. if (has_space_override) {
  93. body->remove_area(area);
  94. }
  95. if (area->has_monitor_callback()) {
  96. area->remove_body_from_query(body, body_shape, area_shape);
  97. }
  98. }
  99. body->remove_constraint(this, 0);
  100. area->remove_constraint(this);
  101. }
  102. //////////////////////////////////
  103. bool GodotArea2Pair2D::setup(real_t p_step) {
  104. bool result_a = area_a->collides_with(area_b);
  105. bool result_b = area_b->collides_with(area_a);
  106. 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)) {
  107. result_a = false;
  108. result_b = false;
  109. }
  110. bool process_collision = false;
  111. process_collision_a = false;
  112. if (result_a != colliding_a) {
  113. if (area_a->has_area_monitor_callback() && area_b_monitorable) {
  114. process_collision_a = true;
  115. process_collision = true;
  116. }
  117. colliding_a = result_a;
  118. }
  119. process_collision_b = false;
  120. if (result_b != colliding_b) {
  121. if (area_b->has_area_monitor_callback() && area_a_monitorable) {
  122. process_collision_b = true;
  123. process_collision = true;
  124. }
  125. colliding_b = result_b;
  126. }
  127. return process_collision;
  128. }
  129. bool GodotArea2Pair2D::pre_solve(real_t p_step) {
  130. if (process_collision_a) {
  131. if (colliding_a) {
  132. area_a->add_area_to_query(area_b, shape_b, shape_a);
  133. } else {
  134. area_a->remove_area_from_query(area_b, shape_b, shape_a);
  135. }
  136. }
  137. if (process_collision_b) {
  138. if (colliding_b) {
  139. area_b->add_area_to_query(area_a, shape_a, shape_b);
  140. } else {
  141. area_b->remove_area_from_query(area_a, shape_a, shape_b);
  142. }
  143. }
  144. return false; // Never do any post solving.
  145. }
  146. void GodotArea2Pair2D::solve(real_t p_step) {
  147. // Nothing to do.
  148. }
  149. GodotArea2Pair2D::GodotArea2Pair2D(GodotArea2D *p_area_a, int p_shape_a, GodotArea2D *p_area_b, int p_shape_b) {
  150. area_a = p_area_a;
  151. area_b = p_area_b;
  152. shape_a = p_shape_a;
  153. shape_b = p_shape_b;
  154. area_a_monitorable = area_a->is_monitorable();
  155. area_b_monitorable = area_b->is_monitorable();
  156. area_a->add_constraint(this);
  157. area_b->add_constraint(this);
  158. }
  159. GodotArea2Pair2D::~GodotArea2Pair2D() {
  160. if (colliding_a) {
  161. if (area_a->has_area_monitor_callback() && area_b_monitorable) {
  162. area_a->remove_area_from_query(area_b, shape_b, shape_a);
  163. }
  164. }
  165. if (colliding_b) {
  166. if (area_b->has_area_monitor_callback() && area_a_monitorable) {
  167. area_b->remove_area_from_query(area_a, shape_a, shape_b);
  168. }
  169. }
  170. area_a->remove_constraint(this);
  171. area_b->remove_constraint(this);
  172. }