From 7d0894e9259b079b0c27f4c516a80e7a9a659738 Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Thu, 31 Dec 2015 20:23:34 -0300 Subject: -Improved convex decomposer for CollisionPolygon2D, fixes #2336 --- scene/2d/collision_polygon_2d.cpp | 52 ++++++++++++++++++++++++++++++++++++--- scene/2d/collision_polygon_2d.h | 1 + 2 files changed, 50 insertions(+), 3 deletions(-) (limited to 'scene') diff --git a/scene/2d/collision_polygon_2d.cpp b/scene/2d/collision_polygon_2d.cpp index 354d54b69f..5ac9172b83 100644 --- a/scene/2d/collision_polygon_2d.cpp +++ b/scene/2d/collision_polygon_2d.cpp @@ -30,7 +30,7 @@ #include "collision_object_2d.h" #include "scene/resources/concave_polygon_shape_2d.h" #include "scene/resources/convex_polygon_shape_2d.h" - +#include "triangulator.h" void CollisionPolygon2D::_add_to_collision_object(Object *p_obj) { if (unparenting || !can_update_body) @@ -48,7 +48,7 @@ void CollisionPolygon2D::_add_to_collision_object(Object *p_obj) { //here comes the sun, lalalala //decompose concave into multiple convex polygons and add them - Vector< Vector > decomp = Geometry::decompose_polygon(polygon); + Vector< Vector > decomp = _decompose_in_convex(); shape_from=co->get_shape_count(); for(int i=0;i convex = memnew( ConvexPolygonShape2D ); @@ -106,6 +106,51 @@ void CollisionPolygon2D::_update_parent() { co->_update_shapes_from_children(); } +Vector< Vector > CollisionPolygon2D::_decompose_in_convex() { + + Vector< Vector > decomp; +#if 0 + //fast but imprecise triangulator, gave us problems + decomp = Geometry::decompose_polygon(polygon); +#else + + List in_poly,out_poly; + + TriangulatorPoly inp; + inp.Init(polygon.size()); + for(int i=0;i::Element*I = out_poly.front();I;I=I->next()) { + + TriangulatorPoly& tp = I->get(); + + decomp[idx].resize(tp.GetNumPoints()); + + for(int i=0;i > decomp = Geometry::decompose_polygon(polygon); + Vector< Vector > decomp = _decompose_in_convex(); + Color c(0.4,0.9,0.1); for(int i=0;i > _decompose_in_convex(); protected: -- cgit v1.2.3