I'm working on a 2D platformer, shooter game and I'm attempting to draw a line to serve as a guide to indicate where the player is shooting. The line should be drawn from the player's sprite to the mouse's position. I need to draw a line between the player and the mouse, at the angle from the player to the mouse, how can I do something like this? Here is a concept picture to explain what I'm talking about:
-
\$\begingroup\$ Which aspect do you have a problem with? Do yoh want the line to always have the same length? \$\endgroup\$– BálintFeb 1, 2017 at 6:47
2 Answers
you should use ShapeRenderer for this.
In your draw loop
batch.begin();
// Draw sprites and stuff here so the line will be draw on them and not under
batch.end();
shapeRenderer.begin(ShapeType.Line);
shapeRenderer.setColor(1, 0, 0, 1); // Red line
shapeRenderer.line(player.x, player.y, mouse.x, mouse.y);
shapeRenderer.end();
This will let you draw a red line between the player and the mouse, the angle will be determined automatically by the x,x2 and y,y2. (You can still compute it if you want to).
I let you the implementation part (draw only on some conditions etc)
Accepted answer is good, but I couldn't use ShapeRenderer as it need to be used on closed batch.
In that case you can use ShapeDrawer which is ShapeRenderer that can be used on open batch.