I want to render an image at coordinates 100, 50, 0. Consider the following code…
package com.mygdx.game;
import com.badlogic.gdx.ApplicationAdapter;
import scenegraph.*;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Matrix4;
/**
* Object representing the core of the game. Which platform it compiles for
* varies
* @author William Andrew Cahill
*/
public class GameLauncher extends ApplicationAdapter
{
// Rendering variables
private SpriteBatch batch; // Object that renders all the sprites
// Region to draw
TextureRegion region;
@Override
public void create()
{
// Initializes batch
batch = new SpriteBatch();
// Stores first image
region = new TextureRegion(new Texture(Gdx.files.internal("res/entities/wiggles/wigglesIdle.png")));
}
@Override
public void render()
{
// Clears the screen
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// Begins drawing
batch.begin();
// Gets matrix and copy
Matrix4 m = batch.getProjectionMatrix();
Matrix4 c = m.cpy();
// Draws
m.translate(100, 50, 0);
batch.draw(region,0,0);
// Restores matrix
batch.setProjectionMatrix(c);
// Ends drawing
batch.end();
}
}
No matter what transformations I make, it stays in the same place. I know that there is a draw method in SpriteBatch that lets me specify where to draw it, but I am trying to set up a scene-graph, and that requires me to translate, rotate, etc using SpriteBatch’s Matrix4 extracted from batch.getProjectionMatrix(). I can make it look like the image slides across the screen if I remove the line that restores the matrix to it’s original form, but I can’t make it shift and stay there. Is my understanding of the projection matrix incorrect? I always have this problem with LibGDX, but I can never remember how to solve it. The docs don’t really explain its behavior.