0

Basically, I want my coordinate system to have (0,0) as the top left corner of the screen(used to this in Swing and Android Canvas drawing). I saw a great thread for doing so Changing the Coordinate System in LibGDX (Java). I used the code that the creator provided but the rectangle I am drawing is still at the bottom of the screen.(screenshot)

enter image description here

Here is my code for drawing the rectangle(I use one class, GameWorld, to manage all the "game objects" and another class, GameRenderer to render all the game objects) Both classes have a method that will be called from the GameScreen's render method. Relevant code in GameScreen.java

public class GameScreen implements Screen {
          //manage game objects
          private GameWorld world;
          //render game objects 
          private GameRenderer renderer;
          public GameScreen() {
                 world = new GameWorld();
                 renderer = new GameRenderer(world);
          }
          public void render(float delta) {
                  world.update(delta);
                   renderer.render();
           }
          .....
  }

And in GameWorld.java

public class GameWorld {
    private Rectangle rectangle;
    public GameWorld() {
              rectangle = new Rectangle(0, 0, 17, 12);
    }
     public void update(float delta) {
            rectangle.setX(rectangle.getX() + 1);
             if(rectangle.getX() > Gdx.graphics.getWidth()) {
                       rectangle.setX(0);
            }
     }
     public Rectangle getRect() {
              return rectangle;
      }
  }

And in GameRenderer.java

public class GameRenderer {
    private ShapeRenderer shapeRenderer;
    private OrthographicCamera cam;
    private GameWorld myWorld;
    public GameRenderer(GameWorld theWorld) {
        myWorld = theWorld;
        cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        cam.setToOrtho(true, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

    }
    public void render() {
        shapeRenderer = new ShapeRenderer();
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
        shapeRenderer.setColor(87/255.0f, 109/255.0f, 120/255.0f, 1);
        Rectangle toDraw = myWorld.getRect();
        shapeRenderer.rect(toDraw.getX(), toDraw.getY(),
                toDraw.getWidth(), toDraw.getHeight());
        shapeRenderer.end();

    }
}

And finally in Desktop.java where I am testing the game,

public class DesktopLauncher {
     public static void main (String[] arg) {
             LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
             config.title = "My test";
             config.width = 272;
             config.height = 408;
             new LwjglApplication(new MyGdxGame(), config);
      }

}

Does anyone see where i am doing wrong? Why is the rectangle still being drawn at the bottom of the screen. I used the provided code from the creator and specified the rectangle's top y coordinate as zero.

Community
  • 1
  • 1
committedandroider
  • 7,124
  • 12
  • 48
  • 114

1 Answers1

1

You didn't apply your camera's projection to the ShapeRenderer.

Add this line right before shapeRenderer.begin():

shapeRenderer.setProjectionMatrix(cam.combined);

Also, you should move ShapeRenderer's instantiation from render() to the class constructor so you aren't creating a new one on every frame.

Tenfour04
  • 39,254
  • 8
  • 47
  • 96
  • committedandroider I think the answer Tenfour04 is correct, I just want to say, why? you do not use 0.0 in the lower left corner, is it not more comfortable? just out of curiosity. – Angel Angel Dec 12 '14 at 20:40
  • Angel, I think 0 will just get interpreted as 0.0 – committedandroider Dec 13 '14 at 00:23
  • @AngelAngel Flash and Swing both use a Y-down system, so it's more comfortable for people who have already been using those. – Tenfour04 Dec 13 '14 at 02:07
  • @Tenfour04 thanks for answering to my thought that was the most comfortable, without adding you do not have to be aware to use, setProjectionMatrix, but of course each one has its way and if more accustomed is logical that appeals more – Angel Angel Dec 13 '14 at 16:22