0

I would like to let the sprite where I left it in the screen, and drag the camera in the world. But the sprite sticks to the camera, and is still at the bottom of the screen when I drag the camera.

This is not happening with a tilemap (the .tmx file), and a renderer : the camera can drag over the tilemap, and the tilemap does not stick to the camera. With a spriteBatch tough, the sprite stays at the bottom of the screen.

Here is my code:

map = MyLoader.manager.get("data/mMap.tmx");
float unitScale = 1 / 64f;
renderer = new OrthogonalTiledMapRenderer(map, unitScale);

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

    camera.update();
    renderer.setView(camera);
    renderer.render();//tilemap works fine

    //sprite
    //batchMap.setProjectionMatrix(camera.combined);//wrong

    batchMap.begin();
    if ( scrFactoryMap.maps.size() > 0 ) {
        scrFactoryMap.getMap(0).draw(batchMap);//sprite sticks to the camera
    }
    str = "string";
    font.draw(batchMap, str, 50,50);//font sticks to the camera
    batchMap.end();
}


private void slerpCamera(){
    //...
    camera.position.add( camX, camY, 0 );
    camera.update();
}
Daahrien
  • 10,190
  • 6
  • 39
  • 71
Paul
  • 6,108
  • 14
  • 72
  • 128

1 Answers1

2

Why did you comment this, an put wrong on it?

//sprite
//batchMap.setProjectionMatrix(camera.combined);//wrong

You must set the new ProjectionMatrix to the SpriteBatch whenever you transform (i.e. move) your camera.

Uncomment it :)

Daahrien
  • 10,190
  • 6
  • 39
  • 71
  • I think it's a trick question. The comment clearly says that this is "wrong". It *cannot* be the solution! :D – noone Jan 23 '14 at 08:02
  • True, thats a possibility :p I thought he thought this was wrong because when using the ProjectionMatrix and his camera is in position 0,0, it would appear like if the world started in the middle of the screen. So he tought that it was wrong :p but actually he just needed to move his camera. Not very good at explaining.. – Daahrien Jan 23 '14 at 09:18
  • Thank you, you were right it almost works, the problem with the projection matrix is that I get a black rectangle instead of my sprite and it is in the top right corner, whereas without it, the sprite appears well in the bottom left corner as expected. Would you know how to put it back at the bottom left corner with the image (it is maybe the back of the image, somehow the settings were inversed from the bottom to top, left to right, and back to front ?) – Paul Jan 23 '14 at 19:08
  • Your starting camera position is (0,0). So just move your camera: cam.position.set(cam.viewportWidth/2, cam.viewportHeight/2); – Daahrien Jan 23 '14 at 20:24
  • Thanks @Lestat, I am not sure, the camera's position is changing all the time when I drag the camera. Is there maybe another way to show the image? When I try this, the black rectangle is still there. – Paul Jan 24 '14 at 00:59
  • Try clearing the screen like this: Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); – Daahrien Jan 24 '14 at 01:07
  • Thanks @Lestat, I found the solution, thanks for your answers, I had to set the position of the sprite, using `-spr.getWidth()*0.5f` and then it locates the sprite in front of the camera, otherwise, it was too much on the right, and I had to set the scale to 0.04f (the black was actually part of the img),I don't know how to automatically set the scale, I am using `camera = new OrthographicCamera(10, 10 * (Gdx.graphics.getHeight() / (float)Gdx.graphics.getWidth()));` and `0.04f` for the scale of the sprite. Maybe I'll find out how to find the correct scale, or if you know something about this? – Paul Jan 24 '14 at 02:51
  • 1
    Instead of scaling it, set its size [Sprite#setSize](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/Sprite.html#setSize%28float,%20float%29). sprite.setSize(1, 1); for example. @Paul – Daahrien Jan 24 '14 at 02:56
  • 1
    if someone has the same issue, `setSize` changes the width/height and I am now able to put the sprite in the middle with just `0,0` instead of `-sprite.getWidth()/2` which I had to use with `setScale`. It all works thank you very much. – Paul Jan 25 '14 at 03:44