Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

libGDX: How to let the camera point to moving sprite?

I'm new to libGDX and android game dev. And I want to achieve this: I have a Screen and within it, a ball sprite that is moving along the X axis. Now I want to center the viewport to the sprite when it moves. Just like in Angry birds, where the camera follows the bird flying across the sky.

How can I implement that within my game using OrthographicCamera?

like image 440
user804293 Avatar asked Jul 26 '12 09:07

user804293


1 Answers

This took me a while of Googling and testing, but I just found something and I think others may appreciate it.

To move the camera (and if you are using a spriteBatch), make sure to call setProjectionMatrix.

Ex:

camera.position.y += 5;  // or whatever you want to change y by...
camera.position.x += 5;
camera.update();    
spriteBatch.setProjectionMatrix(camera.combined);

Hope this helps someone!

like image 111
Richard Avatar answered Oct 04 '22 21:10

Richard