diff options
Diffstat (limited to 'scene/2d')
-rw-r--r-- | scene/2d/collision_polygon_2d.cpp | 55 | ||||
-rw-r--r-- | scene/2d/ray_cast_2d.cpp | 69 | ||||
-rw-r--r-- | scene/2d/ray_cast_2d.h | 2 |
3 files changed, 86 insertions, 40 deletions
diff --git a/scene/2d/collision_polygon_2d.cpp b/scene/2d/collision_polygon_2d.cpp index 39d7705226..38198c496e 100644 --- a/scene/2d/collision_polygon_2d.cpp +++ b/scene/2d/collision_polygon_2d.cpp @@ -41,13 +41,13 @@ void CollisionPolygon2D::_build_polygon() { parent->shape_owner_clear_shapes(owner_id); - if (polygon.size() == 0) { - return; - } - bool solids = build_mode == BUILD_SOLIDS; if (solids) { + if (polygon.size() < 3) { + return; + } + //here comes the sun, lalalala //decompose concave into multiple convex polygons and add them Vector<Vector<Vector2>> decomp = _decompose_in_convex(); @@ -58,6 +58,10 @@ void CollisionPolygon2D::_build_polygon() { } } else { + if (polygon.size() < 2) { + return; + } + Ref<ConcavePolygonShape2D> concave = memnew(ConcavePolygonShape2D); Vector<Vector2> segments; @@ -132,25 +136,28 @@ void CollisionPolygon2D::_notification(int p_what) { break; } - for (int i = 0; i < polygon.size(); i++) { + int polygon_count = polygon.size(); + for (int i = 0; i < polygon_count; i++) { Vector2 p = polygon[i]; - Vector2 n = polygon[(i + 1) % polygon.size()]; + Vector2 n = polygon[(i + 1) % polygon_count]; // draw line with width <= 1, so it does not scale with zoom and break pixel exact editing draw_line(p, n, Color(0.9, 0.2, 0.0, 0.8), 1); } + + if (polygon_count > 2) { #define DEBUG_DECOMPOSE #if defined(TOOLS_ENABLED) && defined(DEBUG_DECOMPOSE) + Vector<Vector<Vector2>> decomp = _decompose_in_convex(); - Vector<Vector<Vector2>> decomp = _decompose_in_convex(); - - Color c(0.4, 0.9, 0.1); - for (int i = 0; i < decomp.size(); i++) { - c.set_hsv(Math::fmod(c.get_h() + 0.738, 1), c.get_s(), c.get_v(), 0.5); - draw_colored_polygon(decomp[i], c); - } + Color c(0.4, 0.9, 0.1); + for (int i = 0; i < decomp.size(); i++) { + c.set_hsv(Math::fmod(c.get_h() + 0.738, 1), c.get_s(), c.get_v(), 0.5); + draw_colored_polygon(decomp[i], c); + } #else - draw_colored_polygon(polygon, get_tree()->get_debug_collisions_color()); + draw_colored_polygon(polygon, get_tree()->get_debug_collisions_color()); #endif + } if (one_way_collision) { Color dcol = get_tree()->get_debug_collisions_color(); //0.9,0.2,0.2,0.4); @@ -211,6 +218,8 @@ void CollisionPolygon2D::set_build_mode(BuildMode p_mode) { _build_polygon(); _update_in_shape_owner(); } + update(); + update_configuration_warning(); } CollisionPolygon2D::BuildMode CollisionPolygon2D::get_build_mode() const { @@ -241,11 +250,27 @@ String CollisionPolygon2D::get_configuration_warning() const { warning += TTR("CollisionPolygon2D only serves to provide a collision shape to a CollisionObject2D derived node. Please only use it as a child of Area2D, StaticBody2D, RigidBody2D, KinematicBody2D, etc. to give them a shape."); } - if (polygon.is_empty()) { + int polygon_count = polygon.size(); + if (polygon_count == 0) { if (!warning.is_empty()) { warning += "\n\n"; } warning += TTR("An empty CollisionPolygon2D has no effect on collision."); + } else { + bool solids = build_mode == BUILD_SOLIDS; + if (solids) { + if (polygon_count < 3) { + if (!warning.is_empty()) { + warning += "\n\n"; + } + warning += TTR("Invalid polygon. At least 3 points are needed in 'Solids' build mode."); + } + } else if (polygon_count < 2) { + if (!warning.is_empty()) { + warning += "\n\n"; + } + warning += TTR("Invalid polygon. At least 2 points are needed in 'Segments' build mode."); + } } return warning; diff --git a/scene/2d/ray_cast_2d.cpp b/scene/2d/ray_cast_2d.cpp index 2cc3a74270..50625a0f39 100644 --- a/scene/2d/ray_cast_2d.cpp +++ b/scene/2d/ray_cast_2d.cpp @@ -159,30 +159,7 @@ void RayCast2D::_notification(int p_what) { if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) { break; } - Transform2D xf; - xf.rotate(target_position.angle()); - xf.translate(Vector2(target_position.length(), 0)); - - // Draw an arrow indicating where the RayCast is pointing to - Color draw_col = get_tree()->get_debug_collisions_color(); - if (!enabled) { - float g = draw_col.get_v(); - draw_col.r = g; - draw_col.g = g; - draw_col.b = g; - } - draw_line(Vector2(), target_position, draw_col, 2); - Vector<Vector2> pts; - float tsize = 8.0; - pts.push_back(xf.xform(Vector2(tsize, 0))); - pts.push_back(xf.xform(Vector2(0, Math_SQRT12 * tsize))); - pts.push_back(xf.xform(Vector2(0, -Math_SQRT12 * tsize))); - Vector<Color> cols; - for (int i = 0; i < 3; i++) { - cols.push_back(draw_col); - } - - draw_primitive(pts, cols, Vector<Vector2>()); + _draw_debug_shape(); } break; @@ -212,7 +189,7 @@ void RayCast2D::_update_raycast_state() { } PhysicsDirectSpaceState2D::RayResult rr; - + bool prev_collision_state = collided; if (dss->intersect_ray(gt.get_origin(), gt.xform(to), rr, exclude, collision_mask, collide_with_bodies, collide_with_areas)) { collided = true; against = rr.collider_id; @@ -224,6 +201,48 @@ void RayCast2D::_update_raycast_state() { against = ObjectID(); against_shape = 0; } + + if (prev_collision_state != collided) { + update(); + } +} + +void RayCast2D::_draw_debug_shape() { + Color draw_col = collided ? Color(1.0, 0.01, 0) : get_tree()->get_debug_collisions_color(); + if (!enabled) { + float g = draw_col.get_v(); + draw_col.r = g; + draw_col.g = g; + draw_col.b = g; + } + + // Draw an arrow indicating where the RayCast is pointing to + const float max_arrow_size = 6; + const float line_width = 1.4; + bool no_line = target_position.length() < line_width; + float arrow_size = CLAMP(target_position.length() * 2 / 3, line_width, max_arrow_size); + + if (no_line) { + arrow_size = target_position.length(); + } else { + draw_line(Vector2(), target_position - target_position.normalized() * arrow_size, draw_col, line_width); + } + + Transform2D xf; + xf.rotate(target_position.angle()); + xf.translate(Vector2(no_line ? 0 : target_position.length() - arrow_size, 0)); + + Vector<Vector2> pts; + pts.push_back(xf.xform(Vector2(arrow_size, 0))); + pts.push_back(xf.xform(Vector2(0, 0.5 * arrow_size))); + pts.push_back(xf.xform(Vector2(0, -0.5 * arrow_size))); + + Vector<Color> cols; + for (int i = 0; i < 3; i++) { + cols.push_back(draw_col); + } + + draw_primitive(pts, cols, Vector<Vector2>()); } void RayCast2D::force_raycast_update() { diff --git a/scene/2d/ray_cast_2d.h b/scene/2d/ray_cast_2d.h index dab3302e25..984c6bda49 100644 --- a/scene/2d/ray_cast_2d.h +++ b/scene/2d/ray_cast_2d.h @@ -51,6 +51,8 @@ class RayCast2D : public Node2D { bool collide_with_areas = false; bool collide_with_bodies = true; + void _draw_debug_shape(); + protected: void _notification(int p_what); void _update_raycast_state(); |