Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7589265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:07:03+00:00 2026-05-30T20:07:03+00:00

I am making a simple flok- or crowd-simulation for a small game. The behaviour

  • 0

I am making a simple flok- or crowd-simulation for a small game.

The behaviour is decent for my needs, but the performance is not.

Basicly I have “alot” of objects(soldiers), which need to spread out while moving to a waypoint.

Ive written a simple version of my code here:

List<Soldier> avoidList = new List<Soldier>();

foreach (Soldier s in gameWorld)
{
    if (this == s)
         continue;

    if (Distance(s, this) <= 5)
    {
         avoidList.Add(s);
    }
}

CalculateNewDirection(avoidList);

So basicly, each soldier checks the distance of every other soldier in the scene, and calculates a direction based on the soldiers which are too close.

Like this picture.

Right now im running through x^2 objects.

So if I have 100 objects, i run through 100^2 = 10.000 objects, just to update each individual objects avoidList.

And if i crank it up to 500 objects, i run through 250.000 objects.

Theres gotta be a different and smarter way to do this!

Hope someone can enlighten me 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T20:07:04+00:00Added an answer on May 30, 2026 at 8:07 pm

    Off the top of my head, it seems like you are doing an unneccessary square root when calulating the distance. You could check the squared distance instead and check against the squared constant.

    Also, there is an improvement to be made where if you find a soldier to avoid, the other soldier will also avoid the first. So each pair only needs to be checked once, if you keep track of multiple avoid lists.

    This can be done by saving only the pairs that are too close. In pseudocode

    for (int i = 0; i < numSoldiers - 1; ++i) {
        for (int j = i + 1; j < numSoldiers; ++j) {
            if (Distance(soldier[i], soldier[j]) < 5) {
                closePairs.Add(new Pair(i, j));
            }
        }
    }
    
    // Calculate directions based on the pairs found.
    

    Those are the basic optimizations. If you need to go deeper than that you will need to sort the soldiers based on positions. If you keep track of which soldiers that are on each 5 unit square then no soldier that are more than 2 squares away can possibly influence the given soldier.

    Sorting the soldiers on the X and Y axis first will also give similar oppotunities, since you know that if the distance to the next guy on one axis is larger than the radius there is no need to check the distance.

    That would give you better performance, but it’s dependent on your data and how sparse the soldiers usually are.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I'm making a simple grid based game, for example, I might have a
I am making a simple game in order to learn a new language. I
I'm making a simple 2 player game in XNA and started looking into saving
I'm making a simple 2D game for the iPhone. It's based on CrashLanding. so
I'm making a simple Image Debugger Visualizer. Code is below. I'm not sure if
I am making simple graphical game in WinForms and currently I would like to
I'm making simple game with very simple thread (1 sec delay) got problem with
i am making simple autosuggestion (autocompleter) plugin with jQuery. Unfortunately i have to use
Making a simple program which will generate a multiple choice form. I have an
Im making a simple wallpaper changer. It works when changing the wallpaper but i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.