collision_object_3d_sw.cpp 7.2 KB

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