ChainShape in Box2D

蓝咒 提交于 2019-12-02 19:11:17

问题



I recently began to learn libgdx and I am stuck at a problem with the CainShape of Box2D.

My first goal is to simply create a box with a ChainShape.

In order to achieve that, I added four Vector2 to an array and use them to create a loop.

The result is depending on the arrangement in the array either an hourglass shaped thing (top left is connected with bottom right and top right is connected with bottom left) or the error

Expression: b2DistanceSquared(v1, v2) > 0.005f * 0.005f

This is the code I used so far:

Vector2[] box = new Vector2[4];

    box[1] = new Vector2(0 -    bounds.getWidth() / 2 / Main.PPM, 0 -   bounds.getHeight() / 2 / Main.PPM);


    box[0] = new Vector2(       bounds.getWidth() / 2 / Main.PPM, 0 -   bounds.getHeight() / 2 / Main.PPM);


    box[2] = new Vector2(0 -    bounds.getWidth() / 2 / Main.PPM,       bounds.getHeight() / 2 / Main.PPM);


    box[3] = new Vector2(       bounds.getWidth() / 2 / Main.PPM,       bounds.getHeight() / 2 / Main.PPM);

    ChainShape chainShape = new ChainShape();
    chainShape.createLoop(box);

    fdef.shape = chainShape;
    fixture = body.createFixture(fdef);

I hope somebody can tell me what I am missing.
Thanks in advance!


回答1:


Adjust the parameters you are passing and put in this code. This code will definitely run

ChainShape chain = new ChainShape();

createChain().Vec2[] vertices = new Vec2[2];
vertices[0] = box2d.coordPixelsToWorld(0,150); 
vertices[1] = box2d.coordPixelsToWorld(width,150);
chain.createChain(vertices, vertices.length);

FixtureDef fd = new FixtureDef();
fd.shape = chain; 
fd.density = 1;
fd.friction = 0.3;
fd.restitution = 0.5;
body.createFixture(fd);

Hope this answers the question if not please ask.



来源:https://stackoverflow.com/questions/35381381/chainshape-in-box2d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!