繁体   English   中英

Box2D中的ChainShape

[英]ChainShape in Box2D


我最近开始学习libgdx,而Box2D的CainShape则困扰我。

我的第一个目标是简单地创建一个带有ChainShape的盒子。

为了实现这一点,我在数组中添加了四个Vector2并使用它们创建一个循环。

结果取决于数组的排列方式是沙漏形的东西(左上与右下相连,右上与左下相连)还是错误

表达式:b2DistanceSquared(v1,v2)> 0.005f * 0.005f

这是我到目前为止使用的代码:

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

我希望有人能告诉我我所缺少的。
提前致谢!

调整要传递的参数,并将其放入此代码中。 此代码肯定会运行

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

希望这可以回答问题,如果不是请问。

暂无
暂无

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

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