godot_collision_object_3d.cpp 7.7 KB

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