1/**************************************************************************/
2/* shape_cast_2d.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 SHAPE_CAST_2D_H
32#define SHAPE_CAST_2D_H
33
34#include "scene/2d/node_2d.h"
35#include "scene/resources/shape_2d.h"
36#include "scene/resources/world_2d.h"
37
38class CollisionObject2D;
39
40class ShapeCast2D : public Node2D {
41 GDCLASS(ShapeCast2D, Node2D);
42
43 bool enabled = true;
44
45 Ref<Shape2D> shape;
46 RID shape_rid;
47 Vector2 target_position = Vector2(0, 50);
48
49 HashSet<RID> exclude;
50 real_t margin = 0.0;
51 uint32_t collision_mask = 1;
52 bool exclude_parent_body = true;
53 bool collide_with_areas = false;
54 bool collide_with_bodies = true;
55
56 // Result
57 int max_results = 32;
58 Vector<PhysicsDirectSpaceState2D::ShapeRestInfo> result;
59 bool collided = false;
60 real_t collision_safe_fraction = 1.0;
61 real_t collision_unsafe_fraction = 1.0;
62
63 Array _get_collision_result() const;
64 void _shape_changed();
65
66protected:
67 void _notification(int p_what);
68 void _update_shapecast_state();
69 static void _bind_methods();
70
71public:
72 void set_collide_with_areas(bool p_clip);
73 bool is_collide_with_areas_enabled() const;
74
75 void set_collide_with_bodies(bool p_clip);
76 bool is_collide_with_bodies_enabled() const;
77
78 void set_enabled(bool p_enabled);
79 bool is_enabled() const;
80
81 void set_shape(const Ref<Shape2D> &p_shape);
82 Ref<Shape2D> get_shape() const;
83
84 void set_target_position(const Vector2 &p_point);
85 Vector2 get_target_position() const;
86
87 void set_margin(real_t p_margin);
88 real_t get_margin() const;
89
90 void set_max_results(int p_max_results);
91 int get_max_results() const;
92
93 void set_collision_mask(uint32_t p_mask);
94 uint32_t get_collision_mask() const;
95
96 void set_collision_mask_value(int p_layer_number, bool p_value);
97 bool get_collision_mask_value(int p_layer_number) const;
98
99 void set_exclude_parent_body(bool p_exclude_parent_body);
100 bool get_exclude_parent_body() const;
101
102 void force_shapecast_update();
103 bool is_colliding() const;
104
105 int get_collision_count() const;
106 Object *get_collider(int p_idx) const;
107 RID get_collider_rid(int p_idx) const;
108 int get_collider_shape(int p_idx) const;
109 Vector2 get_collision_point(int p_idx) const;
110 Vector2 get_collision_normal(int p_idx) const;
111
112 real_t get_closest_collision_safe_fraction() const;
113 real_t get_closest_collision_unsafe_fraction() const;
114
115 void add_exception_rid(const RID &p_rid);
116 void add_exception(const CollisionObject2D *p_node);
117 void remove_exception_rid(const RID &p_rid);
118 void remove_exception(const CollisionObject2D *p_node);
119 void clear_exceptions();
120
121 PackedStringArray get_configuration_warnings() const override;
122
123 ShapeCast2D();
124};
125
126#endif // SHAPE_CAST_2D_H
127