1 /*************************************************************************/
2 /*  ray_cast_2d.cpp                                                      */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md)    */
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 #include "ray_cast_2d.h"
31 #include "collision_object_2d.h"
32 #include "servers/physics_2d_server.h"
33 
set_cast_to(const Vector2 & p_point)34 void RayCast2D::set_cast_to(const Vector2 &p_point) {
35 
36 	cast_to = p_point;
37 	if (is_inside_tree() && (get_tree()->is_editor_hint() || get_tree()->is_debugging_collisions_hint()))
38 		update();
39 }
40 
get_cast_to() const41 Vector2 RayCast2D::get_cast_to() const {
42 
43 	return cast_to;
44 }
45 
set_layer_mask(uint32_t p_mask)46 void RayCast2D::set_layer_mask(uint32_t p_mask) {
47 
48 	layer_mask = p_mask;
49 }
50 
get_layer_mask() const51 uint32_t RayCast2D::get_layer_mask() const {
52 
53 	return layer_mask;
54 }
55 
set_type_mask(uint32_t p_mask)56 void RayCast2D::set_type_mask(uint32_t p_mask) {
57 
58 	type_mask = p_mask;
59 }
60 
get_type_mask() const61 uint32_t RayCast2D::get_type_mask() const {
62 
63 	return type_mask;
64 }
65 
is_colliding() const66 bool RayCast2D::is_colliding() const {
67 
68 	return collided;
69 }
get_collider() const70 Object *RayCast2D::get_collider() const {
71 
72 	if (against == 0)
73 		return NULL;
74 
75 	return ObjectDB::get_instance(against);
76 }
77 
get_collider_shape() const78 int RayCast2D::get_collider_shape() const {
79 
80 	return against_shape;
81 }
get_collision_point() const82 Vector2 RayCast2D::get_collision_point() const {
83 
84 	return collision_point;
85 }
get_collision_normal() const86 Vector2 RayCast2D::get_collision_normal() const {
87 
88 	return collision_normal;
89 }
90 
set_enabled(bool p_enabled)91 void RayCast2D::set_enabled(bool p_enabled) {
92 
93 	enabled = p_enabled;
94 	if (is_inside_tree() && !get_tree()->is_editor_hint())
95 		set_fixed_process(p_enabled);
96 	if (!p_enabled)
97 		collided = false;
98 }
99 
is_enabled() const100 bool RayCast2D::is_enabled() const {
101 
102 	return enabled;
103 }
104 
_notification(int p_what)105 void RayCast2D::_notification(int p_what) {
106 
107 	switch (p_what) {
108 
109 		case NOTIFICATION_ENTER_TREE: {
110 
111 			if (enabled && !get_tree()->is_editor_hint())
112 				set_fixed_process(true);
113 			else
114 				set_fixed_process(false);
115 
116 		} break;
117 		case NOTIFICATION_EXIT_TREE: {
118 
119 			if (enabled)
120 				set_fixed_process(false);
121 
122 		} break;
123 
124 		case NOTIFICATION_DRAW: {
125 
126 			if (!get_tree()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint())
127 				break;
128 			Matrix32 xf;
129 			xf.rotate(cast_to.angle());
130 			xf.translate(Vector2(0, cast_to.length()));
131 
132 			//Vector2 tip = Vector2(0,s->get_length());
133 			Color dcol = get_tree()->get_debug_collisions_color(); //0.9,0.2,0.2,0.4);
134 			draw_line(Vector2(), cast_to, dcol, 3);
135 			Vector<Vector2> pts;
136 			float tsize = 4;
137 			pts.push_back(xf.xform(Vector2(0, tsize)));
138 			pts.push_back(xf.xform(Vector2(0.707 * tsize, 0)));
139 			pts.push_back(xf.xform(Vector2(-0.707 * tsize, 0)));
140 			Vector<Color> cols;
141 			for (int i = 0; i < 3; i++)
142 				cols.push_back(dcol);
143 
144 			draw_primitive(pts, cols, Vector<Vector2>()); //small arrow
145 
146 		} break;
147 
148 		case NOTIFICATION_FIXED_PROCESS: {
149 
150 			if (!enabled)
151 				break;
152 
153 			_update_raycast_state();
154 
155 		} break;
156 	}
157 }
158 
_update_raycast_state()159 void RayCast2D::_update_raycast_state() {
160 	Ref<World2D> w2d = get_world_2d();
161 	ERR_FAIL_COND(w2d.is_null());
162 
163 	Physics2DDirectSpaceState *dss = Physics2DServer::get_singleton()->space_get_direct_state(w2d->get_space());
164 	ERR_FAIL_COND(!dss);
165 
166 	Matrix32 gt = get_global_transform();
167 
168 	Vector2 to = cast_to;
169 	if (to == Vector2())
170 		to = Vector2(0, 0.01);
171 
172 	Physics2DDirectSpaceState::RayResult rr;
173 
174 	if (dss->intersect_ray(gt.get_origin(), gt.xform(to), rr, exclude, layer_mask, type_mask)) {
175 
176 		collided = true;
177 		against = rr.collider_id;
178 		collision_point = rr.position;
179 		collision_normal = rr.normal;
180 		against_shape = rr.shape;
181 	} else {
182 		collided = false;
183 	}
184 }
185 
force_raycast_update()186 void RayCast2D::force_raycast_update() {
187 	_update_raycast_state();
188 }
189 
add_exception_rid(const RID & p_rid)190 void RayCast2D::add_exception_rid(const RID &p_rid) {
191 
192 	exclude.insert(p_rid);
193 }
194 
add_exception(const Object * p_object)195 void RayCast2D::add_exception(const Object *p_object) {
196 
197 	ERR_FAIL_NULL(p_object);
198 	CollisionObject2D *co = ((Object *)p_object)->cast_to<CollisionObject2D>();
199 	if (!co)
200 		return;
201 	add_exception_rid(co->get_rid());
202 }
203 
remove_exception_rid(const RID & p_rid)204 void RayCast2D::remove_exception_rid(const RID &p_rid) {
205 
206 	exclude.erase(p_rid);
207 }
208 
remove_exception(const Object * p_object)209 void RayCast2D::remove_exception(const Object *p_object) {
210 
211 	ERR_FAIL_NULL(p_object);
212 	CollisionObject2D *co = ((Object *)p_object)->cast_to<CollisionObject2D>();
213 	if (!co)
214 		return;
215 	remove_exception_rid(co->get_rid());
216 }
217 
clear_exceptions()218 void RayCast2D::clear_exceptions() {
219 
220 	exclude.clear();
221 }
222 
_bind_methods()223 void RayCast2D::_bind_methods() {
224 
225 	ObjectTypeDB::bind_method(_MD("set_enabled", "enabled"), &RayCast2D::set_enabled);
226 	ObjectTypeDB::bind_method(_MD("is_enabled"), &RayCast2D::is_enabled);
227 
228 	ObjectTypeDB::bind_method(_MD("set_cast_to", "local_point"), &RayCast2D::set_cast_to);
229 	ObjectTypeDB::bind_method(_MD("get_cast_to"), &RayCast2D::get_cast_to);
230 
231 	ObjectTypeDB::bind_method(_MD("is_colliding"), &RayCast2D::is_colliding);
232 	ObjectTypeDB::bind_method(_MD("force_raycast_update"), &RayCast2D::force_raycast_update);
233 
234 	ObjectTypeDB::bind_method(_MD("get_collider"), &RayCast2D::get_collider);
235 	ObjectTypeDB::bind_method(_MD("get_collider_shape"), &RayCast2D::get_collider_shape);
236 	ObjectTypeDB::bind_method(_MD("get_collision_point"), &RayCast2D::get_collision_point);
237 	ObjectTypeDB::bind_method(_MD("get_collision_normal"), &RayCast2D::get_collision_normal);
238 
239 	ObjectTypeDB::bind_method(_MD("add_exception_rid", "rid"), &RayCast2D::add_exception_rid);
240 	ObjectTypeDB::bind_method(_MD("add_exception", "node"), &RayCast2D::add_exception);
241 
242 	ObjectTypeDB::bind_method(_MD("remove_exception_rid", "rid"), &RayCast2D::remove_exception_rid);
243 	ObjectTypeDB::bind_method(_MD("remove_exception", "node"), &RayCast2D::remove_exception);
244 
245 	ObjectTypeDB::bind_method(_MD("clear_exceptions"), &RayCast2D::clear_exceptions);
246 
247 	ObjectTypeDB::bind_method(_MD("set_layer_mask", "mask"), &RayCast2D::set_layer_mask);
248 	ObjectTypeDB::bind_method(_MD("get_layer_mask"), &RayCast2D::get_layer_mask);
249 
250 	ObjectTypeDB::bind_method(_MD("set_type_mask", "mask"), &RayCast2D::set_type_mask);
251 	ObjectTypeDB::bind_method(_MD("get_type_mask"), &RayCast2D::get_type_mask);
252 
253 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), _SCS("set_enabled"), _SCS("is_enabled"));
254 	ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "cast_to"), _SCS("set_cast_to"), _SCS("get_cast_to"));
255 	ADD_PROPERTY(PropertyInfo(Variant::INT, "layer_mask", PROPERTY_HINT_ALL_FLAGS), _SCS("set_layer_mask"), _SCS("get_layer_mask"));
256 	ADD_PROPERTY(PropertyInfo(Variant::INT, "type_mask", PROPERTY_HINT_FLAGS, "Static,Kinematic,Rigid,Character,Area"), _SCS("set_type_mask"), _SCS("get_type_mask"));
257 }
258 
RayCast2D()259 RayCast2D::RayCast2D() {
260 
261 	enabled = false;
262 	against = 0;
263 	collided = false;
264 	against_shape = 0;
265 	layer_mask = 1;
266 	type_mask = Physics2DDirectSpaceState::TYPE_MASK_COLLISION;
267 	cast_to = Vector2(0, 50);
268 }
269