collision_object_2d_sw.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*************************************************************************/
  2. /* collision_object_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 "collision_object_2d_sw.h"
  31. #include "servers/physics_2d/physics_2d_server_sw.h"
  32. #include "space_2d_sw.h"
  33. void CollisionObject2DSW::add_shape(Shape2DSW *p_shape, const Transform2D &p_transform, bool p_disabled) {
  34. Shape s;
  35. s.shape = p_shape;
  36. s.xform = p_transform;
  37. s.xform_inv = s.xform.affine_inverse();
  38. s.bpid = 0; //needs update
  39. s.disabled = p_disabled;
  40. s.one_way_collision = false;
  41. s.one_way_collision_margin = 0;
  42. shapes.push_back(s);
  43. p_shape->add_owner(this);
  44. if (!pending_shape_update_list.in_list()) {
  45. Physics2DServerSW::singletonsw->pending_shape_update_list.add(&pending_shape_update_list);
  46. }
  47. }
  48. void CollisionObject2DSW::set_shape(int p_index, Shape2DSW *p_shape) {
  49. ERR_FAIL_INDEX(p_index, shapes.size());
  50. shapes[p_index].shape->remove_owner(this);
  51. shapes.write[p_index].shape = p_shape;
  52. p_shape->add_owner(this);
  53. if (!pending_shape_update_list.in_list()) {
  54. Physics2DServerSW::singletonsw->pending_shape_update_list.add(&pending_shape_update_list);
  55. }
  56. }
  57. void CollisionObject2DSW::set_shape_metadata(int p_index, const Variant &p_metadata) {
  58. ERR_FAIL_INDEX(p_index, shapes.size());
  59. shapes.write[p_index].metadata = p_metadata;
  60. }
  61. void CollisionObject2DSW::set_shape_transform(int p_index, const Transform2D &p_transform) {
  62. ERR_FAIL_INDEX(p_index, shapes.size());
  63. shapes.write[p_index].xform = p_transform;
  64. shapes.write[p_index].xform_inv = p_transform.affine_inverse();
  65. if (!pending_shape_update_list.in_list()) {
  66. Physics2DServerSW::singletonsw->pending_shape_update_list.add(&pending_shape_update_list);
  67. }
  68. }
  69. void CollisionObject2DSW::set_shape_disabled(int p_idx, bool p_disabled) {
  70. ERR_FAIL_INDEX(p_idx, shapes.size());
  71. CollisionObject2DSW::Shape &shape = shapes.write[p_idx];
  72. if (shape.disabled == p_disabled) {
  73. return;
  74. }
  75. shape.disabled = p_disabled;
  76. if (!space) {
  77. return;
  78. }
  79. if (p_disabled && shape.bpid != 0) {
  80. space->get_broadphase()->remove(shape.bpid);
  81. shape.bpid = 0;
  82. if (!pending_shape_update_list.in_list()) {
  83. Physics2DServerSW::singletonsw->pending_shape_update_list.add(&pending_shape_update_list);
  84. }
  85. } else if (!p_disabled && shape.bpid == 0) {
  86. if (!pending_shape_update_list.in_list()) {
  87. Physics2DServerSW::singletonsw->pending_shape_update_list.add(&pending_shape_update_list);
  88. }
  89. }
  90. }
  91. void CollisionObject2DSW::remove_shape(Shape2DSW *p_shape) {
  92. //remove a shape, all the times it appears
  93. for (int i = 0; i < shapes.size(); i++) {
  94. if (shapes[i].shape == p_shape) {
  95. remove_shape(i);
  96. i--;
  97. }
  98. }
  99. }
  100. void CollisionObject2DSW::remove_shape(int p_index) {
  101. //remove anything from shape to be erased to end, so subindices don't change
  102. ERR_FAIL_INDEX(p_index, shapes.size());
  103. for (int i = p_index; i < shapes.size(); i++) {
  104. if (shapes[i].bpid == 0) {
  105. continue;
  106. }
  107. //should never get here with a null owner
  108. space->get_broadphase()->remove(shapes[i].bpid);
  109. shapes.write[i].bpid = 0;
  110. }
  111. shapes[p_index].shape->remove_owner(this);
  112. shapes.remove(p_index);
  113. if (!pending_shape_update_list.in_list()) {
  114. Physics2DServerSW::singletonsw->pending_shape_update_list.add(&pending_shape_update_list);
  115. }
  116. }
  117. void CollisionObject2DSW::_set_static(bool p_static) {
  118. if (_static == p_static) {
  119. return;
  120. }
  121. _static = p_static;
  122. if (!space) {
  123. return;
  124. }
  125. for (int i = 0; i < get_shape_count(); i++) {
  126. const Shape &s = shapes[i];
  127. if (s.bpid > 0) {
  128. space->get_broadphase()->set_static(s.bpid, _static);
  129. }
  130. }
  131. }
  132. void CollisionObject2DSW::_unregister_shapes() {
  133. for (int i = 0; i < shapes.size(); i++) {
  134. Shape &s = shapes.write[i];
  135. if (s.bpid > 0) {
  136. space->get_broadphase()->remove(s.bpid);
  137. s.bpid = 0;
  138. }
  139. }
  140. }
  141. void CollisionObject2DSW::_update_shapes() {
  142. if (!space) {
  143. return;
  144. }
  145. for (int i = 0; i < shapes.size(); i++) {
  146. Shape &s = shapes.write[i];
  147. if (s.disabled) {
  148. continue;
  149. }
  150. //not quite correct, should compute the next matrix..
  151. Rect2 shape_aabb = s.shape->get_aabb();
  152. Transform2D xform = transform * s.xform;
  153. shape_aabb = xform.xform(shape_aabb);
  154. shape_aabb.grow_by((s.aabb_cache.size.x + s.aabb_cache.size.y) * 0.5 * 0.05);
  155. s.aabb_cache = shape_aabb;
  156. if (s.bpid == 0) {
  157. s.bpid = space->get_broadphase()->create(this, i, shape_aabb, _static);
  158. space->get_broadphase()->set_static(s.bpid, _static);
  159. }
  160. space->get_broadphase()->move(s.bpid, shape_aabb);
  161. }
  162. }
  163. void CollisionObject2DSW::_recheck_shapes() {
  164. if (!space) {
  165. return;
  166. }
  167. for (int i = 0; i < shapes.size(); i++) {
  168. Shape &s = shapes.write[i];
  169. if (s.disabled) {
  170. continue;
  171. }
  172. if (s.bpid != 0) {
  173. space->get_broadphase()->recheck_pairs(s.bpid);
  174. }
  175. }
  176. }
  177. void CollisionObject2DSW::_update_shapes_with_motion(const Vector2 &p_motion) {
  178. if (!space) {
  179. return;
  180. }
  181. for (int i = 0; i < shapes.size(); i++) {
  182. Shape &s = shapes.write[i];
  183. if (s.disabled) {
  184. continue;
  185. }
  186. //not quite correct, should compute the next matrix..
  187. Rect2 shape_aabb = s.shape->get_aabb();
  188. Transform2D xform = transform * s.xform;
  189. shape_aabb = xform.xform(shape_aabb);
  190. shape_aabb = shape_aabb.merge(Rect2(shape_aabb.position + p_motion, shape_aabb.size)); //use motion
  191. s.aabb_cache = shape_aabb;
  192. if (s.bpid == 0) {
  193. s.bpid = space->get_broadphase()->create(this, i, shape_aabb, _static);
  194. space->get_broadphase()->set_static(s.bpid, _static);
  195. }
  196. space->get_broadphase()->move(s.bpid, shape_aabb);
  197. }
  198. }
  199. void CollisionObject2DSW::_set_space(Space2DSW *p_space) {
  200. if (space) {
  201. space->remove_object(this);
  202. for (int i = 0; i < shapes.size(); i++) {
  203. Shape &s = shapes.write[i];
  204. if (s.bpid) {
  205. space->get_broadphase()->remove(s.bpid);
  206. s.bpid = 0;
  207. }
  208. }
  209. }
  210. space = p_space;
  211. if (space) {
  212. space->add_object(this);
  213. _update_shapes();
  214. }
  215. }
  216. void CollisionObject2DSW::_shape_changed() {
  217. _update_shapes();
  218. _shapes_changed();
  219. }
  220. CollisionObject2DSW::CollisionObject2DSW(Type p_type) :
  221. pending_shape_update_list(this) {
  222. _static = true;
  223. type = p_type;
  224. space = nullptr;
  225. instance_id = 0;
  226. canvas_instance_id = 0;
  227. collision_mask = 1;
  228. collision_layer = 1;
  229. pickable = true;
  230. }