本文介绍了画一条平滑的线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用libgdx开发游戏,我想使用形状渲染器绘制平滑线.
I am developing a game using libgdx and i want to draw a smooth line using shape renderer.
shaperenderer.begin(ShapeType.Line);
shaperenderer.line(fisrstVec2,secondVec2);
shaperenderer.end();
我尝试过libgdx博客中的多样本抗锯齿.我还经历了 libgdx中的反锯齿形但是不幸的是,这些行并不是libgdx的最新版本.
I have tried Multi Sample anti aliasing from libgdx blog.I have also went through Anti aliased filed shape in libgdxbut unfortunately these line is not in latest verson of libgdx.
Gdx.gl.glEnable(GL10.GL_LINE_SMOOTH);
Gdx.gl.glEnable(GL10.GL_POINT_SMOOTH);
推荐答案
在配置中启用抗混叠:
对于台式机:
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
config.samples = 2;
new LwjglApplication(new MyGdxGame(Helper.arrayList(arg)), config);
对于Android:
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
config.numSamples = 2;
initialize(new MyGdxGame(null), config);
或者您可以创建一个1x1的白色像素图,并使用它来创建一个精灵并使用该精灵绘制线条,我个人更喜欢此方法,而不是ShapeRenderer(请注意,此方法中没有旋转):
/**
* 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);
}
对以前的方法进行了一些修改,您可以提出此方法来绘制旋转线.
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));
}
这篇关于画一条平滑的线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!