Reputation: 21
I'm new to Libgdx and 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
Upvotes: 2
Views: 872
Reputation: 155
It looks like you are drawing using box2d's debug render. It may work, but is generally not a good approach. You can keep your arc-vertex creating code, but render it differently. Look into com.badlogic.gdx.graphics.glutils.ShapeRenderer.polyline
method. 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.
Upvotes: 1