| 1 | /**************************************************************************/ |
| 2 | /* collision_shape_2d.cpp */ |
| 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 | #include "collision_shape_2d.h" |
| 32 | |
| 33 | #include "collision_object_2d.h" |
| 34 | #include "scene/2d/area_2d.h" |
| 35 | #include "scene/resources/concave_polygon_shape_2d.h" |
| 36 | #include "scene/resources/convex_polygon_shape_2d.h" |
| 37 | |
| 38 | void CollisionShape2D::_shape_changed() { |
| 39 | queue_redraw(); |
| 40 | } |
| 41 | |
| 42 | void CollisionShape2D::_update_in_shape_owner(bool p_xform_only) { |
| 43 | collision_object->shape_owner_set_transform(owner_id, get_transform()); |
| 44 | if (p_xform_only) { |
| 45 | return; |
| 46 | } |
| 47 | collision_object->shape_owner_set_disabled(owner_id, disabled); |
| 48 | collision_object->shape_owner_set_one_way_collision(owner_id, one_way_collision); |
| 49 | collision_object->shape_owner_set_one_way_collision_margin(owner_id, one_way_collision_margin); |
| 50 | } |
| 51 | |
| 52 | Color CollisionShape2D::_get_default_debug_color() const { |
| 53 | SceneTree *st = SceneTree::get_singleton(); |
| 54 | return st ? st->get_debug_collisions_color() : Color(); |
| 55 | } |
| 56 | |
| 57 | void CollisionShape2D::_notification(int p_what) { |
| 58 | switch (p_what) { |
| 59 | case NOTIFICATION_PARENTED: { |
| 60 | collision_object = Object::cast_to<CollisionObject2D>(get_parent()); |
| 61 | if (collision_object) { |
| 62 | owner_id = collision_object->create_shape_owner(this); |
| 63 | if (shape.is_valid()) { |
| 64 | collision_object->shape_owner_add_shape(owner_id, shape); |
| 65 | } |
| 66 | _update_in_shape_owner(); |
| 67 | } |
| 68 | } break; |
| 69 | |
| 70 | case NOTIFICATION_ENTER_TREE: { |
| 71 | if (collision_object) { |
| 72 | _update_in_shape_owner(); |
| 73 | } |
| 74 | } break; |
| 75 | |
| 76 | case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: { |
| 77 | if (collision_object) { |
| 78 | _update_in_shape_owner(true); |
| 79 | } |
| 80 | } break; |
| 81 | |
| 82 | case NOTIFICATION_UNPARENTED: { |
| 83 | if (collision_object) { |
| 84 | collision_object->remove_shape_owner(owner_id); |
| 85 | } |
| 86 | owner_id = 0; |
| 87 | collision_object = nullptr; |
| 88 | } break; |
| 89 | |
| 90 | case NOTIFICATION_DRAW: { |
| 91 | ERR_FAIL_COND(!is_inside_tree()); |
| 92 | |
| 93 | if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) { |
| 94 | break; |
| 95 | } |
| 96 | |
| 97 | if (!shape.is_valid()) { |
| 98 | break; |
| 99 | } |
| 100 | |
| 101 | rect = Rect2(); |
| 102 | |
| 103 | Color draw_col = debug_color; |
| 104 | if (disabled) { |
| 105 | float g = draw_col.get_v(); |
| 106 | draw_col.r = g; |
| 107 | draw_col.g = g; |
| 108 | draw_col.b = g; |
| 109 | draw_col.a *= 0.5; |
| 110 | } |
| 111 | shape->draw(get_canvas_item(), draw_col); |
| 112 | |
| 113 | rect = shape->get_rect(); |
| 114 | rect = rect.grow(3); |
| 115 | |
| 116 | if (one_way_collision) { |
| 117 | // Draw an arrow indicating the one-way collision direction |
| 118 | draw_col = debug_color.inverted(); |
| 119 | if (disabled) { |
| 120 | draw_col = draw_col.darkened(0.25); |
| 121 | } |
| 122 | Vector2 line_to(0, 20); |
| 123 | draw_line(Vector2(), line_to, draw_col, 2); |
| 124 | real_t tsize = 8; |
| 125 | |
| 126 | Vector<Vector2> pts{ |
| 127 | line_to + Vector2(0, tsize), |
| 128 | line_to + Vector2(Math_SQRT12 * tsize, 0), |
| 129 | line_to + Vector2(-Math_SQRT12 * tsize, 0) |
| 130 | }; |
| 131 | |
| 132 | Vector<Color> cols{ draw_col, draw_col, draw_col }; |
| 133 | |
| 134 | draw_primitive(pts, cols, Vector<Vector2>()); |
| 135 | } |
| 136 | } break; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | void CollisionShape2D::set_shape(const Ref<Shape2D> &p_shape) { |
| 141 | if (p_shape == shape) { |
| 142 | return; |
| 143 | } |
| 144 | if (shape.is_valid()) { |
| 145 | shape->disconnect_changed(callable_mp(this, &CollisionShape2D::_shape_changed)); |
| 146 | } |
| 147 | shape = p_shape; |
| 148 | queue_redraw(); |
| 149 | if (collision_object) { |
| 150 | collision_object->shape_owner_clear_shapes(owner_id); |
| 151 | if (shape.is_valid()) { |
| 152 | collision_object->shape_owner_add_shape(owner_id, shape); |
| 153 | } |
| 154 | _update_in_shape_owner(); |
| 155 | } |
| 156 | |
| 157 | if (shape.is_valid()) { |
| 158 | shape->connect_changed(callable_mp(this, &CollisionShape2D::_shape_changed)); |
| 159 | } |
| 160 | |
| 161 | update_configuration_warnings(); |
| 162 | } |
| 163 | |
| 164 | Ref<Shape2D> CollisionShape2D::get_shape() const { |
| 165 | return shape; |
| 166 | } |
| 167 | |
| 168 | bool CollisionShape2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const { |
| 169 | if (!shape.is_valid()) { |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | return shape->_edit_is_selected_on_click(p_point, p_tolerance); |
| 174 | } |
| 175 | |
| 176 | PackedStringArray CollisionShape2D::get_configuration_warnings() const { |
| 177 | PackedStringArray warnings = Node::get_configuration_warnings(); |
| 178 | |
| 179 | CollisionObject2D *col_object = Object::cast_to<CollisionObject2D>(get_parent()); |
| 180 | if (col_object == nullptr) { |
| 181 | warnings.push_back(RTR("CollisionShape2D only serves to provide a collision shape to a CollisionObject2D derived node. Please only use it as a child of Area2D, StaticBody2D, RigidBody2D, CharacterBody2D, etc. to give them a shape." )); |
| 182 | } |
| 183 | if (!shape.is_valid()) { |
| 184 | warnings.push_back(RTR("A shape must be provided for CollisionShape2D to function. Please create a shape resource for it!" )); |
| 185 | } |
| 186 | if (one_way_collision && Object::cast_to<Area2D>(col_object)) { |
| 187 | warnings.push_back(RTR("The One Way Collision property will be ignored when the collision object is an Area2D." )); |
| 188 | } |
| 189 | |
| 190 | Ref<ConvexPolygonShape2D> convex = shape; |
| 191 | Ref<ConcavePolygonShape2D> concave = shape; |
| 192 | if (convex.is_valid() || concave.is_valid()) { |
| 193 | warnings.push_back(RTR("Polygon-based shapes are not meant be used nor edited directly through the CollisionShape2D node. Please use the CollisionPolygon2D node instead." )); |
| 194 | } |
| 195 | |
| 196 | return warnings; |
| 197 | } |
| 198 | |
| 199 | void CollisionShape2D::set_disabled(bool p_disabled) { |
| 200 | disabled = p_disabled; |
| 201 | queue_redraw(); |
| 202 | if (collision_object) { |
| 203 | collision_object->shape_owner_set_disabled(owner_id, p_disabled); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | bool CollisionShape2D::is_disabled() const { |
| 208 | return disabled; |
| 209 | } |
| 210 | |
| 211 | void CollisionShape2D::set_one_way_collision(bool p_enable) { |
| 212 | one_way_collision = p_enable; |
| 213 | queue_redraw(); |
| 214 | if (collision_object) { |
| 215 | collision_object->shape_owner_set_one_way_collision(owner_id, p_enable); |
| 216 | } |
| 217 | update_configuration_warnings(); |
| 218 | } |
| 219 | |
| 220 | bool CollisionShape2D::is_one_way_collision_enabled() const { |
| 221 | return one_way_collision; |
| 222 | } |
| 223 | |
| 224 | void CollisionShape2D::set_one_way_collision_margin(real_t p_margin) { |
| 225 | one_way_collision_margin = p_margin; |
| 226 | if (collision_object) { |
| 227 | collision_object->shape_owner_set_one_way_collision_margin(owner_id, one_way_collision_margin); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | real_t CollisionShape2D::get_one_way_collision_margin() const { |
| 232 | return one_way_collision_margin; |
| 233 | } |
| 234 | |
| 235 | void CollisionShape2D::set_debug_color(const Color &p_color) { |
| 236 | debug_color = p_color; |
| 237 | queue_redraw(); |
| 238 | } |
| 239 | |
| 240 | Color CollisionShape2D::get_debug_color() const { |
| 241 | return debug_color; |
| 242 | } |
| 243 | |
| 244 | bool CollisionShape2D::_property_can_revert(const StringName &p_name) const { |
| 245 | if (p_name == "debug_color" ) { |
| 246 | return true; |
| 247 | } |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | bool CollisionShape2D::_property_get_revert(const StringName &p_name, Variant &r_property) const { |
| 252 | if (p_name == "debug_color" ) { |
| 253 | r_property = _get_default_debug_color(); |
| 254 | return true; |
| 255 | } |
| 256 | return false; |
| 257 | } |
| 258 | |
| 259 | void CollisionShape2D::_validate_property(PropertyInfo &p_property) const { |
| 260 | if (p_property.name == "debug_color" ) { |
| 261 | if (debug_color == _get_default_debug_color()) { |
| 262 | p_property.usage = PROPERTY_USAGE_DEFAULT & ~PROPERTY_USAGE_STORAGE; |
| 263 | } else { |
| 264 | p_property.usage = PROPERTY_USAGE_DEFAULT; |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | void CollisionShape2D::_bind_methods() { |
| 270 | ClassDB::bind_method(D_METHOD("set_shape" , "shape" ), &CollisionShape2D::set_shape); |
| 271 | ClassDB::bind_method(D_METHOD("get_shape" ), &CollisionShape2D::get_shape); |
| 272 | ClassDB::bind_method(D_METHOD("set_disabled" , "disabled" ), &CollisionShape2D::set_disabled); |
| 273 | ClassDB::bind_method(D_METHOD("is_disabled" ), &CollisionShape2D::is_disabled); |
| 274 | ClassDB::bind_method(D_METHOD("set_one_way_collision" , "enabled" ), &CollisionShape2D::set_one_way_collision); |
| 275 | ClassDB::bind_method(D_METHOD("is_one_way_collision_enabled" ), &CollisionShape2D::is_one_way_collision_enabled); |
| 276 | ClassDB::bind_method(D_METHOD("set_one_way_collision_margin" , "margin" ), &CollisionShape2D::set_one_way_collision_margin); |
| 277 | ClassDB::bind_method(D_METHOD("get_one_way_collision_margin" ), &CollisionShape2D::get_one_way_collision_margin); |
| 278 | ClassDB::bind_method(D_METHOD("set_debug_color" , "color" ), &CollisionShape2D::set_debug_color); |
| 279 | ClassDB::bind_method(D_METHOD("get_debug_color" ), &CollisionShape2D::get_debug_color); |
| 280 | |
| 281 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape" , PROPERTY_HINT_RESOURCE_TYPE, "Shape2D" ), "set_shape" , "get_shape" ); |
| 282 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled" ), "set_disabled" , "is_disabled" ); |
| 283 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_way_collision" ), "set_one_way_collision" , "is_one_way_collision_enabled" ); |
| 284 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "one_way_collision_margin" , PROPERTY_HINT_RANGE, "0,128,0.1,suffix:px" ), "set_one_way_collision_margin" , "get_one_way_collision_margin" ); |
| 285 | ADD_PROPERTY(PropertyInfo(Variant::COLOR, "debug_color" ), "set_debug_color" , "get_debug_color" ); |
| 286 | // Default value depends on a project setting, override for doc generation purposes. |
| 287 | ADD_PROPERTY_DEFAULT("debug_color" , Color()); |
| 288 | } |
| 289 | |
| 290 | CollisionShape2D::CollisionShape2D() { |
| 291 | set_notify_local_transform(true); |
| 292 | set_hide_clip_children(true); |
| 293 | debug_color = _get_default_debug_color(); |
| 294 | } |
| 295 | |