Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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();
    auto heading = Math::Normalize(relative_vector);
    auto scaled_heading = heading * ((total_distance - Math::Length(relative_vector)) / total_distance);
    target_heading += scaled_heading;
    if (target.x != target.x)
        __debugbreak();
});
target_heading = Math::Normalize(target_heading);

If anybody can suggest a superior algorithm, I'd be interested in hearing about it.

share|improve this question
    
What's the purpose of total_distance? –  Mihai Maruseac May 2 '12 at 7:00
1  
I guess it's a normalizing factor so that the OP achieves a some kind of weighing of object contributions. I.e. each object tugs on others in its neighbourhood by a factor between 0 and 1 in this way. In all, the outcome should be, mathematically, a chaotic motion like the OP described. Therefore, I'd suggest looking at the N body problem solved with CUDA pdf –  teodron May 2 '12 at 7:57
    
@teodron: That was actually quite helpful. I implemented a kind of "reverse gravity" approach, which mirrored their approach but I inverted the resulting vector. –  DeadMG May 2 '12 at 14:53
    
@DeadMG Glad to hear that! It should be, theoretically, quite similar. You might want to use quad/oct trees or other kind of spatial partitioning methods to speed up your objet-to-object distance queries (actually eliminating the objects that are clearly far away from the current particle instead of iterating over all particles).. or.. parallelize it on the GPU. –  teodron May 2 '12 at 16:04
    
@teodron: The Units variable is an octree. –  DeadMG May 2 '12 at 18:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.