简体   繁体   English

Libgdx-Box2D:将Physics Body Editor加载器蒙版附加到动态纹理

[英]Libgdx - Box2D: Attach Physics Body Editor Loader mask to dynamic texture

I have a texture of a circle, which gets drawn to a new position when a touch drag occurs. 我有一个圆形的纹理,当发生触摸拖动时,该纹理将被绘制到新的位置。 It isn't set up as a body. 它没有设置为主体。

I have made a physics map using Aurelien Ribon's Physics Body Editor Loader GUI to the circle's upper and lower part, and I'd like to draw that mask over the texture's position, and to its new position when a drag occurs. 我已经使用Aurelien Ribon的“ Physics Body Editor”加载程序GUI绘制了一个物理图,并将其绘制到圆的上部和下部,我想在纹理的位置上绘制该蒙版,并在发生拖动时绘制到其新位置。

How can I do this? 我怎样才能做到这一点? In my create method I initialize the variables, the mask gets drawn to the texture's initial position, but when I move it the mask stays at the circle's initial position. 在我的create方法中,我初始化变量,蒙版被绘制到纹理的初始位置,但是当我移动它时,蒙版停留在圆的初始位置。

Here's my code: 这是我的代码:

Create() method: Create()方法:

 //... rest of the method ommited for clarity

    karika = gameWorld.getKarika();

    World world = new World(new Vector2(0, 0), false);


Box2DDebugRenderer renderer = new Box2DDebugRenderer();
    BodyEditorLoader karikaLoader = new BodyEditorLoader(Gdx.files.internal("data/collision-masks/karika.json"));

    BodyDef karikaDef = new BodyDef();
    karikaDef.type = BodyType.DynamicBody;
    karikaDef.position.set(karika.getPosition().x, karika.getPosition().y);
    karikaDef.angle = karika.getRotation();

    Body karikaBody = world.createBody(karikaDef);

    FixtureDef karikaFixture = new FixtureDef();
    karikaFixture.density = 0.5f;
    karikaFixture.friction = 0.8f;
    karikaFixture.restitution = 0.6f;

    karikaLoader.attachFixture(karikaBody, "karika", karikaFixture, karika.getWidth());
    Vector2 karikaBodyOrigin = karikaLoader.getOrigin("karika", karika.getWidth()).cpy();

    //rest of the method ommited for clarity

My render() method: 我的render()方法:

 //...
    batch.begin();
    batch.draw(karikaTexture, karika.getPosition().x, karika.getPosition().y, karika.getWidth() / 2, karika.getHeight() / 2, karika.getWidth(), karika.getHeight(), 1, 1, karika.getRotation(), 0, 0, karikaTexture.getWidth(), karikaTexture.getHeight(), false, false);
    batch.end();
renderer.render(world, cam.combined);
    world.step(1 / 60f, 6, 2);
    //...

The texture that is being drawn in the render method is my circle's texture. 在render方法中绘制的纹理是我圈子的纹理。 As said before, I haven't set that up as a body, only the collision mask. 如前所述,我还没有将其设置为主体,只是防撞面具。

What I'd like to do, is attach the mask to the texture, and keep up with it's position, for example when I drag the circle, the mask should stay on the circle. 我想做的是蒙版附加到纹理上,并保持其位置不变,例如当我拖动圆时,蒙版应保留圆上。

Look into the DebugRenderer . 查看DebugRenderer It works very well, and does precisely what you're asking. 它工作得很好,并且恰好满足您的要求。

Here's how to use it. 这是使用方法。

debugRenderer.render(this.world, camera.combined);

And here's a more thorough tutorial . 是更详尽的教程

I recommend you assign your circle's position using physics object's position rather than trying to assign the physics object to your texture's position. 我建议您使用物理对象的位置分配圆的位置,而不要尝试将物理对象分配给纹理的位置。

In your code, it looks like you create your karikaBody using the current position of karika , but the karikaBody position is never being updated after that. 在您的代码中,好像您使用karikaBody的当前位置创建了karika ,但是karikaBody永远不会更新karikaBody位置。 So your "collision mask" (your physics body) position never changes. 因此,您的“碰撞面罩”(您的物理身体)的位置永远不会改变。

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

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