Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using the Box2D physics engine. And there's a type of shape for box2D called b2PolygonShape.

In this class, you can create polygons. There is also a Set() function that takes an array of points and a vertex count.

Box2D has an example like this:

// This defines a triangle in CCW order.

b2Vec2 vertices[3];

vertices[0].Set(0.0f, 0.0f);

vertices[1].Set(1.0f, 0.0f);

vertices[2].Set(0.0f, 1.0f);

int32 count = 3;

b2PolygonShape polygon;

polygon.Set(vertices, count);

This works. Yet when I tried to practice and mess with this function, I did this:

  b2Vec2 vertices[4];

  vertices[0].Set(0, 0);

  int32 count = 4;

  b2PolygonShape polygon;

  polygon.Set(vertices, count);

When I compiled and ran this, it crashed after the Set() function. Wouldn't this create a square?

Also in the console I got this:

Assertion failed: s > 0.0f

What did I do wrong?

share|improve this question
up vote 8 down vote accepted

That's because the four vertices you provided are in clockwise order and Box2D assumes they're in counter-clockwise, like in your first example. So it should work fine if you change it to:

  b2Vec2 vertices[4];

  vertices[0].Set(0, 0);

  int32 count = 4;

  b2PolygonShape polygon;

  polygon.Set(vertices, count);

And by the way, if you're just creating a box-shaped polygon, you can use the convenience function

void b2PolygonShape::SetAsBox(float32 hx, float32 hy, const b2Vec2& center, float32 angle)
share|improve this answer
Is there a way to make it not assume? What I'm trying to do is make a program where you can just draw shapes, sort of like PHUN. And that's where I came to these errors so I decided to try it out with the rectangle. – xAustechx Jul 25 '10 at 5:15
Not really. Polygons need to be convex and so it need to assume the winding order to verify that. If you're going to be drawing and creating polygons on the fly, you need to pre-process them to make sure they're under the vertex count limit (I think it's 6 or 8 verts) and at that point you might as well wind them correctly before you hand them off to Box2D. – Noel Llopis Jul 25 '10 at 14:25

You could determine the winding of the polygon, and then reverse it if it is anti-clockwise.

One common method is to calculate the area of the polygon using the algorithm below. It will return a +ve number for clockwise winding, and a -ve number for anticlockwise winding.

Here is a Python example:

def poly_area(verts):
    Return area of a simple (ie. non-self-intersecting) polygon.
    Will be negative for counterclockwise winding.
    accum = 0.0
    for i in range(len(verts)):
        j = (i + 1) % len(verts)
        accum += verts[j][0] * verts[i][1] - verts[i][0] * verts[j][1]
    return accum / 2
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.