Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

1075919 Posts in 44152 Topics- by 36120 Members - Latest Member: Royalhandstudios

December 29, 2014, 03:15:17 PM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)Best way to move camera in a roguelike?
Pages: [1]
Print
Author Topic: Best way to move camera in a roguelike?  (Read 542 times)
stef1a
Level 1
*


confused


View Profile
« on: June 16, 2012, 10:38:33 AM »

What's an ideal way to scroll the camera across a map that's larger than the screen as the player moves? For that matter, how are such top-down camera classes/functions written? I've never done one before.

Here's my current display code:
Code:
void Display::render() {
// render tiles
for (unsigned int i = 0; i < game.get_area()->get_width(); i++) {
for (unsigned int j = 0; j < game.get_area()->get_height(); j++) {
apply_surface(i*TILE_SIDE, j*TILE_SIDE,
img_list[game.get_area()->get_tile(i, j)->get_id()], NULL);
}
}
// render entities
for (unsigned int i = 0; i < game.get_ent_list_size(); i++) {
if (game.get_ent(i)) {
int x_ = game.get_ent(i)->get_x();
int y_ = game.get_ent(i)->get_y();
int id_ = game.get_ent(i)->get_id();
apply_surface(x_*TILE_SIDE, y_*TILE_SIDE,
img_list[id_], NULL);
}
}
// render texts...
}

Some ideas I had:

1. Split each map into "sectors," where a sector is defined as an area of the map that fits one screen. For example, the top-leftmost sector would be (0,0), and would span real map coordinates (0,0) to (screen_width, screen_height). When the player moves, check to see if his coordinates cross over the boundaries of one of these sectors, and if they do, update the sector variable appropriately, and then shift the camera over to the next sector.

Disadvantage: A player cannot view the map in between two sectors. This would make navigating in rooms that are cut at sector boundaries difficult and annoying.


2. Create an integer variable that is added to the lower and upper bounds of printing in the render routine (i and j). This variable is incremented/decremented appropriately whenever the player moves more than half the screen width/height into the map -- and it continues to move until he reaches a boundary of the map.

Disadvantage: Gameplay becomes choppy. Sometimes, keeping the camera still is ideal.


3. Allow the player to move the camera on his own using a separate set of keys; do not allow movement past the location of the character (or a certain buffer amount around the character; say, 3 tiles in either direction).

Disadvantage: The player may not always want to move the camera on his own; sometimes, having the computer take over is helpful.


4. Move the camera every x tiles the player moves in a certain direction. That is, say, every time the player moves ten tiles down from his original position, the screen shifts ten tiles down.

Disadvantages: None?

What have been some workable solutions in past/what generally tends to please the user? Thanks in advance!  Coffee

By the way, the player moves in tile-sized movements, not smoothly.
« Last Edit: June 16, 2012, 11:33:39 AM by stef1a » Logged
sublinimal
Level 8
***



View Profile
« Reply #1 on: June 16, 2012, 01:37:32 PM »

I'm surprised you didn't mention the no-brainer solution: keep the player fixed at the center of the screen. That way, the player has the most comprehensive view of the surroundings, and the camera never jumps around awkwardly.

The position of the camera should be the top left corner of a rectangle centered to the player. That is, you can get the position by subtracting half a screen_width from the player's x-pos and half a screen_height from his y-pos. Only draw the tiles inside this rectangle.

You could additionally implement a function to pan the camera and see out-of-border events (if this is even necessary, since I don't know about the gameplay). This function would just move the camera pos around according to key input, redraw the screen rectangle without updating the game state, and finally snap back to the player pos.
Logged
stef1a
Level 1
*


confused


View Profile
« Reply #2 on: June 16, 2012, 04:14:28 PM »

I'm surprised you didn't mention the no-brainer solution: keep the player fixed at the center of the screen. That way, the player has the most comprehensive view of the surroundings, and the camera never jumps around awkwardly.

The position of the camera should be the top left corner of a rectangle centered to the player. That is, you can get the position by subtracting half a screen_width from the player's x-pos and half a screen_height from his y-pos. Only draw the tiles inside this rectangle.

You could additionally implement a function to pan the camera and see out-of-border events (if this is even necessary, since I don't know about the gameplay). This function would just move the camera pos around according to key input, redraw the screen rectangle without updating the game state, and finally snap back to the player pos.

Oh, yeah. I forgot about that.  Facepalm

I wrote up a quick prototype of it, and it works rather well. It's still a bit buggy, but I can work out the kinks later. Thanks -- I like it!  Wink

EDIT: Fixed up the bugs; here's a video of the result: http://www.youtube.com/watch?v=6iobqz8Q6qQ&feature=youtu.be
« Last Edit: June 17, 2012, 05:40:23 AM by stef1a » Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic