如何制作跟随播放机的相机?

时间:2016-10-10 01:13:04

标签: java camera game-physics slick2d

我有一个玩家类,它是x,y位置是用xPos和yPos操纵的,我想创建一个跟随玩家的相机。

到目前为止,我已经尝试过这个:

相机类:

import org.newdawn.slick.GameContainer;

public class Camera {

    public int offsetMaxX;
    public int offsetMaxY;
    public int offsetMinX;
    public int offsetMinY;

    public int camX;
    public int camY;

    private Player player;

    public Camera(Player p) {

        //Setting offset max's and minimums
        offsetMaxX = 0;
        offsetMaxY = 0;
        offsetMinX = 0;
        offsetMinY = 0;

        player = p;

    }

    public void update(GameContainer gc) {

        camX = player.xPos - (gc.getWidth() / 2);
        camY = player.yPos - (gc.getHeight() / 2);

    }

}

我的主要课程中的渲染功能是:

@Override
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {

    //Translate to camera X and Y
    g.translate(-camera.camX, -camera.camY);

    //Scaling map
    g.scale(3, 3);
    //Rendering map
    try {
        mapRender.render();
    } catch (SlickException e1) {
        e1.printStackTrace();
    }

    //Draw player
    player1.drawPlayer(g);

}

这样可行,但相机移动速度比播放器慢。我怎么能修复这段代码,还是有更好的方法呢?

1 个答案:

答案 0 :(得分:0)

我自己修好了。在绘制我的播放器时,我做了

g.translate(.5, .5);

因此,在定位相机时,我必须这样做

camX = ((player.xPos * 1.5f)) - (gc.getWidth() / 2);
camY = (player.yPos * 1.5f) - (gc.getHeight() / 2);