简体   繁体   English

找到两个移动物体的更好交点

[英]Find the better intersection of two moving objects

I would like to optimize dramaticaly one of my algorithm, i will try to explain it the best way that i can. 我想优化我的算法之一,我将尝试以最佳方式解释它。

The subject 主题

We are in a 2D euclidian system at the time t = 0 . 我们在t = 0时处于2D欧几里得系统中。 In this system there is two object : O1 and O2 . 在这个系统中有两个对象: O1O2

O1 and O2 are respectively situated at the point PA and PC . O1O2分别位于PAPC点。

O1 moves at a constant and known speed in direction of the point PB . O1以点PB的方向以恒定且已知的速度移动。 The object will stop when it reach PB. 当物体到达PB时,物体将停止。

O2 can move at a constant and known speed different or not of O1's in any direction. O2可以在任何方向上以不同或不同的O1的恒定和已知速度移动。 At the time 0, O2 has no direction , we will need to find one for it. 在时间0,O2 没有方向 ,我们需要为它找到一个。

The knowns parameters: 知识参数:

  • O1 : Position, direction, speed O1:位置,方向,速度
  • O2 : Position, speed O2:位置,速度

Here is a little diagram of the system. 这是系统的一个小图。

系统图

We would like to find the point PI and the time ti for which : Position of O1 at the time ti = Position of O2 at the time ti = PI . 我们希望找到点P1和时间ti ,其中: Position of O1 at the time ti = Position of O2 at the time ti = PI Then we will make the object O2 move to the point PI to get the O2 direction . 然后我们将使对象O2移动到点P1以获得O2方向

When the direction of O2 (the point PI) is chosen and both objects O1 and O2 are on the move, the objects will never stop or wait for each other. 当选择O2的方向(点PI)并且两个对象O1和O2都在移动时, 对象将永远不会停止或等待彼此。

In this case, the result would be something like this (PI is noted D on this picture). 在这种情况下,结果将是这样的(PI在此图片上标注为D)。 最佳交集

The algorithm 算法

You can find the working algorithm written in JS at this jsfiddle , it is also a great way to understand the problem. 你可以在这个jsfiddle找到用JS编写的工作算法,它也是理解这个问题的好方法。

At this time i use a simple algorithm who works, but can take a lot of operations, i will get the best intersection time, and get the intersection position afterwards. 这时我使用的是一个简单的算法,但是可以进行大量的操作,我会获得最佳的交叉时间,然后获得交叉位置。

To get this time, i will check the position of O1 at a moment, and check if O2 could possibly go to this position at this time. 为了得到这个时间,我会在一刻检查O1的位置,并检查此时O2是否可能到达此位置。 If O2 could not reach the object in time, we will increase the time by 150%, however if O2 could cross the O1-B line at the time, we will decrease the time by 50%. 如果O2无法及时到达物体,我们会将时间增加150%,但是如果O2当时可以越过O1-B线,我们将把时间缩短50%。

Eventually, after many approximations, we will find the perfect time where both objects could meet. 最终,经过多次近似,我们将找到两个物体相遇的最佳时间。

PseudoCode 伪代码

function getOptimalIntersectionTime time
   if distance between O1 and O2 at the time `time` < 1
       return time
   else if O2 could not reach the object O1 at the time `time`
       return getOptimalIntersectionTime time * 1.5
   else
       return getOptimalIntersectionTime time * 0.5

Why am I concern ? 我为什么关心?

My algorithm works, but in some case (eg the "Reverse Case" in the jsFiddle) it will take a large amount of calculus to find the best point. 我的算法有效,但在某些情况下(例如jsFiddle中的“反向情况”),需要大量的微积分才能找到最佳点。

In this jsFiddle, we are using little values for position (-1000 to 1000) and speed (1-200) but this algorithm is dramaticaly slower with bigger numbers. 在这个jsFiddle中,我们使用较小的位置值(-1000到1000)和速度(1-200),但是这个算法在数字较大的情况下显得较慢。

I know that premature optimization is a bad idea, but I'm at the end of the project (which consists on databases insertions / selection and data analysis, including this algorithm called many many times) and this algorithm take up to 80% of the project system ressources in certain cases so an improvement could really improve the stability and the responsiveness of the system. 我知道过早优化是一个坏主意,但我在项目的最后(包括数据库插入/选择和数据分析,包括这个算法多次调用),这个算法占用了80%的项目系统在某些情况下需要资源,因此改进可以真正提高系统的稳定性和响应性。

Without loss of generality, let O2 be located at (0,0). 不失一般性,让O2位于(0,0)。

Let s and v the location and velocity vectors of O1, v2 the speed of O2, and t the time to intercept. sv为O1的位置和速度向量, v2为O2的速度,t为截距时间。 We then have: 然后我们有:

|s + v * t| = t * v2

By the definition of distance: 根据距离的定义:

(sx + vx * t) ^ 2 + (sy + vy * t) ^ 2 = (t * v2) ^ 2

Multiplying this out and reordering terms gives: 乘以这个和重新排序的术语给出:

  sx^ 2 + 2 * sx * vx * t + vx^2 * t^2
+ sy^ 2 + 2 * sy * vy * t + vy^2 * t^2
-                           v2^2 * t^2
= 0

ie

  sx^2 + sy^2 + (2 * sx * vx + 2 * sy * vy) * t + (vx^2 + vy^2 - v2^2) * t^2 = 0
  \---   ---/   \------------   ----------/       \--------   ------/
      \ /                    \ /                           \ /
       c                      b                             a

As you can see, this a quadratic equation in t. 如您所见,这是t中的二次方程。 We can simply apply the quadratic formula to find the two possible values for t (if the equation has no solution, that's because no interception is possible). 我们可以简单地应用二次公式来找到t的两个可能值(如果方程没有解,那是因为没有拦截是可能的)。 You'll probably want to use the earliest future interception, ie the smaller t that is > 0. 您可能希望使用最早的未来拦截,即较小的t> 0。

Once you have computed the t , finding the interception point and from that the interception direction should be easy. 一旦你计算了t ,找到拦截点,从那里拦截方向应该很容易。

To summarize, this problem can be solved in constant time, no iteration is necessary. 总而言之,这个问题可以在恒定的时间内解决,不需要迭代。

You appear to be over-thinking the problem, it should just be simple geometry. 你似乎过分思考问题,它应该只是简单的几何。

Leaving aside the problem of how you define the nearest point , let's solve for the situation where the desired point is midway between PA and PB . 撇开如何定义最近点的问题 ,让我们解决所需点位于PAPB之间的情况。

We have to assume a time period for the entire cycle, let's call that T . 我们必须假设整个周期的时间段,让我们称之为T

PI = (PB - PA) / 2;  // simplified
TI = T / 2;          // simplified

[decompose all formulae for the x and y coordinates separately]. [分别分解x和y坐标的所有公式]。

There are relatively simple formulae for determining the closest intersection of a point (PC) with a line (PA -> PB), although how that's defined is complicated when that line isn't infinitely long. 有一个相对简单的公式用于确定点(PC)与线(PA - > PB)的最近交点,尽管当该线不是无限长时,它的定义是如何复杂的。

Then you need: 然后你需要:

V1 = (PB - PA) / T;  // O1's velocity
V2 = (PI - PC) / T;  // O2's velocity

These last two lines don't depend on the earlier assumptions - if you know the interception point then the velocity is simply the distance travelled divided by the time taken. 最后两行不依赖于先前的假设 - 如果您知道截取点,那么速度就是行进的距离除以所花费的时间。

Hence unless you impose some additional constraints on V2, there is always a solution and it's calculated in a few trivial math operations. 因此,除非你对V2施加一些额外的约束,否则有一个解决方案,并且它是在一些简单的数学运算中计算出来的。

Update: @Meriton's later answer is better than mine. 更新:@Meriton后来的回答比我好。 I recommend trying his first. 我建议尝试他的第一个。

As you realize, we have three, simultaneous equations in the three unknowns vx2, vy2 and t -- respectively the x and y velocities of 02, and time. 如你所知,我们在三个未知数vx2,vy2和t中分别有三个联立方程 - 分别为02和x的y和y速度。 The equations unfortunately are not all linear: 不幸的是,这些方程式并非都是线性的:

x1o + vx1*t == x2o + vx2*t
y1o + vy1*t == y2o + vy2*t
vx2*vx2 + vy2*vy2 == vy*vy

(Here, x1o, y1o, x2o and y2o are coordinates of the initial positions.) (这里,x1o,y1o,x2o和y2o是初始位置的坐标。)

If there is a way to linearize the problem, I don't see it. 如果有办法线性化问题,我没有看到它。 You can however solve iteratively, and quickly, by the same Newton-Raphson technique GPS uses to work out your position from satellite signals. 然而,您可以通过相同的Newton-Raphson技术迭代地快速解决,GPS用于从卫星信号中计算您的位置。 Of course, to fill in the details and implement this will demand some work! 当然,要填写细节并实施这将需要一些工作!

Update: I think that @Alnitak may have linearized your problem rather neatly. 更新:认为 @Alnitak可能会将您的问题线性化,而不是整齐。 Perhaps a combination of his approach and mine therefore would prosper. 也许他的方法和我的方法的组合因此会繁荣。 (I still think that you'll want to use a Newton-Raphson iteration to converge on @Altinak's T .) (我仍然认为你会想要使用Newton-Raphson迭代来收敛@ Altinak的T

Since the speeds are fixed, this should be solvable using the idea of parallel navigation. 由于速度是固定的,这应该可以使用并行导航的思想来解决。 Think of it this way. 这样想吧。 At time 0, there is a line between O1 and O2 (the LOS, or line of sight). 在时间0,在O1和O2之间存在一条线(LOS或视线)。 If O2 follows the optimal intersect path, then at time 1, the line between O1 and O2 will be parallel to the time 0 LOS. 如果O2遵循最佳交叉路径,则在时间1,O1和O2之间的线将与时间0 LOS平行。 Since you have O2's speed, you can calculate the distance it will travel between time 0 and time 1, and from that can calculate where that intersects the time 1 LOS. 由于您具有O2的速度,您可以计算它在时间0和时间1之间行进的距离,并且可以计算它与时间1 LOS相交的位置。 Think of scribing a circle around O2's original position with radius equal to the distance it will travel in that interval of time. 想想在O2的原始位置周围划一圈,其半径等于它在该时间间隔内行进的距离。 The intersection(s) of that circle with the second LOS will contain the solution. 该圆与第二个LOS的交叉点将包含解决方案。 If there is no intersect, there is no solution. 如果没有相交,则没有解决方案。 The beginning of this online book has a diagram and formulas that show the concept: 这本在线书的开头有一个图表和公式,显示了这个概念:

http://www.crcnetbase.com/doi/abs/10.1201/9781420062281.ch2 http://www.crcnetbase.com/doi/abs/10.1201/9781420062281.ch2

This problem has real world applications where you may also find this solution talked about. 这个问题有真实世界的应用程序,你也可以在这里找到这个解决方案。 For instance submarines can use this to plot and maintain an intercept course with their target by keeping the LOS bearing to their target constant as they close on their target. 例如,潜艇可以使用它来绘制和维持与目标的拦截航线,方法是在它们靠近目标时使LOS轴承保持不变。

Edit: 编辑:

在此输入图像描述

This diagram shows what I'm talking about. 该图显示了我在说什么。 This can be solved using trigonometry except for the special case where the target O1 is moving directly towards or away from the missile O2 (which can be solved trivially). 这可以使用三角函数来解决,除了目标O1直接朝向或远离导弹O2(可以简单地解决)的特殊情况。

In the diagram above we can take some arbitrary small amount of time. 在上图中,我们可以花费一些任意的时间。 During that time t1, O1 will have traveled distance b, and O2 will have traveled distance f. 在那个时间t1期间,O1将具有行进距离b,并且O2将具有行进距离f。 The line between O1 and O2 at time t0 is parallel to the line between O1 and O2 at time t1. 在时间t0,O1和O2之间的线与在时间t1的O1和O2之间的线平行。 Since we are given the initial positions of O1 and O2 we know distance d, and since we are given O1's direction, we can simply calculate the angle A. 由于我们给出了O1和O2的初始位置,我们知道距离d,并且由于我们给出了O1的方向,我们可以简单地计算角度A.

So given A, b, f, and d, using the law of Cosines,
a = sqrroot(c^2 + b^2 - (2cb * cos(A)))
and
B = arccos((a^2 + c^2 - b^2)/2ac)
Using the law of Sines
E = arcsin((a * sin(B))/f)  or the ambiguous value of 180 - that value
and with that
BC = 180 - E   (because C = 180 - B - E so C+B = 180 - E

with BC we have the solution, and the any other aspects of the triangle of the initial locations of O1 and O2 and the intersection point can be similarly calculated. 对于BC,我们有解决方案,并且可以类似地计算O1和O2的初始位置的三角形的任何其他方面和交点。 It's been many years since I used my high school trig, so there may be a simplification of this that I've missed, but this hopefully explains the solution approach I initially described. 自从我使用高中三年以来已经很多年了,所以我可能会错过这个简化,但这有希望解释我最初描述的解决方法。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM