簡體   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