Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I know I'm missing something very obvious here, but I have a 1D array that contains integers.I have a map that is 30 x 30 and the center coordinate is 0,0 (This is actually 3D, but I'm essentially removing the Y-axis). Because of this offset, my loop is from -15 to 14, rather than 0 to 29. Is this incorrect?

I'd like to iterate through the array and if the integer is "1", then I'd like to call AddSphere(x,y,z) with the correct x and z values. How would I get the correct values for x and z? The file is designed to be read left to right rather than left to right, right to left, left to right etc... (although if that's a better approach, I can just change the data to match that style).

share|improve this question
up vote 1 down vote accepted

One possible way is to split the integer into two values between 0 and 29 (this is done using integer division and modulus) then offset them by -15:

for (int n = 0; n < 30 * 30; ++n)
    if (Array[n] == 1)
        int x = n % 30 - 15;
        int y = 0;
        int z = n / 30 - 15;

        AddSphere(x, y, z);

Note that -15..14 is asymmetrical. If you want (0,0) to be at the exact centre of your map, you need an odd value instead of 30.

share|improve this answer
Thank you! The asymmetrical aspect also explains why my attempts ended up with a cube overlapping on the edge, or starting slightly in from it. – XSL Dec 29 '12 at 0:27

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.