求两个物体之间的距离

Finding the distance between two objects

本文关键字:之间 距离 两个      更新时间:2023-10-16

对于我的游戏,我需要在特定时间找到两个物体之间的距离。我的问题很简单,findDistance函数可以改进吗?我似乎没有任何问题,我只是好奇它是否可以做得更好。同样,如果我要在3D环境中做这个,我该如何计算呢,因为还有另一个轴?

#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;
}

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;
}

除了用x*x替换pos(x,2),这不能优化并保持通用/准确,但如果你不需要通用,它可以更快:

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;
}

对于3D,只需添加一个tempz到距离:

tempz = (x.z - y.z);
tempz = pow(tempz, 2.0f);
distance = tempx + tempy + tempz;

顺便说一句,"如果没坏,就不要修它。"

我觉得一般情况下pow(foo, 2)会比foo * foo

还要问自己在计算中是否需要距离或square_of_distance是否足够。

例如,要找出哪个距离更大,您可以简单地比较square_of_distances而不是实际距离本身,从而节省了调用sqrt

您当前的方法很好,对于新的协调只需像其他人一样添加它:

struct point{float x, y,z;};
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);
    tempz = (x.z - y.z);
    tempz = pow(tempz, 2.0f);
    distance = tempx + tempy + tmpz;
    distance = sqrt(distance);
    return distance;
}

事实上,对于n维,你有相同的计算距离的公式,但你应该添加新的维度。

Pow()用于一般浮点指数,对于计算平方是低效的。使用明显的乘法代替。

只是加上z,但你真的不能做得比这更好。你可以用x * x代替pow(x, 2),但仅此而已。如果你不需要知道距离,只需要知道两个物体是否碰撞,那么就有一些优化,但除此之外:

float findDistance(point &x, point &y)
{
  float distance, tempx, tempy, tempz;
  tempx = x.x - y.x;
  tempy = x.y - y.y;
  tempz = x.z - y.z;
  distance = sqrt(tempx*tempx + tempy*tempy + tempz*tempz);
  return distance;
}

是你能做的最好的。

如果通过value:传递,可能会获得更好的性能。

float findDistance(point a, point b)
{
    a.x -= b.x;
    a.x *= a.x; 
    a.y -= b.y;
    a.y *= a.y
    return sqrt(a.x * a.y);
}

移动鸭子的答案包含了你应该知道的一切

我不会使用pow函数:

distance = tempx*tempx + tempy*tempy;

,如果你需要碰撞检测的距离,那么你可以考虑使用距离的平方,这样你就不需要调用sqrt()

但是要注意"过早优化"和80-20规则(80%的运行时花费在20%的代码上),所以也许首先运行一些性能测试,只有当你看到你在findDistance函数上花费了大量时间时,才开始优化它。