shape_cast_3d.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**************************************************************************/
  2. /* shape_cast_3d.h */
  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. #pragma once
  31. #include "scene/3d/node_3d.h"
  32. #include "scene/resources/3d/shape_3d.h"
  33. class CollisionObject3D;
  34. class ShapeCast3D : public Node3D {
  35. GDCLASS(ShapeCast3D, Node3D);
  36. bool enabled = true;
  37. #ifndef DISABLE_DEPRECATED
  38. void resource_changed(Ref<Resource> p_res);
  39. #endif
  40. Ref<Shape3D> shape;
  41. RID shape_rid;
  42. Vector3 target_position = Vector3(0, -1, 0);
  43. HashSet<RID> exclude;
  44. real_t margin = 0.0;
  45. uint32_t collision_mask = 1;
  46. bool exclude_parent_body = true;
  47. bool collide_with_areas = false;
  48. bool collide_with_bodies = true;
  49. Ref<Material> debug_material;
  50. Color debug_shape_custom_color = Color(0.0, 0.0, 0.0);
  51. Vector<Vector3> debug_shape_vertices;
  52. Vector<Vector3> debug_line_vertices;
  53. void _create_debug_shape();
  54. void _update_debug_shape();
  55. void _update_debug_shape_material(bool p_check_collision = false);
  56. void _update_debug_shape_vertices();
  57. void _clear_debug_shape();
  58. // Result
  59. int max_results = 32;
  60. Vector<PhysicsDirectSpaceState3D::ShapeRestInfo> result;
  61. bool collided = false;
  62. real_t collision_safe_fraction = 1.0;
  63. real_t collision_unsafe_fraction = 1.0;
  64. RID debug_instance;
  65. Ref<ArrayMesh> debug_mesh;
  66. protected:
  67. void _notification(int p_what);
  68. void _update_shapecast_state();
  69. void _shape_changed();
  70. static void _bind_methods();
  71. public:
  72. void set_collide_with_areas(bool p_clip);
  73. bool is_collide_with_areas_enabled() const;
  74. void set_collide_with_bodies(bool p_clip);
  75. bool is_collide_with_bodies_enabled() const;
  76. void set_enabled(bool p_enabled);
  77. bool is_enabled() const;
  78. void set_shape(const Ref<Shape3D> &p_shape);
  79. Ref<Shape3D> get_shape() const;
  80. void set_target_position(const Vector3 &p_point);
  81. Vector3 get_target_position() const;
  82. void set_margin(real_t p_margin);
  83. real_t get_margin() const;
  84. void set_max_results(int p_max_results);
  85. int get_max_results() const;
  86. void set_collision_mask(uint32_t p_mask);
  87. uint32_t get_collision_mask() const;
  88. void set_collision_mask_value(int p_layer_number, bool p_value);
  89. bool get_collision_mask_value(int p_layer_number) const;
  90. void set_exclude_parent_body(bool p_exclude_parent_body);
  91. bool get_exclude_parent_body() const;
  92. const Color &get_debug_shape_custom_color() const;
  93. void set_debug_shape_custom_color(const Color &p_color);
  94. const Vector<Vector3> &get_debug_shape_vertices() const;
  95. const Vector<Vector3> &get_debug_line_vertices() const;
  96. Ref<StandardMaterial3D> get_debug_material();
  97. Array get_collision_result() const;
  98. int get_collision_count() const;
  99. Object *get_collider(int p_idx) const;
  100. RID get_collider_rid(int p_idx) const;
  101. int get_collider_shape(int p_idx) const;
  102. Vector3 get_collision_point(int p_idx) const;
  103. Vector3 get_collision_normal(int p_idx) const;
  104. real_t get_closest_collision_safe_fraction() const;
  105. real_t get_closest_collision_unsafe_fraction() const;
  106. void force_shapecast_update();
  107. bool is_colliding() const;
  108. void add_exception_rid(const RID &p_rid);
  109. void add_exception(const CollisionObject3D *p_node);
  110. void remove_exception_rid(const RID &p_rid);
  111. void remove_exception(const CollisionObject3D *p_node);
  112. void clear_exceptions();
  113. virtual PackedStringArray get_configuration_warnings() const override;
  114. };