I want to find a good path from a point A to a point B on a limited hardware (CPU: Vortex86, 256 MB) using the A* algorithm. I have a grid of 300x200 cells with fixed obstacles. The hit box to avoid obstacles is a disk.
I'm looking for an optimized way to check if my hit box is in collision with obstacles as it is done very often in A*.
The most obvious way was to check the whole area of the disk like:
bool check(std::function<bool(const Coordinates &)> collide)
{
const std::uint32_t RADIUS2 = radius * radius;
Coordinates cell(-radius, -radius);
for (; cell.x <= radius; cell.x++)
{
for (cell.y = -radius; cell.y <= radius; cell.y++)
{
if (cell.x * cell.x + cell.y * cell.y <= RADIUS2 && !collide(center + cell))
{
return false;
}
}
}
return true;
}
Where radius
is the radius of the disk and center
the coordinates of the center of the disk.
A better solution could be to only check the perimeter of the disk. But both solutions need to use 2 for
loops and it doesn't fit the disk area a big part of the time.
Do you have any solution to do it a clever way?