Making a tile engine

This is a discussion on Making a tile engine within the Game Programming forums, part of the General Programming Boards category; I'm learning DirectX and came to the point where I have to make a tile engine. The place I found ...

  1. #1
    Novice programmer newn's Avatar
    Join Date
    Aug 2010
    Posts
    59

    Making a tile engine

    I'm learning DirectX and came to the point where I have to make a tile engine. The place I found the code was a bit out-dated, so I had to remake it more or less by myself.
    The idea is a side-scroller, let's say it's an FPS game.

    What is the best way to draw objects with which you will be colliding? Drawing them as sprites definitely doesn't work well:

    Code:
    			sprite_handler->Draw(
                column_img,
    				NULL,
    				NULL,
    				&position1,
    				D3DCOLOR_XRGB(255,255,255));
    It just scrolls them together with the screen.

    Drawing them in the tilemap is probably a bad idea too - how would I detect collision?

    And so I'm out of ideas. How should I do it?
    -------
    This is only if you didn't understood what I'm talking about:

    In mario there are columns and blocks that you cannot go trough, you must jump to pass them. So I am trying to draw these so they are kept in place when the tilemap scrolls.

  2. #2
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    199
    Do you mean objects colliding with other objects that aren't part of the tilemap or objects (that aren't part of the map) colliding with the tiles themselves?

    EDIT: Read the end of the your post. Idk much about DirectX, but if you're detecting if the player has collided with the bricks (going from mario), you draw the bricks as part of the tilemap, and the character is not. Just check if the player coords has collided with the brick, which will be at
    (X of the start brick of the map * Brick Tile X Number , Y of the Start Brick of the map * Brick Tile Y Number)
    and the player would just be at the coordinate on the screen. (At least that's how its done in Allegro. I figure that the basic idea should be compatible |: )

    Bounding Box Collision between the two collision bounds.
    Last edited by Rodaxoleaux; 10-02-2011 at 04:46 PM.

  3. #3
    Novice programmer newn's Avatar
    Join Date
    Aug 2010
    Posts
    59
    Well you cannot always fit the texture in a tile, some textures are more detailed - half a tile, one third of tile, they are place in different places. That couldn't be detected if it would be a part of an array.

  4. #4
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    199
    Ah. And the difference between 2D and 3D programming even in a 2D environment has been made even more commodious.

  5. #5
    Novice programmer newn's Avatar
    Join Date
    Aug 2010
    Posts
    59
    So that's basically 3D programming? Cool. I'm wondering how I can do that though, it set me on the "it's going to be hard" mood now.

  6. #6
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    199
    No no. Dont think that. Its not so much that its hard (or maybe it is i dont know) Its more the fact that I know as much about 3D graphics and textures as I do about quantum physics.

  7. #7
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    What is the best way to draw objects with which you will be colliding?
    In summary: the same way you draw any other object. I think this is what Rodax was getting at. It doesn't necessarily have to be "as part of the tilemap", but if you want the object to move with your backgrounds (that is, appear to be stationary in the world) you'll have to perform the same transformations that you apply to your tiles.

    With that said, it is worth noting that collision detection and rendering are generally completely separate. Rodax refers to "Bounding Boxes." Each colliding entity (tile, brick, whatever) has its own bounding box, as does the character. The bounding box doesn't have to match the visual representation of the block, though - it could be twice the size, or a totally different shape, or at an arbitrary location; for the purposes of detecting a collision, it doesn't matter. Obviously mismatches between what the player sees and how collisions work can cause frustrations, and in practice one usually doesn't pull crazy stunts like that without a good reason. And the "box" doesn't have to be a box, so long as you can detect when an arbitrary polygon intersects another arbitrary polygon (obligatory link <- it might take a while for that to sink in but it's good stuff).

    Then when you want to move your character, you check to see if its bounding box collides with any of the other bounding boxes. In a simple implementation this means checking every block in the level. Improvements can be made, though, if you're feeling adventurous: for example you can use Quadtrees to reduce the number of tests.
    Consider this post signed

  8. #8
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    You can perform hit detection using the good old bounding box and then once that test says there is a hit you can find the barycentric coordinates of the hit within the target object's texture and determine if that bitmap texel is opaque or not. If it is then you have a hit, else you have hit nothing. This is good for perfect mouse picking like you find in Roller Coaster Tycoon 1 and 2 but probably is not needed for a tile game. Civilization used a bitmap that had quadrants of color that were arranged in the same fashion as one grid cell. When the mouse was clicked the color of the pixel at the barycentric click position was evaluated and would indicate if the cell click location needed to be incremented, decremented, etc.

    Most old-school NES style tile-based games like Super Mario Bros. and Zelda used hit detection boxes and reduced the box to the point that objects had to overlap to actually be hit. This makes it appear as if the collision is pixel perfect. You can test this theory as well if you have a Wii. Go get Super Mario Wii and when you eat a mushroom and become big find a bullet launcher that is as high as Mario is when he is big. Duck down when the bullet fires and you will see the bullet pass behind Mario's head when it really should have hit him. It did not hit him b/c Mario's hit detection box has been reduced enough that the hit detection failed to detect a hit.

    If you want to test for hits based on time rather than position then you can Google for separating axis theorem and read the resulting links. My suggestion is do not use the tile location as a hit detection b/c this is crude and ugly. Test the sprites on your tile grid as if they are not part of the grid...and they actually should not be a part of the grid anyways.
    Last edited by VirtualAce; 10-03-2011 at 06:52 PM.

  9. #9
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    199
    I need to get into 3D soon but modeling and 3d math is so much more difficult than sprite creation and the old X,Y coordinates |3
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tile engine view area
    By Punksocko in forum Game Programming
    Replies: 15
    Last Post: 10-26-2005, 11:53 PM
  2. advantages of tile-based engine...
    By o0obruceleeo0o in forum Game Programming
    Replies: 25
    Last Post: 07-22-2003, 09:03 AM
  3. Tile Engine in Direct3D
    By frenchfry164 in forum Game Programming
    Replies: 2
    Last Post: 03-30-2003, 03:56 PM
  4. Need lots of help with tile engine
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 06-12-2002, 02:54 AM
  5. Tile engine
    By Stray_Bullet in forum Game Programming
    Replies: 0
    Last Post: 03-08-2002, 12:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21