[英]Easy way of drawing a straight dotted line in libgdx?
I would like to draw a straight dotted line in libgdx for android game between to points of the screen. 我想在libgdx中绘制一条直线虚线,用于Android游戏之间的屏幕点。
Currently I have the following code to draw the not-dotted line, using a ShapeRenderer
: 目前我有以下代码使用
ShapeRenderer
绘制非虚线:
shapeRenderer.begin(ShapeType.Line);
//draws normal line, would prefer it dotted...............
shapeRenderer.line(touchPos.x, touchPos.y, someSprite.getX(), someSprite().getY());
shapeRenderer.end();
I have seen another question about dotted lines , but it is a bit of an overkill for me since I don't need it curved etc. I just need a straight dotted line , like 我已经看到了关于虚线的另一个问题 ,但是对我来说这有点过分,因为我不需要弯曲等等。我只需要一条直线虚线 ,就像
...............................................................................................
.................................................. .............................................
Was thinking about having a loop that just calculates the positions of the dots on the line and just draws dots there? 正在考虑有一个循环,只计算线上点的位置,只是在那里画点? but is that really necessary, does anyone know a simpler way?
但这是否真的有必要,有没有人知道更简单的方法?
Currently I am using this method to draw the dotted lines : 目前我正在使用此方法绘制虚线 :
/**
* Draws a dotted line between to points (x1,y1) and (x2,y2).
* @param shapeRenderer
* @param dotDist (distance between dots)
* @param x1
* @param y1
* @param x2
* @param y2
*/
private void drawDottedLine(ShapeRenderer shapeRenderer, int dotDist, float x1, float y1, float x2, float y2) {
shapeRenderer.begin(ShapeType.Point);
Vector2 vec2 = new Vector2(x2, y2).sub(new Vector2(x1, y1));
float length = vec2.len();
for(int i = 0; i < length; i += dotDist) {
vec2.clamp(length - i, length - i);
shapeRenderer.point(x1 + vec2.x, y1 + vec2.y, 0);
}
shapeRenderer.end();
}
So basically I calculated the vector of the line to draw and looped through it to draw the dots based on the desired dot distance. 所以基本上我计算了绘制线的矢量并通过它循环以根据所需的点距离绘制点。 A distance of 10 looked quite nice in my test:
在我的测试中,10的距离看起来相当不错:
drawDottedLine(shapeRenderer, 10, x1, y1, x2, y2);
This works quite smooth for me. 这对我来说非常顺利。 If you have a nicer way of drawing dotted lines then please let me know.
如果您有更好的绘制虚线的方法,请告诉我。
You could draw a series of points, which in turn will make a dotted line. 您可以绘制一系列点,这些点又会形成虚线。 And create a for loop which draws a series of points until the end.
并创建一个for循环,绘制一系列点直到结束。 The code would look something like this:
代码看起来像这样:
shapeRenderer.begin(ShapeType.Point);
for (float i = touchPos.x; i < someSprite.getX(); i += (someSprite.getY() / touchPos.y))
{
for (float j = touchPos.y; j < someSprite.getY(); j += (someSprite.getX() / touchPos.x))
{
// floats used because the increment might be decimal places.
shapeRenderer.point(i, j, 0);
}
}
shapeRenderer.end();
Don't know if this would work for you, but you can give it a try and edit the increment values for i
and j
to your desire. 不知道这对你是否有用,但你可以尝试一下,根据你的需要编辑
i
和j
的增量值。
Overall, I'm afraid that there is no predefined function for drawing dotted lines in libgdx. 总的来说,我担心在libgdx中没有用于绘制虚线的预定义函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.