• Advertisement
Sign in to follow this  

tile math problem

This topic is 3740 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

I have a 64x64 texture that has 4 32x32 textures on it. I have a number, based on a timer, that goes from 1 to 4 that represents the region I want to draw. I need to tell a draw method the coordinates of a 32/32 region of this texture to use. I figured out the x coordinate pattern : int x1 = ((region + 1) % 2) * 32; I cant for the life of me figure out the Y pattern. Heres an input / output chart (r = region) r x1 y1 x2 y2 1 0 0 32 32 2 32 0 32 32 3 0 32 32 64 4 32 32 64 64 Its the y1 region I cant figure out.

Share this post


Link to post
Share on other sites
Advertisement
Your current x calculation is wrong. It would get you the opposite region to the one you wanted (so region 0 would get you r1, region 1 would get you r0).


// region must be an integer
int x1 = (region % 2) * 32;
int y1 = (region / 2) * 32;



Alan

Share this post


Link to post
Share on other sites
I think that using a zero-based index would simplify things a little bit -

x1 = (region % HORIZONTAL_TILES) * TILE_WIDTH
x2 = x1 + TILE_WIDTH

y1 = (region / HORIZONTAL_TILES) * TILE_HEIGHT (integer division will truncate the remainder of this division)
y2 = y1 + TILE_HEIGHT

this would give (I think...)

r x1 y1 x2 y2
0 0 0 32 32
1 32 0 32 32
2 0 32 32 64
3 32 32 64 64

If your timer values have to be 1-4, you could just subtract 1 from the timer value before using as a region index.

Share this post


Link to post
Share on other sites
I also recommend using r from 0..3 instead of from 1..4 - it makes these kinds of things much easier. Here is a way of getting your numbers using bit shifting:

for( int r = 0; r < 4; ++r )
{
int x1 = ( r & 1 ) << 5;
int y1 = ( r & 2 ) << 4;
int x2 = ( ( ( r & ( r >> 1 ) ) & 1 ) + 1 ) << 5;
int y2 = y1 + 32;
printf( "r=%d, x1=%2d, y1=%2d, x2=%2d, y2=%2d\n", r, x1, y1, x2, y2 );
}

gives

r=0, x1= 0, y1= 0, x2=32, y2=32
r=1, x1=32, y1= 0, x2=32, y2=32
r=2, x1= 0, y1=32, x2=32, y2=64
r=3, x1=32, y1=32, x2=64, y2=64

Share this post


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

  • Advertisement