ray_cast_3d.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**************************************************************************/
  2. /* ray_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. #ifndef RAY_CAST_3D_H
  31. #define RAY_CAST_3D_H
  32. #include "scene/3d/node_3d.h"
  33. class CollisionObject3D;
  34. class RayCast3D : public Node3D {
  35. GDCLASS(RayCast3D, Node3D);
  36. bool enabled = true;
  37. bool collided = false;
  38. ObjectID against;
  39. RID against_rid;
  40. int against_shape = 0;
  41. Vector3 collision_point;
  42. Vector3 collision_normal;
  43. int collision_face_index = -1;
  44. Vector3 target_position = Vector3(0, -1, 0);
  45. HashSet<RID> exclude;
  46. uint32_t collision_mask = 1;
  47. bool exclude_parent_body = true;
  48. Node *debug_shape = nullptr;
  49. Ref<Material> debug_material;
  50. Color debug_shape_custom_color = Color(0.0, 0.0, 0.0);
  51. int debug_shape_thickness = 2;
  52. Vector<Vector3> debug_shape_vertices;
  53. Vector<Vector3> debug_line_vertices;
  54. void _create_debug_shape();
  55. void _update_debug_shape();
  56. void _update_debug_shape_material(bool p_check_collision = false);
  57. void _update_debug_shape_vertices();
  58. void _clear_debug_shape();
  59. bool collide_with_areas = false;
  60. bool collide_with_bodies = true;
  61. bool hit_from_inside = false;
  62. bool hit_back_faces = true;
  63. protected:
  64. void _notification(int p_what);
  65. void _update_raycast_state();
  66. static void _bind_methods();
  67. public:
  68. void set_collide_with_areas(bool p_enabled);
  69. bool is_collide_with_areas_enabled() const;
  70. void set_collide_with_bodies(bool p_enabled);
  71. bool is_collide_with_bodies_enabled() const;
  72. void set_hit_from_inside(bool p_enabled);
  73. bool is_hit_from_inside_enabled() const;
  74. void set_hit_back_faces(bool p_enabled);
  75. bool is_hit_back_faces_enabled() const;
  76. void set_enabled(bool p_enabled);
  77. bool is_enabled() const;
  78. void set_target_position(const Vector3 &p_point);
  79. Vector3 get_target_position() const;
  80. void set_collision_mask(uint32_t p_mask);
  81. uint32_t get_collision_mask() const;
  82. void set_collision_mask_value(int p_layer_number, bool p_value);
  83. bool get_collision_mask_value(int p_layer_number) const;
  84. void set_exclude_parent_body(bool p_exclude_parent_body);
  85. bool get_exclude_parent_body() const;
  86. const Color &get_debug_shape_custom_color() const;
  87. void set_debug_shape_custom_color(const Color &p_color);
  88. const Vector<Vector3> &get_debug_shape_vertices() const;
  89. const Vector<Vector3> &get_debug_line_vertices() const;
  90. Ref<StandardMaterial3D> get_debug_material();
  91. int get_debug_shape_thickness() const;
  92. void set_debug_shape_thickness(const int p_debug_thickness);
  93. void force_raycast_update();
  94. bool is_colliding() const;
  95. Object *get_collider() const;
  96. RID get_collider_rid() const;
  97. int get_collider_shape() const;
  98. Vector3 get_collision_point() const;
  99. Vector3 get_collision_normal() const;
  100. int get_collision_face_index() const;
  101. void add_exception_rid(const RID &p_rid);
  102. void add_exception(const CollisionObject3D *p_node);
  103. void remove_exception_rid(const RID &p_rid);
  104. void remove_exception(const CollisionObject3D *p_node);
  105. void clear_exceptions();
  106. RayCast3D();
  107. };
  108. #endif // RAY_CAST_3D_H