godot_area_pair_2d.cpp 7.0 KB

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