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 6762283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:18:06+00:00 2026-05-26T14:18:06+00:00

For my game, I need to find the distance between two objects at certain

  • 0

For my game, I need to find the distance between two objects at certain times. My question is simply can the findDistance function be improved? I don’t seem to have any issues, I’m only curious if it could have been done better. Also, if I were to do this in a 3D setting how would I go about calculating it since there is another axis?

#include <math.h>
#include <iostream>

struct point{float x, y;};

float findDistance(point &x, point &y)
{
    float distance, tempx, tempy;
    tempx = (x.x - y.x);
    tempx = pow(tempx, 2.0f);
    tempy = (x.y - y.y);
    tempy = pow(tempy, 2.0f);
    distance = tempx + tempy;
    distance = sqrt(distance);
    return distance;
}

int main()
{
    point a, b;
    a.x = -2;
    a.y = -3;
    b.x = -4;
    b.y = 4;

    std::cout << "The distance between point x and y is: " << findDistance(a, b) << std::endl;
    return 0;
}
  • 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-26T14:18:07+00:00Added an answer on May 26, 2026 at 2:18 pm

    3d:

    float findDistance(point &x, point &y)
    {
        float distance, tempx, tempy, tempz;
        tempx = (x.x - y.x);
        tempx = tempx * tempx; //compiler _might_ be able to make this faster
        tempy = (x.y - y.y);
        tempy = tempy * tempy;
        tempz = (x.z - y.z);
        tempz = tempz * tempz;
        distance = tempx + tempy + tempz;
        distance = sqrt(distance);
        return distance;
    }
    

    Other than replacing pos(x,2) with x*x, this cannot be optimized and remain generic/accurate, but if you don’t need generic, it can be faster:

    bool distanceLessThan(point &x, point &y, float distance)
    {
        float tempx, tempy, tempz, tempd;
        tempx = std::abs(x.x - y.x);
        tempy = std::abs(x.y - y.y);
        tempz = std::abs(x.z - y.z);
        if (tempx + tempy + tempz < distance) //for small distances, don't square
            return true;
        if (tempx + tempy + tempz > distance/2) //for large distances, don't square
            return false;
        tempx = tempx * tempx;
        tempy = tempy * tempy;
        tempz = tempz * tempz;
        tempd = distance * distance; 
        if (tempx + tempy + tempz < tempd)
            return true;
        return false;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing a game and I need to find a way of getting the
For some game where one would need to find anagrams from a bunch of
I'm working on a PHP Jeopardy game, I need a way to find out
Given two files containing list of words(around million), We need to find out the
I have a tile based game and I need to find the closest tile
I'm currently working on a flash game and I need to know how to
I need to make a game in Java for a project. What I'm trying
I need to implement a FIFO queue for messages on a game server so
I'm writing a game in python with pygame and need to render text onto
What precisely do I need to make a multi-player game on smartphones like Blackberry

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.