collision_object_2d_sw.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*************************************************************************/
  2. /* collision_object_2d_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_2d_sw.h"
  30. #include "space_2d_sw.h"
  31. void CollisionObject2DSW::add_shape(Shape2DSW *p_shape,const Matrix32& 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. s.trigger=false;
  38. shapes.push_back(s);
  39. p_shape->add_owner(this);
  40. _update_shapes();
  41. _shapes_changed();
  42. }
  43. void CollisionObject2DSW::set_shape(int p_index,Shape2DSW *p_shape){
  44. ERR_FAIL_INDEX(p_index,shapes.size());
  45. shapes[p_index].shape->remove_owner(this);
  46. shapes[p_index].shape=p_shape;
  47. p_shape->add_owner(this);
  48. _update_shapes();
  49. _shapes_changed();
  50. }
  51. void CollisionObject2DSW::set_shape_transform(int p_index,const Matrix32& p_transform){
  52. ERR_FAIL_INDEX(p_index,shapes.size());
  53. shapes[p_index].xform=p_transform;
  54. shapes[p_index].xform_inv=p_transform.affine_inverse();
  55. _update_shapes();
  56. _shapes_changed();
  57. }
  58. void CollisionObject2DSW::remove_shape(Shape2DSW *p_shape) {
  59. //remove a shape, all the times it appears
  60. for(int i=0;i<shapes.size();i++) {
  61. if (shapes[i].shape==p_shape) {
  62. remove_shape(i);
  63. i--;
  64. }
  65. }
  66. }
  67. void CollisionObject2DSW::remove_shape(int p_index){
  68. //remove anything from shape to be erased to end, so subindices don't change
  69. ERR_FAIL_INDEX(p_index,shapes.size());
  70. for(int i=p_index;i<shapes.size();i++) {
  71. if (shapes[i].bpid==0)
  72. continue;
  73. //should never get here with a null owner
  74. space->get_broadphase()->remove(shapes[i].bpid);
  75. shapes[i].bpid=0;
  76. }
  77. shapes[p_index].shape->remove_owner(this);
  78. shapes.remove(p_index);
  79. _shapes_changed();
  80. }
  81. void CollisionObject2DSW::_set_static(bool p_static) {
  82. if (_static==p_static)
  83. return;
  84. _static=p_static;
  85. if (!space)
  86. return;
  87. for(int i=0;i<get_shape_count();i++) {
  88. Shape &s=shapes[i];
  89. if (s.bpid>0) {
  90. space->get_broadphase()->set_static(s.bpid,_static);
  91. }
  92. }
  93. }
  94. void CollisionObject2DSW::_unregister_shapes() {
  95. for(int i=0;i<shapes.size();i++) {
  96. Shape &s=shapes[i];
  97. if (s.bpid>0) {
  98. space->get_broadphase()->remove(s.bpid);
  99. s.bpid=0;
  100. }
  101. }
  102. }
  103. void CollisionObject2DSW::_update_shapes() {
  104. if (!space)
  105. return;
  106. for(int i=0;i<shapes.size();i++) {
  107. Shape &s=shapes[i];
  108. if (s.bpid==0) {
  109. s.bpid=space->get_broadphase()->create(this,i);
  110. space->get_broadphase()->set_static(s.bpid,_static);
  111. }
  112. //not quite correct, should compute the next matrix..
  113. Rect2 shape_aabb=s.shape->get_aabb();
  114. Matrix32 xform = transform * s.xform;
  115. shape_aabb=xform.xform(shape_aabb);
  116. s.aabb_cache=shape_aabb;
  117. s.aabb_cache=s.aabb_cache.grow( (s.aabb_cache.size.x + s.aabb_cache.size.y)*0.5*0.05 );
  118. space->get_broadphase()->move(s.bpid,s.aabb_cache);
  119. }
  120. }
  121. void CollisionObject2DSW::_update_shapes_with_motion(const Vector2& p_motion) {
  122. if (!space)
  123. return;
  124. for(int i=0;i<shapes.size();i++) {
  125. Shape &s=shapes[i];
  126. if (s.bpid==0) {
  127. s.bpid=space->get_broadphase()->create(this,i);
  128. space->get_broadphase()->set_static(s.bpid,_static);
  129. }
  130. //not quite correct, should compute the next matrix..
  131. Rect2 shape_aabb=s.shape->get_aabb();
  132. Matrix32 xform = transform * s.xform;
  133. shape_aabb=xform.xform(shape_aabb);
  134. shape_aabb=shape_aabb.merge(Rect2( shape_aabb.pos+p_motion,shape_aabb.size)); //use motion
  135. s.aabb_cache=shape_aabb;
  136. space->get_broadphase()->move(s.bpid,shape_aabb);
  137. }
  138. }
  139. void CollisionObject2DSW::_set_space(Space2DSW *p_space) {
  140. if (space) {
  141. space->remove_object(this);
  142. for(int i=0;i<shapes.size();i++) {
  143. Shape &s=shapes[i];
  144. if (s.bpid) {
  145. space->get_broadphase()->remove(s.bpid);
  146. s.bpid=0;
  147. }
  148. }
  149. }
  150. space=p_space;
  151. if (space) {
  152. space->add_object(this);
  153. _update_shapes();
  154. }
  155. }
  156. void CollisionObject2DSW::_shape_changed() {
  157. _update_shapes();
  158. _shapes_changed();
  159. }
  160. CollisionObject2DSW::CollisionObject2DSW(Type p_type) {
  161. _static=true;
  162. type=p_type;
  163. space=NULL;
  164. instance_id=0;
  165. }