I've read a lot about transformations: model to world space, world to camera space and projection transformationsl but when programming it, I can't get things right. I think I'm missing something, so here is my example code:

package com.test.rendering;


public class Test extends ApplicationAdapter {
Texture texture;
Matrix4 projection;
Matrix4 transform;
SpriteBatch batch;
float angle = 0f;

public void create () {
    float width = Gdx.graphics.getWidth();
    float height = Gdx.graphics.getHeight();

    projection = new Matrix4();
            -width / 2,
            width / 2,
            -height / 2,
            height / 2,

    batch = new SpriteBatch();

    texture = new Texture("badlogic.jpg");
    Gdx.gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

public void render () {

    angle = (angle + 0.5f * Gdx.graphics.getDeltaTime()) % 360;

    transform = new Matrix4();
    //translate the world so we can see the texture at z = 0
    transform.translate(0.0f, 0.0f, -5.0f);
    transform.rotate(Vector3.X, angle);


    batch.draw(texture, -texture.getWidth() / 2, -texture.getHeight() / 2);

public void dispose () {

If you run this code you will see a very weird transformation: the texture starts rotating, but then stretches until some part of the screen, and then slowly disappears. I was expecting to see the texture just rotating around the x axi, with no stretching. Why is this happening? What is wrong with my code?

| improve this question | | | | |
  • \$\begingroup\$ Why not just use a Camera? This is the exact reason why it exists. \$\endgroup\$ – Charanor May 19 '17 at 13:22

Personally I always just use an OrthographicCamera like so:

    camera.setToOrtho(false, viewportWidth, viewportHeight);
    camera.position.set(x, y);

That's all you need for a spritebatch. Not sure why you're using Matrix4.setToProjection() that is for 3d, I think you might want to use MAtrix.setToOrtho2d(x, y, w, h); But I would recommend just using a camera.

| improve this answer | | | | |
  • \$\begingroup\$ I know it might seems weird but i really want a perspective projection, as i said i'm getting a weird behaviour, if you could run the example code and tell me what you think that could be the problem it would help. \$\endgroup\$ – Paulo Marcio May 18 '17 at 11:51
  • \$\begingroup\$ Why are you rotating the translation matrix? If the sprites are being rendered at an angle they will be misshapen and disappear once they are in front of or behind the clipping planes. If you want a perspective projection use a PerspectiveCamera. But it doesn't make sense with a spritebatch. \$\endgroup\$ – Niall Quinlan May 19 '17 at 10:48
  • \$\begingroup\$ You just want the sprite to rotate? Try changing that rotation to z axis. X axis runs left to right so rotation around that will tilt up/down I think. \$\endgroup\$ – Niall Quinlan May 19 '17 at 10:51

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.