collision_object_3d.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. /**************************************************************************/
  2. /* collision_object_3d.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_3d.h"
  31. #include "scene/resources/3d/shape_3d.h"
  32. void CollisionObject3D::_notification(int p_what) {
  33. switch (p_what) {
  34. case NOTIFICATION_ENTER_TREE: {
  35. if (_are_collision_shapes_visible()) {
  36. debug_shape_old_transform = get_global_transform();
  37. for (const KeyValue<uint32_t, ShapeData> &E : shapes) {
  38. debug_shapes_to_update.insert(E.key);
  39. }
  40. _update_debug_shapes();
  41. }
  42. #ifdef TOOLS_ENABLED
  43. if (Engine::get_singleton()->is_editor_hint()) {
  44. set_notify_local_transform(true); // Used for warnings and only in editor.
  45. }
  46. #endif
  47. } break;
  48. case NOTIFICATION_EXIT_TREE: {
  49. if (debug_shapes_count > 0) {
  50. _clear_debug_shapes();
  51. }
  52. } break;
  53. case NOTIFICATION_ENTER_WORLD: {
  54. if (area) {
  55. PhysicsServer3D::get_singleton()->area_set_transform(rid, get_global_transform());
  56. } else {
  57. PhysicsServer3D::get_singleton()->body_set_state(rid, PhysicsServer3D::BODY_STATE_TRANSFORM, get_global_transform());
  58. }
  59. bool disabled = !is_enabled();
  60. if (disabled && (disable_mode != DISABLE_MODE_REMOVE)) {
  61. _apply_disabled();
  62. }
  63. if (!disabled || (disable_mode != DISABLE_MODE_REMOVE)) {
  64. Ref<World3D> world_ref = get_world_3d();
  65. ERR_FAIL_COND(!world_ref.is_valid());
  66. RID space = world_ref->get_space();
  67. if (area) {
  68. PhysicsServer3D::get_singleton()->area_set_space(rid, space);
  69. } else {
  70. PhysicsServer3D::get_singleton()->body_set_space(rid, space);
  71. }
  72. _space_changed(space);
  73. }
  74. _update_pickable();
  75. } break;
  76. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  77. update_configuration_warnings();
  78. } break;
  79. case NOTIFICATION_TRANSFORM_CHANGED: {
  80. if (only_update_transform_changes) {
  81. return;
  82. }
  83. if (area) {
  84. PhysicsServer3D::get_singleton()->area_set_transform(rid, get_global_transform());
  85. } else {
  86. PhysicsServer3D::get_singleton()->body_set_state(rid, PhysicsServer3D::BODY_STATE_TRANSFORM, get_global_transform());
  87. }
  88. _on_transform_changed();
  89. } break;
  90. case NOTIFICATION_VISIBILITY_CHANGED: {
  91. _update_pickable();
  92. } break;
  93. case NOTIFICATION_EXIT_WORLD: {
  94. bool disabled = !is_enabled();
  95. if (!disabled || (disable_mode != DISABLE_MODE_REMOVE)) {
  96. if (callback_lock > 0) {
  97. ERR_PRINT("Removing a CollisionObject node during a physics callback is not allowed and will cause undesired behavior. Remove with call_deferred() instead.");
  98. } else {
  99. if (area) {
  100. PhysicsServer3D::get_singleton()->area_set_space(rid, RID());
  101. } else {
  102. PhysicsServer3D::get_singleton()->body_set_space(rid, RID());
  103. }
  104. _space_changed(RID());
  105. }
  106. }
  107. if (disabled && (disable_mode != DISABLE_MODE_REMOVE)) {
  108. _apply_enabled();
  109. }
  110. } break;
  111. case NOTIFICATION_DISABLED: {
  112. _apply_disabled();
  113. } break;
  114. case NOTIFICATION_ENABLED: {
  115. _apply_enabled();
  116. } break;
  117. }
  118. }
  119. void CollisionObject3D::set_collision_layer(uint32_t p_layer) {
  120. collision_layer = p_layer;
  121. if (area) {
  122. PhysicsServer3D::get_singleton()->area_set_collision_layer(get_rid(), p_layer);
  123. } else {
  124. PhysicsServer3D::get_singleton()->body_set_collision_layer(get_rid(), p_layer);
  125. }
  126. }
  127. uint32_t CollisionObject3D::get_collision_layer() const {
  128. return collision_layer;
  129. }
  130. void CollisionObject3D::set_collision_mask(uint32_t p_mask) {
  131. collision_mask = p_mask;
  132. if (area) {
  133. PhysicsServer3D::get_singleton()->area_set_collision_mask(get_rid(), p_mask);
  134. } else {
  135. PhysicsServer3D::get_singleton()->body_set_collision_mask(get_rid(), p_mask);
  136. }
  137. }
  138. uint32_t CollisionObject3D::get_collision_mask() const {
  139. return collision_mask;
  140. }
  141. void CollisionObject3D::set_collision_layer_value(int p_layer_number, bool p_value) {
  142. ERR_FAIL_COND_MSG(p_layer_number < 1, "Collision layer number must be between 1 and 32 inclusive.");
  143. ERR_FAIL_COND_MSG(p_layer_number > 32, "Collision layer number must be between 1 and 32 inclusive.");
  144. uint32_t collision_layer_new = get_collision_layer();
  145. if (p_value) {
  146. collision_layer_new |= 1 << (p_layer_number - 1);
  147. } else {
  148. collision_layer_new &= ~(1 << (p_layer_number - 1));
  149. }
  150. set_collision_layer(collision_layer_new);
  151. }
  152. bool CollisionObject3D::get_collision_layer_value(int p_layer_number) const {
  153. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Collision layer number must be between 1 and 32 inclusive.");
  154. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Collision layer number must be between 1 and 32 inclusive.");
  155. return get_collision_layer() & (1 << (p_layer_number - 1));
  156. }
  157. void CollisionObject3D::set_collision_mask_value(int p_layer_number, bool p_value) {
  158. ERR_FAIL_COND_MSG(p_layer_number < 1, "Collision layer number must be between 1 and 32 inclusive.");
  159. ERR_FAIL_COND_MSG(p_layer_number > 32, "Collision layer number must be between 1 and 32 inclusive.");
  160. uint32_t mask = get_collision_mask();
  161. if (p_value) {
  162. mask |= 1 << (p_layer_number - 1);
  163. } else {
  164. mask &= ~(1 << (p_layer_number - 1));
  165. }
  166. set_collision_mask(mask);
  167. }
  168. bool CollisionObject3D::get_collision_mask_value(int p_layer_number) const {
  169. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Collision layer number must be between 1 and 32 inclusive.");
  170. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Collision layer number must be between 1 and 32 inclusive.");
  171. return get_collision_mask() & (1 << (p_layer_number - 1));
  172. }
  173. void CollisionObject3D::set_collision_priority(real_t p_priority) {
  174. collision_priority = p_priority;
  175. if (!area) {
  176. PhysicsServer3D::get_singleton()->body_set_collision_priority(get_rid(), p_priority);
  177. }
  178. }
  179. real_t CollisionObject3D::get_collision_priority() const {
  180. return collision_priority;
  181. }
  182. void CollisionObject3D::set_disable_mode(DisableMode p_mode) {
  183. if (disable_mode == p_mode) {
  184. return;
  185. }
  186. bool disabled = is_inside_tree() && !is_enabled();
  187. if (disabled) {
  188. // Cancel previous disable mode.
  189. _apply_enabled();
  190. }
  191. disable_mode = p_mode;
  192. if (disabled) {
  193. // Apply new disable mode.
  194. _apply_disabled();
  195. }
  196. }
  197. CollisionObject3D::DisableMode CollisionObject3D::get_disable_mode() const {
  198. return disable_mode;
  199. }
  200. void CollisionObject3D::_apply_disabled() {
  201. switch (disable_mode) {
  202. case DISABLE_MODE_REMOVE: {
  203. if (is_inside_tree()) {
  204. if (callback_lock > 0) {
  205. ERR_PRINT("Disabling a CollisionObject node during a physics callback is not allowed and will cause undesired behavior. Disable with call_deferred() instead.");
  206. } else {
  207. if (area) {
  208. PhysicsServer3D::get_singleton()->area_set_space(rid, RID());
  209. } else {
  210. PhysicsServer3D::get_singleton()->body_set_space(rid, RID());
  211. }
  212. _space_changed(RID());
  213. }
  214. }
  215. } break;
  216. case DISABLE_MODE_MAKE_STATIC: {
  217. if (!area && (body_mode != PhysicsServer3D::BODY_MODE_STATIC)) {
  218. PhysicsServer3D::get_singleton()->body_set_mode(rid, PhysicsServer3D::BODY_MODE_STATIC);
  219. }
  220. } break;
  221. case DISABLE_MODE_KEEP_ACTIVE: {
  222. // Nothing to do.
  223. } break;
  224. }
  225. }
  226. void CollisionObject3D::_apply_enabled() {
  227. switch (disable_mode) {
  228. case DISABLE_MODE_REMOVE: {
  229. if (is_inside_tree()) {
  230. RID space = get_world_3d()->get_space();
  231. if (area) {
  232. PhysicsServer3D::get_singleton()->area_set_space(rid, space);
  233. } else {
  234. PhysicsServer3D::get_singleton()->body_set_space(rid, space);
  235. }
  236. _space_changed(space);
  237. }
  238. } break;
  239. case DISABLE_MODE_MAKE_STATIC: {
  240. if (!area && (body_mode != PhysicsServer3D::BODY_MODE_STATIC)) {
  241. PhysicsServer3D::get_singleton()->body_set_mode(rid, body_mode);
  242. }
  243. } break;
  244. case DISABLE_MODE_KEEP_ACTIVE: {
  245. // Nothing to do.
  246. } break;
  247. }
  248. }
  249. void CollisionObject3D::_input_event_call(Camera3D *p_camera, const Ref<InputEvent> &p_input_event, const Vector3 &p_pos, const Vector3 &p_normal, int p_shape) {
  250. GDVIRTUAL_CALL(_input_event, p_camera, p_input_event, p_pos, p_normal, p_shape);
  251. emit_signal(SceneStringName(input_event), p_camera, p_input_event, p_pos, p_normal, p_shape);
  252. }
  253. void CollisionObject3D::_mouse_enter() {
  254. GDVIRTUAL_CALL(_mouse_enter);
  255. emit_signal(SceneStringName(mouse_entered));
  256. }
  257. void CollisionObject3D::_mouse_exit() {
  258. GDVIRTUAL_CALL(_mouse_exit);
  259. emit_signal(SceneStringName(mouse_exited));
  260. }
  261. void CollisionObject3D::set_body_mode(PhysicsServer3D::BodyMode p_mode) {
  262. ERR_FAIL_COND(area);
  263. if (body_mode == p_mode) {
  264. return;
  265. }
  266. body_mode = p_mode;
  267. if (is_inside_tree() && !is_enabled() && (disable_mode == DISABLE_MODE_MAKE_STATIC)) {
  268. return;
  269. }
  270. PhysicsServer3D::get_singleton()->body_set_mode(rid, p_mode);
  271. }
  272. void CollisionObject3D::_space_changed(const RID &p_new_space) {
  273. }
  274. void CollisionObject3D::set_only_update_transform_changes(bool p_enable) {
  275. only_update_transform_changes = p_enable;
  276. }
  277. bool CollisionObject3D::is_only_update_transform_changes_enabled() const {
  278. return only_update_transform_changes;
  279. }
  280. void CollisionObject3D::_update_pickable() {
  281. if (!is_inside_tree()) {
  282. return;
  283. }
  284. bool pickable = ray_pickable && is_visible_in_tree();
  285. if (area) {
  286. PhysicsServer3D::get_singleton()->area_set_ray_pickable(rid, pickable);
  287. } else {
  288. PhysicsServer3D::get_singleton()->body_set_ray_pickable(rid, pickable);
  289. }
  290. }
  291. bool CollisionObject3D::_are_collision_shapes_visible() {
  292. return is_inside_tree() && get_tree()->is_debugging_collisions_hint() && !Engine::get_singleton()->is_editor_hint();
  293. }
  294. void CollisionObject3D::_update_shape_data(uint32_t p_owner) {
  295. if (_are_collision_shapes_visible()) {
  296. if (debug_shapes_to_update.is_empty()) {
  297. callable_mp(this, &CollisionObject3D::_update_debug_shapes).call_deferred();
  298. }
  299. debug_shapes_to_update.insert(p_owner);
  300. }
  301. }
  302. void CollisionObject3D::_shape_changed(const Ref<Shape3D> &p_shape) {
  303. for (KeyValue<uint32_t, ShapeData> &E : shapes) {
  304. ShapeData &shapedata = E.value;
  305. ShapeData::ShapeBase *shape_bases = shapedata.shapes.ptrw();
  306. for (int i = 0; i < shapedata.shapes.size(); i++) {
  307. ShapeData::ShapeBase &s = shape_bases[i];
  308. if (s.shape == p_shape && s.debug_shape.is_valid()) {
  309. Ref<Mesh> mesh = s.shape->get_debug_mesh();
  310. RS::get_singleton()->instance_set_base(s.debug_shape, mesh->get_rid());
  311. }
  312. }
  313. }
  314. }
  315. void CollisionObject3D::_update_debug_shapes() {
  316. ERR_FAIL_NULL(RenderingServer::get_singleton());
  317. if (!is_inside_tree()) {
  318. debug_shapes_to_update.clear();
  319. return;
  320. }
  321. for (const uint32_t &shapedata_idx : debug_shapes_to_update) {
  322. if (shapes.has(shapedata_idx)) {
  323. ShapeData &shapedata = shapes[shapedata_idx];
  324. ShapeData::ShapeBase *shape_bases = shapedata.shapes.ptrw();
  325. for (int i = 0; i < shapedata.shapes.size(); i++) {
  326. ShapeData::ShapeBase &s = shape_bases[i];
  327. if (s.shape.is_null() || shapedata.disabled) {
  328. if (s.debug_shape.is_valid()) {
  329. RS::get_singleton()->free(s.debug_shape);
  330. s.debug_shape = RID();
  331. --debug_shapes_count;
  332. }
  333. continue;
  334. }
  335. if (s.debug_shape.is_null()) {
  336. s.debug_shape = RS::get_singleton()->instance_create();
  337. RS::get_singleton()->instance_set_scenario(s.debug_shape, get_world_3d()->get_scenario());
  338. s.shape->connect_changed(callable_mp(this, &CollisionObject3D::_shape_changed).bind(s.shape), CONNECT_DEFERRED);
  339. ++debug_shapes_count;
  340. }
  341. Ref<Mesh> mesh = s.shape->get_debug_mesh();
  342. RS::get_singleton()->instance_set_base(s.debug_shape, mesh->get_rid());
  343. RS::get_singleton()->instance_set_transform(s.debug_shape, get_global_transform() * shapedata.xform);
  344. }
  345. }
  346. }
  347. debug_shapes_to_update.clear();
  348. }
  349. void CollisionObject3D::_clear_debug_shapes() {
  350. ERR_FAIL_NULL(RenderingServer::get_singleton());
  351. for (KeyValue<uint32_t, ShapeData> &E : shapes) {
  352. ShapeData &shapedata = E.value;
  353. ShapeData::ShapeBase *shape_bases = shapedata.shapes.ptrw();
  354. for (int i = 0; i < shapedata.shapes.size(); i++) {
  355. ShapeData::ShapeBase &s = shape_bases[i];
  356. if (s.debug_shape.is_valid()) {
  357. RS::get_singleton()->free(s.debug_shape);
  358. s.debug_shape = RID();
  359. if (s.shape.is_valid()) {
  360. s.shape->disconnect_changed(callable_mp(this, &CollisionObject3D::_update_shape_data));
  361. }
  362. }
  363. }
  364. }
  365. debug_shapes_count = 0;
  366. }
  367. void CollisionObject3D::_on_transform_changed() {
  368. if (debug_shapes_count > 0 && !debug_shape_old_transform.is_equal_approx(get_global_transform())) {
  369. debug_shape_old_transform = get_global_transform();
  370. for (KeyValue<uint32_t, ShapeData> &E : shapes) {
  371. ShapeData &shapedata = E.value;
  372. if (shapedata.disabled) {
  373. continue; // If disabled then there are no debug shapes to update.
  374. }
  375. const ShapeData::ShapeBase *shape_bases = shapedata.shapes.ptr();
  376. for (int i = 0; i < shapedata.shapes.size(); i++) {
  377. if (shape_bases[i].debug_shape.is_null()) {
  378. continue;
  379. }
  380. RS::get_singleton()->instance_set_transform(shape_bases[i].debug_shape, debug_shape_old_transform * shapedata.xform);
  381. }
  382. }
  383. }
  384. }
  385. void CollisionObject3D::set_ray_pickable(bool p_ray_pickable) {
  386. ray_pickable = p_ray_pickable;
  387. _update_pickable();
  388. }
  389. bool CollisionObject3D::is_ray_pickable() const {
  390. return ray_pickable;
  391. }
  392. void CollisionObject3D::_bind_methods() {
  393. ClassDB::bind_method(D_METHOD("set_collision_layer", "layer"), &CollisionObject3D::set_collision_layer);
  394. ClassDB::bind_method(D_METHOD("get_collision_layer"), &CollisionObject3D::get_collision_layer);
  395. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &CollisionObject3D::set_collision_mask);
  396. ClassDB::bind_method(D_METHOD("get_collision_mask"), &CollisionObject3D::get_collision_mask);
  397. ClassDB::bind_method(D_METHOD("set_collision_layer_value", "layer_number", "value"), &CollisionObject3D::set_collision_layer_value);
  398. ClassDB::bind_method(D_METHOD("get_collision_layer_value", "layer_number"), &CollisionObject3D::get_collision_layer_value);
  399. ClassDB::bind_method(D_METHOD("set_collision_mask_value", "layer_number", "value"), &CollisionObject3D::set_collision_mask_value);
  400. ClassDB::bind_method(D_METHOD("get_collision_mask_value", "layer_number"), &CollisionObject3D::get_collision_mask_value);
  401. ClassDB::bind_method(D_METHOD("set_collision_priority", "priority"), &CollisionObject3D::set_collision_priority);
  402. ClassDB::bind_method(D_METHOD("get_collision_priority"), &CollisionObject3D::get_collision_priority);
  403. ClassDB::bind_method(D_METHOD("set_disable_mode", "mode"), &CollisionObject3D::set_disable_mode);
  404. ClassDB::bind_method(D_METHOD("get_disable_mode"), &CollisionObject3D::get_disable_mode);
  405. ClassDB::bind_method(D_METHOD("set_ray_pickable", "ray_pickable"), &CollisionObject3D::set_ray_pickable);
  406. ClassDB::bind_method(D_METHOD("is_ray_pickable"), &CollisionObject3D::is_ray_pickable);
  407. ClassDB::bind_method(D_METHOD("set_capture_input_on_drag", "enable"), &CollisionObject3D::set_capture_input_on_drag);
  408. ClassDB::bind_method(D_METHOD("get_capture_input_on_drag"), &CollisionObject3D::get_capture_input_on_drag);
  409. ClassDB::bind_method(D_METHOD("get_rid"), &CollisionObject3D::get_rid);
  410. ClassDB::bind_method(D_METHOD("create_shape_owner", "owner"), &CollisionObject3D::create_shape_owner);
  411. ClassDB::bind_method(D_METHOD("remove_shape_owner", "owner_id"), &CollisionObject3D::remove_shape_owner);
  412. ClassDB::bind_method(D_METHOD("get_shape_owners"), &CollisionObject3D::_get_shape_owners);
  413. ClassDB::bind_method(D_METHOD("shape_owner_set_transform", "owner_id", "transform"), &CollisionObject3D::shape_owner_set_transform);
  414. ClassDB::bind_method(D_METHOD("shape_owner_get_transform", "owner_id"), &CollisionObject3D::shape_owner_get_transform);
  415. ClassDB::bind_method(D_METHOD("shape_owner_get_owner", "owner_id"), &CollisionObject3D::shape_owner_get_owner);
  416. ClassDB::bind_method(D_METHOD("shape_owner_set_disabled", "owner_id", "disabled"), &CollisionObject3D::shape_owner_set_disabled);
  417. ClassDB::bind_method(D_METHOD("is_shape_owner_disabled", "owner_id"), &CollisionObject3D::is_shape_owner_disabled);
  418. ClassDB::bind_method(D_METHOD("shape_owner_add_shape", "owner_id", "shape"), &CollisionObject3D::shape_owner_add_shape);
  419. ClassDB::bind_method(D_METHOD("shape_owner_get_shape_count", "owner_id"), &CollisionObject3D::shape_owner_get_shape_count);
  420. ClassDB::bind_method(D_METHOD("shape_owner_get_shape", "owner_id", "shape_id"), &CollisionObject3D::shape_owner_get_shape);
  421. ClassDB::bind_method(D_METHOD("shape_owner_get_shape_index", "owner_id", "shape_id"), &CollisionObject3D::shape_owner_get_shape_index);
  422. ClassDB::bind_method(D_METHOD("shape_owner_remove_shape", "owner_id", "shape_id"), &CollisionObject3D::shape_owner_remove_shape);
  423. ClassDB::bind_method(D_METHOD("shape_owner_clear_shapes", "owner_id"), &CollisionObject3D::shape_owner_clear_shapes);
  424. ClassDB::bind_method(D_METHOD("shape_find_owner", "shape_index"), &CollisionObject3D::shape_find_owner);
  425. GDVIRTUAL_BIND(_input_event, "camera", "event", "event_position", "normal", "shape_idx");
  426. GDVIRTUAL_BIND(_mouse_enter);
  427. GDVIRTUAL_BIND(_mouse_exit);
  428. 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, "event_position"), PropertyInfo(Variant::VECTOR3, "normal"), PropertyInfo(Variant::INT, "shape_idx")));
  429. ADD_SIGNAL(MethodInfo("mouse_entered"));
  430. ADD_SIGNAL(MethodInfo("mouse_exited"));
  431. ADD_PROPERTY(PropertyInfo(Variant::INT, "disable_mode", PROPERTY_HINT_ENUM, "Remove,Make Static,Keep Active"), "set_disable_mode", "get_disable_mode");
  432. ADD_GROUP("Collision", "collision_");
  433. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_layer", "get_collision_layer");
  434. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
  435. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "collision_priority"), "set_collision_priority", "get_collision_priority");
  436. ADD_GROUP("Input", "input_");
  437. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "input_ray_pickable"), "set_ray_pickable", "is_ray_pickable");
  438. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "input_capture_on_drag"), "set_capture_input_on_drag", "get_capture_input_on_drag");
  439. BIND_ENUM_CONSTANT(DISABLE_MODE_REMOVE);
  440. BIND_ENUM_CONSTANT(DISABLE_MODE_MAKE_STATIC);
  441. BIND_ENUM_CONSTANT(DISABLE_MODE_KEEP_ACTIVE);
  442. }
  443. uint32_t CollisionObject3D::create_shape_owner(Object *p_owner) {
  444. ShapeData sd;
  445. uint32_t id;
  446. if (shapes.size() == 0) {
  447. id = 0;
  448. } else {
  449. id = shapes.back()->key() + 1;
  450. }
  451. sd.owner_id = p_owner ? p_owner->get_instance_id() : ObjectID();
  452. shapes[id] = sd;
  453. return id;
  454. }
  455. void CollisionObject3D::remove_shape_owner(uint32_t owner) {
  456. ERR_FAIL_COND(!shapes.has(owner));
  457. shape_owner_clear_shapes(owner);
  458. shapes.erase(owner);
  459. }
  460. void CollisionObject3D::shape_owner_set_disabled(uint32_t p_owner, bool p_disabled) {
  461. ERR_FAIL_COND(!shapes.has(p_owner));
  462. ShapeData &sd = shapes[p_owner];
  463. if (sd.disabled == p_disabled) {
  464. return;
  465. }
  466. sd.disabled = p_disabled;
  467. for (int i = 0; i < sd.shapes.size(); i++) {
  468. if (area) {
  469. PhysicsServer3D::get_singleton()->area_set_shape_disabled(rid, sd.shapes[i].index, p_disabled);
  470. } else {
  471. PhysicsServer3D::get_singleton()->body_set_shape_disabled(rid, sd.shapes[i].index, p_disabled);
  472. }
  473. }
  474. _update_shape_data(p_owner);
  475. }
  476. bool CollisionObject3D::is_shape_owner_disabled(uint32_t p_owner) const {
  477. ERR_FAIL_COND_V(!shapes.has(p_owner), false);
  478. return shapes[p_owner].disabled;
  479. }
  480. void CollisionObject3D::get_shape_owners(List<uint32_t> *r_owners) {
  481. for (const KeyValue<uint32_t, ShapeData> &E : shapes) {
  482. r_owners->push_back(E.key);
  483. }
  484. }
  485. PackedInt32Array CollisionObject3D::_get_shape_owners() {
  486. PackedInt32Array ret;
  487. for (const KeyValue<uint32_t, ShapeData> &E : shapes) {
  488. ret.push_back(E.key);
  489. }
  490. return ret;
  491. }
  492. void CollisionObject3D::shape_owner_set_transform(uint32_t p_owner, const Transform3D &p_transform) {
  493. ERR_FAIL_COND(!shapes.has(p_owner));
  494. ShapeData &sd = shapes[p_owner];
  495. sd.xform = p_transform;
  496. for (int i = 0; i < sd.shapes.size(); i++) {
  497. if (area) {
  498. PhysicsServer3D::get_singleton()->area_set_shape_transform(rid, sd.shapes[i].index, p_transform);
  499. } else {
  500. PhysicsServer3D::get_singleton()->body_set_shape_transform(rid, sd.shapes[i].index, p_transform);
  501. }
  502. }
  503. _update_shape_data(p_owner);
  504. }
  505. Transform3D CollisionObject3D::shape_owner_get_transform(uint32_t p_owner) const {
  506. ERR_FAIL_COND_V(!shapes.has(p_owner), Transform3D());
  507. return shapes[p_owner].xform;
  508. }
  509. Object *CollisionObject3D::shape_owner_get_owner(uint32_t p_owner) const {
  510. ERR_FAIL_COND_V(!shapes.has(p_owner), nullptr);
  511. return ObjectDB::get_instance(shapes[p_owner].owner_id);
  512. }
  513. void CollisionObject3D::shape_owner_add_shape(uint32_t p_owner, const Ref<Shape3D> &p_shape) {
  514. ERR_FAIL_COND(!shapes.has(p_owner));
  515. ERR_FAIL_COND(p_shape.is_null());
  516. ShapeData &sd = shapes[p_owner];
  517. ShapeData::ShapeBase s;
  518. s.index = total_subshapes;
  519. s.shape = p_shape;
  520. if (area) {
  521. PhysicsServer3D::get_singleton()->area_add_shape(rid, p_shape->get_rid(), sd.xform, sd.disabled);
  522. } else {
  523. PhysicsServer3D::get_singleton()->body_add_shape(rid, p_shape->get_rid(), sd.xform, sd.disabled);
  524. }
  525. sd.shapes.push_back(s);
  526. total_subshapes++;
  527. _update_shape_data(p_owner);
  528. update_gizmos();
  529. }
  530. int CollisionObject3D::shape_owner_get_shape_count(uint32_t p_owner) const {
  531. ERR_FAIL_COND_V(!shapes.has(p_owner), 0);
  532. return shapes[p_owner].shapes.size();
  533. }
  534. Ref<Shape3D> CollisionObject3D::shape_owner_get_shape(uint32_t p_owner, int p_shape) const {
  535. ERR_FAIL_COND_V(!shapes.has(p_owner), Ref<Shape3D>());
  536. ERR_FAIL_INDEX_V(p_shape, shapes[p_owner].shapes.size(), Ref<Shape3D>());
  537. return shapes[p_owner].shapes[p_shape].shape;
  538. }
  539. int CollisionObject3D::shape_owner_get_shape_index(uint32_t p_owner, int p_shape) const {
  540. ERR_FAIL_COND_V(!shapes.has(p_owner), -1);
  541. ERR_FAIL_INDEX_V(p_shape, shapes[p_owner].shapes.size(), -1);
  542. return shapes[p_owner].shapes[p_shape].index;
  543. }
  544. void CollisionObject3D::shape_owner_remove_shape(uint32_t p_owner, int p_shape) {
  545. ERR_FAIL_NULL(RenderingServer::get_singleton());
  546. ERR_FAIL_COND(!shapes.has(p_owner));
  547. ERR_FAIL_INDEX(p_shape, shapes[p_owner].shapes.size());
  548. ShapeData::ShapeBase &s = shapes[p_owner].shapes.write[p_shape];
  549. int index_to_remove = s.index;
  550. if (area) {
  551. PhysicsServer3D::get_singleton()->area_remove_shape(rid, index_to_remove);
  552. } else {
  553. PhysicsServer3D::get_singleton()->body_remove_shape(rid, index_to_remove);
  554. }
  555. if (s.debug_shape.is_valid()) {
  556. RS::get_singleton()->free(s.debug_shape);
  557. if (s.shape.is_valid()) {
  558. s.shape->disconnect_changed(callable_mp(this, &CollisionObject3D::_shape_changed));
  559. }
  560. --debug_shapes_count;
  561. }
  562. shapes[p_owner].shapes.remove_at(p_shape);
  563. for (KeyValue<uint32_t, ShapeData> &E : shapes) {
  564. for (int i = 0; i < E.value.shapes.size(); i++) {
  565. if (E.value.shapes[i].index > index_to_remove) {
  566. E.value.shapes.write[i].index -= 1;
  567. }
  568. }
  569. }
  570. total_subshapes--;
  571. }
  572. void CollisionObject3D::shape_owner_clear_shapes(uint32_t p_owner) {
  573. ERR_FAIL_COND(!shapes.has(p_owner));
  574. while (shape_owner_get_shape_count(p_owner) > 0) {
  575. shape_owner_remove_shape(p_owner, 0);
  576. }
  577. update_gizmos();
  578. }
  579. uint32_t CollisionObject3D::shape_find_owner(int p_shape_index) const {
  580. ERR_FAIL_INDEX_V(p_shape_index, total_subshapes, UINT32_MAX);
  581. for (const KeyValue<uint32_t, ShapeData> &E : shapes) {
  582. for (int i = 0; i < E.value.shapes.size(); i++) {
  583. if (E.value.shapes[i].index == p_shape_index) {
  584. return E.key;
  585. }
  586. }
  587. }
  588. //in theory it should be unreachable
  589. ERR_FAIL_V_MSG(UINT32_MAX, "Can't find owner for shape index " + itos(p_shape_index) + ".");
  590. }
  591. CollisionObject3D::CollisionObject3D(RID p_rid, bool p_area) {
  592. rid = p_rid;
  593. area = p_area;
  594. set_notify_transform(true);
  595. if (p_area) {
  596. PhysicsServer3D::get_singleton()->area_attach_object_instance_id(rid, get_instance_id());
  597. } else {
  598. PhysicsServer3D::get_singleton()->body_attach_object_instance_id(rid, get_instance_id());
  599. PhysicsServer3D::get_singleton()->body_set_mode(rid, body_mode);
  600. }
  601. }
  602. void CollisionObject3D::set_capture_input_on_drag(bool p_capture) {
  603. capture_input_on_drag = p_capture;
  604. }
  605. bool CollisionObject3D::get_capture_input_on_drag() const {
  606. return capture_input_on_drag;
  607. }
  608. PackedStringArray CollisionObject3D::get_configuration_warnings() const {
  609. PackedStringArray warnings = Node3D::get_configuration_warnings();
  610. if (shapes.is_empty()) {
  611. warnings.push_back(RTR("This node has no shape, so it can't collide or interact with other objects.\nConsider adding a CollisionShape3D or CollisionPolygon3D as a child to define its shape."));
  612. }
  613. Vector3 scale = get_transform().get_basis().get_scale();
  614. if (!(Math::is_zero_approx(scale.x - scale.y) && Math::is_zero_approx(scale.y - scale.z))) {
  615. warnings.push_back(RTR("With a non-uniform scale this node will probably not function as expected.\nPlease make its scale uniform (i.e. the same on all axes), and change the size in children collision shapes instead."));
  616. }
  617. return warnings;
  618. }
  619. CollisionObject3D::CollisionObject3D() {
  620. set_notify_transform(true);
  621. //owner=
  622. //set_transform_notify(true);
  623. }
  624. CollisionObject3D::~CollisionObject3D() {
  625. ERR_FAIL_NULL(PhysicsServer3D::get_singleton());
  626. PhysicsServer3D::get_singleton()->free(rid);
  627. }