开发者

box2d with custom polygon crashes

I created a polygon which has 5 vertices, and all vertices are generated by VertexHelper.

Why does the program get SIGABRT at b2Assert(area > b2_epsilon) in ComputeCentroid() in b2PolygonShape.cpp?

The program r开发者_运维知识库uns well when I use shape.SetAsBox(.359375, 1.0) instead of shape.Set(vertices, count).

It seems that somethings wrong during calculating centroid when shape.Set() is used, but I don't know how to deal with this problem.

Here's the code:

b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.awake = NO;
bodyDef.position.Set(3.125, 3.125);
bodyDef.angle = -.785398163397;
spriteBody = world->CreateBody(&bodyDef);
spriteBody->SetUserData(sprite);
b2MassData massData = {2.0, b2Vec2(0.021875, 0.203125), 0.0};
spriteBody->SetMassData(&massData);
int32 count = 5;
b2Vec2 vertices[] = {
    b2Vec2(-11.5f / PTM_RATIO, -16.0f / PTM_RATIO),
    b2Vec2(-10.5f / PTM_RATIO, 15.0f / PTM_RATIO),
    b2Vec2(10.5f / PTM_RATIO, 15.0f / PTM_RATIO),
    b2Vec2(10.5f / PTM_RATIO, -5.0f / PTM_RATIO),
    b2Vec2(-5.5f / PTM_RATIO, -16.0f / PTM_RATIO)
};
b2PolygonShape shape;
shape.Set(vertices, count);
b2FixtureDef fixtureDef;
fixtureDef.shape = &shape;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.2f;
fixtureDef.restitution = 0.7f;
spriteBody->CreateFixture(&fixtureDef);


It looks like you've wound your vertices the wrong way. I think they should be anticlockwise in box2d, at least by default.

You're assertion will be failing because the calculation for area will be returning a negative value, far less than b2_epsilon

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜