2

For some reason when i make my map bigger then 15 x 25 the camera gets all buggy, at the top part of the map the camera jumps showing the bottom of the map, and at the bottom it jumps to the top.

Here's the code for it:

    public int getXOffset(){
        int offset_x = 0;

        //the first thing we are going to need is the half-width of the screen, to calculate if the player is in the middle of our screen
        int half_width = (int) (Game.WINDOW_WIDTH/Game.SCALE/2);

        //next up is the maximum offset, this is the most right side of the map, minus half of the screen offcourse
        int maxX = (int) (map.getWidth()*32)-half_width;

        //now we have 3 cases here
        if(player.getX() < half_width){
            //the player is between the most left side of the map, which is zero and half a screen size which is 0+half_screen
            offset_x = 0;
        }else if(player.getX() > maxX){
            //the player is between the maximum point of scrolling and the maximum width of the map
            //the reason why we substract half the screen again is because we need to set our offset to the topleft position of our screen
            offset_x = maxX-half_width;
        }else{
            //the player is in between the 2 spots, so we set the offset to the player, minus the half-width of the screen
            offset_x = (int) (player.getX()-half_width);
        }

        return offset_x;
    }

    public int getYOffset(){
        int offset_y = 0;

        int half_heigth = (int) (Game.WINDOW_HEIGTH/Game.SCALE/2);

        int maxY = (int) (map.getHeight()*32)-half_heigth;

        if(player.getY() < half_heigth){
            offset_y = 0;
        }else if(player.getY() > maxY){
            offset_y = maxY-half_heigth;
        }else{
            offset_y = (int) (player.getY()-half_heigth);
        }

        return offset_y;
    }

In my render method i use this:

map.render(-(offset_x%32), -(offset_y%32), offset_x/32, offset_y/32, 33, 19);

Here's a .gif of the problem, I'm jumping up and down in this: http://i.gyazo.com/11efe44d6e872c334533cd3a40ddf2b9.gif

1 Answers1

0

Instead of rendering an offset to your map, try just using

g.translate(camera.x-(width/2),camera.y-(height/2);

It's much more manageable this way, your camera's co-ordinates would be tied to the players.

Malii
  • 448
  • 6
  • 17