Advertisement Jump to content
Sign in to follow this  

2d Side Scroller Help

This topic is 4887 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everyone. I am working on a 2d side scroller.... I am sorry if you don't think I should post this here. =( I am designing a 2d map format. my program loads the map into memory and all of its graphics just fine. The whole map rendered fine. Each tile is 32x32. I only want to draw a portion of the map at a time however to increase the speed. I need to know every time the starting tile, MapTile[0][0].i_X, is increased by 32 or decreased by 32. If I can get this info, then I can properly judge what part of the map I should start rendering from. Right now I am using a counter to do that. I have a variable called counter of type integer. Everytime the tile has been increaed by 32, I do counter++ and when the tile has been decreased by 32, I do counter-- respectively. The problem with my system is that is is unreliable. At the beginning of the game, counter should be 0, when the MapTile[0][0].i_X = 0. And when I scroll back and forth, the values are correct. However sometimes if I try scrolling the map back and forth really fast, MapTile[0][0].i_X's starting position might change to -1 or either 1 when it should be 0 when it is at its default position. So basically the computer isn't adding the values as fast and as accurately as I need it to. therefore I guess my way of doing it is crappy. Any other suggestions out there? By the way, I am using SDL if that is of any help. Thanks in advance! GOD Bless you Always!!!

Share this post

Link to post
Share on other sites
Hey thanks for the link. I read threw it and I am not sure if it helps me. see, I know how to render a map. I am just having problems upon making it scroll mathematically correctly. Visually speaking, it scrolls fine. However the numbers don't always add up. Anyone else have an idea? Thanks in advance!

Share this post

Link to post
Share on other sites
for the solution to your problem i need to know what is i_X:

- if i_X is number of pixels the "camera" shows into the map then instead of increasing ctr when i_X increases by 32 do something like:
ctr = i_X / 32;
(always do it and ctr and i_X will never be out of sync)

- if i_X is just offset of the tiles (ranging from 0..31 then you want something like:

// increase i_X like before then do:
ctr += i_X /32; i_X %= 32;

if you are intereseted in why the ctr went out of sync in the first place,
i see two possible causes for your problem:

a) if the tile changes by more than 32 than ctr++ isnt enough (you need to increase ctr by 2).

b) if you have floating point calculations you may have some error that gets bigger and bigger, if you use the result of previous calc as input to the current calc.
the mathematical subject that studies this error increasing algorithms (among other things) is numerical analysis.
sometimes its easy to find a way to make the error not grow -
alot of times it is better to start from a zero postion and calc entirly instead of incremental calculations.
for example:
when i first attempted 3d i rotated some points 1 degree every tick. the points got messed up very fast because i used the previous result as input, something like: Loc = rotate(Loc,1) the error increased every tick.
instead the better approuch is: Loc = rotate(startLoc, ticks % 360)
this way there is a small error, but its the same small error every time and doesnt increase.

Share this post

Link to post
Share on other sites
thanks for your help. It helped me quite a bit. I got it working now.

However I have one new bug. I don't quite understand why I have this problem.

I have created my own Map format. It is a binary format. I load the map into memory each I load my game. However, I also have a function that can write(Generate) Map Levels.

For my tile structure, it looks like so,

struct s_MapTile
int i_U;
int i_V;
int i_Y;
int i_X;

int i_SpecialInfo[4];

And I can generate the map just fine. When I load the map into memory, all the tiles rendered exactly where they should.

i_SpecialInfo is an array of 4 ints similar to how it is done in Mappy. It is for any special info you have about a tile. For example, if it is a door, or if there should be collision, or if it is an special item like health and such.

My map is 20*20. Relatively small map.

However when I set a tile's i_SpecialInfo[0]=1, when I load the map, I get some where value like -84987538. It is not set to 1. Why is this?

Thanks in advance!

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!