collision_object_sw.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*************************************************************************/
  2. /* collision_object_sw.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "collision_object_sw.h"
  30. #include "space_sw.h"
  31. void CollisionObjectSW::add_shape(ShapeSW *p_shape,const Transform& p_transform) {
  32. Shape s;
  33. s.shape=p_shape;
  34. s.xform=p_transform;
  35. s.xform_inv=s.xform.affine_inverse();
  36. s.bpid=0; //needs update
  37. shapes.push_back(s);
  38. p_shape->add_owner(this);
  39. _update_shapes();
  40. _shapes_changed();
  41. }
  42. void CollisionObjectSW::set_shape(int p_index,ShapeSW *p_shape){
  43. ERR_FAIL_INDEX(p_index,shapes.size());
  44. shapes[p_index].shape->remove_owner(this);
  45. shapes[p_index].shape=p_shape;
  46. p_shape->add_owner(this);
  47. _update_shapes();
  48. _shapes_changed();
  49. }
  50. void CollisionObjectSW::set_shape_transform(int p_index,const Transform& p_transform){
  51. ERR_FAIL_INDEX(p_index,shapes.size());
  52. shapes[p_index].xform=p_transform;
  53. shapes[p_index].xform_inv=p_transform.affine_inverse();
  54. _update_shapes();
  55. _shapes_changed();
  56. }
  57. void CollisionObjectSW::remove_shape(ShapeSW *p_shape) {
  58. //remove a shape, all the times it appears
  59. for(int i=0;i<shapes.size();i++) {
  60. if (shapes[i].shape==p_shape) {
  61. remove_shape(i);
  62. i--;
  63. }
  64. }
  65. }
  66. void CollisionObjectSW::remove_shape(int p_index){
  67. //remove anything from shape to be erased to end, so subindices don't change
  68. ERR_FAIL_INDEX(p_index,shapes.size());
  69. for(int i=p_index;i<shapes.size();i++) {
  70. if (shapes[i].bpid==0)
  71. continue;
  72. //should never get here with a null owner
  73. space->get_broadphase()->remove(shapes[i].bpid);
  74. shapes[i].bpid=0;
  75. }
  76. shapes[p_index].shape->remove_owner(this);
  77. shapes.remove(p_index);
  78. _shapes_changed();
  79. }
  80. void CollisionObjectSW::_set_static(bool p_static) {
  81. if (_static==p_static)
  82. return;
  83. _static=p_static;
  84. if (!space)
  85. return;
  86. for(int i=0;i<get_shape_count();i++) {
  87. Shape &s=shapes[i];
  88. if (s.bpid>0) {
  89. space->get_broadphase()->set_static(s.bpid,_static);
  90. }
  91. }
  92. }
  93. void CollisionObjectSW::_unregister_shapes() {
  94. for(int i=0;i<shapes.size();i++) {
  95. Shape &s=shapes[i];
  96. if (s.bpid>0) {
  97. space->get_broadphase()->remove(s.bpid);
  98. s.bpid=0;
  99. }
  100. }
  101. }
  102. void CollisionObjectSW::_update_shapes() {
  103. if (!space)
  104. return;
  105. for(int i=0;i<shapes.size();i++) {
  106. Shape &s=shapes[i];
  107. if (s.bpid==0) {
  108. s.bpid=space->get_broadphase()->create(this,i);
  109. space->get_broadphase()->set_static(s.bpid,_static);
  110. }
  111. //not quite correct, should compute the next matrix..
  112. AABB shape_aabb=s.shape->get_aabb();
  113. Transform xform = transform * s.xform;
  114. shape_aabb=xform.xform(shape_aabb);
  115. s.aabb_cache=shape_aabb;
  116. s.aabb_cache=s.aabb_cache.grow( (s.aabb_cache.size.x + s.aabb_cache.size.y)*0.5*0.05 );
  117. space->get_broadphase()->move(s.bpid,s.aabb_cache);
  118. }
  119. }
  120. void CollisionObjectSW::_update_shapes_with_motion(const Vector3& p_motion) {
  121. if (!space)
  122. return;
  123. for(int i=0;i<shapes.size();i++) {
  124. Shape &s=shapes[i];
  125. if (s.bpid==0) {
  126. s.bpid=space->get_broadphase()->create(this,i);
  127. space->get_broadphase()->set_static(s.bpid,_static);
  128. }
  129. //not quite correct, should compute the next matrix..
  130. AABB shape_aabb=s.shape->get_aabb();
  131. Transform xform = transform * s.xform;
  132. shape_aabb=xform.xform(shape_aabb);
  133. shape_aabb=shape_aabb.merge(AABB( shape_aabb.pos+p_motion,shape_aabb.size)); //use motion
  134. s.aabb_cache=shape_aabb;
  135. space->get_broadphase()->move(s.bpid,shape_aabb);
  136. }
  137. }
  138. void CollisionObjectSW::_set_space(SpaceSW *p_space) {
  139. if (space) {
  140. space->remove_object(this);
  141. for(int i=0;i<shapes.size();i++) {
  142. Shape &s=shapes[i];
  143. if (s.bpid) {
  144. space->get_broadphase()->remove(s.bpid);
  145. s.bpid=0;
  146. }
  147. }
  148. }
  149. space=p_space;
  150. if (space) {
  151. space->add_object(this);
  152. _update_shapes();
  153. }
  154. }
  155. void CollisionObjectSW::_shape_changed() {
  156. _update_shapes();
  157. _shapes_changed();
  158. }
  159. CollisionObjectSW::CollisionObjectSW(Type p_type) {
  160. _static=true;
  161. type=p_type;
  162. space=NULL;
  163. instance_id=0;
  164. }