How to draw a line using libgdx?

I am trying to draw some lines for debugging using libgdx, but I failed. this is my code

public class MainMenu extends Screen implements InputProcessor { private OrthographicCamera camera; SpriteBatch myBatch; ShapeRenderer shapeDebugger; public MainMenu(Game game) { super(game); camera= new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); camera.setToOrtho(true, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); camera.update();} @Override public void render(float delta) { Gdx.gl.glClearColor(0, 0, 0, 1); Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); myBatch.begin(); stage.draw(); Gdx.gl10.glLineWidth(10); shapeDebugger.setProjectionMatrix(camera.combined); shapeDebugger.begin(ShapeType.Line); shapeDebugger.setColor(1, 1, 1, 1); shapeDebugger.line(2, 2, 5, 5); myBatch.end(); } } 

I get an error from the line

shapeDebugger.setProjectionMatrix (camera.combined);

@ Pranav008

Thank you very much. I did not expect that I need to initiate it. but i have a real problem. I draw a line in the center of the game screen as follows.

  Gdx.gl10.glLineWidth(2); shapeDebugger.setProjectionMatrix(camera.combined); shapeDebugger.begin(ShapeType.Line); shapeDebugger.setColor(1, 1, 1, 1); shapeDebugger.line(Gdx.graphics.getWidth()/2, 0,Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()); shapeDebugger.end(); 

when I try to resize the screen, it does not update the center, it goes far to the right.

+4
source share
3 answers

You must have a nullpointerException because you did not make any ShapeRenderer object. Insert this line into your constructor.

 shapeDebugger=new ShapeRenderer(); 
+5
source

Remember that nesting Shaperender using SpriteBatch can cause problems.

Mark the link.

+3
source

My answer is too late for you, but for people like the same positioning problems.

The second question about position after resizing is that the viewport has not changed. Aldo the size of your window has changed your application. Stil uses the same pixel size that is set to create by camera.setToOrtho

Update view port when resizing!

 //----------------------------------------------------------------- @Override public void resize (int width, int height) { camera.setToOrtho(true, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); camera.update(); } 
+1
source

Source: https://habr.com/ru/post/1493344/


All Articles