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 > decimals.

#859 - Lord Graga - Sat Jan 11, 2003 11:13 am

Hey all. I am currently working on a RPG who uses 16x16 tiles.
I would like to use following method to test collision(2 is the walkable tile):
Code:

   cx = self.x/16;
   cy = self.y/16;
   if(map[cy][cx] == 2 && map[cy][cx+1] == 2 && map[cy+1][cx] == 2 && map[cy+1][cx+1] == 2) dir[di]=0;

what i would like to do is this:
Code:

   cx = self.x/16;
   cy = self.y/16;
   if(map[cy][cx] == 2 && map[cy][cx+0.9375] == 2 && map[cy+0.9375][cx] == 2 && map[cy+0.9375][cx+0.9375] == 2) dir[di]=0;

As you can see, i would like to use a decimal for it. But i can't :(

#904 - subbie - Sun Jan 12, 2003 12:53 am

Just use fix point. Shift by 8 should be enough.

#906 - Touchstone - Sun Jan 12, 2003 1:16 am

Problem is you cannot index an array with floats, the index have to be an integer. Think of your array as a set och drawers. You can't access half a drawer, you'd have to open it up completely. Same goes for arrays.
_________________
You can't beat our meat

#1029 - Papa Smurf Advanced - Mon Jan 13, 2003 7:23 pm

'ello,

Try :

cx = (self.x / 16) * 256;
cy = (self.y / 16) * 256;

u32 Fixed = (u32) (0.9375 * 256);


// now u can add cx/cy with Fixed

u32 x = (cx + Fixed) >> 8;
u32 y = (cy + Fixed) >> 8;


if(map[y][x] == 2 && map[y][x] == 2 &&
map[y][x] == 2 && map[y][x] == 2)

dir[di]=0;


-Papa Smurf Advanced-

#1032 - Splam - Mon Jan 13, 2003 8:52 pm

I don't think fixed point maths is the answer here. It looks like Lord Graga is trying to check "inside" a tile ? ie instead of checking collisions against the edge of every 8x8 he's trying to check slightly inside that (can't really see any reason for that code otherwise).

Simply can't be done that way, you need to scale your check position instead and have a finer control, almost in the way Papa Smurf (hehe crazy name) says but that still won't do what you want, you're still only checking the same tile because after all the fixed point stuff you're ending up with the same result a 0 or 1 addition to the tile postion.

#1045 - col - Tue Jan 14, 2003 1:31 am

Splam wrote:
I don't think fixed point maths is the answer here. It looks like Lord Graga is trying to check "inside" a tile ? ie instead of checking collisions against the edge of every 8x8 he's trying to check slightly inside that (can't really see any reason for that code otherwise).


i agree.

I would suggest to keep a 'sub-tile' 8x8 collision map for every different type of collidable tile.
When a possible collision is detected via the normal 8x8 tilemap test, the index value of that tile can be used to find the 'sub-tile' map for that tile. Then the sub-tile map is used to test with pixel detail.

With a little thought and good design, not very many of these sub-tile maps will be needed - just a few levels, a few angles for slopes, maybe a few curves.
It should also be straightforward to mirror and flip them to save even more precious rom - or use bitmaps so each mini-map will need only 8bytes!!

cheers

col.

#1048 - ampz - Tue Jan 14, 2003 2:02 am

Depending on the size of the map, a full collision bitmap may be a convenient solution.

#1049 - col - Tue Jan 14, 2003 3:39 am

ampz wrote:
Depending on the size of the map, a full collision bitmap may be a convenient solution.


however, if you do take the time to assosciate each collidable graphic tile with a relevant collision map tile, you can build as many levels as you like with your graphical tiles, and change them(levels) at will, and the collision will work automatically!! it's also highly scalable - huge levels will work just as well as small ones, with little overhead.

No fiddly re-building of a seperate collision map - this approach would facillitate eg. a built in level editor

(shit man - i've almost convinced myself to start writing a platform game lol duper harioland here we come)

cheers

col