2016-03-22 26 views
11

Sto sviluppando un gioco usando libgdx e voglio disegnare una linea liscia usando il renderizzatore di forme.Disegna una linea liscia

 shaperenderer.begin(ShapeType.Line); 
     shaperenderer.line(fisrstVec2,secondVec2); 
     shaperenderer.end(); 

ho cercato Multi Sample anti aliasing dal blog libgdx. Sono anche passato attraverso Anti aliased filed shape in libgdx ma sfortunatamente queste righe non si trovano nell'ultima versione di libgdx.

Gdx.gl.glEnable(GL10.GL_LINE_SMOOTH); 
    Gdx.gl.glEnable(GL10.GL_POINT_SMOOTH); 
+2

Lo stai eseguendo su dispositivo Android o desktop? –

+3

Sto eseguendo questo nei dispositivi Android. –

+10

'GL_LINE_SMOOTH' è obsoleto (da OpenGL ES 1.0, che LibGDX non supporta più). Dovresti abilitare il multicampionamento impostando 'numSamples' nella ApplicationConfiguration che passi nel tuo gioco dalla classe di avvio. Se non funziona, forse stai testando su una GPU che non la supporta. È anche possibile ottenere linee morbide senza anti-aliasing disegnando sprite rettangolari sottili e magre che presentano alcune imbottiture vuote sui lati. – Tenfour04

risposta

1

Abilita anti-alising nella configurazione:

Per Desktop:

LwjglApplicationConfiguration config = new LwjglApplicationConfiguration(); 
    config.samples = 2; 
    new LwjglApplication(new MyGdxGame(Helper.arrayList(arg)), config); 

Per Android:

AndroidApplicationConfiguration config = new AndroidApplicationConfiguration(); 
    config.numSamples = 2; 
    initialize(new MyGdxGame(null), config); 

Oppure si potrebbe creare una pixmap bianco 1x1 e usalo per creare uno spr ite e tracciare la linea utilizzando tale sprite, io personalmente preferisco questo metodo istead di ShapeRenderer (si noti che non v'è alcuna rotazione in questo metodo):

/** 
* draws a line on the given axis between given points 
* 
* @param batch  spriteBatch 
* @param axis  axis of the line, vertical or horizontal 
* @param x   x position of the start of the line 
* @param y   y position of the start of the line 
* @param widthHeight width or height of the line according to the axis 
* @param thickness thickness of the line 
* @param color  color of the line, if the color is null, color will not be changed. 
*/ 
public static void line(SpriteBatch batch, Axis axis, float x, float y, float widthHeight, float thickness, Color color, float alpha) { 
    if (color != null) sprite.setColor(color); 
    sprite.setAlpha(alpha); 
    if (axis == Axis.vertical) { 
     sprite.setSize(thickness, widthHeight); 
    } else if (axis == Axis.horizontal) { 
     sprite.setSize(widthHeight, 1); 
    } 
    sprite.setPosition(x,y); 
    sprite.draw(batch); 
    sprite.setAlpha(1); 
} 

Con alcune modifiche al metodo precedente, si può trovare con questo metodo per disegnare una linea con rotazione.

public static void rotationLine(SpriteBatch batch, float x1, float y1, float x2, float y2, float thickness, Color color, float alpha) { 
    // set color and alpha 
    if (color != null) sprite.setColor(color); 
    sprite.setAlpha(alpha); 
    // set origin and rotation 
    sprite.setOrigin(0,0); 
    sprite.setRotation(getDegree(x2,y2, x1, y1)); 
    // set position and dimension 
    sprite.setSize(distance(x1,y1,x2,y2),thickness); 
    sprite.setPosition(x1, y1); 
    // draw 
    sprite.draw(batch); 
    // reset rotation 
    sprite.rotate(0); 
} 

public static float getDegree(float x, float y, float originX, float originY) { 
    float angle = (float) Math.toDegrees(Math.atan2(y - originY, x - originX)); 
    while (angle < 0 || angle > 360) 
     if (angle < 0) angle += 360; 
     else if (angle > 360) angle -= 360; 
    return angle; 
} 

public static float distance(float x, float y, float x2, float y2) { 
    return (float) Math.sqrt(Math.pow((x2 - x), 2) + Math.pow((y2 - y), 2)); 
} 
+0

Il metodo ruotato funziona solo se abiliti il ​​multicampionamento. –

+0

@GuilhermeCamposHazan Il multicampionamento non è abilitato per impostazione predefinita? – ossobuko

+0

No, è opzionale. L'impostazione predefinita è samples = 0. –