如何找到反射光线的角度来匹配点

发布于 2024-09-06 21:55:35 字数 274 浏览 9 评论 0原文

这是我正在制作的坦克游戏,

请参阅图片以获得清晰的想法:链接文本< /a>

我想预先计算到达 T2 点的精确角度。

T1:点开始

T2:点目标

V1(a,b):线

反射点:这就是我正在寻找的:)

编辑:看到一些“代码”会很酷:p

this is for a Tank game I am making

Please see pic for a clear idea :link text

I want to precompute the exacte angle to hit Point T2.

T1:point start

T2:point Target

V1(a,b):line

reflect point : this is what I m looking for :)

Edit:it would be cool to see some "Code" :p

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

方圜几里 2024-09-13 21:55:35

了解反射期间线/向量发生了什么会很有用。维基百科为此提供了一张漂亮的图片:

reflection

在这张图片中,在正确的位置反射,两个角度相同

现在,这与你有什么关系?让我们再看看您的情况。

花式图表

请注意,由于反射定律,角度 ab 相等。这对我们有好处,因为如果我们知道这一点,我们就知道 cd 也相等! (它们是直角三角形)

所以我们知道:

a = b
c = d

我们很快意识到我们有相似的三角形。意思是,相应的边彼此成比例。数学上的意思是:

A / C = B / D
A / B = C / D
A / (A+B) =  B / (A+B) = C / P = D / P

因此,如果您知道 AB(您应该知道),您可以通过添加 C 找到您的反射点 为交集的 x 值。

您可以这样找到 C:

Given:
  A (distance from shooting tank to wall)
  B (distance from target tank to wall)
  P (x distance between points)
Find:
  C (x distance from shooting tank where wall is to be hit)

A / (A+B) = C / P
C = A*P / (A+B)   <- here it is

例如,如果您的第一个水箱位于 (1,5) 处,第二个水箱位于 (3,7) 处,并且您的墙是 x 轴:

A = 5
B = 7
P = 3-1 = 2

therefore:
C = 5*2 / (5+7)
  = 10/12
  = 5/6

因此,如果你的坦克想要击中 (3,7) 处的坦克,则应向 (0,5/6) 射击。

对于更通用的解决方案:

if the wall is the X axis, and you have shooting tank at (s_x,s_y)
  and hit tank at (h_x,h_y), the point to be shot at is:

[ s_x + s_y * (h_x-s_x) / (h_y + s_y), 0 ]

替代方案,任意墙壁放置/方向

上述解决方案的问题是您的墙壁必须是您的 x 轴。如果不是怎么办?

首先,您需要找到每个点到墙壁的距离 - AB

  1. 找到 w,即 墙方向的单位向量
  2. w 中找到 v,它是垂直于墙的单位向量。如果w = [x,by],则v = [-y,x]
  3. 找到r_s,它是从射击坦克到墙上任何已知点的矢量。
  4. 找到r_h,它是从你的命中坦克到墙上任何已知点的矢量。
  5. 距离A = | v. r_s |,其中 .点积操作员。这可以通过 [l,m] 找到。 [n,o] = l*n + m*o
  6. 距离 B = | v. r_h |

找到 AB 后,找到 P,它是与墙平行的距离。为此:

  1. 找到q,它是从被击中坦克到射击坦克的向量
  2. 距离P = |瓦特。 q |

现在您已经有了 ABP,您有两种方法可以选择:

  1. 查找首先在上述方法中求解 C,然后找到从射击坦克和墙壁开始的 v 交点,并添加 C*w code> 到该交点。

  2. 你可以找到你必须射击的角度(从v),它是P/(A+B)的反正切。

It'd be useful to see what happens to lines/vectors during reflection. Wikipedia provides a nice picture for this:

reflection

Where, in this picture, in a proper reflection, both angles are the same.

Now, what does that have to do with you? Let's take a look again at your situation.

fancy diagram

Note that, due to the laws of reflection, the angles a and b are equal. That's good for us, because if we know that, we know c and d are also equal! (They are right triangles)

So we know:

a = b
c = d

We soon realize that we have similar triangles. Meaning, the corresponding sides are proportional to eachother. Meaning, mathematically:

A / C = B / D
A / B = C / D
A / (A+B) =  B / (A+B) = C / P = D / P

So, if you know A and B, which you should, you can find your reflection point by adding C to the x value of the intersection.

You can find C this way:

Given:
  A (distance from shooting tank to wall)
  B (distance from target tank to wall)
  P (x distance between points)
Find:
  C (x distance from shooting tank where wall is to be hit)

A / (A+B) = C / P
C = A*P / (A+B)   <- here it is

For example, if your first tank is at (1,5) and your second tank is at (3,7), and your wall is the x axis:

A = 5
B = 7
P = 3-1 = 2

therefore:
C = 5*2 / (5+7)
  = 10/12
  = 5/6

So your tank should shoot towards (0,5/6) if it wants to hit a tank at (3,7).

For a more general solution:

if the wall is the X axis, and you have shooting tank at (s_x,s_y)
  and hit tank at (h_x,h_y), the point to be shot at is:

[ s_x + s_y * (h_x-s_x) / (h_y + s_y), 0 ]

Alternative, with arbitrary wall placement/direction

The problem with the above solution is that your wall has to be your x axis. What if it's not?

First, you need to find the distance from each point to the wall -- A and B:

  1. Find w, which is the unit vector in the direction of the wall.
  2. From w, find v, which is the unit vector perpendicular to the wall. If w = [x,by], v = [-y,x].
  3. Find r_s, which is the vector from your shooting tank to any known point on your wall.
  4. Find r_h, which is the vector from your hit tank to any known point on your wall.
  5. The distance A = | v . r_s |, where . is the dot product operator. This can be found by [l,m] . [n,o] = l*n + m*o
  6. The distance B = | v . r_h |

Once you find A and B, find P, which is the distance parallel to the wall. To do that:

  1. Find q, which is the vector from the hit tank to the shooting tank
  2. The distance P = | w . q |

Now that you have A, B, and P, you have two ways to go:

  1. Find the point on the wall to aim for, by first solving for C in the method above and then finding the intersection of v starting from your shooting tank and your wall, and adding C*w to that intersection point.

  2. You can find the angle (from v) that you must shoot, and it's the inverse tangent of P/(A+B).

柳絮泡泡 2024-09-13 21:55:35

使用 V1 作为反射轴,将 T2 反射到 V1 的另一侧(我们将这个新点称为 T2'); T1 和 T2' 之间的线将在您想要的点处与 V1 相交。从这一点来看,只需简单的三角学就能找出任何角度。

http://en.wikipedia.org/wiki/Transformation_%28geometry%29#反思

Reflect T2 on the other side of V1, using V1 as the axis of reflection (we'll call this new point T2'); The line between T1 and T2' will intersect V1 at the point you want. From that point it's a matter of simple trigonometry to figure out what any angles are.

http://en.wikipedia.org/wiki/Transformation_%28geometry%29#Reflection

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文