collision_object_3d.cpp 27 KB

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