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