# Why is my transform stretching?

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;

import...

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

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

projection = new Matrix4();
projection.setToProjection(
-width / 2,
width / 2,
-height / 2,
height / 2,
1,
100);

batch = new SpriteBatch();
batch.setTransformMatrix(transform);
batch.setProjectionMatrix(projection);

Gdx.gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}

@Override
public void render () {
Gdx.gl.glClear(Gdx.gl.GL_COLOR_BUFFER_BIT);

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.setTransformMatrix(transform);

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

@Override
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?

• Why not just use a Camera? This is the exact reason why it exists. – 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);
camera.update();
batch.setProjectionMatrix(camera.combined);


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.

• 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. – Paulo Marcio May 18 '17 at 11:51
• 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. – Niall Quinlan May 19 '17 at 10:48
• 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. – Niall Quinlan May 19 '17 at 10:51