Scrolling tile world?

Started by
1 comment, last by En3my 21 years, 11 months ago
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. http://www.gamedev.net/reference/articles/article1242.asp Thanks a lot for your help! I hope that my next game will be a tile based one.
Advertisement
The AND is used like a fast MOD, to give the remainder.

[edited by - Michalson on May 5, 2002 9:46:18 AM]
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
and
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]

This topic is closed to new replies.

Advertisement