# Collision Detection - Need Help Finding the tile!

3 posts

 817 posts What am I doing wrong? say I have a small simple map array like this… (not actual size) ```map_array = [ [0,0,0], [0,0,0], [1,1,1] ] ``` at each 0 is empty space. At each 1, a tile is blitted. First we find out where our player is in comparison to the map_array by doing this… ```player.yToMap = Math.abs(mapHolder.y - player.y); player.xToMap = Math.abs(mapHolder.x - player.x); player.yToTiles = Math.round((mapHolder.height - player.yToMap)/25); player.xToTiles = Math.round((mapHolder.width - player.xToMap)/25); ``` each tile is 25×25; now I just want to find the index of the tile hes about to collide with. So that hopefully I can plugin the x,y,width,height of that object. I tried using getObjectUnderPoint, and using player.?toTiles + direction. However I can’t get it to feed me an index. I tried using indexOf along with it. I may not even be approaching this correctly altogether, I simply want to fix my collisions. They work fine… almost ```while(j < ground_a.length){ solveCollision(j); findTOC(); j++; } ``` Except the obvious fact that a while statement searching through every single last tile is very redundant and since its in a loop function itself, will apply multiple collisions at once and move the player farther back than it should. (collisions are done axis-aligned bounding box style). 4090 posts If you already know the position of the player, then you’re done. Say it’s at `[16][8]`… if moving right, you’ll want to check `[17][8]` up to `[n][8]` if its speed is higher than the tile size. Same for any other direction; you know the starting position and the final one, all you have to do is to check for future collision in the path between those points. About your loop checking tiles that it shouldn’t, that’s what `break` is for. 817 posts Senekis, you are amazing. Thank you. Edit: I just fixed the elusive bug…. The one I’ve been working weeks, maybe even a month or two on fixing. I went through several different collision systems before staying with this AABB one I have, I knew it had to work. Guess what was causing all this mad chaos? Not applying a break statement properly. I had tried using one quite a few times, but nothing. I feel like a super programming noob right now, but it doesn’t matter. I did it. I am so going to post an in depth tutorial on all of this once I figure one last thing out… Ignoring inner tile collisions. But I already know how to do that.