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

  • SEARCH
  • Home
  • 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 9022693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:34:49+00:00 2026-06-16T05:34:49+00:00

I have two locations, My player location (2D) and I’m looping over a vector

  • 0

I have two locations, My player location (2D) and I’m looping over a vector of entitys each with a location (2D), I would like my function to return the closest distance to my location, But I don’t know how to do it because I got stuck, in c# I could just use linq, But i’m learning c++ My function looks like this:

const Entity GetNearestEntity()
{
    for (Entity i : fb.Entities)
    {
        double xDiff = (double)abs(fb.PlayerLocation.X - i.X);
        double yDiff = (double)abs(fb.PlayerLocation.Y - i.Y);

        //Stuck here.. 
        //How can i get the closest object out of my vector<Entity> collection?
    }
}

help appreciated!

  • 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-06-16T05:34:51+00:00Added an answer on June 16, 2026 at 5:34 am

    This may be easier to do by keeping track of the index of the closest element to you, which would be easier in a ‘for’ loop like:

    Entity GetNearestEntity()
    {
        int closestEnt = 0;
        double smallestDist = -1.0;
    
        for (int i = 0; i < Entities.length(); i++) 
        {
            double xDiff = (double)abs(fb.PlayerLocation.X - Entities[i].X);
            double yDiff = (double)abs(fb.PlayerLocation.Y - Entities[i].Y);
            double totalDist = sqrt(pow(xDiff, 2) + pow(yDiff, 2));
    
            if ((totalDist < smallestDist) || (smallestDist == -1.0))
            {
                closestEnt = i;
                smallestDist = totalDist;
            }
        }
    
        return Entities[closestEnt];
    }
    

    This may not compile off the bat, it’s been a while since I’ve played with C++, and I’m blanking on whether that’s the right way to do square roots and powers. However, it has the nice benefit of only keeping track of a double and an int, instead of an object.

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

Sidebar

Related Questions

I have two locations. A current location (=visible) and a target location where the
I have two GPS locations. For each I am creating a bounding box in
I have a function that pulls data from two locations and places the returned
I have two different receive ports and two receive locations - one location assigned
I have two tables, events and locations . Each has a column named venue_id
I have two tables, Locations and Facilities They map to two classes, public Location
Given I have two models: company and location . A company has many locations
I have two tables, locations and location groups CREATE TABLE locations ( location_id INT
We have two databases, in two separate locations. One of the databases resides in
We have two different ldap providers in two different physical office locations. When 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.