LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

Need help with wall and floor collision

Root / Programming Questions / [.]

mystman12Created:
Hey, I've been trying to figure this out myself, but I'm having trouble figuring out an efficient way to handle this... I'm working on a platformer, and I can't figure out how to properly program collision with tiles. Currently, the game is programmed so that I have an array of different tiles to create the map with. Each tile is stored in a three-dimensional array like this: TILE[X,Y,TILE]. The player's position is also stored with four variables, PX, POF, PY, and POF. PX and PY hold the tile they are currently on, and POFX and POFY (Player OFfset) store the location of the tile they are on. So, for example, if you wanted to find the value of where the player is standing, you would check TILE like this:
IF TILE[POFX,POFY,MAP[PX,PY,0] != 0 THEN... 'THE 0 IN MAP IS THE MAP LAYER. OTHER LAYERS ARE USED FOR BG PLACEMENT, ENEMY PLACEMENT, ETC.
Also, I handle switching player position by saying that when POFX (or POFY) is >= 31 it gets 31 subtracted from it and PX (or PY) gets advanced by one, and basically the same thing in reverse for when it's less than 0. So, now that I have that explained, I have a couple questions. 1. How should I go about programming collision. I know it should be a DEF block, and I sort of have an idea on how to do it, but frankly I can't seem to think of a way to do it that doesn't seem way too complex for something so seemingly simple. Basically, my game is locked in at 60 FPS via VSYNC 1, so when PVX or PVY (Player Velocity) is greater than 1, the player is going to move more than 1 pixel, thus I need a function that every frame figures out the path the player is going to take between frames, and checks it for any obstacles. Also remember the function needs to not just stop when it hits something. For example, if the player has an X velocity of 8 and a Y velocity of 8, and it ens up hitting a vertical wall after travelling two pixels, the player should still be moved down the rest of the way, and only the X movement should be stopped. 2. Should I keep the game programmed the way I have it, with PX and POFX handled seperately, or would I be better off handling movement with a variable that keeps track of the player's exact location (PPX=79 [Player Precise] instead of PX=2 and POFX=15) and then simply divide that value by 32 and get the remainder each frame to fill in the values needed to find which tile the player is on and where they are on each tile. (PX=PPX/32 and POFX = the remainder) Hopefully I describes the issue clearly. This is keeping me from continuing my program, and while I would have liked to have figured out a solution myself, I've decided it would be best at this point to ask for some help and advice. Thanks!

There is another thread where I talk about a generic solution for collisions. Here is a key with an example that I've done: NDS4S343 That key contains a function to detect a collision with certain tile. That function get the sprite collider and check if that overlap the tile t based on the speed in pixel of it. Here is an example of what you can do:
//This function represent what you should do on the "update" phase of your game loop.
DEF update_
 DEC VY,GRAVITY

 //Wall collition
 IF collisionWIthTileT(WALL,PLAYER,VX,0)>=0 THEN VX=0
 IF collisionWIthTileT(WALL,PLAYER,0,VY)>=0 THEN VY=0

END
Also, note that the example in the key doesn't use offset for the player. I think that variable should be used only if you can define a collider for each tile(For example: to make slopes)