0

I am attempting to divide my game world into Sectors/tiles. And identify the tile where the player/object is at depending on the player/object position. I do not have a limited world size, so the world could be of any size. The tile size is defined and constant. So, the entire world is divided into square tiles of a given dimension.

Example: If I am dividing my world into equal sized square tiles of, say 300x300.

  • If the player is within positions (0,0) to (300,300), the tile number should be (0,0)
  • If the player is within positions (0,0) to (300,-300), the tile number should be (0,-1)
  • If the player is within positions (-300,0) to (-600,300), the tile number should be (-2,-0)

More example inputs and outputs:

  • Player position is (10,10), tile number should be (0,0)

  • Player position is (-10,10), tile number should be (-1,0)

  • Player position is (10,-10), tile number should be (0,-1)

  • Player position is (-10,-10), tile number should be (-1,-1)

  • Player position is (1,1), tile number should be (0,0)

  • Player position is (1,-1), tile number should be (0,-1)

  • Player position is (-1,1), tile number should be (-1,0)

  • Player position is (-1,-1), tile number should be (-1,-1)

  • Player position is (-450,100), tile number should be (-2,0)

  • Player position is (450,100), tile number should be (1,0)

  • Player position is (-450,-100), tile number should be (-2,-1)

  • Player position is (450,-100), tile number should be (1,-1)

I have the below code, which seems to work correctly only when X/Z is positive. Once it is on the negative side of X/Z, i am not sure how i could calculate the position.

public static Sector GetCurrentSector(Vector3 p_position)
{
    int x = Mathf.FloorToInt(p_position.x / cellSize);
    int z = Mathf.FloorToInt(p_position.z / cellSize);

    return new Sector(x, z);
}

I have checked the below questions, which seemed to be related to mine:

Note: I am also not sure what this problem is called! So, it makes it very difficult for me to search. My search led me to multiple places related to circles, radius, points within circle etc.,

vvvvv
  • 25,404
  • 19
  • 49
  • 81
  • What exactly does the method return in case of - say - `(-450,0)`? – Paul Kertscher May 19 '20 at 11:11
  • As far as I can see your solution is correct in principle. What are the actual result you are getting? – JonasH May 19 '20 at 11:16
  • your world has dimension for example 1000x1000 with (0,0) as the center of world? – Frenchy May 19 '20 at 11:27
  • Hi Paul, Thanks for the reply, It seems to be working!!! – Tamil Ninja May 19 '20 at 11:31
  • Hi Paul, All, Thanks for the reply, It seems to be working!!! There is some bug causing the position passed to the method to be incorrect. I tried by directly passing in the values to the method and execute it, the values returned seem to be right. Much appreciated. – Tamil Ninja May 19 '20 at 11:37

1 Answers1

0

i suggest you to work with int and not FloorToInt and uses the integer division:

public static Sector GetCurrentSector(Vector3 p_position)
{
    int x = (int)p_position.x / (int)cellSize;
    int z = (int)p_position.z / (int)cellSize;

    return new Sector(x, z);
}

my answer is not correct, your method was correct use matf.FloorInt is better that integer division for negative number.

Frenchy
  • 16,386
  • 3
  • 16
  • 39