ivansafrin/Polycode — Issue #79: Physics2D crashes if an ENTITY_MESH has >8 vertices
Author: mcclure
Created: 2011-12-28 20:52:03+00:00
State: open

Using Polycode c3c534c with minor patches. I've got some code that looks like:

```C++
ScreenMesh *s = new ScreenMesh(Mesh::TRIFAN_MESH);
Mesh *m = s->getMesh();
Polygon *p = new Polygon();
for(vcvi b = points.begin(); b != points.end(); b++) {
p->addVertex(b->x,b->y,0);
}
m->addPolygon(p);
physics()->addPhysicsChild(s, physicsType, isStatic, friction, density, restitution, isSensor, fixedRotation);
```

If "points" contains 8 points, this code works fine.

If "points" contains 9 points, this code crashes inside Box2D.

I've seen the crash happen in different places, I've seen it in

#0	0x100161c73 in b2PolygonShape::Set at basic_string.tcc:685
#1	0x1001422e1 in Polycode::PhysicsScreenEntity::PhysicsScreenEntity at basic_string.tcc:685
#2	0x10013f848 in Polycode::PhysicsScreen::addPhysicsChild at basic_string.tcc:685

And also deep inside the render() call, inside something like calculatepolygoncollision(). The top of the stack where the crash occurs always seems to be inside Box2D code.

If there is an unavoidable limitation that Box2D can only go up to 8 vertices, it might be helpful for either the addPhysicsChild to detect we are about to add a too-many-vertices object and fail; or, at least, for this limitation to be documented.