COllide with scrolling map

Started by
2 comments, last by jagguy2 15 years, 2 months ago
I have a scrolling 2D vertical background with 32X32 tiles and a player sprite. The background map is held in 2 2D arrays that hold ,x,y values and row number which only changes when arrays are flipped. The map has 2 arrays which flip over on top of each other where appropriate so it gives the illusion of a never ending map. I have a total offset value which records amount of scrolling the map is done and it resets to 0 when I swap the arrays. q1)How do I calculate when the player sprite collides with background tiles because the tiles are moving?. I cant see a quicker way than adding an offset to the player sprie and working out its position in relation to the background map when it starts at 0 Total offset. Then searching through the background map to find the right tile. THis is seems slow as much searching is needed.
Advertisement
It doesn't seem like it'd be too difficult to find the right tile. It's not as if you have to check each one, right? If the player is at position 600, 30 with respect to the map, and each tile is 32x32, then 600/32 and 30/32, with the remainder truncated, should give you the right indices to check for your 2D array (18, 0), no?

Then, you just have to remember to keep the size of the player into account. For instance, if the player is 32 pixels wide and 32 pixels tall, then it's actually overlapping up to 4 tiles at once.
I've found these tutorials very helpful in learning how to do grid based collision. All the code is AS2.0 but it's fairly easy to do the same stuff in C++
i can get it to work but it isnt easy as i thought.

I have 2 2D arrays and I need to keep track of which array is on top of the other and swap around when the screen scrolls down far enough so the last row is visible and swap array around so I have a scrolling endless background.

I need to keep track of the total amount of offset I have moved and reset to 0 when swapping arrays around and keep track of player sprites position row,col relative to the world map by adding the offset. THis way I can get what tile I am on .

To get what tile I am on I hold a global copy of each 2D array background map (withought actual images) but with row,col and tileno.

There are a lot of ifs in all this but it works and I wonder is there a simpler way?






This topic is closed to new replies.

Advertisement