我使用libGDX。 当我在Box2D中尝试创建具有多边形形状的夹具时,我得到以下错误。:
java: ./Box2D/Collision/b2Distance.h:103: const b2Vec2& b2DistanceProxy::GetVertex(int32) const: Assertion `0 <= index && index < m_count' failed.
当我不做Box2D world.step()
时,我不会再出现此错误。
所以我在WorldContactListener中注释了所有内容,并再次添加了world.step()。
我仍然得到相同的错误。
当我用圆形替换多边形时,一切正常。 所以这就是我创建多边形的方式:
PolygonShape shape = new PolygonShape();
float ppm = Game.PixelsPerMeter;
Vector2[] vertices = new Vector2[3];
vertices[0] = new Vector2(0f/ppm , 0f );
vertices[1] = new Vector2(1/ppm , 1f/ppm );
vertices[2] = new Vector2(0f/ppm ,1f/ppm);
shape.set(vertices);
以下是我如何在Box2D世界中添加所有内容:
float ppm = Game.PixelsPerMeter
BodyDef bdef = new BodyDef();
bdef.position.set(100/ ppm, 200/ ppm);
bdef.type = BodyDef.BodyType.DynamicBody;
b2dbody = world.createBody(bdef);
FixtureDef mainFdef = new FixtureDef();
mainFdef.shape = Shape; //this is the shape from above of course
b2dbody.createFixture(mainFdef).setUserData(this);
如果你能告诉我什么是错的,我会很高兴的!
谢谢
答案 0 :(得分:1)
更多的猜测,但你的ppm转换是否正确? 1 / ppm(您指出为75)给出了相当小的值。我没有深入研究box2d代码的内容,但是当以米为单位定义对象时效果最好,创建一个顶点为0,0和0,0.0133(1cm)的多边形可能会“混淆”(意思是某种形式)舍入错误或代码中某处的某些东西,因此它无法区分顶点并认为它们中至少有3个。)
例如,具有3个版本的顶点代码的裸骨应用程序会在前2个版本(小值)上产生运行时异常,但没有运行时异常具有更大的值:
/* Version 1 (your code) - Runtime error
vertices[0] = new Vector2(0f/ppm , 0f );
vertices[1] = new Vector2(1f/ppm , 1f/ppm );
vertices[2] = new Vector2(0f/ppm ,1f/ppm);
*/
/* Version 2 (your actual values) - Runtime error
vertices[0] = new Vector2(0f , 0f );
vertices[1] = new Vector2(0.0133f , .0133f );
vertices[2] = new Vector2(0f , 0.0133f);
*/
/* Version 3 (larger values) - No error
vertices[0] = new Vector2(0f , 0f );
vertices[1] = new Vector2(1f , 1f );
vertices[2] = new Vector2(0f ,1f);
*/