0

I'm currently making a game in JavaFX in which a player moves around inside a level. The entire level isn't shown at once so I'm using a viewport that follows the player around. I'm doing this by modifying the GraphicsContext object i'm using to draw. The following code describes it:

canvas = new Canvas(ROOM_WIDTH, ROOM_HEIGHT);
gc = canvas.getGraphicsContext2D();
gc.scale(SCALE_X, SCALE_Y); //SCALE_X, SCALE_Y = 2
...
double viewX = -(player.getX() - player.getPrevX());
double viewY = -(player.getY() - player.getPrevY());

if(player.getX() > ROOM_WIDTH/(4*SCALE_X) && 
                player.getX() < (4*SCALE_X-1)*ROOM_WIDTH/(4*SCALE_X))
{
    gc.translate(viewX, 0);
}
if(player.getY() > ROOM_HEIGHT/(4*SCALE_Y) && 
                player.getY() < (4*SCALE_Y-1)*ROOM_HEIGHT/(4*SCALE_Y))
{
    gc.translate(0, viewY);
}

So basically, if the player isn't at the edges of the room, the GraphicsContext object translates to the player's position.

Now this has caused some problems elsewhere. Because I'm scaling the GraphicsContext object, everything is zoomed in. I'm using pixelated graphics, and thus I don't want any interpolation because that results in blurry images. My question is: Is there a way to remove the interpolation done by the GraphicsContext object or the Canvas object (whichever it is), and how would I go about doing this?

I'm just using the GraphicsContext drawImage and fillRect methods when creating my level. An image describing what is drawn can be seen in the following link:

Blurry player object

I hope you can be able to help me. Thank you :)

Xaril
  • 85
  • 1
  • 11
  • Closely related question: http://stackoverflow.com/q/16089304/2991525 – fabian Apr 28 '16 at 09:22
  • @fabian That doesn't help unfortunately. Even if I resize the image, it is then drawn to the canvas which is scaled to be twice as large. It is the canvas scaling that creates the blur, which I'm trying to remove. – Xaril Apr 28 '16 at 11:01

0 Answers0