Results 1 to 4 of 4

Thread: [RESOLVED] Endless looping tilemap

  1. #1
    Member
    Join Date
    May 11
    Posts
    48

    Resolved [RESOLVED] Endless looping tilemap

    I have a tile map engine where if you scroll all the way to the right, it crashes.
    I really don't understand why as any other direction simply keeps the camera from moving past the border.

    I would also like to make the world round, so moving all the way in one direction starts you on the opposite side.

    Code:
    spriteBatch.Begin();
    
                Vector2 firstSquare = new Vector2(Camera.Location.X / Tile.TileStepX, Camera.Location.Y / Tile.TileStepY);
                int firstX = (int)firstSquare.X;
                int firstY = (int)firstSquare.Y;
    
                Vector2 squareOffset = new Vector2(Camera.Location.X % Tile.TileStepX, Camera.Location.Y % Tile.TileStepY);
                int offsetX = (int)squareOffset.X;
                int offsetY = (int)squareOffset.Y;
    
                for (int x = 0; x < squaresDown; x++)
                {
                    for (int y = 0; y < squaresAcross; y++)
                    {
                        int rowYOffset = 0;
    
                        if ((firstX + x) % 2 == 1)
                            rowYOffset = Tile.OddRowYOffset;
    
                        foreach (int tileID in myMap.Rows[y + firstY].Columns[x + firstX].BaseTiles)
                        {
                            spriteBatch.Draw(
                                Tile.TileSetTexture, new Rectangle((x * Tile.TileStepX) - offsetX + baseOffsetX, (y * Tile.TileStepY) - offsetY + rowYOffset + baseOffsetY, Tile.TileWidth, Tile.TileHeight),
                                Tile.GetSourceRectangle(tileID), Color.White);
                        }
                    }

  2. #2
    Member
    Join Date
    May 11
    Posts
    48

    Re: Endless looping tilemap

    The error I get is an array out of bounds.

    /hate arrays.

  3. #3
    Member
    Join Date
    May 11
    Posts
    48

    Re: Endless looping tilemap

    I fixed it... added this to the Camera.cs

    Code:
                if (offset.X > 50 || offset.Y > 50)
                {
                }
                else
                {
                    Location += offset;
                }

  4. #4
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 10
    Location
    Denmark
    Posts
    521

    Re: [RESOLVED] Endless looping tilemap

    Quote Originally Posted by Jneiswin View Post
    I would also like to make the world round, so moving all the way in one direction starts you on the opposite side.
    Making a round (spherical) world out of triangles, rectangles, hexagons or similar isn't possible with nice results. I tried many different ways of doing this (and read loads of material) - the best results was with very large maps created by subdivisions from one the regular polyhedrons (typically the dodecahedron or occasionally the icosahedron), but every such map will have anomalies.
    You're batter off just settling for repetition on a rectangular map (ie. going out of the map to the right lets you enter to the left).

    And by the way there is a sub-forum here for XNA (Games and Graphics programming), if you wan't to post other XNA related questions. I'm afraid it's not nearly as comprehensive as the one at Dream.In.Code, but hopefully it will grow ).
    In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •