• Announcements

Archived

This topic is now archived and is closed to further replies.

map Scrolling in a 2D game

Recommended Posts

I'm working on a 2d strategy game using openGL, but I'm unsure as to how to handle scrolling of the map. My GUI looks something like this. -------------- |######***| |######***| |######***| |**********| -------------- # - being the map tiles. * - being various buttons and other parts of the Gui. My problem is should map scrollng be done with keyboard input, mouse input or both. I'd like both but am unsure how to get it work using the mouse. Can anyone offer a suggestion?

Share on other sites
if the x of the mouse position is >= then screenwidth-10(?) then
xoffset++;

[edited by - ilankt on July 8, 2003 3:30:02 AM]

Share on other sites
Like Ilankt said, you can either establish a zone on your map, and if the mouse is outside this zone, then scroll in the appropriate direction, or you could just have a set of buttons on your GUI, and scroll when they are pressed, it depends on your needs, but I''d use the first.

(Then you can also scroll at an angle easily, just see if you are in the Top left, Middle left of the border etc.

Share on other sites
let's pretend you are picking an area near the edge of the screen 8 pixels in width that will be sensitive to scrolling, and the closer to the edge of the screen you are, the more you want the display to scroll.

for x scrolling, you want to scroll to the left when the mousex is less than 8, and to scroll right, the mousex must be greater than or equal to SCREENWIDTH-8

for y scolling, scroll up whenever mousey is less than 8, and scroll down whenever mousey is greater than or equal to SCREENHEIGHT-8

int GetScroll(int position,int screenmax,int sensitivity){    if(position<sensitivity)     {        return(position-sensitivity);    }    else if(position>=screenmax-sensitivity)     {        return(screenmax-position);    }    else        return(0);}

the function will work for either x or y. to get the particulars:

xscroll=GetScroll(mousex,SCREENWIDTH,8);yscroll=GetScroll(mousey,SCREENHEIGHT,8);

also, you might want the function to return a floating point value rather than an int, because you may wish to implement an option in your game that specifies a scrolling speed that you would multiply this value by.

• Forum Statistics

• Total Topics
627735
• Total Posts
2978855

• 10
• 10
• 21
• 14
• 12