SFML community forums

Help => General => Topic started by: URSvAir14 on February 26, 2014, 09:10:21 pm

Title: What's the best method to give an entity a "search" range?
Post by: URSvAir14 on February 26, 2014, 09:10:21 pm
Hello all!
I just have a quick (and fairly noob-ish) question about enemies in a game having a "search" range. Say for instance I want to have a turret that will attack an enemy unit within a specified range, what is the best and most efficient way of finding an enemy unit if you have more than one enemy?

I was thinking about having the turret find the distance between itself and every enemy in the container and then selecting the closest one that is within it's range. However wouldn't that just make the game lag if there are more turrets added? So which method would you guys say is the best for making a "search" range?

Thanks :D
Title: Re: What's the best method to give an entity a "search" range?
Post by: MadMartin on February 26, 2014, 09:15:03 pm
Just do the brute force check "every turret <-> every enemy". If you have few turrets and enemies (like 100 or so) the check shouldn't be that bad on today's computers.

But if you really happen to experience any lags, use a spatial data structure like a quadtree to organize your objects and to reduce the number of tests that are needed.
Title: Re: What's the best method to give an entity a "search" range?
Post by: URSvAir14 on February 26, 2014, 09:22:17 pm
Okay, thank you :)
Title: Re: What's the best method to give an entity a "search" range?
Post by: Nexus on February 26, 2014, 10:24:42 pm
Already a spatial grid (the non-hierarchical version of quadtrees) will show massive performance improvements. See here (http://en.sfml-dev.org/forums/index.php?topic=13766.0) for detailed information.
Title: Re: What's the best method to give an entity a "search" range?
Post by: insomniac on March 05, 2014, 07:17:05 pm
I use this to check distances.
 /**
 * Finds the distance IN TILES between a pair of x,y coordinates
 */

inline int path_length(int x1, int y1, int x2, int y2)
{
        return std::abs(x1 - x2) + std::abs(y1 - y2);
}

int enemy_distance = path_length(enemy->x, enemy->y, this->x, this->y);
Title: What's the best method to give an entity a "search" range?
Post by: The Terminator on March 06, 2014, 12:20:42 am
You could also render a transparent sf::Circleshape at the same position as the turret and check for collision.
Title: Re: What's the best method to give an entity a "search" range?
Post by: zsbzsb on March 06, 2014, 02:20:58 am
You could also render a transparent sf::Circleshape at the same position as the turret and check for collision.

Ummm, the point to a transparent shape would be what? SFML doesn't even support circular collision detection so you would need to write that yourself and that effectively obsoletes a transparent shape since a transparent shape would have no impact on the collision detection.
Title: What's the best method to give an entity a "search" range?
Post by: The Terminator on March 06, 2014, 02:43:45 am
You can use the points and bounds of the Circleshape in your collision code.
Title: Re: What's the best method to give an entity a "search" range?
Post by: zsbzsb on March 06, 2014, 02:48:50 am
You can use the points and bounds of the Circleshape in your collision code.

Why go to the trouble when something as simple as below works just as effectively and is much more efficient for circular collision detection?

        private static float Distance(Vector2f A, Vector2f B) // returns distance between two points
        {
            return (float)Trigonometry.SqrRoot((B.X - A.X) * (B.X - A.X) + (B.Y - A.Y) * (B.Y - A.Y));
        }
        private static bool Collides(Circle A, Circle B) // simple collision detection between two circles
        {
            float dist = Distance(A.Position, B.Position); // gets distance between circles
            return dist < A.Radius + B.Radius; // if dist < combined radius we have a collision
        }