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\$
  • \$\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
3
\$\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\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.