map scrolling using glTranslatef()

Apprentice
Posts: 7
Joined: 2011.03
Post: #1
Hi Guys,

I'm trying to implement a scrolling function to my tile map and so far i have managed to do most of it with glTranslatef().
i.e.
-----------------------
glPushMatrix();
glTranslatef(240 - player.pixelLocationX, 160 - player.pixelLocationY, 0);

[tileMap renderMap mapX:player.MapLocationX - 1 mapY:player.MapLocationY - 1 width:shownTilesWide + 2 height:shownTilesHeight + 2];

[player render];
glPopMatrix();

--------------------

This way, the player is always centered on the screen and the map scrolls under him. however, the map won't stop scrolling even when it has reached its bounds (leftmost/rightmost/topmost/bottommost), until it scrolled to the center next to the player. What i'd like to do is having the map stops scrolling once any of its bounds appear on the screen and having the player move freely towards the bounds, and only resume scrolling when there is no bounds in view. I'know there are other ways to achieve the effect, but i'd like to learn how to do it with glTranslatef().
Could someone please tell me how can this be done?

Thanks in advance.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
clamp your coords to the allowable range (probably taking into account the screen size) before translating
Quote this message in a reply
Apprentice
Posts: 7
Joined: 2011.03
Post: #3
I came up with this concept which i haven't tested yet. Do you guys think it would work?

int cameraX = 0;
int cameraY = 0;
int mapStartX = 0;
int mapStartY = 0;

inside the rendering method:

if(isLeftBound) {
cameraX = 0;
mapStartX = 0;
} else if (isRightBound) {
cameraX = 0;
mapStartX = tileMapWidth - NbrOfTilesShownOnScreenHorizontal;
} else {
cameraX = 240 - player.pixelLocationX;
mapStartX = player.mapLocationX - leftOffsetTiles;
}

if(isBottomBound) {
cameraY = 0;
mapStartY = 0;
}else if(isTopBound) {
cameraY = 0;
mapStartY = tileMapHeight - NbrOfTilesShownOnScreenVertical;
}else {
cameraY = 160 - player.pixelLocationY;
mapStartY = player.mapLocationY - bottomOffsetTiles;
}

glPushMatrix();
glTranslatef(cameraX, cameraY, 0);

[tileMap renderMap mapX:mapStartX - 1 mapY:mapStartY - 1 width:NbrOfTilesShownOnScreenHorizontal + 2 height:NbrOfTilesShownOnScreenVertical+ 2];

[player render];
glPopMatrix();

What do you guys think?
Please do share if you know a better way of doing this.

Thanks.
Quote this message in a reply
Apprentice
Posts: 7
Joined: 2011.03
Post: #4
Hi Guys,

I tested out my code and it seems to be working. However, i have encountered another problem. now, 30% of time when i'm moving toward the upper bound of the map, my tile map image gets distorted. not sure how to describe it, there are lines/triangles in multiple color being drawn over the map, but all other images still being rendered correctly. also, when this happens the game's frame rate suffers noticeably. I checked my rendering code but couldn't find anything that might have caused it. and if i restart the program, the image distortion may not be there again (most of the time everything is rendered correctly).
any idea what is causing the problem?

Thanks in advance for any insight.
Quote this message in a reply
Post Reply