This topic is now archived and is closed to further replies.


Scrolling tile world?

Recommended Posts

En3my    122
Hi, I''m reading through 2 articles called "Tiling in DirectX" written by Martin Estevao. However I have run into a part of the code that I don''t fully understand. offset_x = world_camerax & (TILE_SIZE - 1); offset_y = world_cameray & (TILE_SIZE - 1); I understand the concept of the offset values, but I don''t understand the bitwise operation. Could someone please explain in english what is happening in the above code. It''s taken out of context, so you probably need to take a look at the original article as well. It can be found here, and it''s relatively short so people with experince with programming tile based games can probably just take a fast look and understand what the code above is doing. Thanks a lot for your help! I hope that my next game will be a tile based one.

Share this post

Link to post
Share on other sites
Alimonster    185
Say you want the remainder of a power-of-two number (for example 32). The number one less than that power-of-two will have all 1s up until that point... e.g.,

00011111 = 31
00100000 = 32

Therefore, the bitwise 'and' will cycle through every number below that power-of-two: you have 0s in every position greater than or equal to the power, so those bits will never be set after the 'and'. Therefore, it acts as a quick modulus. The values will cycle (in your example) from the values [0..TILE_SIZE-1] inclusive.

[edited by - Alimonster on May 5, 2002 10:06:27 AM]

Share this post

Link to post
Share on other sites