简体   繁体   English

libgdx box2d drawarc

[英]libgdx box2d drawarc

I'm new to Libgdx and box2d. 我是Libgdx和box2d的新手。 I needed to draw arc. 我需要画弧线。 I searched for a function finally I came up like below 我搜索了一个函数,最后我像下面这样

public void drawarc (float centerx, float centery,float radius, float anglefrom, float anglediff, int steps)
{
    EdgeShape ps = new EdgeShape();

    FixtureDef psfd = new FixtureDef();
    psfd.shape = ps;

    BodyDef psbd = new BodyDef();
    psbd.allowSleep = true;
    psbd.awake = true;
    psbd.position.set(centerx, centery);
    psbd.gravityScale = 0;

    Vector2[] vertices = new Vector2[steps];

    for (int i = 0; i < steps; i++) {
        double angle=Math.toRadians(anglefrom+anglediff/steps*i);
        Vector2 sc = new Vector2((float)(radius * Math.cos(angle)), 
                (float)(radius * Math.sin(angle)));
        vertices[i] = sc;
    }

    Body psd = world.createBody(psbd);

    for (int i = 1; i < steps; i++) {
        ps.set(vertices[i-1], vertices[i]);
        psd.createFixture(psfd);
    }
}

Its working properly but I'm not sure if its the correct way or not. 它的工作正常,但我不确定它的正确方法。 Would you please check and tell me if its the efficient/correct way or not? 您能检查一下并告诉我它是否有效/正确吗?

Thanks 谢谢

It looks like you are drawing using box2d's debug render. 看起来您正在使用box2d的调试渲染进行绘制。 It may work, but is generally not a good approach. 它可能有效,但通常不是一个好的方法。 You can keep your arc-vertex creating code, but render it differently. 您可以保持arc-vertex创建代码,但是以不同的方式呈现它。 Look into com.badlogic.gdx.graphics.glutils.ShapeRenderer.polyline method. 查看com.badlogic.gdx.graphics.glutils.ShapeRenderer.polyline方法。 This is also not the best solution, but is quite easy and way more efficient than your method, because you are creating a new physical body, when you don't need one. 这也不是最好的解决方案,但是比您的方法更容易实现,而且效率更高,因为您不需要的时候就可以创建一个新的物理身体。

Note that you shouldn't use debug draw for game rendering, since it is debug draw and not very fast. 请注意,您不应该将调试绘制用于游戏渲染,因为它是调试绘制并且速度不是很快。 Proper way would probably be using the Mesh class. 正确的方法可能是使用Mesh类。

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

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