I'm currently partway through the advanced 3D tutorial, but I've been playing around with making some simple games.
In one of my projects, the player runs around on a large map with a bunch of trees and stuff on it.
This is how I'm currently checking for tree collision:
Code: Select all
vec2 intended_position = player.GetPos() + movement_from_keyboard;
bool can_move = true;
for (int i = 0; i < all_the_trees.size(); i++)
{
if (all_the_trees[i].CollidesWith(intended_position))
{
can_move = false;
// if one collision is found, no point in continuing to check the rest
break;
}
}
if (can_move)
{
player.MoveTo(intended_position);
}
I've seen lots of games like Factorio where there's a huge map with potentially millions of trees and other collision-causing objects, so I know it's possible... how do they do it? My friend (who knows nothing about programming) suggested "well only check the trees that are near you." as if that were obvious. Thing is, I don't know how I'd filter out "just the ones near me" without looping through all of them to do a distance comparison.
So is there some obvious trick that I'm missing?