Skip to content
0.2.0-dev Download ZIP
139 lines ·5.1 KB Raw
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#pragma once
32
33#include "scene/3d/node_3d.h"
34#include "scene/resources/material.h"
35
36class ArrayMesh;
37class CollisionObject3D;
38
39class RayCast3D : public Node3D {
40 GDCLASS(RayCast3D, Node3D);
41
42 bool enabled = true;
43 bool collided = false;
44 ObjectID against;
45 RID against_rid;
46 int against_shape = 0;
47 Vector3 collision_point;
48 Vector3 collision_normal;
49 int collision_face_index = -1;
50
51 Vector3 target_position = Vector3(0, -1, 0);
52 HashSet<RID> exclude;
53
54 uint32_t collision_mask = 1;
55 bool exclude_parent_body = true;
56
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 void _physics_debug_changed();
69
70 bool collide_with_areas = false;
71 bool collide_with_bodies = true;
72
73 bool hit_from_inside = false;
74 bool hit_back_faces = true;
75
76 RID debug_instance;
77 Ref<ArrayMesh> debug_mesh;
78
79protected:
80 void _notification(int p_what);
81 void _update_raycast_state();
82 static void _bind_methods();
83
84public:
85 void set_collide_with_areas(bool p_enabled);
86 bool is_collide_with_areas_enabled() const;
87
88 void set_collide_with_bodies(bool p_enabled);
89 bool is_collide_with_bodies_enabled() const;
90
91 void set_hit_from_inside(bool p_enabled);
92 bool is_hit_from_inside_enabled() const;
93
94 void set_hit_back_faces(bool p_enabled);
95 bool is_hit_back_faces_enabled() const;
96
97 void set_enabled(bool p_enabled);
98 bool is_enabled() const;
99
100 void set_target_position(const Vector3 &p_point);
101 Vector3 get_target_position() const;
102
103 void set_collision_mask(uint32_t p_mask);
104 uint32_t get_collision_mask() const;
105
106 void set_collision_mask_value(int p_layer_number, bool p_value);
107 bool get_collision_mask_value(int p_layer_number) const;
108
109 void set_exclude_parent_body(bool p_exclude_parent_body);
110 bool get_exclude_parent_body() const;
111
112 const Color &get_debug_shape_custom_color() const;
113 void set_debug_shape_custom_color(const Color &p_color);
114
115 const Vector<Vector3> &get_debug_shape_vertices() const;
116 const Vector<Vector3> &get_debug_line_vertices() const;
117
118 Ref<StandardMaterial3D> get_debug_material();
119
120 int get_debug_shape_thickness() const;
121 void set_debug_shape_thickness(const int p_debug_thickness);
122
123 void force_raycast_update();
124 bool is_colliding() const;
125 Object *get_collider() const;
126 RID get_collider_rid() const;
127 int get_collider_shape() const;
128 Vector3 get_collision_point() const;
129 Vector3 get_collision_normal() const;
130 int get_collision_face_index() const;
131
132 void add_exception_rid(const RID &p_rid);
133 void add_exception(RequiredParam<const CollisionObject3D> p_node);
134 void remove_exception_rid(const RID &p_rid);
135 void remove_exception(RequiredParam<const CollisionObject3D> p_node);
136 void clear_exceptions();
137
138 RayCast3D();
139};