Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Pretty much all I'm trying to do is revolve my 2D Array by its container. I'm using this array for a background and I seem to be having problems with it revolving.

public class TileTransformer : GridConstants
{
    public Tile[,] Tiles;
    ContentManager Content;
    public TileTransformer(ContentManager content)
    {
        Content = content;

    }
    public Tile[,] Wraping(Tile[,] tiles,Point shift)
    {
        Tiles = tiles;
        for (int x = shift.X; x < 0; x++)//Left shift
        {
            for (int X = 0; X < GridWidth; X++)
            {
                for (int Y = 0; Y < GridHeight; Y++)
                {
                    if (X + 1 >GridWidth-1)
                    {
                        Tiles[0, Y].Container =tiles[X, Y].Container;
                    }
                    else
                    {
                        Tiles[X+1, Y].Container =tiles[X, Y].Container;
                    }
                }
            }
        }
        for (int x = shift.X; x > 0; x--)//right shift
        {
            for (int X = 0; X < GridWidth; X++)
            {
                for (int Y = 0; Y< GridHeight; Y++)
                {
                    if (X-1==-1)
                    {
                        Tiles[GridWidth-1, Y].Container =tiles[0, Y].Container;
                    }
                    else
                    {
                        Tiles[X - 1, Y].Container =tiles[X, Y].Container;
                    }
                }
            }
        }
        for (int y = shift.Y; y > 0; y--)//shift up
        {
            for (int X = 0; X < GridWidth; X++)
            {
                for (int Y = 0; Y < GridHeight; Y++)
                {
                    if (Y - 1 == -1)
                    {
                        Tiles[X, GridHeight-1].Container = tiles[X, Y].Container;
                    }
                    else
                    {
                        Tiles[X, Y - 1].Container = tiles[X, Y].Container;
                    }
                }
            }
        }
        for (int y = shift.Y; y < 0; y++)//shift down
        {
            for (int X = 0; X < GridWidth; X++)
            {
                for (int Y = 0; Y < GridHeight; Y++)
                {
                    if (Y + 1 == GridHeight)
                    {
                        Tiles[X, 0].Container = tiles[X, Y].Container;
                    }
                    else
                    {
                        Tiles[X, Y + 1].Container = tiles[X, Y].Container;
                    }
                }
            }
        }
        return Tiles;
    }

Now the Problems that I'm having is either when I shift up or left it seems the whole array is cleared back to the default state. Also when I'm revolving the array it appears to stretch it upon the sides of the screen that it is shifting towards.

share|improve this question
    
Hint: You're going to have a hard time shifting an array in place without using any temporary storage. –  Byte56 Oct 26 '12 at 15:12
    
I actually tried using a temporary storage and it came out with the same results. :/ I've been playing with this code for awhile now and it still seems to stump me. It honestly looks fairly simple to achieve but looks can be deceiving. –  Brett Oct 26 '12 at 15:21
3  
Write out your algorithm on paper. Use a small grid, you'll get it. –  Byte56 Oct 26 '12 at 15:42
    
You probably don’t want a “don’t do that” answer, but… maybe don’t do that? You could instead keep a (ox,oy) offset vector and override all array accesses so that they add the offset. Then shifting the array is reduced down to changing the offset value. –  Sam Hocevar Oct 27 '12 at 12:13

1 Answer 1

It should be as simple as this, obviously untested but should point you in right direction

public Tile[,] Wraping(Tile[,] tiles,Point shift) {
  Tiles = tiles;
  Tile[,] shiftedTiles;

  for (int y = 0; y < GridHeight; y++) {
    for (int x = 0; x < GridWidth; x++) {
      shiftedTiles[(shift.X + x) % GridWidth, (shift.Y + y) % GridHeight] = tiles[x, y];
    }
  }
  return shiftedTiles;
}

The % part is modulus which ensure that the grid X,Y are wrapped round without using if statements to check edge boundaries.

If that doesn't quite work try exchanging the parameters around to:

 shiftedTiles[x,y] = tiles[(shift.X + x) % GridWidth, (shift.Y + y) % GridHeight];
share|improve this answer

Your Answer

 
discard

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.