0
\$\begingroup\$

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:enter image description here

\$\endgroup\$
1
  • \$\begingroup\$ Which aspect do you have a problem with? Do yoh want the line to always have the same length? \$\endgroup\$
    – Bálint
    Feb 1 '17 at 6:47
4
\$\begingroup\$

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)

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .