Skip to content

Commit

Permalink
Ensure that array passed to physics is always counter clockwise, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
reduz committed Nov 14, 2018
1 parent 88bfb27 commit 16022da
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
15 changes: 15 additions & 0 deletions core/math/geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,21 @@ class Geometry {
return Vector<Vector<Vector2> >();
}

static bool is_polygon_clockwise(const Vector<Vector2> &p_polygon) {
int c = p_polygon.size();
if (c < 3)
return false;
const Vector2 *p = p_polygon.ptr();
real_t sum = 0;
for (int i = 0; i < c; i++) {
const Vector2 &v1 = p[i];
const Vector2 &v2 = p[(i + 1) % c];
sum += (v2.x - v1.x) * (v2.y + v1.y);
}

return sum > 0.0f;
}

static PoolVector<PoolVector<Face3> > separate_objects(PoolVector<Face3> p_array);

static PoolVector<Face3> wrap_geometry(PoolVector<Face3> p_array, real_t *p_error = NULL); ///< create a "wrap" that encloses the given geometry
Expand Down
7 changes: 6 additions & 1 deletion scene/resources/convex_polygon_shape_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ bool ConvexPolygonShape2D::_edit_is_selected_on_click(const Point2 &p_point, dou

void ConvexPolygonShape2D::_update_shape() {

Physics2DServer::get_singleton()->shape_set_data(get_rid(), points);
Vector<Vector2> final_points = points;
if (Geometry::is_polygon_clockwise(final_points)) { //needs to be counter clockwise
final_points.invert();
}
Physics2DServer::get_singleton()->shape_set_data(get_rid(), final_points);
emit_changed();
}

Expand All @@ -55,6 +59,7 @@ void ConvexPolygonShape2D::set_point_cloud(const Vector<Vector2> &p_points) {
void ConvexPolygonShape2D::set_points(const Vector<Vector2> &p_points) {

points = p_points;

_update_shape();
}

Expand Down

3 comments on commit 16022da

@volzhs
Copy link
Contributor

@volzhs volzhs commented on 16022da Nov 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems pointed wrong issue number...

@akien-mga
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume it was meant to be #15631.

@reduz
Copy link
Member Author

@reduz reduz commented on 16022da Nov 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, sorry, had so many open I mixed them up

Please sign in to comment.