collision_object.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*************************************************************************/
  2. /* collision_object.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.h"
  31. #include "scene/scene_string_names.h"
  32. #include "servers/physics_server.h"
  33. void CollisionObject::_notification(int p_what) {
  34. switch (p_what) {
  35. case NOTIFICATION_ENTER_WORLD: {
  36. if (area)
  37. PhysicsServer::get_singleton()->area_set_transform(rid, get_global_transform());
  38. else
  39. PhysicsServer::get_singleton()->body_set_state(rid, PhysicsServer::BODY_STATE_TRANSFORM, get_global_transform());
  40. RID space = get_world()->get_space();
  41. if (area) {
  42. PhysicsServer::get_singleton()->area_set_space(rid, space);
  43. } else
  44. PhysicsServer::get_singleton()->body_set_space(rid, space);
  45. _update_pickable();
  46. //get space
  47. };
  48. case NOTIFICATION_TRANSFORM_CHANGED: {
  49. if (area)
  50. PhysicsServer::get_singleton()->area_set_transform(rid, get_global_transform());
  51. else
  52. PhysicsServer::get_singleton()->body_set_state(rid, PhysicsServer::BODY_STATE_TRANSFORM, get_global_transform());
  53. } break;
  54. case NOTIFICATION_VISIBILITY_CHANGED: {
  55. _update_pickable();
  56. } break;
  57. case NOTIFICATION_EXIT_WORLD: {
  58. if (area) {
  59. PhysicsServer::get_singleton()->area_set_space(rid, RID());
  60. } else
  61. PhysicsServer::get_singleton()->body_set_space(rid, RID());
  62. } break;
  63. }
  64. }
  65. void CollisionObject::_input_event(Node *p_camera, const Ref<InputEvent> &p_input_event, const Vector3 &p_pos, const Vector3 &p_normal, int p_shape) {
  66. if (get_script_instance()) {
  67. get_script_instance()->call(SceneStringNames::get_singleton()->_input_event, p_camera, p_input_event, p_pos, p_normal, p_shape);
  68. }
  69. emit_signal(SceneStringNames::get_singleton()->input_event, p_camera, p_input_event, p_pos, p_normal, p_shape);
  70. }
  71. void CollisionObject::_mouse_enter() {
  72. if (get_script_instance()) {
  73. get_script_instance()->call(SceneStringNames::get_singleton()->_mouse_enter);
  74. }
  75. emit_signal(SceneStringNames::get_singleton()->mouse_entered);
  76. }
  77. void CollisionObject::_mouse_exit() {
  78. if (get_script_instance()) {
  79. get_script_instance()->call(SceneStringNames::get_singleton()->_mouse_exit);
  80. }
  81. emit_signal(SceneStringNames::get_singleton()->mouse_exited);
  82. }
  83. void CollisionObject::_update_pickable() {
  84. if (!is_inside_tree())
  85. return;
  86. bool pickable = ray_pickable && is_inside_tree() && is_visible_in_tree();
  87. if (area)
  88. PhysicsServer::get_singleton()->area_set_ray_pickable(rid, pickable);
  89. else
  90. PhysicsServer::get_singleton()->body_set_ray_pickable(rid, pickable);
  91. }
  92. void CollisionObject::set_ray_pickable(bool p_ray_pickable) {
  93. ray_pickable = p_ray_pickable;
  94. _update_pickable();
  95. }
  96. bool CollisionObject::is_ray_pickable() const {
  97. return ray_pickable;
  98. }
  99. void CollisionObject::_bind_methods() {
  100. ClassDB::bind_method(D_METHOD("set_ray_pickable", "ray_pickable"), &CollisionObject::set_ray_pickable);
  101. ClassDB::bind_method(D_METHOD("is_ray_pickable"), &CollisionObject::is_ray_pickable);
  102. ClassDB::bind_method(D_METHOD("set_capture_input_on_drag", "enable"), &CollisionObject::set_capture_input_on_drag);
  103. ClassDB::bind_method(D_METHOD("get_capture_input_on_drag"), &CollisionObject::get_capture_input_on_drag);
  104. ClassDB::bind_method(D_METHOD("get_rid"), &CollisionObject::get_rid);
  105. ClassDB::bind_method(D_METHOD("create_shape_owner", "owner"), &CollisionObject::create_shape_owner);
  106. ClassDB::bind_method(D_METHOD("remove_shape_owner", "owner_id"), &CollisionObject::remove_shape_owner);
  107. ClassDB::bind_method(D_METHOD("get_shape_owners"), &CollisionObject::_get_shape_owners);
  108. ClassDB::bind_method(D_METHOD("shape_owner_set_transform", "owner_id", "transform"), &CollisionObject::shape_owner_set_transform);
  109. ClassDB::bind_method(D_METHOD("shape_owner_get_transform", "owner_id"), &CollisionObject::shape_owner_get_transform);
  110. ClassDB::bind_method(D_METHOD("shape_owner_get_owner", "owner_id"), &CollisionObject::shape_owner_get_owner);
  111. ClassDB::bind_method(D_METHOD("shape_owner_set_disabled", "owner_id", "disabled"), &CollisionObject::shape_owner_set_disabled);
  112. ClassDB::bind_method(D_METHOD("is_shape_owner_disabled", "owner_id"), &CollisionObject::is_shape_owner_disabled);
  113. ClassDB::bind_method(D_METHOD("shape_owner_add_shape", "owner_id", "shape"), &CollisionObject::shape_owner_add_shape);
  114. ClassDB::bind_method(D_METHOD("shape_owner_get_shape_count", "owner_id"), &CollisionObject::shape_owner_get_shape_count);
  115. ClassDB::bind_method(D_METHOD("shape_owner_get_shape", "owner_id", "shape_id"), &CollisionObject::shape_owner_get_shape);
  116. ClassDB::bind_method(D_METHOD("shape_owner_get_shape_index", "owner_id", "shape_id"), &CollisionObject::shape_owner_get_shape_index);
  117. ClassDB::bind_method(D_METHOD("shape_owner_remove_shape", "owner_id", "shape_id"), &CollisionObject::shape_owner_remove_shape);
  118. ClassDB::bind_method(D_METHOD("shape_owner_clear_shapes", "owner_id"), &CollisionObject::shape_owner_clear_shapes);
  119. ClassDB::bind_method(D_METHOD("shape_find_owner", "shape_index"), &CollisionObject::shape_find_owner);
  120. BIND_VMETHOD(MethodInfo("_input_event", PropertyInfo(Variant::OBJECT, "camera"), PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent"), PropertyInfo(Variant::VECTOR3, "click_position"), PropertyInfo(Variant::VECTOR3, "click_normal"), PropertyInfo(Variant::INT, "shape_idx")));
  121. ADD_SIGNAL(MethodInfo("input_event", PropertyInfo(Variant::OBJECT, "camera", PROPERTY_HINT_RESOURCE_TYPE, "Node"), PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent"), PropertyInfo(Variant::VECTOR3, "click_position"), PropertyInfo(Variant::VECTOR3, "click_normal"), PropertyInfo(Variant::INT, "shape_idx")));
  122. ADD_SIGNAL(MethodInfo("mouse_entered"));
  123. ADD_SIGNAL(MethodInfo("mouse_exited"));
  124. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "input_ray_pickable"), "set_ray_pickable", "is_ray_pickable");
  125. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "input_capture_on_drag"), "set_capture_input_on_drag", "get_capture_input_on_drag");
  126. }
  127. uint32_t CollisionObject::create_shape_owner(Object *p_owner) {
  128. ShapeData sd;
  129. uint32_t id;
  130. if (shapes.size() == 0) {
  131. id = 0;
  132. } else {
  133. id = shapes.back()->key() + 1;
  134. }
  135. sd.owner = p_owner;
  136. shapes[id] = sd;
  137. return id;
  138. }
  139. void CollisionObject::remove_shape_owner(uint32_t owner) {
  140. ERR_FAIL_COND(!shapes.has(owner));
  141. shape_owner_clear_shapes(owner);
  142. shapes.erase(owner);
  143. }
  144. void CollisionObject::shape_owner_set_disabled(uint32_t p_owner, bool p_disabled) {
  145. ERR_FAIL_COND(!shapes.has(p_owner));
  146. ShapeData &sd = shapes[p_owner];
  147. sd.disabled = p_disabled;
  148. for (int i = 0; i < sd.shapes.size(); i++) {
  149. if (area) {
  150. PhysicsServer::get_singleton()->area_set_shape_disabled(rid, sd.shapes[i].index, p_disabled);
  151. } else {
  152. PhysicsServer::get_singleton()->body_set_shape_disabled(rid, sd.shapes[i].index, p_disabled);
  153. }
  154. }
  155. }
  156. bool CollisionObject::is_shape_owner_disabled(uint32_t p_owner) const {
  157. ERR_FAIL_COND_V(!shapes.has(p_owner), false);
  158. return shapes[p_owner].disabled;
  159. }
  160. void CollisionObject::get_shape_owners(List<uint32_t> *r_owners) {
  161. for (Map<uint32_t, ShapeData>::Element *E = shapes.front(); E; E = E->next()) {
  162. r_owners->push_back(E->key());
  163. }
  164. }
  165. Array CollisionObject::_get_shape_owners() {
  166. Array ret;
  167. for (Map<uint32_t, ShapeData>::Element *E = shapes.front(); E; E = E->next()) {
  168. ret.push_back(E->key());
  169. }
  170. return ret;
  171. }
  172. void CollisionObject::shape_owner_set_transform(uint32_t p_owner, const Transform &p_transform) {
  173. ERR_FAIL_COND(!shapes.has(p_owner));
  174. ShapeData &sd = shapes[p_owner];
  175. sd.xform = p_transform;
  176. for (int i = 0; i < sd.shapes.size(); i++) {
  177. if (area) {
  178. PhysicsServer::get_singleton()->area_set_shape_transform(rid, sd.shapes[i].index, p_transform);
  179. } else {
  180. PhysicsServer::get_singleton()->body_set_shape_transform(rid, sd.shapes[i].index, p_transform);
  181. }
  182. }
  183. }
  184. Transform CollisionObject::shape_owner_get_transform(uint32_t p_owner) const {
  185. ERR_FAIL_COND_V(!shapes.has(p_owner), Transform());
  186. return shapes[p_owner].xform;
  187. }
  188. Object *CollisionObject::shape_owner_get_owner(uint32_t p_owner) const {
  189. ERR_FAIL_COND_V(!shapes.has(p_owner), NULL);
  190. return shapes[p_owner].owner;
  191. }
  192. void CollisionObject::shape_owner_add_shape(uint32_t p_owner, const Ref<Shape> &p_shape) {
  193. ERR_FAIL_COND(!shapes.has(p_owner));
  194. ERR_FAIL_COND(p_shape.is_null());
  195. ShapeData &sd = shapes[p_owner];
  196. ShapeData::ShapeBase s;
  197. s.index = total_subshapes;
  198. s.shape = p_shape;
  199. if (area) {
  200. PhysicsServer::get_singleton()->area_add_shape(rid, p_shape->get_rid(), sd.xform);
  201. } else {
  202. PhysicsServer::get_singleton()->body_add_shape(rid, p_shape->get_rid(), sd.xform);
  203. }
  204. sd.shapes.push_back(s);
  205. total_subshapes++;
  206. }
  207. int CollisionObject::shape_owner_get_shape_count(uint32_t p_owner) const {
  208. ERR_FAIL_COND_V(!shapes.has(p_owner), 0);
  209. return shapes[p_owner].shapes.size();
  210. }
  211. Ref<Shape> CollisionObject::shape_owner_get_shape(uint32_t p_owner, int p_shape) const {
  212. ERR_FAIL_COND_V(!shapes.has(p_owner), Ref<Shape>());
  213. ERR_FAIL_INDEX_V(p_shape, shapes[p_owner].shapes.size(), Ref<Shape>());
  214. return shapes[p_owner].shapes[p_shape].shape;
  215. }
  216. int CollisionObject::shape_owner_get_shape_index(uint32_t p_owner, int p_shape) const {
  217. ERR_FAIL_COND_V(!shapes.has(p_owner), -1);
  218. ERR_FAIL_INDEX_V(p_shape, shapes[p_owner].shapes.size(), -1);
  219. return shapes[p_owner].shapes[p_shape].index;
  220. }
  221. void CollisionObject::shape_owner_remove_shape(uint32_t p_owner, int p_shape) {
  222. ERR_FAIL_COND(!shapes.has(p_owner));
  223. ERR_FAIL_INDEX(p_shape, shapes[p_owner].shapes.size());
  224. int index_to_remove = shapes[p_owner].shapes[p_shape].index;
  225. if (area) {
  226. PhysicsServer::get_singleton()->area_remove_shape(rid, index_to_remove);
  227. } else {
  228. PhysicsServer::get_singleton()->body_remove_shape(rid, index_to_remove);
  229. }
  230. shapes[p_owner].shapes.remove(p_shape);
  231. for (Map<uint32_t, ShapeData>::Element *E = shapes.front(); E; E = E->next()) {
  232. for (int i = 0; i < E->get().shapes.size(); i++) {
  233. if (E->get().shapes[i].index > index_to_remove) {
  234. E->get().shapes.write[i].index -= 1;
  235. }
  236. }
  237. }
  238. total_subshapes--;
  239. }
  240. void CollisionObject::shape_owner_clear_shapes(uint32_t p_owner) {
  241. ERR_FAIL_COND(!shapes.has(p_owner));
  242. while (shape_owner_get_shape_count(p_owner) > 0) {
  243. shape_owner_remove_shape(p_owner, 0);
  244. }
  245. }
  246. uint32_t CollisionObject::shape_find_owner(int p_shape_index) const {
  247. ERR_FAIL_INDEX_V(p_shape_index, total_subshapes, 0);
  248. for (const Map<uint32_t, ShapeData>::Element *E = shapes.front(); E; E = E->next()) {
  249. for (int i = 0; i < E->get().shapes.size(); i++) {
  250. if (E->get().shapes[i].index == p_shape_index) {
  251. return E->key();
  252. }
  253. }
  254. }
  255. //in theory it should be unreachable
  256. return 0;
  257. }
  258. CollisionObject::CollisionObject(RID p_rid, bool p_area) {
  259. rid = p_rid;
  260. area = p_area;
  261. capture_input_on_drag = false;
  262. ray_pickable = true;
  263. set_notify_transform(true);
  264. total_subshapes = 0;
  265. if (p_area) {
  266. PhysicsServer::get_singleton()->area_attach_object_instance_id(rid, get_instance_id());
  267. } else {
  268. PhysicsServer::get_singleton()->body_attach_object_instance_id(rid, get_instance_id());
  269. }
  270. //set_transform_notify(true);
  271. }
  272. void CollisionObject::set_capture_input_on_drag(bool p_capture) {
  273. capture_input_on_drag = p_capture;
  274. }
  275. bool CollisionObject::get_capture_input_on_drag() const {
  276. return capture_input_on_drag;
  277. }
  278. String CollisionObject::get_configuration_warning() const {
  279. String warning = Spatial::get_configuration_warning();
  280. if (shapes.empty()) {
  281. if (warning == String()) {
  282. warning += "\n";
  283. }
  284. warning += TTR("This node has no shape, so it can't collide or interact with other objects.\nConsider adding a CollisionShape or CollisionPolygon as a child to define its shape.");
  285. }
  286. return warning;
  287. }
  288. CollisionObject::CollisionObject() {
  289. capture_input_on_drag = false;
  290. ray_pickable = true;
  291. set_notify_transform(true);
  292. //owner=
  293. //set_transform_notify(true);
  294. }
  295. CollisionObject::~CollisionObject() {
  296. PhysicsServer::get_singleton()->free(rid);
  297. }