简体   繁体   English

如何计算给定方向的最小平移向量?

[英]How to calculate the Minimum Translation Vector in a given direction?

The Separating Axis Theorem returns a minimum translation vector in an arbitrary direction. 分离轴定理返回任意方向的最小平移向量。 A variation of this problem would be to calculate the minimum translation vector in a given direction. 该问题的变化是计算给定方向上的最小平移向量。 I was wondering how to calculate this with polygons and circles but I'm stuck in the circle-circle case. 我想知道如何用多边形和圆圈计算这个,但我陷入了圆圈的情况。 Does anybody have an idea of how to approach this problem? 有没有人知道如何解决这个问题?

A signature of this function would be: 这个函数的签名是:

Vector minimum_tranlation_vector_between(Circle a, Circle b, Vector axis);

Thanks in advance! 提前致谢!

Suppose that the center co-ordinates and radius of circle a are given by ax, ay and ar 假设中心坐标和圆半径a由ax,ay和ar给出

Also suppose that we have a vector v and we want to push the circles apart by moving the first circle in the direction of v by an amount d. 还假设我们有一个向量v,我们希望通过在v方向上移动第一个圆d来推动圆圈分开。 The circles will be touching when: 在下列情况下,圈子会触及:

((ax + (vx * d)) - bx) 2 + ((ay + (vy * d)) - by) 2 = (ar + br) 2 ((ax +(vx * d)) - bx) 2 +((ay +(vy * d)) - by) 2 =(ar + br) 2

This equation just calculates the squared distance between the offset circle centers and sets it equal to the square of the sum of the radii. 该等式仅计算偏移圆心之间的平方距离,并将其设置为等于半径之和的平方。

We can simplify it a bit by moving both circles by subtracting the second one's center point from both so that the second circle is at the origin, which removes it from the equation: 我们可以通过从两者中减去第二个圆的中心点来移动两个圆来简化它,以便第二个圆位于原点,从而将其从等式中移除:

(ax + (vx * d)) 2 + (ay + (vy * d)) 2 = (ar + br) 2 (ax +(vx * d)) 2 +(ay +(vy * d)) 2 =(ar + br) 2

Now we just need to solve for d and multiply it by v to get the result (the separation vector). 现在我们只需要求解d并将其乘以v得到结果(分离向量)。 I cheated and used an online solver , there are the 2 solutions (formatted as code because the formatter keeps trying to turn the multiplies into italics): 我作弊并使用了在线求解器 ,有2个解决方案(格式化为代码,因为格式化程序一直试图将乘法变为斜体):

d = -(sqrt((v.y²+v.x²)*e-a.x²*v.y²+2*a.x*v.x*a.y*v.y-v.x²*a.y²)+a.y*v.y+a.x*v.x)/(v.y²+v.x²)
d = (sqrt((v.y²+v.x²)*e-a.x²*v.y²+2*a.x*v.x*a.y*v.y-v.x²*a.y²)-a.y*v.y-a.x*v.x)/(v.y²+v.x²)

Here's some proof of concept code in Java: 以下是Java中概念代码的一些证明:

double calculateSeparationDistance(double circle1x, double circle1y, double radius1, double circle2x, double circle2y, double radius2, double vectorx, double vectory)
{
    double a = circle1x-circle2x;
    double b = vectorx;
    double c = circle1y-circle2y;
    double d = vectory;
    double e = (radius1 + radius2) * (radius1 + radius2);

    // 2 possible solutions:
    double distance = -(Math.sqrt(((d*d)+(b*b))*e-(a*a)*(d*d)+2*a*b*c*d-(b*b)*(c*c))+c*d+a*b)/((d*d)+(b*b));
    // double distance = (Math.sqrt(((d*d)+(b*b))*e-(a*a)*(d*d)+2*a*b*c*d-(b*b)*(c*c))-c*d-a*b)/((d*d)+(b*b));

    // add (distance * vectorx, distance * vectory) to first circle, or subtract from second circle to separate them 
    return distance;
}

The two solutions correspond to pushing the first circle out of the second one in either direction. 这两个解决方案对应于在任一方向上将第一个圆推出第二个圆。 You can chose whichever solution has the smallest absolute value, or choose the one with the positive sign. 您可以选择具有最小绝对值的解决方案,或选择具有正号的解决方案。

Failure case: when the circles are not intersecting and the vector does not put them on a collision course then there are no solutions and the equation gives the square root of a negative value, so either do an intersection test beforehand or check the sign of the value before passing it to sqrt(). 失败案例:当圆圈没有交叉且矢量没有将它们放在碰撞路线上时,则没有解决方案,并且等式给出负值的平方根,因此要么事先进行交叉测试,要么检查符号将值传递给sqrt()之前的值。

Copying the naming scheme from samgak, but reducing everything to the standard situation for quadratic equation gives the procedure 从samgak复制命名方案,但是将所有内容减少到二次方程的标准情况给出了该过程

double calculateSeparationDistance(double circle1x, double circle1y, double radius1, double circle2x, double circle2y, double radius2, double vectorx, double vectory)
{
    double dx = circle1x-circle2x;
    double vx = vectorx;
    double dy = circle1y-circle2y;
    double vy = vectory;
    double R2 = (radius1 + radius2) * (radius1 + radius2);

    // the equation is 
    // (dx+vx*d)²+(dy+vy*d)² = R2
    // expanding and reordering by degree
    // (vx²+vy²)*d²+2*(vx*dx+vy*dy)*d+(dx²+dy²-R2) = 0

    double a = vx*vx;
    double b = 2*(vx*dx+vy*dy);
    double c = dx*dx+dy*dy-R2

    // Note that we want the smaller (in absolute value) solution, 
    // and that the selection by the solution formula depends on
    // the sign of b. Setting (b,d):=(-b,-d) still results in an
    // equation with a solution.

    double sign = 1;
    if ( b<0 ) { b=-b; sign = -1; } 

    // Real solutions do not exist if the discriminant is negative
    // here that means that abs(dx*vx+dx*vy) is too small, 
    // geometrically that the circles never touch if moving in
    // direction v.

    if( b*b < 4*a*c ) return 0; // or 1e308 or throw exception

    // apply stable standard solution formula for the smaller solution:
    double d = -(2*c)/(b + Math.sqrt(b*b-4*a*c));

    return sign*d;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 分离轴定理-包含和最小平移向量 - Separating Axis Theorem - Containment and the minimum translation vector 如何在给定的索引范围内找到向量的最小元素? - How to find minimum element in a given range of indices for a vector? 如何找到矢量路径的方向 - How to find direction of a vector path c ++如何从向量求和给定数字的向量中找到最小数量的元素 - c++ How to find the minimum number of elements from a vector that sum to a given number 获取给定向量与数据库中向量之间的最小欧几里得距离 - Get minimum Euclidean distance between a given vector and vectors in the database 给定Vector3和距离的列表,如何计算分段线上的点? - How do I calculate a point on a segmented line given a list of Vector3 and a distance? 给定2d坐标系中的一组点,如果我们只访问所有点一次,如何计算最小距离和路径? - Given a set of points in 2d coordinates system, how to calculate the minimum distance and the path if we visit all the points only once? 如何计算搜索图表的最短预期时间? - How to calculate minimum expected time for searching a graph? 如何计算列表的最小不公平总和 - how to calculate the minimum unfairness sum of a list K均值-如何计算最小距离 - K-means - how to calculate minimum distance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM