点和线段交叉点。两个都在动

时间:2012-10-02 18:48:17

标签: wolfram-mathematica 2d collision-detection point line-segment

如何判断固定速度的移动点与端点移动的线段之间的时间和交点?端点独立移动,但仅在正y方向上移动,并且也在固定速度下移动。

情况图片:

intersection.png

因此,随着时间t,L1移动到L2,R1移动到R2,P1移动到P2。在某些时刻,P在时间t的位置应位于由L和R在时间t形成的线段上的某处。

我把我在mathematica中想出来的每一段关系都搞定了,并让它解决了t,这让我得到了答案,但是当我尝试实现它时似乎没有用(没有发现碰撞。我'我对mathematica很新,并且不知道我在做什么,所以我希望有更多经验的人会注意到我试图解决方程系统的根本错误。感谢阅读!< / p>

其中U是时间t的线段。

  • Ly = L1y +(L2y-L1y)* t
  • Ry = R1y +(R2y-R1y)* t
  • Py = P1y +(P2y-P1y)* t
  • Px = P1x +(P2x-P1x)* t
  • Ux = L1x +(R1x-L1x)* m
  • Uy = Ly +(Ry - Ly)* m
  • m =(Px-L1x)/(R1x-L1x)

解决t其中:

  • Ux = Px
  • Uy = Py

解决方案:

  • A =(P2x-P1x)*(L2y-L1y) - (P2x-P1x)*(R2y-R1y)
  • B =(P2x-P1x)* L1y +(P2x-P1x)* R1y-P1x *(L2y-L1y)+ L1x *(L2y-L1y)+ P1x *(R2y-R1y)-L1x *(R2y- R1y)+(P2y-P1y)*(R1x-L1x) - (R1x-L1x)*(L2y-L1y)
  • C = P1x * L1y-P1x * R1y-L1y * L1x + R1y * L1x + P1y *(R1x-L1x)-L1y *(R1x-L1x)

t =( - B + -sqrt(B ^ 2 - 4AC))/ 2A

2 个答案:

答案 0 :(得分:1)

数值解决方案:

l1 = {0.969, 0.594};
l2 = {0.892, 0.895};
r1 = {0.75880, 0.90366};
r2 = {0.22, 0.57};
p = {0.337+ 0.8764 t, 0.726 + 0.252 t};
s1 = l1 + ( l2 - l1) t;
s2 =  r1 + (r2 - r1) t;
lx = ( (1 + ci) s1 + (1 - ci) s2 )/2 ;
ciz = (ci /. 
   Solve[ Dot[ {px, 
      py}  - ( (1 + ci) {s1x, s1y} + (1 - ci) {s2x, s2y} )/
      2 , {s1x, s1y} - {s2x, s2y}] == 0, ci][[1]]);
cizt = Simplify[
   ciz /. { px -> p[[1]] , py -> p[[2]] , 
     s1x -> (l1 + ( l2 - l1) t)[[1]], 
     s1y -> (l1 + ( l2 - l1) t)[[2]] , 
     s2x -> (r1 + ( r2 - r1) t)[[1]], 
     s2y -> (r1 + ( r2 - r1) t)[[2]] }];
distance[t_] = 
  Simplify[Norm[lx - p]^2  /. ci -> cizt ,  Assumptions -> {Im[t] == 0} ];
Plot[distance[t], {t, 0, 1}]
possiblesolution = FindMinimum[distance[t], {t, 0, 1}]
If[ Chop[possiblesolution[[1]]] == 0, 
 tp = (t /. possiblesolution[[2]]); Print["possible hit at t=", tp]; 
 If[Abs[cizt /. possiblesolution[[2]]] > 1, 
 Print["missed off the end"], 
 Animate[Show[Graphics[{Point[{p /. t -> 0, p /. t -> 1} ]}], 
Graphics[{Line[{l1, r1} ]}], Graphics[{Line[{l2, r2} ]}], 
Graphics[{Dashed, Line[{s1, s2} /. t -> a]}], 
If[a < tp, Graphics[{Red, Line[{p /. t -> 0, p /. t -> a}]}], 
 Graphics[{Red, Line[{p /. t -> 0, p /. t -> a}], Blue, 
   Line[{p /. t -> tp, p /. t -> a}]}]]], {a, 0, 1}]]]

如果你看一下距离函数,你会看到最小值是7阶多项式的根。要健壮,你需要查看所有真实的根。

编辑 - 基于Mr Wizard的解决方案的更好的版本。我通过检查交点是 on 段来改进它,而不仅仅是在点之间的infinte线上。 此示例生成随机问题,并在找到有效解决方案的问题后停止。


solutions = {}
While[Length[solutions] == 0,
 {l1, l2, r1, r2, p1, p2} = RandomReal[{0, 1}, 2] &  /@ Range[6];
 p = p1 + (p2 - p1) t  ;
 s1 = l1 + ( l2 - l1) t;
 s2 =  r1 + (r2 - r1) t;
 realsols = 
  Solve[ { 0 < t < 1 ,  Det[ { s1 - s2 , p - s2}]  == 0 ,Dot[ p - s2  , p - s1 ] < 0 } ];
 If[Length[realsols] > 0, solutions = Sort[ (t /. realsols)]; 
  tp = solutions[[1]]];] 
Animate[Show[
  Graphics[{Point[{p1, p2} ]}],
  Graphics[{Green, Line[{l1, r1} ]}],
  Graphics[{Orange, Line[{l2, r2} ]}],
  Graphics[{Dashed, Line[{s1, s2} /. t -> a]}],
  If[a < tp,
   Graphics[{Red, Line[{p1, p /. t -> a}]}],
   Graphics[{
     Red, Line[{p1, p /. t -> a}],
     Blue, Line[{p /. t -> tp, p /. t -> a}]}
    ]]], {a, 0, 1}]

不过,让Solve []找到所有解决方案然后选择有效解决方案而不是解决约束条件会更快。


(Do [ realsols = 
    Solve[ { Det[ { s1 - s2 , p - s2}]  == 0 , 0 < t < 1, 
      Dot[ p - s2  , p - s1 ] < 0 } ] , {10} ]; realsols)  // Timing
(Do [realsols = 
    Select[ Solve[ {  Det[ { s1 - s2 , p - s2}]  == 0   , 
       0 < t < 1}  ] , 
     Dot[ p - s2  , p - s1 ] < 0 /. #  & ]   , {100} ]; 
  realsols) // Timing
(Do [realsols = 
    Select[ Solve[ {  Det[ { s1 - s2 , p - s2}]  == 0   } ] ,  0 <= t <= 1  &&    Dot[ p - s2  , p - s1 ]  < 0  /. #   & ]   , {100} ]; 
  realsols ) // Timing

第一种形式更漂亮: - )

答案 1 :(得分:0)

定义点:

{L1, L2, R2, R1, P1, P2} =
   {{0.01`, 0.31`}, {0, 2.`}, {2.985`, 1.9`}, {2.995`, 0.95`},
    {1.7`, 1.82`}, {1.23`, 0.87`}};

定义函数:

L := L1 (1 - t) + L2 t
R := R1 (1 - t) + R2 t
P := P1 (1 - t) + P2 t

可视化问题:

Manipulate[
 Block[{t = tt},
  Graphics[{
    {Red, Polygon[{L1, L2, R2, R1}]},
    {White, Thick, Line[{P1, P2}]},
    {Black, PointSize[Large], Point@{L, R, P}, Line[{L, R}]}
   }, Background -> Gray, PlotRangePadding -> 0.7]
 ],
 {tt, 0, 1}
]

Mathematica graphics

求解t并观察解决方案是否符合可视化:

Reduce[{Det[{R - L, P - L}] == 0, 0 < t < 1}, t]
  

t == 0.525873

利润。