Espozo wrote:
Isn't it like you load the object's x position, divide it by the width of each block to find the starting block, and also divide the hitbox width by the width of each block to find how many tiles over you look for?
To avoid getting touching the subject of how hitboxes are defined (some people like them at the top left corner, others in the center, etc.), it might be better to say that you check all blocks from one corner of the hitbox to another. For example, to look for collisions against the ground when falling, you'd check all blocks between (left, bottom) and (right, bottom). And yes, coordinates must be divided by the dimensions of the blocks that define the collision map.
If the game is only made of blocks, this is all it takes to do proper collision detection: move horizontally, check for solid blocks in the direction of the movement, eject if solid blocks are found; move vertically, check for solid blocks in the direction of the movement, eject if solid blocks are found.
If you have slopes though, things can get quite a bit more complicated, and there are hundreds of ways to implement those depending on the kind of levels you want to make. Generally, slopes affect the speed of objects (speeding them up or down depending on the angle and the direction of the movement), and after being moved horizontally, they "snap" to the ground. The exact way in which an object snaps to the ground is what varies the most from game to game... One of the most common solutions is to sample only a single point directly below the center of the object, and the other is to sample both sides and go with the highest. Both of these methods have advantages and disadvantages, and should be tweaked to be able to handle all kinds of structures you plan on drawing in your levels.
Quote:
I kind of wonder how collision maps are generally stored. I'd probably do a byte per block. This way, when you're dividing the object's position and width/height, you can store that number into x or y and not do anything else and you'll be able to offset the collision table exactly where you want to.
Personally, I'm against having a collision map separate from the level map, for reasons explained in
this post, unless the game is going out of its way to hide the grid where the map is defined, like tepples pointed out.