2-D game, rectangular hitboxes, and diagonal movement hit detection
Members - Reputation: 140
Posted 21 April 2014 - 10:13 PM
Members - Reputation: 338
Posted 22 April 2014 - 07:37 AM
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.