collision_object_2d.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*************************************************************************/
  2. /* collision_object_2d.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.h"
  30. #include "servers/physics_2d_server.h"
  31. void CollisionObject2D::_update_shapes_from_children() {
  32. shapes.resize(0);
  33. for(int i=0;i<get_child_count();i++) {
  34. Node* n = get_child(i);
  35. n->call("_add_to_collision_object",this);
  36. }
  37. // _update_shapes();
  38. }
  39. void CollisionObject2D::_notification(int p_what) {
  40. switch(p_what) {
  41. case NOTIFICATION_ENTER_SCENE: {
  42. if (area)
  43. Physics2DServer::get_singleton()->area_set_transform(rid,get_global_transform());
  44. else
  45. Physics2DServer::get_singleton()->body_set_state(rid,Physics2DServer::BODY_STATE_TRANSFORM,get_global_transform());
  46. RID space = get_world_2d()->get_space();
  47. if (area) {
  48. Physics2DServer::get_singleton()->area_set_space(rid,space);
  49. } else
  50. Physics2DServer::get_singleton()->body_set_space(rid,space);
  51. //get space
  52. }
  53. case NOTIFICATION_TRANSFORM_CHANGED: {
  54. if (area)
  55. Physics2DServer::get_singleton()->area_set_transform(rid,get_global_transform());
  56. else
  57. Physics2DServer::get_singleton()->body_set_state(rid,Physics2DServer::BODY_STATE_TRANSFORM,get_global_transform());
  58. } break;
  59. case NOTIFICATION_EXIT_SCENE: {
  60. if (area) {
  61. Physics2DServer::get_singleton()->area_set_space(rid,RID());
  62. } else
  63. Physics2DServer::get_singleton()->body_set_space(rid,RID());
  64. } break;
  65. }
  66. }
  67. void CollisionObject2D::_update_shapes() {
  68. if (!rid.is_valid())
  69. return;
  70. if (area)
  71. Physics2DServer::get_singleton()->area_clear_shapes(rid);
  72. else
  73. Physics2DServer::get_singleton()->body_clear_shapes(rid);
  74. for(int i=0;i<shapes.size();i++) {
  75. if (shapes[i].shape.is_null())
  76. continue;
  77. if (area)
  78. Physics2DServer::get_singleton()->area_add_shape(rid,shapes[i].shape->get_rid(),shapes[i].xform);
  79. else {
  80. Physics2DServer::get_singleton()->body_add_shape(rid,shapes[i].shape->get_rid(),shapes[i].xform);
  81. if (shapes[i].trigger)
  82. Physics2DServer::get_singleton()->body_set_shape_as_trigger(rid,i,shapes[i].trigger);
  83. }
  84. }
  85. }
  86. bool CollisionObject2D::_set(const StringName& p_name, const Variant& p_value) {
  87. String name=p_name;
  88. if (name=="shape_count") {
  89. shapes.resize(p_value);
  90. _update_shapes();
  91. _change_notify();
  92. } else if (name.begins_with("shapes/")) {
  93. int idx=name.get_slice("/",1).to_int();
  94. String what=name.get_slice("/",2);
  95. if (what=="shape")
  96. set_shape(idx,RefPtr(p_value));
  97. else if (what=="transform")
  98. set_shape_transform(idx,p_value);
  99. else if (what=="trigger")
  100. set_shape_as_trigger(idx,p_value);
  101. } else
  102. return false;
  103. return true;
  104. }
  105. bool CollisionObject2D::_get(const StringName& p_name,Variant &r_ret) const {
  106. String name=p_name;
  107. if (name=="shape_count") {
  108. r_ret= shapes.size();
  109. } else if (name.begins_with("shapes/")) {
  110. int idx=name.get_slice("/",1).to_int();
  111. String what=name.get_slice("/",2);
  112. if (what=="shape")
  113. r_ret= get_shape(idx);
  114. else if (what=="transform")
  115. r_ret= get_shape_transform(idx);
  116. else if (what=="trigger")
  117. r_ret= is_shape_set_as_trigger(idx);
  118. } else
  119. return false;
  120. return true;
  121. }
  122. void CollisionObject2D::_get_property_list( List<PropertyInfo> *p_list) const {
  123. p_list->push_back( PropertyInfo(Variant::INT,"shape_count",PROPERTY_HINT_RANGE,"0,256,1",PROPERTY_USAGE_NOEDITOR) );
  124. for(int i=0;i<shapes.size();i++) {
  125. String path="shapes/"+itos(i)+"/";
  126. p_list->push_back( PropertyInfo(Variant::OBJECT,path+"shape",PROPERTY_HINT_RESOURCE_TYPE,"Shape2D",PROPERTY_USAGE_NOEDITOR) );
  127. p_list->push_back( PropertyInfo(Variant::TRANSFORM,path+"transform",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR) );
  128. p_list->push_back( PropertyInfo(Variant::BOOL,path+"trigger",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR) );
  129. }
  130. }
  131. void CollisionObject2D::_bind_methods() {
  132. ObjectTypeDB::bind_method(_MD("add_shape","shape:Shape2D","transform"),&CollisionObject2D::add_shape,DEFVAL(Matrix32()));
  133. ObjectTypeDB::bind_method(_MD("get_shape_count"),&CollisionObject2D::get_shape_count);
  134. ObjectTypeDB::bind_method(_MD("set_shape","shape_idx","shape:Shape"),&CollisionObject2D::set_shape);
  135. ObjectTypeDB::bind_method(_MD("set_shape_transform","shape_idx","transform"),&CollisionObject2D::set_shape_transform);
  136. ObjectTypeDB::bind_method(_MD("set_shape_as_trigger","shape_idx","enable"),&CollisionObject2D::set_shape_as_trigger);
  137. ObjectTypeDB::bind_method(_MD("get_shape:Shape2D","shape_idx"),&CollisionObject2D::get_shape);
  138. ObjectTypeDB::bind_method(_MD("get_shape_transform","shape_idx"),&CollisionObject2D::get_shape_transform);
  139. ObjectTypeDB::bind_method(_MD("is_shape_set_as_trigger","shape_idx"),&CollisionObject2D::is_shape_set_as_trigger);
  140. ObjectTypeDB::bind_method(_MD("remove_shape","shape_idx"),&CollisionObject2D::remove_shape);
  141. ObjectTypeDB::bind_method(_MD("clear_shapes"),&CollisionObject2D::clear_shapes);
  142. ObjectTypeDB::bind_method(_MD("get_rid"),&CollisionObject2D::get_rid);
  143. }
  144. void CollisionObject2D::add_shape(const Ref<Shape2D>& p_shape, const Matrix32& p_transform) {
  145. ShapeData sdata;
  146. sdata.shape=p_shape;
  147. sdata.xform=p_transform;
  148. sdata.trigger=false;
  149. shapes.push_back(sdata);
  150. _update_shapes();
  151. }
  152. int CollisionObject2D::get_shape_count() const {
  153. return shapes.size();
  154. }
  155. void CollisionObject2D::set_shape(int p_shape_idx, const Ref<Shape2D>& p_shape) {
  156. ERR_FAIL_INDEX(p_shape_idx,shapes.size());
  157. shapes[p_shape_idx].shape=p_shape;
  158. _update_shapes();
  159. }
  160. void CollisionObject2D::set_shape_transform(int p_shape_idx, const Matrix32& p_transform) {
  161. ERR_FAIL_INDEX(p_shape_idx,shapes.size());
  162. shapes[p_shape_idx].xform=p_transform;
  163. _update_shapes();
  164. }
  165. Ref<Shape2D> CollisionObject2D::get_shape(int p_shape_idx) const {
  166. ERR_FAIL_INDEX_V(p_shape_idx,shapes.size(),Ref<Shape2D>());
  167. return shapes[p_shape_idx].shape;
  168. }
  169. Matrix32 CollisionObject2D::get_shape_transform(int p_shape_idx) const {
  170. ERR_FAIL_INDEX_V(p_shape_idx,shapes.size(),Matrix32());
  171. return shapes[p_shape_idx].xform;
  172. }
  173. void CollisionObject2D::remove_shape(int p_shape_idx) {
  174. ERR_FAIL_INDEX(p_shape_idx,shapes.size());
  175. shapes.remove(p_shape_idx);
  176. _update_shapes();
  177. }
  178. void CollisionObject2D::set_shape_as_trigger(int p_shape_idx, bool p_trigger) {
  179. ERR_FAIL_INDEX(p_shape_idx,shapes.size());
  180. shapes[p_shape_idx].trigger=p_trigger;
  181. if (!area && rid.is_valid()) {
  182. Physics2DServer::get_singleton()->body_set_shape_as_trigger(rid,p_shape_idx,p_trigger);
  183. }
  184. }
  185. bool CollisionObject2D::is_shape_set_as_trigger(int p_shape_idx) const {
  186. ERR_FAIL_INDEX_V(p_shape_idx,shapes.size(),false);
  187. return shapes[p_shape_idx].trigger;
  188. }
  189. void CollisionObject2D::clear_shapes() {
  190. shapes.clear();
  191. _update_shapes();
  192. }
  193. CollisionObject2D::CollisionObject2D(RID p_rid, bool p_area) {
  194. rid=p_rid;
  195. area=p_area;
  196. if (p_area) {
  197. Physics2DServer::get_singleton()->area_attach_object_instance_ID(rid,get_instance_ID());
  198. } else {
  199. Physics2DServer::get_singleton()->body_attach_object_instance_ID(rid,get_instance_ID());
  200. }
  201. }
  202. CollisionObject2D::CollisionObject2D() {
  203. //owner=
  204. }
  205. CollisionObject2D::~CollisionObject2D() {
  206. Physics2DServer::get_singleton()->free(rid);
  207. }