area_pair_3d_sw.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*************************************************************************/
  2. /* area_pair_3d_sw.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 "area_pair_3d_sw.h"
  31. #include "collision_solver_3d_sw.h"
  32. bool AreaPair3DSW::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) && CollisionSolver3DSW::solve_static(body->get_shape(body_shape), body->get_transform() * body->get_shape_transform(body_shape), area->get_shape(area_shape), area->get_transform() * area->get_shape_transform(area_shape), nullptr, this)) {
  37. result = true;
  38. }
  39. if (result != colliding) {
  40. if (result) {
  41. if (area->get_space_override_mode() != PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED) {
  42. body->add_area(area);
  43. }
  44. if (area->has_monitor_callback()) {
  45. area->add_body_to_query(body, body_shape, area_shape);
  46. }
  47. } else {
  48. if (area->get_space_override_mode() != PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED) {
  49. body->remove_area(area);
  50. }
  51. if (area->has_monitor_callback()) {
  52. area->remove_body_from_query(body, body_shape, area_shape);
  53. }
  54. }
  55. colliding = result;
  56. }
  57. return false; //never do any post solving
  58. }
  59. void AreaPair3DSW::solve(real_t p_step) {
  60. }
  61. AreaPair3DSW::AreaPair3DSW(Body3DSW *p_body, int p_body_shape, Area3DSW *p_area, int p_area_shape) {
  62. body = p_body;
  63. area = p_area;
  64. body_shape = p_body_shape;
  65. area_shape = p_area_shape;
  66. colliding = false;
  67. body->add_constraint(this, 0);
  68. area->add_constraint(this);
  69. if (p_body->get_mode() == PhysicsServer3D::BODY_MODE_KINEMATIC) {
  70. p_body->set_active(true);
  71. }
  72. }
  73. AreaPair3DSW::~AreaPair3DSW() {
  74. if (colliding) {
  75. if (area->get_space_override_mode() != PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED) {
  76. body->remove_area(area);
  77. }
  78. if (area->has_monitor_callback()) {
  79. area->remove_body_from_query(body, body_shape, area_shape);
  80. }
  81. }
  82. body->remove_constraint(this);
  83. area->remove_constraint(this);
  84. }
  85. ////////////////////////////////////////////////////
  86. bool Area2Pair3DSW::setup(real_t p_step) {
  87. bool result = false;
  88. if (area_a->is_shape_set_as_disabled(shape_a) || area_b->is_shape_set_as_disabled(shape_b)) {
  89. result = false;
  90. } else if (area_a->test_collision_mask(area_b) && CollisionSolver3DSW::solve_static(area_a->get_shape(shape_a), area_a->get_transform() * area_a->get_shape_transform(shape_a), area_b->get_shape(shape_b), area_b->get_transform() * area_b->get_shape_transform(shape_b), nullptr, this)) {
  91. result = true;
  92. }
  93. if (result != colliding) {
  94. if (result) {
  95. if (area_b->has_area_monitor_callback() && area_a->is_monitorable()) {
  96. area_b->add_area_to_query(area_a, shape_a, shape_b);
  97. }
  98. if (area_a->has_area_monitor_callback() && area_b->is_monitorable()) {
  99. area_a->add_area_to_query(area_b, shape_b, shape_a);
  100. }
  101. } else {
  102. if (area_b->has_area_monitor_callback() && area_a->is_monitorable()) {
  103. area_b->remove_area_from_query(area_a, shape_a, shape_b);
  104. }
  105. if (area_a->has_area_monitor_callback() && area_b->is_monitorable()) {
  106. area_a->remove_area_from_query(area_b, shape_b, shape_a);
  107. }
  108. }
  109. colliding = result;
  110. }
  111. return false; //never do any post solving
  112. }
  113. void Area2Pair3DSW::solve(real_t p_step) {
  114. }
  115. Area2Pair3DSW::Area2Pair3DSW(Area3DSW *p_area_a, int p_shape_a, Area3DSW *p_area_b, int p_shape_b) {
  116. area_a = p_area_a;
  117. area_b = p_area_b;
  118. shape_a = p_shape_a;
  119. shape_b = p_shape_b;
  120. colliding = false;
  121. area_a->add_constraint(this);
  122. area_b->add_constraint(this);
  123. }
  124. Area2Pair3DSW::~Area2Pair3DSW() {
  125. if (colliding) {
  126. if (area_b->has_area_monitor_callback()) {
  127. area_b->remove_area_from_query(area_a, shape_a, shape_b);
  128. }
  129. if (area_a->has_area_monitor_callback()) {
  130. area_a->remove_area_from_query(area_b, shape_b, shape_a);
  131. }
  132. }
  133. area_a->remove_constraint(this);
  134. area_b->remove_constraint(this);
  135. }