godot_area_pair_3d.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /**************************************************************************/
  2. /* godot_area_pair_3d.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_3d.h"
  31. #include "godot_collision_solver_3d.h"
  32. bool GodotAreaPair3D::setup(real_t p_step) {
  33. bool result = false;
  34. if (area->collides_with(body) && GodotCollisionSolver3D::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)) {
  35. result = true;
  36. }
  37. process_collision = false;
  38. has_space_override = false;
  39. if (result != colliding) {
  40. if ((int)area->get_param(PhysicsServer3D::AREA_PARAM_GRAVITY_OVERRIDE_MODE) != PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED) {
  41. has_space_override = true;
  42. } else if ((int)area->get_param(PhysicsServer3D::AREA_PARAM_LINEAR_DAMP_OVERRIDE_MODE) != PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED) {
  43. has_space_override = true;
  44. } else if ((int)area->get_param(PhysicsServer3D::AREA_PARAM_ANGULAR_DAMP_OVERRIDE_MODE) != PhysicsServer3D::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 GodotAreaPair3D::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 GodotAreaPair3D::solve(real_t p_step) {
  79. // Nothing to do.
  80. }
  81. GodotAreaPair3D::GodotAreaPair3D(GodotBody3D *p_body, int p_body_shape, GodotArea3D *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() == PhysicsServer3D::BODY_MODE_KINEMATIC) {
  89. p_body->set_active(true);
  90. }
  91. }
  92. GodotAreaPair3D::~GodotAreaPair3D() {
  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);
  103. area->remove_constraint(this);
  104. }
  105. ////////////////////////////////////////////////////
  106. bool GodotArea2Pair3D::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) && !GodotCollisionSolver3D::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)) {
  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 GodotArea2Pair3D::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 GodotArea2Pair3D::solve(real_t p_step) {
  150. // Nothing to do.
  151. }
  152. GodotArea2Pair3D::GodotArea2Pair3D(GodotArea3D *p_area_a, int p_shape_a, GodotArea3D *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. GodotArea2Pair3D::~GodotArea2Pair3D() {
  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. }
  176. ////////////////////////////////////////////////////
  177. bool GodotAreaSoftBodyPair3D::setup(real_t p_step) {
  178. bool result = false;
  179. if (
  180. area->collides_with(soft_body) &&
  181. GodotCollisionSolver3D::solve_static(
  182. soft_body->get_shape(soft_body_shape),
  183. soft_body->get_transform() * soft_body->get_shape_transform(soft_body_shape),
  184. area->get_shape(area_shape),
  185. area->get_transform() * area->get_shape_transform(area_shape),
  186. nullptr,
  187. this)) {
  188. result = true;
  189. }
  190. process_collision = false;
  191. has_space_override = false;
  192. if (result != colliding) {
  193. if ((int)area->get_param(PhysicsServer3D::AREA_PARAM_GRAVITY_OVERRIDE_MODE) != PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED) {
  194. has_space_override = true;
  195. } else if (area->get_wind_force_magnitude() > CMP_EPSILON) {
  196. has_space_override = true;
  197. }
  198. if (area->has_monitor_callback()) {
  199. process_collision = true;
  200. }
  201. colliding = result;
  202. }
  203. return process_collision;
  204. }
  205. bool GodotAreaSoftBodyPair3D::pre_solve(real_t p_step) {
  206. if (!process_collision) {
  207. return false;
  208. }
  209. if (colliding) {
  210. if (has_space_override) {
  211. body_has_attached_area = true;
  212. soft_body->add_area(area);
  213. }
  214. if (area->has_monitor_callback()) {
  215. area->add_soft_body_to_query(soft_body, soft_body_shape, area_shape);
  216. }
  217. } else {
  218. if (has_space_override) {
  219. body_has_attached_area = false;
  220. soft_body->remove_area(area);
  221. }
  222. if (area->has_monitor_callback()) {
  223. area->remove_soft_body_from_query(soft_body, soft_body_shape, area_shape);
  224. }
  225. }
  226. return false; // Never do any post solving.
  227. }
  228. void GodotAreaSoftBodyPair3D::solve(real_t p_step) {
  229. // Nothing to do.
  230. }
  231. GodotAreaSoftBodyPair3D::GodotAreaSoftBodyPair3D(GodotSoftBody3D *p_soft_body, int p_soft_body_shape, GodotArea3D *p_area, int p_area_shape) {
  232. soft_body = p_soft_body;
  233. area = p_area;
  234. soft_body_shape = p_soft_body_shape;
  235. area_shape = p_area_shape;
  236. soft_body->add_constraint(this);
  237. area->add_constraint(this);
  238. }
  239. GodotAreaSoftBodyPair3D::~GodotAreaSoftBodyPair3D() {
  240. if (colliding) {
  241. if (body_has_attached_area) {
  242. body_has_attached_area = false;
  243. soft_body->remove_area(area);
  244. }
  245. if (area->has_monitor_callback()) {
  246. area->remove_soft_body_from_query(soft_body, soft_body_shape, area_shape);
  247. }
  248. }
  249. soft_body->remove_constraint(this);
  250. area->remove_constraint(this);
  251. }