Help discover a more elegant way to calculate "edge perspective"

Ok typically in a tile engine, you have a number of tiles, a character, and a viewport. You’ll usually draw only the tiles that are needed (the amount that can fit within the viewport, plus 1 tile edge around to prevent “black out” tiles on the edges)

Most of the time when you get this up and going, and you locate your character to position 0,0… 25% of your screen will be the tiles in the tile map, and the other 75% will be blackness.

Now most rpg’s on SNES would never let you get to an edge of a map, but if you did, there was a “edge perspective” algorithm going on, to stop the tile map from scrolling and start the player sprite scrolling.
So if you were at the top left corner of a map, your sprite would appear at (0, 0) and the map would start drawing at pixel 0,0. You move right one position, your sprite is now 1, 0, and the map is being drawn still at 0,0 (top left corner of viewport).

Hopefully that makes sense… That said, here’s my drawing function which calculates this. I am hoping to find someone to offer a more elegant way of doing this. I’m not that great with the math, so I have some conditional branching to make things easier.

Let me know your thoughts!


	public void draw_edge_persepctive(Graphics2D g, Sprite sprite, int x, int y, int offsetx, int offsety, int width, int height) {
		//x and y are in tile coordinate system
		//offsetx and offsety are in pixel coordinate system
		//width and height are in tiles coordinate system (how many tiles wide and high is the viewport)
		//hw is "half width" offset by 1
		//hh is "half height" offset by 1
		//our drawing area of tiles to draw is a rectangle
		//xa is the left edge of that rectangle of tiles to draw
		//xb is the right edge
		//ya is the top edge
		//yb is the bottom edge
		
		int hw = (width - 1) / 2;
		int hh = (height - 1) / 2;
		int xa = x - hw;
		int xb = x + hw;
		int ya = y - hh;
		int yb = y + hh;
		
		//sx, sy is the sprite's or player's position that we will calculate to draw on the screen
		//this will change based on how far away we are from the edges of the map
		
		int sx = 0;
		int sy = 0;
		if(x < 255 - hw) {
			sx = Math.min((x * 32) - offsetx, hw * 32);
		}
		else {
			sx = Math.max(hw * 32,-offsetx + ((width - 1) - (255 - x)) * 32);
		}
		
		if(y < 255 - hh) {
			sy = Math.min((y * 32) - offsety, hh * 32);
		}
		else {
			sy = Math.max(hh * 32,-offsety + ((height - 1) - (255 - y)) * 32);
		}
		
		//rx, ry is how many pixels to subtract from drawing location on the screen
		//we need this because when we are close to an edge, we want to "lock" the tiles to the viewport
		//at least until we are far enough away from an edge to draw "normally"
		
		int rx = 0;
		int ry = 0;
		if(x < hw) rx = -offsetx + (x - hw) * 32;
		if(y < hh) ry = -offsety + (y - hh) * 32;
		if(x == hw && offsetx > 0) rx = -offsetx;
		if(y == hh && offsety > 0) ry = -offsety;
		
		if(x > 255 - hw) rx = -offsetx + (hw - (255 - x)) * 32;
		if(y > 255 - hh) ry = -offsety + (hh - (255 - y)) * 32;
		if(x == 255 - hw && offsetx < 0) rx = -offsetx;
		if(y == 255 - hh && offsety < 0) ry = -offsety;
		
		if(rx < 0) xb += hw;
		if(ry < 0) yb += hh;
		if(rx > 0) xa += -hw;
		if(ry > 0) ya += -hh;
		
		
		for(int ix = xa - 1; ix <= xb + 1; ix++) {
			for(int iy = ya - 1; iy <= yb + 1; iy++) {
				if(ix < 0 || ix >= 256 || iy < 0 || iy >= 256) continue;
				TileType tile = TileType.tile_types[tiles[ix][iy][0]];
				
				int dx = offsetx + (32 * (ix - x + hw));
				int dy = offsety + (32 * (iy - y + hh));
				
				tile.draw(g, rx + dx, ry + dy, 2);
			}
		}
		
		sprite.draw(g, sx, sy, 2);
	}

Here’s what it looks like, in case my explanation was poor…

Seems to me it’s a matter of “don’t scroll the map if there’s no more map”. If your character is represented with a map position and not simply glued to the center, it should automatically Do The Right Thing as far as moving the character on the screen when the map freezes.

I should probably mention that this is tile based movement.

Try out this program

http://pastebin.com/v0nV5Vsr

edit: corrected a small error in the program

Hmm, the problem I see with what you’re doing is you’re relying on being able to draw the entire map, which won’t be possible in my case.

Perhaps I can still use this example, somehow.