Thread: Precision in a tile map grid?

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    8

    Precision in a tile map grid?

    I'm not sure if the title is completely accurate, so please let me know if I should change it.

    I'm trying to get into game programming, so I'm creating really basic games (the one I'm currently working on, which is also the first, is a 2D game in only one room, so I can get used to writing characters, enemies, and other objects, along with movement and combat, without having to worry about implementing other rooms and the transition between them.) and working my way up.

    My current problem is this:
    The only way that I can think of to make a map grid is to have a two-dimensional array, for example, *datatype* mapcoord[400][200], if I wanted to have a map 400 tiles wide and 200 tiles high. Then, I could set the value of each position to a number, for example, the boundaries of the map and the walls could be set to -1, open space could be 0, enemies are set to 1, etc.

    The problem is that I'd also like to have precision and smooth movement, using floating point numbers, so an object could be located at point 32.0416912, 4.194002, instead of just being at 32, 4.
    How would I implement this?

    Thank you.

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Dynamic (read: moving) objects are not part of the tile map. They are sprites on the map.

    Code:
    int spriteRow = spriteWorldCoords.y / map.cellsize.y;
    int spriteColumn = spriteWorldCoords.x / map.cellsize.x;
    int spriteMapOffset = spriteRow * map.cellsPerColumn + spriteColumn

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    8
    Quote Originally Posted by VirtualAce View Post
    Dynamic (read: moving) objects are not part of the tile map. They are sprites on the map.

    Code:
    int spriteRow = spriteWorldCoords.y / map.cellsize.y;
    int spriteColumn = spriteWorldCoords.x / map.cellsize.x;
    int spriteMapOffset = spriteRow * map.cellsPerColumn + spriteColumn
    I know that they are not part of the tile map, but I thought that I would add different things like enemies and other interactable (can't think of a real word to use there) objects to the map so that other objects can know that they're there (for example, if I was increasing the player's X value because they were moving to the right, and the function found out that the value of the X coordinate that the player is trying to reach held the value of 1, meaning that there was an enemy occupying that spot, then the player would not be able to move there, and would take damage).

    I'm assuming that the 3 lines you provided were for getting the width and height of the sprite on the map grid? If you could please elaborate more on that I'd appreciate it.

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Sprite collisions can be map based but they don't have to be and IMO should not be. The code I provided allows you to translate from world coords to map or array coords so that you can check the sprite location in the map.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 3D Tile Rendering
    By dcco in forum Game Programming
    Replies: 7
    Last Post: 06-24-2011, 11:43 PM
  2. Tile-map performance
    By VirtualAce in forum Game Programming
    Replies: 41
    Last Post: 04-19-2006, 10:55 PM
  3. Tile help
    By MadCow257 in forum Game Programming
    Replies: 3
    Last Post: 09-10-2005, 08:30 PM
  4. Tile engines
    By Stray_Bullet in forum Game Programming
    Replies: 7
    Last Post: 03-21-2002, 08:12 PM
  5. Tile Editor
    By Nick in forum Linux Programming
    Replies: 3
    Last Post: 08-10-2001, 11:24 PM