Box2D中的ChainShape [英] ChainShape in Box2D

查看:573
本文介绍了Box2D中的ChainShape的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我最近开始学习libgdx,我遇到了Box2D的CainShape问题。


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

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

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

为了达到这个目的,我在一个数组中添加了四个Vector2并使用它们来创建一个循环。

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


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

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!

推荐答案


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

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.

这篇关于Box2D中的ChainShape的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆