Charachter scrolling off of the screen.

I’m working on a 2D RPG (like Final Fantasy 2, Zelda 3, ect). I’ve got the scenery to scroll as the charachter moves, but the charachter seems to get pushed off the screen. I am trying to make it so that the charachter always stays in the center of the screen as you move, as the scenery scrolls. I played with the timerfunction, and I was able to keep my characther centered, but I realized that when the scenery textures became to large (or too small), the charachter would eventually fall off the screen. Can anyone help me with this?

How do you do it?

[ul][]If you transform everything and call gl/gluOrtho() only once, make sure to transform player coordinates too. Seems like player coordinates don’t get transformed.[]If you change the values given to gl/gluOrtho(), everything should move automatically… In this case remember that things must NOT be transformed.[/ul]

zadeh, teh description of your problem is so vague that it is not possible for us to provide you with an answer. You will have to explain how and what you do exactly.

After rereading it, I think he’s trying to do something weird… The easiest thing (and what I do) is basically to use the real coordinates in the game and then apply the scroll transformation. To everything, not just to the scenery.

If you want to make things really simple, just get rid of any transformation used for scrolling and draw everything with the game coordinates. Use gl(u)Ortho() for the scrolling, calling it every frame (or at least when the scroll moves). Modify the parameters so they scroll. This way you’ll make everything scroll (through if you want to draw a HUD or something else, make sure to restore the original one after you’re done with the scrolled part). For example:

gluOrtho(scroll_x, scroll_y, scroll_x + screen_width, scroll_y + screen_height);

A similar technique can be used for zooming.

I know I really should post some code, but I assumed (apparently, incorrectly) that this problem was common enough, that it would immediately ring a bell. I will post some code as soon as I can.

I am actually translating twice. Once in the GLUT_KEY_FUNCTION. For example, every time the LEFT ARROW is pressed, everything (charachter + scenery) shifts to the right, a constant amount (via the glTranslate function). The second translation occurs within the charachter rendering code, where the characther gets shifted a value that is ‘almost’ opposite the scenery shift. For now, here is some pseudo-code:

CHARAC MOVING LEFT, RENDERING FUNCTION

if “THE LEFT KEY IS PRESSED”
{
{

//DRAW CHARACTHER FACING THE LEFT
GL_POLYGON
vector…
vector…
vector…
GL_END

glPushMatrix;

glTranslatef(-.09, 0, 09);

glPopMatrix;

}
}

GL_KEY_FUNCTION
{
IF (GLUT_KEY == LEFT)
{
gltranslate(.1,0,0);
}
}

THE problem appears to be that the characther is being rendered faster than the scenery (or maybe vice-versa), so the translations for either must be adjusted to maintain synchronization (to keep the charachter in the center of the screen while the scenery scrolls). I understand that their are a few approaches to getting such an effect, but I can’t see which approach will eliminate my problem. Actually, the translational bias that I am using right now, will most likely work fine. (If the charachter ‘appears’ to be perfectly centered for a short period of time, the laws of probability would suggest that by the end of the game the charachter will still appear centered, and so no cumulitive effect would cause a conspicious flaw). But, I’ve noticed that using larger, or too many, textures in a scene will cause a large de-synch, and I’m sure that I will eventually want to include scenes that are significantly more compicated than others. So using a constant bias, is not a good solution. I need to do this the right way.

Wouldn’t it be easier if you kept track of the player position in two variables (X and Y) and then calculated the scrolling and such when it came to drawing using those variables?

If you want something working quickly, when drawing, call this code first, then draw everything using the world coordinates (not the screen coordinates). Replace variables as appropiate. It should do the trick, as well as gets centered to player coordinates.

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluOrtho(player_x - screen_width / 2,
         player_x + screen_width / 2,
         player_y - screen_height / 2,
         player_y + screen_height / 2);

That sounds like a good idea, I’ll try that approach. Thank you :slight_smile:

Wow, you still have that horrible code structure… once again, you should learn some structured programming.

And what is that supposed to mean: "glPushMatrix; glTranslatef(-.09, 0, 09); glPopMatrix; ?

You’re welcome :slight_smile:

Oops, I missed that detail. For some reason I thought he was doing some drawing between the glTranslatef() and glPopMatrix() o_o’ Still, even if that was the issue, it isn’t really a very reliable approach…