How to draw a line in LibGDX?

Draw a line in LibGdx that passes for a certain point

  • In LibGdx there's a really easy way to draw shapes, currently I'm drawing lines like this: shapeRenderer.begin(ShapeType.Line); shapeRenderer.line(x1, y1, x2, y2); shapeRenderer.end(); There's a defined starting and final point, let's call them point 1 and point 2, respectively. I need to draw a line that starts in point 1, pass point 2 and continues in the same direction, like this: Any ideas how can this be done?

  • Answer:

    Let's say you have two vectors representing your two points: Vector2 p1 = new Vector2(x1, y1); Vector2 p2 = new Vector2(x2, y2); Then what you want to do can be achieved by simply doing that: Vector2 p3 = new Vector2(p2).sub(p1).add(p2); shapeRenderer.line(p2.x, p2.y, p3.x, p3.y); You can change the distance between p2 and p3 by using the scl() method when computing p3: Vector2 p3 = new Vector2(p2).sub(p1).scl(0.5f).add(p2); For example if you use scl(1f) or don't use it at all like in the previous code snippet, distance between p2 and p3 will be the same as between p1 and p2. If you call scl(0.5f) length will be divided by 2, etc.

Cesar Orozco at Stack Overflow Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.