Hi I have created this function which be be used to separate a game world into chunks. Each chunk is 32 blocks in size. This formula calculates the start coordinate of a given chunk number of a given world coordinate. It works good but I think it is possible to optimize it but I have no idea. It is important that the formula works with negative numbers.
static int wrld2chnk(int wrld)
{
if (wrld % 32 != 0)
{
if (wrld < 0)
wrld -= (32 + wrld % 32);
else
wrld -= wrld % 32;
}
return wrld;
}
// these are example values which can be used to validate the result. The first value in each rpw is the method input, the second is the output:
(0, 0);
(31, 0);
(32, 32);
(33, 32);
(-1, -32);
(-31, -32);
(-32, -32);
(-33, -64);
(-34, -64);
(-64, -64);
(-70, -96);