2-D game, rectangular hitboxes, and diagonal movement hit detection

Started by
0 comments, last by PunCrathod 9 years, 12 months ago
I'm doing a game and have gotten standard hit detection down for straight movements, but diagonal ones have me wondering the best way to go about. The sprite hitboxes are all rectangular and the main BG blocks are all squares that are 8x8 pixels. For my straight movements, I basically take the hitbox, copy it, move the copy freely and if the points are within a solid, then the speed the normal box would be moving at is reduced by how much into the wall it is. Diagonal movements, however, are a bit trickier. If I move the copy box freely both directions, there can be situations where resolving is not clear. If I do vertical and then horizontal, it WILL work, but has some side effects. If the box is falling and there's a narrow space to the side it could fit into, the desired behavior is that the box will go into the space and land on the floor. If I do both movements separately, it could bypass that space. I attached an image showing what I'm talking about. The numbers are hexadecimal. In the image, the green box is trying to move down and right. The first thing I thought of trying was move the copy box diagonally, check if all bottom points EXCEPT the bottom right are in a solid, then push up if need be. After that, check if all the right points EXCEPT the bottom right are in a solid, then push left if need be. Finally, the bottom right point is checked. The problem is how to resolve after here. If I push it up, I have to check the right side AGAIN to see if anything is in a solid. Likewise, if you fell past the empty point and there's a wall there, he could be pushed up and even come to a halt. If I push it out both ways, he'd basically stop on the corner. He needs to slide smoothly along the walls, but be able to squeeze into the spaces he can fit in. Is there anything I'm missing? Thank you.
- Sivak
Advertisement

For some reason I can't see the attached image? So sorry if this is of no help.

I'm going to assume all of your BG blocks are in a uniform grid(aka each and every block is exactly n pixels apart and there can be empty block between that are each n pixel wide)

You can march through the grid by doing the following.

From the corner that is closest to the direction you are moving(bottom right if you are moving down and right, top right if you are moving up and right etc,) take x distance to the the next block in the grid and divide it by x speed to get the time it takes to hit the next block and do the same for y axis. Then see wich time was shorter and move in both directions at once using speed*timetoreachnextblock and you have now arrived to the block you would hit first. If you hit something solid then do what usually happens when hittin solid things and then continue with adding the timetoreachnextblock to a timeused variable and repeat until the timeused reaches 1 frame.

This way when you hit a block your hitbox will always be at the border of the block and you will also know wich side it hit. This makes it really easy to figure out where the hitbox needs to be moved next.

This topic is closed to new replies.

Advertisement