I was really confused about this until I had a Dr. Pepper and an epiphany.
I think you could do something along the lines of, If sprite was to occupy the same spot as another solid object return him to the tile he previously occupying. Like for instance: If I was to run into a wall at full speed I would fall flat on my ass if it wasn't a paper mache wall. Now think of it in tile space if your sprite was to run into a wall at full speed then you could make him fall flat on his ass by setting his X or Y or whatever into the tile he was in. Perhaps maybe right in the middle of the tile, to the top left, or whatever looks natural.
Now to actually calculate the collision you could do something like this. Make a method like this:
sO would be the thing you don't want your sprite in
You pass it your sprites information
1 2 3 4 5 6 7 8 9 10 11
| return boolean CheckCollision(int X, int Y, int Height, int Width,int Layer){ int sOX, sOY, sOHeight, sOWidth; int sOLayer; boolean hit = false; if(((X+Width >= sOX && Y+Height >= sOHeight) || (x <= sOX+sOWidth && y <= sOY+sOHeight))&&Layer == sOLayer) { hit = true; }
return hit; } |
Those parenthesis are extremely important. If this does work like I want it to it should return true when your sprite gets within the solid object. But then again I wrote it so it might just be powered by my voodoo magic brain and this method doesn't work anywhere else haha. Tell me if this works I would like to know. (Also parallax games are cool.)