I just started with libgdx and I’m still figuring out how the coordinate system work.The code below would draw the image at [0,0] without the Orthographic camera
What I can’t understand is why the code doesn’t draw the image when I uncomment the camera and viewport. I thought the camera is at point [0,0] by default. But I’m not sure why I can’t see the image.
[Edit]
Reading the question I posted this morning, I guess what I want to ask is how camera and viewport affect the coorindates that is passed to the SpriteBatch.
Working code
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
public class SpriteBatchSample extends GdxSample {
private static final Color BACKGROUND_COLOR = new Color(0.39f, 0.58f, 0.92f, 1.0f);
private static final float WORLD_TO_SCREEN = 1f;
private static final float SCENE_WIDTH = 100f;
private static final float SCENE_HEIGHT = 100f;
private OrthographicCamera camera;
private Viewport viewport;
private SpriteBatch batch;
private Texture cavemanTexture;
private Texture sbTexture;
private Color oldColor;
@Override
public void create() {
camera = new OrthographicCamera();
viewport = new FitViewport(SCENE_WIDTH, SCENE_HEIGHT, camera);
//Gdx.app.log("SpriteBatch", camera.position.toString());
batch = new SpriteBatch();
oldColor = new Color();
camera.position.set(100/2f, 100/2f,0);
cavemanTexture = new Texture(Gdx.files.internal("data/sb.png"));
cavemanTexture.setFilter(TextureFilter.Nearest, TextureFilter.Nearest);
}
@Override
public void dispose() {
batch.dispose();
cavemanTexture.dispose();
sbTexture.dispose();
}
@Override
public void render() {
Gdx.gl.glClearColor(BACKGROUND_COLOR.r,
BACKGROUND_COLOR.g,
BACKGROUND_COLOR.b,
BACKGROUND_COLOR.a);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
//batch.setProjectionMatrix(camera.combined);
batch.begin();
int width = cavemanTexture.getWidth();
int height = cavemanTexture.getHeight();
float originX = width * 0.5f;
float originY = height * 0.5f;
Gdx.app.log("SpriteBatchSample", "width " + width + "n"
+ "height: " + height + "n"
+ "originX" + originX + "n"
);
// Draw
batch.draw(cavemanTexture,
0, 0, // x, y
originX, originY, // originX, originY
cavemanTexture.getWidth(), cavemanTexture.getHeight(),// width, height
WORLD_TO_SCREEN, WORLD_TO_SCREEN, // scaleX, scaleY
0.0f, // rotation
0, 0, // srcX, srcY
cavemanTexture.getWidth(),cavemanTexture.getHeight(), // srcWidth, srcHeight
false, false); // flipX, flipY
batch.end();
}
@Override
public void resize(int width, int height) {
//viewport.update(width, height, false);
}
}
Not Working
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
public class SpriteBatchSample extends GdxSample {
private static final Color BACKGROUND_COLOR = new Color(0.39f, 0.58f, 0.92f, 1.0f);
private static final float WORLD_TO_SCREEN = 1f;
private static final float SCENE_WIDTH = 100f;
private static final float SCENE_HEIGHT = 100f;
private OrthographicCamera camera;
private Viewport viewport;
private SpriteBatch batch;
private Texture cavemanTexture;
private Texture sbTexture;
private Color oldColor;
@Override
public void create() {
camera = new OrthographicCamera();
viewport = new FitViewport(SCENE_WIDTH, SCENE_HEIGHT, camera);
//Gdx.app.log("SpriteBatch", camera.position.toString());
batch = new SpriteBatch();
oldColor = new Color();
camera.position.set(100/2f, 100/2f,0);
cavemanTexture = new Texture(Gdx.files.internal("data/sb.png"));
cavemanTexture.setFilter(TextureFilter.Nearest, TextureFilter.Nearest);
}
@Override
public void dispose() {
batch.dispose();
cavemanTexture.dispose();
sbTexture.dispose();
}
@Override
public void render() {
Gdx.gl.glClearColor(BACKGROUND_COLOR.r,
BACKGROUND_COLOR.g,
BACKGROUND_COLOR.b,
BACKGROUND_COLOR.a);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
int width = cavemanTexture.getWidth();
int height = cavemanTexture.getHeight();
float originX = width * 0.5f;
float originY = height * 0.5f;
Gdx.app.log("SpriteBatchSample", "width " + width + "n"
+ "height: " + height + "n"
+ "originX" + originX + "n"
);
// Draw
batch.draw(cavemanTexture,
0, 0, // x, y
originX, originY, // originX, originY
cavemanTexture.getWidth(), cavemanTexture.getHeight(),// width, height
WORLD_TO_SCREEN, WORLD_TO_SCREEN, // scaleX, scaleY
0.0f, // rotation
0, 0, // srcX, srcY
cavemanTexture.getWidth(),cavemanTexture.getHeight(), // srcWidth, srcHeight
false, false); // flipX, flipY
batch.end();
}
@Override
public void resize(int width, int height) {
viewport.update(width, height, false);
}
}