2D tiles and XNA
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.
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,
Simple. Eh?
//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?
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.
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,
http://www.xnaresources.com/pages.asp?pageid=8
In one of the tutorials you will find similar code to what ive just shown you,
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement