[LibGDX] ShapeRenderer producing weird results

I’m having trouble with ShapeRenderer. All I’m doing is creating an instance of ShapeRender then I’m calling this method when I render:


public void draw() {
	sr.begin(ShapeType.Line);
	sr.rect(15.5f, 13.75f, 279f, 247.5f);
	sr.end();
}

This is the result that I see on the screen.

http://img546.imageshack.us/img546/2308/u7wk.png

It seems to be drawing the rectangle at (10, 11) with a size of (181, 214). I sussed that the problem is probably the projection matrix, so I tried to set the projection matrix to the projection matrix of the stage. When I do that though, nothing is rendered. What could be the problem?

Hm, well think about setting the projection matrix to the stage, how would that help? Unless your stage has a seperate size or coordinate system all your doing is making shit complicated by setting something that is the same as it would have been if you had done it normally.

Care to paste your camera constructor and any settings you change afterwards.

Also I suggest you chuck the pixel coordinate shit out the window and get yourself a proper measurement system, meters or something.

That was my original thinking, though I couldn’t think of any other reason of why it’s not rendering correctly.
All I’m doing is setting up a stage (I’m extending stage in my screen class, as I see no reason why it’d be a bad idea), then I’m instantiating a separate class which at the moment simply calculates the bounds of the rectangle to draw. This is my resize method:


public void resize(int width, int height) {
	uiArea.resize(width, height); // calculates the bounds of the rectangle
	super.setViewport(width, height); // sets the stage viewport
}

This is my render method:


	public void render(float delta) {
	Gdx.gl.glClearColor(bg.getRed(), bg.getGreen(), bg.getBlue(), 1f);
	Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

	super.act(delta);
	super.draw();

	uiArea.draw(); // calls the draw method which I posted in my OP
}

If you’re wondering, the values I posted in my OP for sr.rect are the values for the screen size of (310, 275).

Right so you have no camera then, instead you are actually using the stages camera.

In that case you do have to set the projection matrix to the stages camera (I have in my code).

I have literally no idea why it is not rendering after that. In my code I have my shape renderer first in the render method, after the GL code and it seems to work. If I move it after all my backgrounds and sprites, it does jack.

I fixed this, finally…
At first I found a quick fix which was to manually set the combined matrix of the stage camera. After some snooping around through the source code I figured that I had to update the camera after setting the viewport.

This is the working render method, if anyone else finds this thread in the future:


@Override
public void resize(int width, int height) {
	super.setViewport(width, height);
	super.getCamera().update();
	uiArea.setProjectionMatrix(super.getCamera().combined);
	uiArea.resize(width, height);
}

To add onto this, just update the camera every loop iteration, it’s not expensive and it saves you from having this issue all over again when you decide to have a dynamic camera.

Prevent any future problems, n1 though, problem solved is always a relief hehe.