Hey,
I'm running through some game tutorials and have just implemented my camera. It scrolls, following the player sprite, and culls tiles from the map outside of bounds.
One thing I'm noticing is some minor artifact tearing between some of the tiles that make up the on-screen map. Originally, I assumed it was because I coded my camera X and Y as Integers, and my player coordinates as floats, so there was some classic data loss from downcasting float -> int. I modified that, and removed integer casts that related to passing player coordinates to the camera. This has reduced the tearing, but everything still jitters a small amount.
The player only moves at a speed of 1.0f. If I run it at 3.0f, it is more noticable. Where could I be going wrong?
Thanks,
Stitchs (code below).
package dev.mygame.display;
public class Camera
{
private float x;
private float y;
private int w;
private int h;
private int focusX;
private int focusY;
public Camera(float x, float y, int w, int h)
{
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
public float getX()
{
return x;
}
public void setX(float x)
{
this.x = x;
}
public float getY()
{
return y;
}
public void setY(float y)
{
this.y = y;
}
public int getFocusX()
{
return focusX;
}
public void setFocus(float focusX, float focusY)
{
float lerp = 1.0f;
x += lerp * (focusX - w/2 - x);
y += lerp * (focusY - h/2 - y);
}
public int getFocusY()
{
return focusY;
}
public int getWidth()
{
return w;
}
public int getHeight()
{
return h;
}
public void tick()
{
// centre the camera on the focus point
//focusX -= w/2;
//focusY -= h/2;
// set the origin so we can offset everything else
x = focusX;
y = focusY;
}
}
package dev.mygame.entities.creatures;
import java.awt.Graphics;
import dev.mygame.gfx.Assets;
import dev.mygame.Game;
public class Player extends Creature
{
private Game game;
public Player(Game game, float x, float y)
{
super(game, x, y, Creature.DEFAULT_WIDTH, Creature.DEFAULT_HEIGHT);
this.game = game;
}
@Override
public void tick()
{
getInput();
move();
}
private void getInput()
{
xMove = 0;
yMove = 0;
if(game.getKeyManager().up)
{
yMove = -speed;
}
if(game.getKeyManager().down)
{
yMove = +speed;
}
if(game.getKeyManager().left)
{
xMove = -speed;
}
if(game.getKeyManager().right)
{
xMove = +speed;
}
}
@Override
public void render(Graphics g)
{
g.drawImage(Assets.playerOne, (int)(x - game.getCamera().getX()), (int)(y - game.getCamera().getY()), width, height, null);
}
}
package dev.mygame.states;
import java.awt.Graphics;
import dev.mygame.entities.creatures.Player;
import dev.mygame.gfx.Assets;
import dev.mygame.Game;
import dev.mygame.worlds.World;
import dev.mygame.display.Camera;
public class GameState extends State
{
private Player player;
private World world;
public GameState(Game game)
{
super(game);
world = new World(game, "/maps/World1.json");
player = new Player(game, world.getSpawnX(), world.getSpawnY());
}
@Override
public void tick()
{
player.tick();
game.getCamera().setFocus(player.getX() + player.getWidth()/2, player.getY() + player.getHeight()/2);
}
@Override
public void render(Graphics g)
{
world.render(g);
player.render(g);
}
}
↧