area_pair_2d_sw.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*************************************************************************/
  2. /* area_pair_2d_sw.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "area_pair_2d_sw.h"
  31. #include "collision_solver_2d_sw.h"
  32. bool AreaPair2DSW::setup(real_t p_step) {
  33. bool result = false;
  34. if (area->test_collision_mask(body) && CollisionSolver2DSW::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. if (result != colliding) {
  38. if (result) {
  39. if (area->get_space_override_mode() != Physics2DServer::AREA_SPACE_OVERRIDE_DISABLED) {
  40. body->add_area(area);
  41. }
  42. if (area->has_monitor_callback()) {
  43. area->add_body_to_query(body, body_shape, area_shape);
  44. }
  45. } else {
  46. if (area->get_space_override_mode() != Physics2DServer::AREA_SPACE_OVERRIDE_DISABLED) {
  47. body->remove_area(area);
  48. }
  49. if (area->has_monitor_callback()) {
  50. area->remove_body_from_query(body, body_shape, area_shape);
  51. }
  52. }
  53. colliding = result;
  54. }
  55. return false; //never do any post solving
  56. }
  57. void AreaPair2DSW::solve(real_t p_step) {
  58. }
  59. AreaPair2DSW::AreaPair2DSW(Body2DSW *p_body, int p_body_shape, Area2DSW *p_area, int p_area_shape) {
  60. body = p_body;
  61. area = p_area;
  62. body_shape = p_body_shape;
  63. area_shape = p_area_shape;
  64. colliding = false;
  65. body->add_constraint(this, 0);
  66. area->add_constraint(this);
  67. if (p_body->get_mode() == Physics2DServer::BODY_MODE_KINEMATIC) { //need to be active to process pair
  68. p_body->set_active(true);
  69. }
  70. }
  71. AreaPair2DSW::~AreaPair2DSW() {
  72. if (colliding) {
  73. if (area->get_space_override_mode() != Physics2DServer::AREA_SPACE_OVERRIDE_DISABLED) {
  74. body->remove_area(area);
  75. }
  76. if (area->has_monitor_callback()) {
  77. area->remove_body_from_query(body, body_shape, area_shape);
  78. }
  79. }
  80. body->remove_constraint(this);
  81. area->remove_constraint(this);
  82. }
  83. //////////////////////////////////
  84. bool Area2Pair2DSW::setup(real_t p_step) {
  85. bool result = false;
  86. if (area_a->test_collision_mask(area_b) && CollisionSolver2DSW::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)) {
  87. result = true;
  88. }
  89. if (result != colliding) {
  90. if (result) {
  91. if (area_b->has_area_monitor_callback() && area_a->is_monitorable()) {
  92. area_b->add_area_to_query(area_a, shape_a, shape_b);
  93. }
  94. if (area_a->has_area_monitor_callback() && area_b->is_monitorable()) {
  95. area_a->add_area_to_query(area_b, shape_b, shape_a);
  96. }
  97. } else {
  98. if (area_b->has_area_monitor_callback() && area_a->is_monitorable()) {
  99. area_b->remove_area_from_query(area_a, shape_a, shape_b);
  100. }
  101. if (area_a->has_area_monitor_callback() && area_b->is_monitorable()) {
  102. area_a->remove_area_from_query(area_b, shape_b, shape_a);
  103. }
  104. }
  105. colliding = result;
  106. }
  107. return false; //never do any post solving
  108. }
  109. void Area2Pair2DSW::solve(real_t p_step) {
  110. }
  111. Area2Pair2DSW::Area2Pair2DSW(Area2DSW *p_area_a, int p_shape_a, Area2DSW *p_area_b, int p_shape_b) {
  112. area_a = p_area_a;
  113. area_b = p_area_b;
  114. shape_a = p_shape_a;
  115. shape_b = p_shape_b;
  116. colliding = false;
  117. area_a->add_constraint(this);
  118. area_b->add_constraint(this);
  119. }
  120. Area2Pair2DSW::~Area2Pair2DSW() {
  121. if (colliding) {
  122. if (area_b->has_area_monitor_callback()) {
  123. area_b->remove_area_from_query(area_a, shape_a, shape_b);
  124. }
  125. if (area_a->has_area_monitor_callback()) {
  126. area_a->remove_area_from_query(area_b, shape_b, shape_a);
  127. }
  128. }
  129. area_a->remove_constraint(this);
  130. area_b->remove_constraint(this);
  131. }