area_pair_2d_sw.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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->is_shape_set_as_disabled(area_shape) || body->is_shape_set_as_disabled(body_shape)) {
  35. result = false;
  36. } else 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(), NULL, this)) {
  37. result = true;
  38. }
  39. if (result != colliding) {
  40. if (result) {
  41. if (area->get_space_override_mode() != Physics2DServer::AREA_SPACE_OVERRIDE_DISABLED)
  42. body->add_area(area);
  43. if (area->has_monitor_callback())
  44. area->add_body_to_query(body, body_shape, area_shape);
  45. } else {
  46. if (area->get_space_override_mode() != Physics2DServer::AREA_SPACE_OVERRIDE_DISABLED)
  47. body->remove_area(area);
  48. if (area->has_monitor_callback())
  49. area->remove_body_from_query(body, body_shape, area_shape);
  50. }
  51. colliding = result;
  52. }
  53. return false; //never do any post solving
  54. }
  55. void AreaPair2DSW::solve(real_t p_step) {
  56. }
  57. AreaPair2DSW::AreaPair2DSW(Body2DSW *p_body, int p_body_shape, Area2DSW *p_area, int p_area_shape) {
  58. body = p_body;
  59. area = p_area;
  60. body_shape = p_body_shape;
  61. area_shape = p_area_shape;
  62. colliding = false;
  63. body->add_constraint(this, 0);
  64. area->add_constraint(this);
  65. if (p_body->get_mode() == Physics2DServer::BODY_MODE_KINEMATIC) //need to be active to process pair
  66. p_body->set_active(true);
  67. }
  68. AreaPair2DSW::~AreaPair2DSW() {
  69. if (colliding) {
  70. if (area->get_space_override_mode() != Physics2DServer::AREA_SPACE_OVERRIDE_DISABLED)
  71. body->remove_area(area);
  72. if (area->has_monitor_callback())
  73. area->remove_body_from_query(body, body_shape, area_shape);
  74. }
  75. body->remove_constraint(this);
  76. area->remove_constraint(this);
  77. }
  78. //////////////////////////////////
  79. bool Area2Pair2DSW::setup(real_t p_step) {
  80. bool result = false;
  81. if (area_a->is_shape_set_as_disabled(shape_a) || area_b->is_shape_set_as_disabled(shape_b)) {
  82. result = false;
  83. } else 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(), NULL, this)) {
  84. result = true;
  85. }
  86. if (result != colliding) {
  87. if (result) {
  88. if (area_b->has_area_monitor_callback() && area_a->is_monitorable())
  89. area_b->add_area_to_query(area_a, shape_a, shape_b);
  90. if (area_a->has_area_monitor_callback() && area_b->is_monitorable())
  91. area_a->add_area_to_query(area_b, shape_b, shape_a);
  92. } else {
  93. if (area_b->has_area_monitor_callback() && area_a->is_monitorable())
  94. area_b->remove_area_from_query(area_a, shape_a, shape_b);
  95. if (area_a->has_area_monitor_callback() && area_b->is_monitorable())
  96. area_a->remove_area_from_query(area_b, shape_b, shape_a);
  97. }
  98. colliding = result;
  99. }
  100. return false; //never do any post solving
  101. }
  102. void Area2Pair2DSW::solve(real_t p_step) {
  103. }
  104. Area2Pair2DSW::Area2Pair2DSW(Area2DSW *p_area_a, int p_shape_a, Area2DSW *p_area_b, int p_shape_b) {
  105. area_a = p_area_a;
  106. area_b = p_area_b;
  107. shape_a = p_shape_a;
  108. shape_b = p_shape_b;
  109. colliding = false;
  110. area_a->add_constraint(this);
  111. area_b->add_constraint(this);
  112. }
  113. Area2Pair2DSW::~Area2Pair2DSW() {
  114. if (colliding) {
  115. if (area_b->has_area_monitor_callback())
  116. area_b->remove_area_from_query(area_a, shape_a, shape_b);
  117. if (area_a->has_area_monitor_callback())
  118. area_a->remove_area_from_query(area_b, shape_b, shape_a);
  119. }
  120. area_a->remove_constraint(this);
  121. area_b->remove_constraint(this);
  122. }