collision_object_sw.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*************************************************************************/
  2. /* collision_object_sw.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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_sw.h"
  31. #include "servers/physics/physics_server_sw.h"
  32. #include "space_sw.h"
  33. void CollisionObjectSW::add_shape(ShapeSW *p_shape, const Transform &p_transform) {
  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. shapes.push_back(s);
  40. p_shape->add_owner(this);
  41. if (!pending_shape_update_list.in_list()) {
  42. PhysicsServerSW::singleton->pending_shape_update_list.add(&pending_shape_update_list);
  43. }
  44. //_update_shapes();
  45. //_shapes_changed();
  46. }
  47. void CollisionObjectSW::set_shape(int p_index, ShapeSW *p_shape) {
  48. ERR_FAIL_INDEX(p_index, shapes.size());
  49. shapes[p_index].shape->remove_owner(this);
  50. shapes.write[p_index].shape = p_shape;
  51. p_shape->add_owner(this);
  52. if (!pending_shape_update_list.in_list()) {
  53. PhysicsServerSW::singleton->pending_shape_update_list.add(&pending_shape_update_list);
  54. }
  55. //_update_shapes();
  56. //_shapes_changed();
  57. }
  58. void CollisionObjectSW::set_shape_transform(int p_index, const Transform &p_transform) {
  59. ERR_FAIL_INDEX(p_index, shapes.size());
  60. shapes.write[p_index].xform = p_transform;
  61. shapes.write[p_index].xform_inv = p_transform.affine_inverse();
  62. if (!pending_shape_update_list.in_list()) {
  63. PhysicsServerSW::singleton->pending_shape_update_list.add(&pending_shape_update_list);
  64. }
  65. //_update_shapes();
  66. //_shapes_changed();
  67. }
  68. void CollisionObjectSW::remove_shape(ShapeSW *p_shape) {
  69. //remove a shape, all the times it appears
  70. for (int i = 0; i < shapes.size(); i++) {
  71. if (shapes[i].shape == p_shape) {
  72. remove_shape(i);
  73. i--;
  74. }
  75. }
  76. }
  77. void CollisionObjectSW::remove_shape(int p_index) {
  78. //remove anything from shape to be erased to end, so subindices don't change
  79. ERR_FAIL_INDEX(p_index, shapes.size());
  80. for (int i = p_index; i < shapes.size(); i++) {
  81. if (shapes[i].bpid == 0)
  82. continue;
  83. //should never get here with a null owner
  84. space->get_broadphase()->remove(shapes[i].bpid);
  85. shapes.write[i].bpid = 0;
  86. }
  87. shapes[p_index].shape->remove_owner(this);
  88. shapes.remove(p_index);
  89. if (!pending_shape_update_list.in_list()) {
  90. PhysicsServerSW::singleton->pending_shape_update_list.add(&pending_shape_update_list);
  91. }
  92. //_update_shapes();
  93. //_shapes_changed();
  94. }
  95. void CollisionObjectSW::_set_static(bool p_static) {
  96. if (_static == p_static)
  97. return;
  98. _static = p_static;
  99. if (!space)
  100. return;
  101. for (int i = 0; i < get_shape_count(); i++) {
  102. const Shape &s = shapes[i];
  103. if (s.bpid > 0) {
  104. space->get_broadphase()->set_static(s.bpid, _static);
  105. }
  106. }
  107. }
  108. void CollisionObjectSW::_unregister_shapes() {
  109. for (int i = 0; i < shapes.size(); i++) {
  110. Shape &s = shapes.write[i];
  111. if (s.bpid > 0) {
  112. space->get_broadphase()->remove(s.bpid);
  113. s.bpid = 0;
  114. }
  115. }
  116. }
  117. void CollisionObjectSW::_update_shapes() {
  118. if (!space)
  119. return;
  120. for (int i = 0; i < shapes.size(); i++) {
  121. Shape &s = shapes.write[i];
  122. if (s.bpid == 0) {
  123. s.bpid = space->get_broadphase()->create(this, i);
  124. space->get_broadphase()->set_static(s.bpid, _static);
  125. }
  126. //not quite correct, should compute the next matrix..
  127. AABB shape_aabb = s.shape->get_aabb();
  128. Transform xform = transform * s.xform;
  129. shape_aabb = xform.xform(shape_aabb);
  130. s.aabb_cache = shape_aabb;
  131. s.aabb_cache = s.aabb_cache.grow((s.aabb_cache.size.x + s.aabb_cache.size.y) * 0.5 * 0.05);
  132. Vector3 scale = xform.get_basis().get_scale();
  133. s.area_cache = s.shape->get_area() * scale.x * scale.y * scale.z;
  134. space->get_broadphase()->move(s.bpid, s.aabb_cache);
  135. }
  136. }
  137. void CollisionObjectSW::_update_shapes_with_motion(const Vector3 &p_motion) {
  138. if (!space)
  139. return;
  140. for (int i = 0; i < shapes.size(); i++) {
  141. Shape &s = shapes.write[i];
  142. if (s.bpid == 0) {
  143. s.bpid = space->get_broadphase()->create(this, i);
  144. space->get_broadphase()->set_static(s.bpid, _static);
  145. }
  146. //not quite correct, should compute the next matrix..
  147. AABB shape_aabb = s.shape->get_aabb();
  148. Transform xform = transform * s.xform;
  149. shape_aabb = xform.xform(shape_aabb);
  150. shape_aabb = shape_aabb.merge(AABB(shape_aabb.position + p_motion, shape_aabb.size)); //use motion
  151. s.aabb_cache = shape_aabb;
  152. space->get_broadphase()->move(s.bpid, shape_aabb);
  153. }
  154. }
  155. void CollisionObjectSW::_set_space(SpaceSW *p_space) {
  156. if (space) {
  157. space->remove_object(this);
  158. for (int i = 0; i < shapes.size(); i++) {
  159. Shape &s = shapes.write[i];
  160. if (s.bpid) {
  161. space->get_broadphase()->remove(s.bpid);
  162. s.bpid = 0;
  163. }
  164. }
  165. }
  166. space = p_space;
  167. if (space) {
  168. space->add_object(this);
  169. _update_shapes();
  170. }
  171. }
  172. void CollisionObjectSW::_shape_changed() {
  173. _update_shapes();
  174. _shapes_changed();
  175. }
  176. CollisionObjectSW::CollisionObjectSW(Type p_type) :
  177. pending_shape_update_list(this) {
  178. _static = true;
  179. type = p_type;
  180. space = NULL;
  181. instance_id = 0;
  182. collision_layer = 1;
  183. collision_mask = 1;
  184. ray_pickable = true;
  185. }