collision_object.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /*************************************************************************/
  2. /* collision_object.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "core/engine.h"
  32. #include "mesh_instance.h"
  33. #include "scene/scene_string_names.h"
  34. #include "servers/physics_server.h"
  35. void CollisionObject::_notification(int p_what) {
  36. switch (p_what) {
  37. case NOTIFICATION_ENTER_TREE: {
  38. if (_are_collision_shapes_visible()) {
  39. debug_shape_old_transform = get_global_transform();
  40. for (Map<uint32_t, ShapeData>::Element *E = shapes.front(); E; E = E->next()) {
  41. debug_shapes_to_update.insert(E->key());
  42. }
  43. _update_debug_shapes();
  44. }
  45. } break;
  46. case NOTIFICATION_EXIT_TREE: {
  47. if (debug_shapes_count > 0) {
  48. _clear_debug_shapes();
  49. }
  50. } break;
  51. case NOTIFICATION_ENTER_WORLD: {
  52. if (area) {
  53. PhysicsServer::get_singleton()->area_set_transform(rid, get_global_transform());
  54. } else {
  55. PhysicsServer::get_singleton()->body_set_state(rid, PhysicsServer::BODY_STATE_TRANSFORM, get_global_transform());
  56. }
  57. RID space = get_world()->get_space();
  58. if (area) {
  59. PhysicsServer::get_singleton()->area_set_space(rid, space);
  60. } else {
  61. PhysicsServer::get_singleton()->body_set_space(rid, space);
  62. }
  63. _update_pickable();
  64. //get space
  65. } break;
  66. case NOTIFICATION_TRANSFORM_CHANGED: {
  67. if (area) {
  68. PhysicsServer::get_singleton()->area_set_transform(rid, get_global_transform());
  69. } else {
  70. PhysicsServer::get_singleton()->body_set_state(rid, PhysicsServer::BODY_STATE_TRANSFORM, get_global_transform());
  71. }
  72. _on_transform_changed();
  73. } break;
  74. case NOTIFICATION_VISIBILITY_CHANGED: {
  75. _update_pickable();
  76. } break;
  77. case NOTIFICATION_EXIT_WORLD: {
  78. if (area) {
  79. PhysicsServer::get_singleton()->area_set_space(rid, RID());
  80. } else {
  81. PhysicsServer::get_singleton()->body_set_space(rid, RID());
  82. }
  83. } break;
  84. }
  85. }
  86. void CollisionObject::set_collision_layer(uint32_t p_layer) {
  87. collision_layer = p_layer;
  88. if (area) {
  89. PhysicsServer::get_singleton()->area_set_collision_layer(get_rid(), p_layer);
  90. } else {
  91. PhysicsServer::get_singleton()->body_set_collision_layer(get_rid(), p_layer);
  92. }
  93. }
  94. uint32_t CollisionObject::get_collision_layer() const {
  95. return collision_layer;
  96. }
  97. void CollisionObject::set_collision_mask(uint32_t p_mask) {
  98. collision_mask = p_mask;
  99. if (area) {
  100. PhysicsServer::get_singleton()->area_set_collision_mask(get_rid(), p_mask);
  101. } else {
  102. PhysicsServer::get_singleton()->body_set_collision_mask(get_rid(), p_mask);
  103. }
  104. }
  105. uint32_t CollisionObject::get_collision_mask() const {
  106. return collision_mask;
  107. }
  108. void CollisionObject::set_collision_layer_bit(int p_bit, bool p_value) {
  109. uint32_t collision_layer = get_collision_layer();
  110. if (p_value) {
  111. collision_layer |= 1 << p_bit;
  112. } else {
  113. collision_layer &= ~(1 << p_bit);
  114. }
  115. set_collision_layer(collision_layer);
  116. }
  117. bool CollisionObject::get_collision_layer_bit(int p_bit) const {
  118. return get_collision_layer() & (1 << p_bit);
  119. }
  120. void CollisionObject::set_collision_mask_bit(int p_bit, bool p_value) {
  121. uint32_t mask = get_collision_mask();
  122. if (p_value) {
  123. mask |= 1 << p_bit;
  124. } else {
  125. mask &= ~(1 << p_bit);
  126. }
  127. set_collision_mask(mask);
  128. }
  129. bool CollisionObject::get_collision_mask_bit(int p_bit) const {
  130. return get_collision_mask() & (1 << p_bit);
  131. }
  132. void CollisionObject::_input_event(Node *p_camera, const Ref<InputEvent> &p_input_event, const Vector3 &p_pos, const Vector3 &p_normal, int p_shape) {
  133. if (get_script_instance()) {
  134. get_script_instance()->call(SceneStringNames::get_singleton()->_input_event, p_camera, p_input_event, p_pos, p_normal, p_shape);
  135. }
  136. emit_signal(SceneStringNames::get_singleton()->input_event, p_camera, p_input_event, p_pos, p_normal, p_shape);
  137. }
  138. void CollisionObject::_mouse_enter() {
  139. if (get_script_instance()) {
  140. get_script_instance()->call(SceneStringNames::get_singleton()->_mouse_enter);
  141. }
  142. emit_signal(SceneStringNames::get_singleton()->mouse_entered);
  143. }
  144. void CollisionObject::_mouse_exit() {
  145. if (get_script_instance()) {
  146. get_script_instance()->call(SceneStringNames::get_singleton()->_mouse_exit);
  147. }
  148. emit_signal(SceneStringNames::get_singleton()->mouse_exited);
  149. }
  150. void CollisionObject::_update_pickable() {
  151. if (!is_inside_tree()) {
  152. return;
  153. }
  154. bool pickable = ray_pickable && is_visible_in_tree();
  155. if (area) {
  156. PhysicsServer::get_singleton()->area_set_ray_pickable(rid, pickable);
  157. } else {
  158. PhysicsServer::get_singleton()->body_set_ray_pickable(rid, pickable);
  159. }
  160. }
  161. bool CollisionObject::_are_collision_shapes_visible() {
  162. return is_inside_tree() && get_tree()->is_debugging_collisions_hint() && !Engine::get_singleton()->is_editor_hint();
  163. }
  164. void CollisionObject::_update_shape_data(uint32_t p_owner) {
  165. if (_are_collision_shapes_visible()) {
  166. if (debug_shapes_to_update.empty()) {
  167. call_deferred("_update_debug_shapes");
  168. }
  169. debug_shapes_to_update.insert(p_owner);
  170. }
  171. }
  172. void CollisionObject::_shape_changed(const Ref<Shape> &p_shape) {
  173. for (Map<uint32_t, ShapeData>::Element *E = shapes.front(); E; E = E->next()) {
  174. ShapeData &shapedata = E->get();
  175. ShapeData::ShapeBase *shapes = shapedata.shapes.ptrw();
  176. for (int i = 0; i < shapedata.shapes.size(); i++) {
  177. ShapeData::ShapeBase &s = shapes[i];
  178. if (s.shape == p_shape && s.debug_shape.is_valid()) {
  179. Ref<Mesh> mesh = s.shape->get_debug_mesh();
  180. VS::get_singleton()->instance_set_base(s.debug_shape, mesh->get_rid());
  181. }
  182. }
  183. }
  184. }
  185. void CollisionObject::_update_debug_shapes() {
  186. if (!is_inside_tree()) {
  187. debug_shapes_to_update.clear();
  188. return;
  189. }
  190. for (Set<uint32_t>::Element *shapedata_idx = debug_shapes_to_update.front(); shapedata_idx; shapedata_idx = shapedata_idx->next()) {
  191. if (shapes.has(shapedata_idx->get())) {
  192. ShapeData &shapedata = shapes[shapedata_idx->get()];
  193. ShapeData::ShapeBase *shapes = shapedata.shapes.ptrw();
  194. for (int i = 0; i < shapedata.shapes.size(); i++) {
  195. ShapeData::ShapeBase &s = shapes[i];
  196. if (s.shape.is_null() || shapedata.disabled) {
  197. if (s.debug_shape.is_valid()) {
  198. VS::get_singleton()->free(s.debug_shape);
  199. s.debug_shape = RID();
  200. --debug_shapes_count;
  201. }
  202. }
  203. if (!s.debug_shape.is_valid()) {
  204. s.debug_shape = VS::get_singleton()->instance_create();
  205. VS::get_singleton()->instance_set_scenario(s.debug_shape, get_world()->get_scenario());
  206. if (!s.shape->is_connected("changed", this, "_shape_changed")) {
  207. s.shape->connect("changed", this, "_shape_changed", varray(s.shape), CONNECT_DEFERRED);
  208. }
  209. ++debug_shapes_count;
  210. }
  211. Ref<Mesh> mesh = s.shape->get_debug_mesh();
  212. VS::get_singleton()->instance_set_base(s.debug_shape, mesh->get_rid());
  213. VS::get_singleton()->instance_set_transform(s.debug_shape, get_global_transform() * shapedata.xform);
  214. }
  215. }
  216. }
  217. debug_shapes_to_update.clear();
  218. }
  219. void CollisionObject::_clear_debug_shapes() {
  220. for (Map<uint32_t, ShapeData>::Element *E = shapes.front(); E; E = E->next()) {
  221. ShapeData &shapedata = E->get();
  222. ShapeData::ShapeBase *shapes = shapedata.shapes.ptrw();
  223. for (int i = 0; i < shapedata.shapes.size(); i++) {
  224. ShapeData::ShapeBase &s = shapes[i];
  225. if (s.debug_shape.is_valid()) {
  226. VS::get_singleton()->free(s.debug_shape);
  227. s.debug_shape = RID();
  228. if (s.shape.is_valid() && s.shape->is_connected("changed", this, "_shape_changed")) {
  229. s.shape->disconnect("changed", this, "_shape_changed");
  230. }
  231. }
  232. }
  233. }
  234. debug_shapes_count = 0;
  235. }
  236. void CollisionObject::_on_transform_changed() {
  237. if (debug_shapes_count > 0 && !debug_shape_old_transform.is_equal_approx(get_global_transform())) {
  238. debug_shape_old_transform = get_global_transform();
  239. for (Map<uint32_t, ShapeData>::Element *E = shapes.front(); E; E = E->next()) {
  240. ShapeData &shapedata = E->get();
  241. const ShapeData::ShapeBase *shapes = shapedata.shapes.ptr();
  242. for (int i = 0; i < shapedata.shapes.size(); i++) {
  243. VS::get_singleton()->instance_set_transform(shapes[i].debug_shape, debug_shape_old_transform * shapedata.xform);
  244. }
  245. }
  246. }
  247. }
  248. void CollisionObject::set_ray_pickable(bool p_ray_pickable) {
  249. ray_pickable = p_ray_pickable;
  250. _update_pickable();
  251. }
  252. bool CollisionObject::is_ray_pickable() const {
  253. return ray_pickable;
  254. }
  255. void CollisionObject::_bind_methods() {
  256. ClassDB::bind_method(D_METHOD("set_collision_layer", "layer"), &CollisionObject::set_collision_layer);
  257. ClassDB::bind_method(D_METHOD("get_collision_layer"), &CollisionObject::get_collision_layer);
  258. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &CollisionObject::set_collision_mask);
  259. ClassDB::bind_method(D_METHOD("get_collision_mask"), &CollisionObject::get_collision_mask);
  260. ClassDB::bind_method(D_METHOD("set_collision_layer_bit", "bit", "value"), &CollisionObject::set_collision_layer_bit);
  261. ClassDB::bind_method(D_METHOD("get_collision_layer_bit", "bit"), &CollisionObject::get_collision_layer_bit);
  262. ClassDB::bind_method(D_METHOD("set_collision_mask_bit", "bit", "value"), &CollisionObject::set_collision_mask_bit);
  263. ClassDB::bind_method(D_METHOD("get_collision_mask_bit", "bit"), &CollisionObject::get_collision_mask_bit);
  264. ClassDB::bind_method(D_METHOD("set_ray_pickable", "ray_pickable"), &CollisionObject::set_ray_pickable);
  265. ClassDB::bind_method(D_METHOD("is_ray_pickable"), &CollisionObject::is_ray_pickable);
  266. ClassDB::bind_method(D_METHOD("set_capture_input_on_drag", "enable"), &CollisionObject::set_capture_input_on_drag);
  267. ClassDB::bind_method(D_METHOD("get_capture_input_on_drag"), &CollisionObject::get_capture_input_on_drag);
  268. ClassDB::bind_method(D_METHOD("get_rid"), &CollisionObject::get_rid);
  269. ClassDB::bind_method(D_METHOD("create_shape_owner", "owner"), &CollisionObject::create_shape_owner);
  270. ClassDB::bind_method(D_METHOD("remove_shape_owner", "owner_id"), &CollisionObject::remove_shape_owner);
  271. ClassDB::bind_method(D_METHOD("get_shape_owners"), &CollisionObject::_get_shape_owners);
  272. ClassDB::bind_method(D_METHOD("shape_owner_set_transform", "owner_id", "transform"), &CollisionObject::shape_owner_set_transform);
  273. ClassDB::bind_method(D_METHOD("shape_owner_get_transform", "owner_id"), &CollisionObject::shape_owner_get_transform);
  274. ClassDB::bind_method(D_METHOD("shape_owner_get_owner", "owner_id"), &CollisionObject::shape_owner_get_owner);
  275. ClassDB::bind_method(D_METHOD("shape_owner_set_disabled", "owner_id", "disabled"), &CollisionObject::shape_owner_set_disabled);
  276. ClassDB::bind_method(D_METHOD("is_shape_owner_disabled", "owner_id"), &CollisionObject::is_shape_owner_disabled);
  277. ClassDB::bind_method(D_METHOD("shape_owner_add_shape", "owner_id", "shape"), &CollisionObject::shape_owner_add_shape);
  278. ClassDB::bind_method(D_METHOD("shape_owner_get_shape_count", "owner_id"), &CollisionObject::shape_owner_get_shape_count);
  279. ClassDB::bind_method(D_METHOD("shape_owner_get_shape", "owner_id", "shape_id"), &CollisionObject::shape_owner_get_shape);
  280. ClassDB::bind_method(D_METHOD("shape_owner_get_shape_index", "owner_id", "shape_id"), &CollisionObject::shape_owner_get_shape_index);
  281. ClassDB::bind_method(D_METHOD("shape_owner_remove_shape", "owner_id", "shape_id"), &CollisionObject::shape_owner_remove_shape);
  282. ClassDB::bind_method(D_METHOD("shape_owner_clear_shapes", "owner_id"), &CollisionObject::shape_owner_clear_shapes);
  283. ClassDB::bind_method(D_METHOD("shape_find_owner", "shape_index"), &CollisionObject::shape_find_owner);
  284. ClassDB::bind_method(D_METHOD("_update_debug_shapes"), &CollisionObject::_update_debug_shapes);
  285. ClassDB::bind_method(D_METHOD("_shape_changed", "shape"), &CollisionObject::_shape_changed);
  286. 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")));
  287. 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")));
  288. ADD_SIGNAL(MethodInfo("mouse_entered"));
  289. ADD_SIGNAL(MethodInfo("mouse_exited"));
  290. ADD_GROUP("Collision", "collision_");
  291. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_layer", "get_collision_layer");
  292. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
  293. ADD_GROUP("Input", "input_");
  294. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "input_ray_pickable"), "set_ray_pickable", "is_ray_pickable");
  295. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "input_capture_on_drag"), "set_capture_input_on_drag", "get_capture_input_on_drag");
  296. }
  297. uint32_t CollisionObject::create_shape_owner(Object *p_owner) {
  298. ShapeData sd;
  299. uint32_t id;
  300. if (shapes.size() == 0) {
  301. id = 0;
  302. } else {
  303. id = shapes.back()->key() + 1;
  304. }
  305. sd.owner = p_owner;
  306. shapes[id] = sd;
  307. return id;
  308. }
  309. void CollisionObject::remove_shape_owner(uint32_t owner) {
  310. ERR_FAIL_COND(!shapes.has(owner));
  311. shape_owner_clear_shapes(owner);
  312. shapes.erase(owner);
  313. }
  314. void CollisionObject::shape_owner_set_disabled(uint32_t p_owner, bool p_disabled) {
  315. ERR_FAIL_COND(!shapes.has(p_owner));
  316. ShapeData &sd = shapes[p_owner];
  317. if (sd.disabled == p_disabled) {
  318. return;
  319. }
  320. sd.disabled = p_disabled;
  321. for (int i = 0; i < sd.shapes.size(); i++) {
  322. if (area) {
  323. PhysicsServer::get_singleton()->area_set_shape_disabled(rid, sd.shapes[i].index, p_disabled);
  324. } else {
  325. PhysicsServer::get_singleton()->body_set_shape_disabled(rid, sd.shapes[i].index, p_disabled);
  326. }
  327. }
  328. _update_shape_data(p_owner);
  329. }
  330. bool CollisionObject::is_shape_owner_disabled(uint32_t p_owner) const {
  331. ERR_FAIL_COND_V(!shapes.has(p_owner), false);
  332. return shapes[p_owner].disabled;
  333. }
  334. void CollisionObject::get_shape_owners(List<uint32_t> *r_owners) {
  335. for (Map<uint32_t, ShapeData>::Element *E = shapes.front(); E; E = E->next()) {
  336. r_owners->push_back(E->key());
  337. }
  338. }
  339. Array CollisionObject::_get_shape_owners() {
  340. Array ret;
  341. for (Map<uint32_t, ShapeData>::Element *E = shapes.front(); E; E = E->next()) {
  342. ret.push_back(E->key());
  343. }
  344. return ret;
  345. }
  346. void CollisionObject::shape_owner_set_transform(uint32_t p_owner, const Transform &p_transform) {
  347. ERR_FAIL_COND(!shapes.has(p_owner));
  348. ShapeData &sd = shapes[p_owner];
  349. sd.xform = p_transform;
  350. for (int i = 0; i < sd.shapes.size(); i++) {
  351. if (area) {
  352. PhysicsServer::get_singleton()->area_set_shape_transform(rid, sd.shapes[i].index, p_transform);
  353. } else {
  354. PhysicsServer::get_singleton()->body_set_shape_transform(rid, sd.shapes[i].index, p_transform);
  355. }
  356. }
  357. _update_shape_data(p_owner);
  358. }
  359. Transform CollisionObject::shape_owner_get_transform(uint32_t p_owner) const {
  360. ERR_FAIL_COND_V(!shapes.has(p_owner), Transform());
  361. return shapes[p_owner].xform;
  362. }
  363. Object *CollisionObject::shape_owner_get_owner(uint32_t p_owner) const {
  364. ERR_FAIL_COND_V(!shapes.has(p_owner), nullptr);
  365. return shapes[p_owner].owner;
  366. }
  367. void CollisionObject::shape_owner_add_shape(uint32_t p_owner, const Ref<Shape> &p_shape) {
  368. ERR_FAIL_COND(!shapes.has(p_owner));
  369. ERR_FAIL_COND(p_shape.is_null());
  370. ShapeData &sd = shapes[p_owner];
  371. ShapeData::ShapeBase s;
  372. s.index = total_subshapes;
  373. s.shape = p_shape;
  374. if (area) {
  375. PhysicsServer::get_singleton()->area_add_shape(rid, p_shape->get_rid(), sd.xform, sd.disabled);
  376. } else {
  377. PhysicsServer::get_singleton()->body_add_shape(rid, p_shape->get_rid(), sd.xform, sd.disabled);
  378. }
  379. sd.shapes.push_back(s);
  380. total_subshapes++;
  381. _update_shape_data(p_owner);
  382. }
  383. int CollisionObject::shape_owner_get_shape_count(uint32_t p_owner) const {
  384. ERR_FAIL_COND_V(!shapes.has(p_owner), 0);
  385. return shapes[p_owner].shapes.size();
  386. }
  387. Ref<Shape> CollisionObject::shape_owner_get_shape(uint32_t p_owner, int p_shape) const {
  388. ERR_FAIL_COND_V(!shapes.has(p_owner), Ref<Shape>());
  389. ERR_FAIL_INDEX_V(p_shape, shapes[p_owner].shapes.size(), Ref<Shape>());
  390. return shapes[p_owner].shapes[p_shape].shape;
  391. }
  392. int CollisionObject::shape_owner_get_shape_index(uint32_t p_owner, int p_shape) const {
  393. ERR_FAIL_COND_V(!shapes.has(p_owner), -1);
  394. ERR_FAIL_INDEX_V(p_shape, shapes[p_owner].shapes.size(), -1);
  395. return shapes[p_owner].shapes[p_shape].index;
  396. }
  397. void CollisionObject::shape_owner_remove_shape(uint32_t p_owner, int p_shape) {
  398. ERR_FAIL_COND(!shapes.has(p_owner));
  399. ERR_FAIL_INDEX(p_shape, shapes[p_owner].shapes.size());
  400. ShapeData::ShapeBase &s = shapes[p_owner].shapes.write[p_shape];
  401. int index_to_remove = s.index;
  402. if (area) {
  403. PhysicsServer::get_singleton()->area_remove_shape(rid, index_to_remove);
  404. } else {
  405. PhysicsServer::get_singleton()->body_remove_shape(rid, index_to_remove);
  406. }
  407. if (s.debug_shape.is_valid()) {
  408. VS::get_singleton()->free(s.debug_shape);
  409. if (s.shape.is_valid() && s.shape->is_connected("changed", this, "_shape_changed")) {
  410. s.shape->disconnect("changed", this, "_shape_changed");
  411. }
  412. --debug_shapes_count;
  413. }
  414. shapes[p_owner].shapes.remove(p_shape);
  415. for (Map<uint32_t, ShapeData>::Element *E = shapes.front(); E; E = E->next()) {
  416. for (int i = 0; i < E->get().shapes.size(); i++) {
  417. if (E->get().shapes[i].index > index_to_remove) {
  418. E->get().shapes.write[i].index -= 1;
  419. }
  420. }
  421. }
  422. total_subshapes--;
  423. }
  424. void CollisionObject::shape_owner_clear_shapes(uint32_t p_owner) {
  425. ERR_FAIL_COND(!shapes.has(p_owner));
  426. while (shape_owner_get_shape_count(p_owner) > 0) {
  427. shape_owner_remove_shape(p_owner, 0);
  428. }
  429. }
  430. uint32_t CollisionObject::shape_find_owner(int p_shape_index) const {
  431. ERR_FAIL_INDEX_V(p_shape_index, total_subshapes, 0);
  432. for (const Map<uint32_t, ShapeData>::Element *E = shapes.front(); E; E = E->next()) {
  433. for (int i = 0; i < E->get().shapes.size(); i++) {
  434. if (E->get().shapes[i].index == p_shape_index) {
  435. return E->key();
  436. }
  437. }
  438. }
  439. //in theory it should be unreachable
  440. return 0;
  441. }
  442. CollisionObject::CollisionObject(RID p_rid, bool p_area) {
  443. rid = p_rid;
  444. area = p_area;
  445. capture_input_on_drag = false;
  446. ray_pickable = true;
  447. set_notify_transform(true);
  448. total_subshapes = 0;
  449. if (p_area) {
  450. PhysicsServer::get_singleton()->area_attach_object_instance_id(rid, get_instance_id());
  451. } else {
  452. PhysicsServer::get_singleton()->body_attach_object_instance_id(rid, get_instance_id());
  453. }
  454. //set_transform_notify(true);
  455. }
  456. void CollisionObject::set_capture_input_on_drag(bool p_capture) {
  457. capture_input_on_drag = p_capture;
  458. }
  459. bool CollisionObject::get_capture_input_on_drag() const {
  460. return capture_input_on_drag;
  461. }
  462. String CollisionObject::get_configuration_warning() const {
  463. String warning = Spatial::get_configuration_warning();
  464. if (shapes.empty()) {
  465. if (!warning.empty()) {
  466. warning += "\n\n";
  467. }
  468. 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.");
  469. }
  470. return warning;
  471. }
  472. CollisionObject::CollisionObject() {
  473. capture_input_on_drag = false;
  474. ray_pickable = true;
  475. set_notify_transform(true);
  476. //owner=
  477. //set_transform_notify(true);
  478. }
  479. CollisionObject::~CollisionObject() {
  480. PhysicsServer::get_singleton()->free(rid);
  481. }