collision_object_3d.cpp 27 KB

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