Public Group

# 2D tiles and XNA

This topic is 4164 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi folks. I am using C# and XNA. I have seen that a lot of 2d games use some sort of tile-based collision in platform/maze/rpg type games, and have been trying to work out how to do this but all of the examples/tutorials I have managed to find seem to go a bit deeper than I am able to understand. They often include scrolling and movement physics and all kinds of other complicated stuff and I can’t weed out the bits I need. What I want is just the collision detection and response part. I can use a 2d vector of ints representing tiles to draw my game screen, and I can put a moving character sprite onto the screen. But I cannot make it so that the character can only move in the areas I want it to. I read something about making tiles walkable/non-walkable. How would I do this? And more importantly, how do I know my character’s position in regards to the type of tile it is standing on? I would be great if anyone can give me a very simple example or point me in the direction of a tutorial that concentrates solely on the collision detection aspect of a tile-based game.

##### Share on other sites
If you have a second array of integers to represent the tiles, you can have them on either '1' (which will trigger a collision) or '0' (which means you can walk past the tile). So [1][1] = 1; Will make that tile collidable. Then you take the players co-ordinates and pump them into this formula,

//Find the tile that the player is on, on the X axisPlayerTileX = PlayerX / TileSize;//Find the tile that the player is on, on the Y axisPlayerTileY = PlayerY / TileSize;//If the tile is collidable, stop player movementif(array[PlayerTileY][PlayerTileX] == 1){PlayerMove = false;}

Simple. Eh?

##### Share on other sites
Yes very simple, doh. Probably why I could not find any tutorials on it. For the same reason you dont see many online tutorials on how to inhale. Thanks for the help, that is exactly what I wanted.

##### Share on other sites
Well actually, if you go here:

http://www.xnaresources.com/pages.asp?pageid=8

In one of the tutorials you will find similar code to what ive just shown you,

1. 1
2. 2
Rutin
19
3. 3
4. 4
khawk
14
5. 5
A4L
13

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633743
• Total Posts
3013643
×