godot_collision_object_2d.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /**************************************************************************/
  2. /* godot_collision_object_2d.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_collision_object_2d.h"
  31. #include "godot_physics_server_2d.h"
  32. #include "godot_space_2d.h"
  33. void GodotCollisionObject2D::add_shape(GodotShape2D *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. GodotPhysicsServer2D::godot_singleton->pending_shape_update_list.add(&pending_shape_update_list);
  46. }
  47. }
  48. void GodotCollisionObject2D::set_shape(int p_index, GodotShape2D *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. GodotPhysicsServer2D::godot_singleton->pending_shape_update_list.add(&pending_shape_update_list);
  55. }
  56. }
  57. void GodotCollisionObject2D::set_shape_transform(int p_index, const Transform2D &p_transform) {
  58. ERR_FAIL_INDEX(p_index, shapes.size());
  59. shapes.write[p_index].xform = p_transform;
  60. shapes.write[p_index].xform_inv = p_transform.affine_inverse();
  61. if (!pending_shape_update_list.in_list()) {
  62. GodotPhysicsServer2D::godot_singleton->pending_shape_update_list.add(&pending_shape_update_list);
  63. }
  64. }
  65. void GodotCollisionObject2D::set_shape_disabled(int p_idx, bool p_disabled) {
  66. ERR_FAIL_INDEX(p_idx, shapes.size());
  67. GodotCollisionObject2D::Shape &shape = shapes.write[p_idx];
  68. if (shape.disabled == p_disabled) {
  69. return;
  70. }
  71. shape.disabled = p_disabled;
  72. if (!space) {
  73. return;
  74. }
  75. if (p_disabled && shape.bpid != 0) {
  76. space->get_broadphase()->remove(shape.bpid);
  77. shape.bpid = 0;
  78. if (!pending_shape_update_list.in_list()) {
  79. GodotPhysicsServer2D::godot_singleton->pending_shape_update_list.add(&pending_shape_update_list);
  80. }
  81. } else if (!p_disabled && shape.bpid == 0) {
  82. if (!pending_shape_update_list.in_list()) {
  83. GodotPhysicsServer2D::godot_singleton->pending_shape_update_list.add(&pending_shape_update_list);
  84. }
  85. }
  86. }
  87. void GodotCollisionObject2D::remove_shape(GodotShape2D *p_shape) {
  88. //remove a shape, all the times it appears
  89. for (int i = 0; i < shapes.size(); i++) {
  90. if (shapes[i].shape == p_shape) {
  91. remove_shape(i);
  92. i--;
  93. }
  94. }
  95. }
  96. void GodotCollisionObject2D::remove_shape(int p_index) {
  97. //remove anything from shape to be erased to end, so subindices don't change
  98. ERR_FAIL_INDEX(p_index, shapes.size());
  99. for (int i = p_index; i < shapes.size(); i++) {
  100. if (shapes[i].bpid == 0) {
  101. continue;
  102. }
  103. //should never get here with a null owner
  104. space->get_broadphase()->remove(shapes[i].bpid);
  105. shapes.write[i].bpid = 0;
  106. }
  107. shapes[p_index].shape->remove_owner(this);
  108. shapes.remove_at(p_index);
  109. if (!pending_shape_update_list.in_list()) {
  110. GodotPhysicsServer2D::godot_singleton->pending_shape_update_list.add(&pending_shape_update_list);
  111. }
  112. // _update_shapes();
  113. // _shapes_changed();
  114. }
  115. void GodotCollisionObject2D::_set_static(bool p_static) {
  116. if (_static == p_static) {
  117. return;
  118. }
  119. _static = p_static;
  120. if (!space) {
  121. return;
  122. }
  123. for (int i = 0; i < get_shape_count(); i++) {
  124. const Shape &s = shapes[i];
  125. if (s.bpid > 0) {
  126. space->get_broadphase()->set_static(s.bpid, _static);
  127. }
  128. }
  129. }
  130. void GodotCollisionObject2D::_unregister_shapes() {
  131. for (int i = 0; i < shapes.size(); i++) {
  132. Shape &s = shapes.write[i];
  133. if (s.bpid > 0) {
  134. space->get_broadphase()->remove(s.bpid);
  135. s.bpid = 0;
  136. }
  137. }
  138. }
  139. void GodotCollisionObject2D::_update_shapes() {
  140. if (!space) {
  141. return;
  142. }
  143. for (int i = 0; i < shapes.size(); i++) {
  144. Shape &s = shapes.write[i];
  145. if (s.disabled) {
  146. continue;
  147. }
  148. //not quite correct, should compute the next matrix..
  149. Rect2 shape_aabb = s.shape->get_aabb();
  150. Transform2D xform = transform * s.xform;
  151. shape_aabb = xform.xform(shape_aabb);
  152. shape_aabb.grow_by((s.aabb_cache.size.x + s.aabb_cache.size.y) * 0.5 * 0.05);
  153. s.aabb_cache = shape_aabb;
  154. if (s.bpid == 0) {
  155. s.bpid = space->get_broadphase()->create(this, i, shape_aabb, _static);
  156. space->get_broadphase()->set_static(s.bpid, _static);
  157. }
  158. space->get_broadphase()->move(s.bpid, shape_aabb);
  159. }
  160. }
  161. void GodotCollisionObject2D::_update_shapes_with_motion(const Vector2 &p_motion) {
  162. if (!space) {
  163. return;
  164. }
  165. for (int i = 0; i < shapes.size(); i++) {
  166. Shape &s = shapes.write[i];
  167. if (s.disabled) {
  168. continue;
  169. }
  170. //not quite correct, should compute the next matrix..
  171. Rect2 shape_aabb = s.shape->get_aabb();
  172. Transform2D xform = transform * s.xform;
  173. shape_aabb = xform.xform(shape_aabb);
  174. shape_aabb = shape_aabb.merge(Rect2(shape_aabb.position + p_motion, shape_aabb.size)); //use motion
  175. s.aabb_cache = shape_aabb;
  176. if (s.bpid == 0) {
  177. s.bpid = space->get_broadphase()->create(this, i, shape_aabb, _static);
  178. space->get_broadphase()->set_static(s.bpid, _static);
  179. }
  180. space->get_broadphase()->move(s.bpid, shape_aabb);
  181. }
  182. }
  183. void GodotCollisionObject2D::_set_space(GodotSpace2D *p_space) {
  184. GodotSpace2D *old_space = space;
  185. space = p_space;
  186. if (old_space) {
  187. old_space->remove_object(this);
  188. for (int i = 0; i < shapes.size(); i++) {
  189. Shape &s = shapes.write[i];
  190. if (s.bpid) {
  191. old_space->get_broadphase()->remove(s.bpid);
  192. s.bpid = 0;
  193. }
  194. }
  195. }
  196. if (space) {
  197. space->add_object(this);
  198. _update_shapes();
  199. }
  200. }
  201. void GodotCollisionObject2D::_shape_changed() {
  202. _update_shapes();
  203. _shapes_changed();
  204. }
  205. GodotCollisionObject2D::GodotCollisionObject2D(Type p_type) :
  206. pending_shape_update_list(this) {
  207. type = p_type;
  208. }