简体   繁体   English

使用Polygonshape创建夹具时,Box2D引发错误

[英]Box2D throws error when creating fixture with Polygonshape

i work with libGDX. 我使用libGDX。 When i am trying in Box2D to create a fixture having a polygon-shape i get following error.: 当我在Box2D中尝试创建具有多边形形状的灯具时,出现以下错误:

java: ./Box2D/Collision/b2Distance.h:103: const b2Vec2& b2DistanceProxy::GetVertex(int32) const: Assertion `0 <= index && index < m_count' failed.

When i don't do Box2D's world.step() , i don't get this error any more. 当我不执行Box2D的world.step() ,我不再收到此错误。

So i commented out everything in my WorldContactListener and i added world.step() again. 所以我注释掉了WorldContactListener中的所有内容,然后再次添加了world.step()。

I am still getting the same Error. 我仍然遇到相同的错误。

When i replace the polygon-shape with a circle-shape everything works fine. 当我将多边形替换为圆形时,一切正常。 So here is how i am creating my polygon-shape: 所以这就是我创建多边形的方式:

 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);

And here is how i am adding everything it the Box2D world: 这就是我如何在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);

I would be really happy if you could tell me whats wrong! 如果您能告诉我怎么了,我将非常高兴!

Thank You 谢谢

More of a wild guess, but do you have your ppm conversion going the right way? 更多的猜测,但您的ppm转换是否正确? 1/ppm (which you indicate is 75) gives quite a small value. 1 / ppm(您表示为75)给出的值很小。 I didn't dig into the bowels of the box2d code, but as it works best when objects are defined in meters, creating a polygon with vertices 0,0 and 0,0.0133 (1cm) might "confuse it" (meaning some kind of rounding error or something somewhere in the code so it can't distinguish vertices and thinks there are not at least 3 of them.) 我没有深入研究box2d代码的内容,但是当以米为单位定义对象时,这种方法最有效,因此创建一个顶点为0,0和0,0.0133(1cm)的多边形可能会“混淆它”(表示某种舍入错误或代码中的某处,因此它无法区分顶点,并认为其中至少有3个。)

For example, a bare bones app with 3 versions of your vertices code yields runtime exceptions on the first 2 versions (small values) but no runtime exception with larger values: 例如,具有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);
*/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM