Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: What's the best method to give an entity a "search" range?  (Read 4225 times)

0 Members and 1 Guest are viewing this topic.

URSvAir14

  • Newbie
  • *
  • Posts: 14
    • View Profile
What's the best method to give an entity a "search" range?
« 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

MadMartin

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: What's the best method to give an entity a "search" range?
« Reply #1 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.

URSvAir14

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: What's the best method to give an entity a "search" range?
« Reply #2 on: February 26, 2014, 09:22:17 pm »
Okay, thank you :)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: What's the best method to give an entity a "search" range?
« Reply #3 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 for detailed information.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

insomniac

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: What's the best method to give an entity a "search" range?
« Reply #4 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);

The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile
What's the best method to give an entity a "search" range?
« Reply #5 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.
Current Projects:
Technoport

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: What's the best method to give an entity a "search" range?
« Reply #6 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.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile
What's the best method to give an entity a "search" range?
« Reply #7 on: March 06, 2014, 02:43:45 am »
You can use the points and bounds of the Circleshape in your collision code.
Current Projects:
Technoport

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: What's the best method to give an entity a "search" range?
« Reply #8 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
        }
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

 

anything