# Algorithm for potential fields

Rather than computing potential fields in a grid and performing a lookup for them, I've decided to query the potential fields at a given point in 3D space by simply querying all the objects in a sphere of a given size- after this, I decide that their contribution to the final target is minimal and not worth computing.

However, after this, my efforts seem to be falling a bit short. Everything that I've tried results in either absolutely no effect whatsoever, or the units endlessly jerk around in circles.

I have the target position given by A*, and the direction I need to be facing in to face that target.

Right now, I'm running with this:

float total_distance = 0;
auto units = Units.collision(Physics::Sphere(unit->GetPosition(), range));
std::for_each(units.begin(), units.end(), [&](Unit* u) {
if (u == unit)
return;
if (Math::Length(u->GetPosition() - unit->GetPosition()) > range)
return;
total_distance += Math::Length(unit->GetPosition() - u->GetPosition());
});
std::for_each(units.begin(), units.end(), [&](Unit* u) {
if (u == unit)
return;
if (Math::Length(u->GetPosition() - unit->GetPosition()) > range)
return;
auto relative_vector = unit->GetPosition() - u->GetPosition();

• What's the purpose of total_distance? – Mihai Maruseac May 2 '12 at 7:00
• @teodron: The Units variable is an octree. – DeadMG May 2 '12 at 18:33