gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > platform game: scrolling x and y direction, please help???

#80213 - aik6980 - Thu Apr 20, 2006 1:31 am

Hi,
I begun to build my first platform game in last 2 months. I've got some questions, hope you guy can help

1. i've done my BiltBackground function, it work and fast enough but how can i make it faster using DMA?

Code:

void BlitBG(CStage *st){
   int x=0,y=0;
   for(y=0;y<32;y++){
      for(x=0;x<32;x++) st->bg[0].mapData[x+y*32]=bgMap0[x+st->_xOffset+y*(st->_Width)];
   }
}


2. Here's my code for CollisionTest. I found the problem when I try to make Y-axis scrolling down (yes, only when my character 's falling) :)
. It seem like they cannot detect my ground tile (because it will scroll away when my character y position is not in the screen center yet)

Code:

// collision test between player and map Y(ground)
u8 collisionTest_PVMy(CStage *st, CSprite *obj){
   int loop=0;
   int tx=0,ty=0;
   int tw=obj->tileWidth;
   int th=obj->tileHeight;

   tx=((obj->px)>>3)+st->_xOffset;
   ty=((obj->py)>>3)+(th)+st->_yOffset; //add offset from the stage to match the collision array position
   
   while(loop<tw){
      if(bgCollision[((tx+loop)+(ty)*(st->_Width))]==0x0004){
         return HITDOWN;
      }
      loop++;
   }
   return OK;
}


3. how could i scroll in both x, y direction and blit below 1 tile/frame. You can see my BlitBG it will adjust the offset at least by 1 tile, however i want to make it move more smoother.

4. some kind of stupid question. where can i post my game here? i'm looking for some comment for my further development.


thank you very much!!!

#80854 - Schultz - Tue Apr 25, 2006 9:29 pm

I shall leave a commentary about your first topic.

First of, you may have noticed that within your code lies a complexity in the order of O(n*m), since, if there are n tiles in your left map side and m tiles in your top size the algorithm shall execute n*m steps.
I shall lend you a method to make such complexity of the order of O(n+m), which is far faster than O(n*m).
The point is, you do not need to copy all the tile data. Instead, copy the next line and proceed a scroll, such as:

Game in position 0:

XXXX
XXXX
XXXX
XXXX

Consider the reference point located at (0, 0).

Game in position 1:

XXXX#
XXXX#
XXXX#
XXXX#

Consider the reference point at (1, 0).

That way, the number of transfers shall drop significantly.
Such thing is done by copying a new line of tile data instead of a whole map, so copy a line only, instead of a entire bidimensional array. You shall then have to solely displace the reference point so as to drag the screen. That is a better method indeed.

SO, INSTEAD OF OPTIMISING YOUR CODE USING DMA TRANSFERS, WHICH ARE BRUTE FORCE OPTIMISATION METHODS, RELY ON A BETTER MATHEMATICAL THEORY BEHIND YOUR CODING.

Never forget, mathematics is perfect.