2016-03-22 21 views
11

Tôi đang phát triển trò chơi bằng libgdx và tôi muốn vẽ đường trơn tru bằng cách sử dụng trình kết xuất hình dạng.Vẽ một đường trơn tru

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

Tôi đã thử Multi Sample anti aliasing từ blog libgdx. Tôi cũng đã trải qua Anti aliased filed shape in libgdx nhưng tiếc là những dòng này không nằm trong verson libgdx mới nhất.

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

Bạn có chạy trên thiết bị Android hoặc máy tính để bàn này không? –

+3

Tôi đang chạy ứng dụng này trên thiết bị Android. –

+10

'GL_LINE_SMOOTH' đã lỗi thời (từ OpenGL ES 1.0, LibGDX không còn hỗ trợ). Bạn nên bật tính năng lấy mẫu nhiều lần bằng cách đặt 'numSamples' trong ApplicationConfiguration bạn chuyển vào trò chơi của bạn từ lớp trình khởi chạy. Nếu điều đó không hiệu quả, có lẽ bạn đang thử nghiệm trên một GPU không hỗ trợ nó. Nó cũng có thể để có được đường trơn tru mà không chống răng cưa bằng cách vẽ dài, hình chữ nhật gợn sprites có một số đệm trống ở hai bên. – Tenfour04

Trả lời

1

Enable chống alising trong cấu hình:

Đối với máy tính để bàn:

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

Đối với Android:

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

Hoặc bạn có thể tạo ra một pixmap trắng 1x1 và sử dụng nó để tạo một spr ite và vẽ đường sử dụng ma đó, cá nhân tôi thích phương pháp này istead của ShapeRenderer (lưu ý rằng không có luân chuyển trong phương pháp này):

/** 
* 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); 
} 

Với một số sửa đổi để các phương pháp trước đó, bạn có thể đưa ra với phương pháp này để vẽ một đường với xoay.

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

Phương thức xoay của bạn chỉ hoạt động nếu bạn kích hoạt tính năng ghép nhiều lớp. –

+0

@GuilhermeCamposHazan Không được kích hoạt nhiều chế độ theo mặc định? – ossobuko

+0

Không, tùy chọn của nó. Mặc định là mẫu = 0. –

Các vấn đề liên quan