Hi,
I'm inclined to think that it is always better to start with simple logic.
If it works, but not fast enough, you can start thinking of ways to improve/faster it.
So, here is a simple way to proceed, first. I'm not saying it is the best, though.
The idea would be breaking collision checking in small for loops.
Code: Select all
-- Player vs ennemies
for i = 1,#badguys do
-- test player vs badguys [i]
end
-- Player vs bullets
for i = 1,#bullets do
-- test player vs bullets[i]
end
-- player_bullets vs ennemies
for i = 1, #player_bullets do
for j = 1,#badguys do
-- test player_bullets[i] vs badguys[j]
end
end
On the top of that, there are lots of tricks that can be used to reduce the number of collisions checks. But that would be very dependant of the gameplay, though.
If most of the time, ennemies are near each other, you can think of a way to group them, and then keep track of a bounding box that surrounds the whole group. Then each loop, the player will be tested only against this very bounding box. And if this collision test happened to be true, you can start loop trough the complete set of ennemies to identify exactly whom the player is actually colliding with.
Same goes for the bullets...
Also, the way collision checking is done is quite important. If you're dealing with simple AABBs (bounding rectangles), that won't matter so much. But if collision checking is a quite complicated process because you're dealing with more complex shapes , you might want to break the previous logic in two steps: make simple AABBs checks to find possible colliding pairs, and collect them. Then out of the loop, use the exact collision checking algorithm to detect if there's really collision and run the ad hoc logic.
[Disclaimer: Overthinking here (
took too much coffee)]
I am also thinking of a way to cluster the space, yet it is not proven to be the efficient solution here.
The general idea is the partition the space in virtual grid, where each cell would be a square larger than the largest entity in the game (width or height, actually). Then each update loop, compute the x, y cell coordinate for each moving entity, and skip collision check when the player's x,y grid coordinates are different than the other entity tested against the player (i.e they are not in the same grid tile). It may be interesting actually if all entities have approximately the same size (I mean player and ennemies) and also if the grid is reasonably huge...