collision_object.cpp 22 KB

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