I'm having an issue updating my character position when I call both updateCamera() and setting the projection matrix of spritebatch.
When update camera is called my character doesn't seem to be in the correct position(zooming along faster than her bounding box).
Setting the projection matrix of spriteBatch to cam.combined she doesn't appear on screen at all. Is there something I'm doing wrong here?
The problem code is within the render() method.
public class LevelRenderer {
private static final float CAMERA_WIDTH = 10f;
private static final float CAMERA_HEIGHT = 7f;
private LevelTwo level;
private OrthographicCamera cam;
//debug rendering
ShapeRenderer debugRenderer = new ShapeRenderer();
private TextureRegion anaFrame;
ShapeRenderer renderer;
Skeleton skeleton;
SkeletonJson json;
SkeletonRenderer skelRend;
Animation runAnimation;
Bone root;
private SpriteBatch spriteBatch;
private boolean debug = true;
private int width;
private int height;
private float ppuX;
private float ppuY;
private Ana ana;
float time;
Array<Event> events = new Array();
public void setSize (int w, int h){
this.width = w;
this.height = h;
ppuX = (float)width / CAMERA_WIDTH;
ppuY = (float)height / CAMERA_HEIGHT;
loadTextures();
}
public LevelRenderer(LevelTwo level, boolean debug){
this.level = level;
ana = level.getAna();
this.cam = new OrthographicCamera(CAMERA_WIDTH, CAMERA_HEIGHT);
this.cam.position.set(CAMERA_WIDTH / 2f, CAMERA_HEIGHT / 2f, 0);
this.cam.update();
this.debug = debug;
spriteBatch = new SpriteBatch();
}
private void loadTextures(){
TextureAtlas atlas = new TextureAtlas(Gdx.files.internal("images/Ana.atlas"));
json = new SkeletonJson(atlas);
json.setScale(ppuY/665.0f);
SkeletonData skeletonData = json.readSkeletonData(Gdx.files.internal("images/Ana.json"));
skeleton = new Skeleton(skeletonData);
runAnimation = skeletonData.findAnimation("run");
root = skeleton.getRootBone();
skeleton.updateWorldTransform();
skelRend = new SkeletonRenderer();
}
public void render(){
updateCamera(ana.getPosition().x, CAMERA_HEIGHT / 2);
spriteBatch.setProjectionMatrix(cam.combined);
spriteBatch.begin();
drawAna();
spriteBatch.end();
skeleton.updateWorldTransform();
if(debug)
drawDebug();
}
private void updateCamera(float x, float y){
if(ana.getPosition().x > CAMERA_WIDTH / 2 || ana.getPosition().x < CAMERA_WIDTH / 2){
cam.position.set(x, y, 0);
cam.update();
}
}
private void drawAna(){
float lastTime = time;
time += Gdx.graphics.getDeltaTime();
events.clear();
if (ana.getState() == State.WALKING)
runAnimation.apply(skeleton, lastTime, time, true, events);
else
skeleton.setBonesToSetupPose();
root.setX(ana.getPosition().x * ppuX);
if(ana.getFacingLeft() == false)
{
root.setX(-1 * (ana.getPosition().x * ppuX));
}
root.setY(ana.getPosition().y * ppuY);
skeleton.setFlipX(!ana.getFacingLeft());
skelRend.draw(spriteBatch, skeleton);
}
private void drawDebug(){
debugRenderer.setProjectionMatrix(cam.combined);
debugRenderer.begin(ShapeType.Line);
for(Block block : level.getBlocks()){
Rectangle rect = block.getBounds();
float x1 = block.getPosition().x + rect.x;
float y1 = block.getPosition().y + rect.y;
debugRenderer.setColor(new Color(1, 0, 0, 1));
debugRenderer.rect(x1, y1, rect.width, rect.height);
}
Rectangle rect = ana.getBounds();
float x1 = ana.getPosition().x;
float y1 = ana.getPosition().y;
debugRenderer.setColor(new Color(0, 1, 0, 1));
debugRenderer.rect(x1 - (rect.width/2), y1, rect.width, rect.height);
debugRenderer.end();
}
}