0

I am making a 2D game and I've decided implementing a player-following camera to it, so you can explore a big map, and load a small part of the map in the window.

But now I have a little problem, I want the map to follow the player, so the player is always centered, example on how it should look like this but a bit more centered ( did it in paint) :

img
(source: gyazo.com)

But currently it looks like this:

img2
(source: gyazo.com)

Because offX & offY = 0, I need to think of a formula to make the camera centered by the player.

This is how I work out (move the camera), right in the drawMap method:

private void renderMap(Graphics2D g) {
    int[][] tiledMap = this.map.getMap();
    for (int i = 0; i < this.map.getHeight(); i++) {
        for (int j = 0; j < this.map.getWidth(); j++) {
            int currentRow = tiledMap[i][j] ;

            if (currentRow == 0) {
                g.drawImage(img, (j + offX) * map.getTileSize(), (i + offY) * map.getTileSize(), this);
            }    

            if (currentRow == 1) {
                g.setColor(Color.green);
                g.fillRect((j + offX) * map.getTileSize(),
                        (i + offY) * map.getTileSize(), map.getTileSize(),
                        map.getTileSize());                 
            }               
        }           
    }
}

Basically it ads the offX to the tile x and offY to the tile y. The tilesize is 30 px, so the map is moving by tile size which is a problem I need to sort aswell, OR just move the player by the tile size, and then the walking will be just too fast, because the camera can only load by one tile, if we move one tile per player movement, the camera eventually won't stay centred to the player.

How can I make the camera be centred all the time to the player?

This is my code:

package snow;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.swing.JPanel;

public class GameController extends JPanel {

    private static final long serialVersionUID = 1L;
    private Player myPlayer = new Player(5, 5, 25, 25);
    private TileMap map;
    private Image img;

    public GameController() {
        this.map = new TileMap(new File("src/snow/Tiles/map1.txt"));
        try {
            this.img = ImageIO.read(new File("data/snow.png"));
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        try {
            this.map.buildTile();
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }   
    }

    public void update() {
    }

    public void render(Graphics2D g) {
        g.setColor(Color.red);
        g.fillRect(myPlayer.getX(), myPlayer.getY(),
                myPlayer.getWidth(), myPlayer.getHeight());
    }

    public void paintComponent(Graphics g) { 
        Graphics2D gfx = (Graphics2D) g;
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, 765, 503);
        renderMap(gfx);
        render(gfx);
    }

    private void renderMap(Graphics2D g) {
        int[][] tiledMap = this.map.getMap();
        for (int i = 0; i < this.map.getHeight(); i++) {
            for (int j = 0; j < this.map.getWidth(); j++) {
                int currentRow = tiledMap[i][j] ;

                if (currentRow == 0) {
                    g.drawImage(img, (j + offX) * map.getTileSize(), (i + offY) * map.getTileSize(), this);
                }    

                if (currentRow == 1) {
                    g.setColor(Color.green);
                    g.fillRect((j + offX) * map.getTileSize(),
                            (i + offY) * map.getTileSize(), map.getTileSize(),
                            map.getTileSize());                 
                }               
            }           
        }
    }

    private int offX = 0, offY = 0;

    public void movePlayer(int x, int y) { 
        int[][] tiledMap = this.map.getMap();
        myPlayer.updateX(x);
        myPlayer.updateY(y); 
        offX = +10;
        offY = +7;
    }
}

package snow;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class TileMap {

    private File tileMap;
    private int tileWidth;
    private int tileHeight;
    private int tileSize = 30;
    private int[][] map;

    public TileMap(File tileMap) {
        this.tileMap = tileMap;
    }

    public void buildTile() throws NumberFormatException, IOException {
        try {
            BufferedReader reader = new BufferedReader(new FileReader(this.tileMap));
            this.tileWidth = Integer.parseInt(reader.readLine());
            this.tileHeight = Integer.parseInt(reader.readLine());
            map = new int[this.tileHeight][this.tileWidth];
            for (int i = 0; i < this.tileHeight; i++) {
                String line = reader.readLine();
                String[] chars = line.split(" ");
                for (int j = 0; j < this.tileWidth; j++) {
                    if (j >= chars.length) {
                        return;
                    }
                    map[i][j] = Integer.parseInt(chars[j]);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public int[][] getMap() {
        return this.map;
    }

    public int getWidth() {
        return this.tileWidth;
    }

    public int getHeight() {
        return this.tileHeight;
    }

    public int getTileSize() {
        return this.tileSize;
    }
}
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Jony Kale
  • 979
  • 3
  • 15
  • 35
  • 1
    1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) One way to get image(s) for an example is to hot-link to the images seen in [this answer](http://stackoverflow.com/a/19209651/418556). – Andrew Thompson Dec 31 '13 at 13:34

1 Answers1

0

I don't entirely understand your code, but I would suggest that inside this code block

for (int i = 0; i < this.map.getHeight(); i++) {
    for (int j = 0; j < this.map.getWidth(); j++) {

you say something like

currentRow = map[myPlayer.getX()-this.map.getWidth()/2][myPlayer.getY()-this.map.getHeight()/2]

and then leave the rest of your drawing code as is.

Epiglottal Axolotl
  • 1,048
  • 8
  • 17