View Single Post
Old 12 August 2019, 10:23   #51
zero
Registered User
 
Join Date: Jun 2016
Location: UK
Posts: 428
Quote:
Originally Posted by Shatterhand View Post
The grid idea always looks good on paper but, when I try to implement it, I am still doing basically the same number of comparisions on code so still no banana.
Then you are doing it wrong ;-)

Bounding box looks like this:

Code:
if (x >= box_left) && (x <= box_right) && (y >= box_top) && (y <= box_bottom)
Where as grid looks like this:

Code:
if (player_grid == bullet_grid)
So it's one test instead of four. One trick you can do is to use this to eliminate most of the bullets, which will be far away from the player, and then do a more accurate test on just the ones that are very close. It helps if the player has a tiny hit box like typical Japanese shooters.

Edit: I forgot another classic example of grid collision detection: PacMan! In PacMan collisions happen when PacMan and a ghost occupy the same tile. It actually make the game more exciting because the ghosts can get really close to the player, actually touching, and the player can still get away sometimes. Doesn't feel janky at all.
zero is offline  
 
Page generated in 0.09414 seconds with 11 queries