如何找到反射光线的角度来匹配点
这是我正在制作的坦克游戏,
请参阅图片以获得清晰的想法:链接文本< /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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
了解反射期间线/向量发生了什么会很有用。维基百科为此提供了一张漂亮的图片:
在这张图片中,在正确的位置反射,两个角度相同。
现在,这与你有什么关系?让我们再看看您的情况。
请注意,由于反射定律,角度
a
和b
相等。这对我们有好处,因为如果我们知道这一点,我们就知道c
和d
也相等! (它们是直角三角形)所以我们知道:
我们很快意识到我们有相似的三角形。意思是,相应的边彼此成比例。数学上的意思是:
因此,如果您知道
A
和B
(您应该知道),您可以通过添加C 找到您的反射点
为交集的 x 值。您可以这样找到 C:
例如,如果您的第一个水箱位于
(1,5)
处,第二个水箱位于(3,7)
处,并且您的墙是 x 轴:因此,如果你的坦克想要击中
(3,7)
处的坦克,则应向(0,5/6)
射击。对于更通用的解决方案:
替代方案,任意墙壁放置/方向
上述解决方案的问题是您的墙壁必须是您的 x 轴。如果不是怎么办?
首先,您需要找到每个点到墙壁的距离 -
A
和B
:w
,即 墙方向的单位向量。w
中找到v
,它是垂直于墙的单位向量。如果w = [x,by]
,则v = [-y,x]
。r_s
,它是从射击坦克到墙上任何已知点的矢量。r_h
,它是从你的命中坦克到墙上任何已知点的矢量。A = | v. r_s |
,其中.
是点积操作员。这可以通过[l,m] 找到。 [n,o] = l*n + m*o
找到
A
和B
后,找到P
,它是与墙平行的距离。为此:q
,它是从被击中坦克到射击坦克的向量P = |瓦特。 q |
现在您已经有了
A
、B
和P
,您有两种方法可以选择:查找首先在上述方法中求解 C,然后找到从射击坦克和墙壁开始的
v
交点,并添加C*w
code> 到该交点。你可以找到你必须射击的角度(从
v
),它是P/(A+B)
的反正切。It'd be useful to see what happens to lines/vectors during reflection. Wikipedia provides a nice picture for this:
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.
Note that, due to the laws of reflection, the angles
a
andb
are equal. That's good for us, because if we know that, we knowc
andd
are also equal! (They are right triangles)So we know:
We soon realize that we have similar triangles. Meaning, the corresponding sides are proportional to eachother. Meaning, mathematically:
So, if you know
A
andB
, which you should, you can find your reflection point by addingC
to the x value of the intersection.You can find C this way:
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:So your tank should shoot towards
(0,5/6)
if it wants to hit a tank at(3,7)
.For a more general solution:
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
andB
:w
, which is the unit vector in the direction of the wall.w
, findv
, which is the unit vector perpendicular to the wall. Ifw = [x,by]
,v = [-y,x]
.r_s
, which is the vector from your shooting tank to any known point on your wall.r_h
, which is the vector from your hit tank to any known point on your wall.A = | v . r_s |
, where.
is the dot product operator. This can be found by[l,m] . [n,o] = l*n + m*o
B = | v . r_h |
Once you find
A
andB
, findP
, which is the distance parallel to the wall. To do that:q
, which is the vector from the hit tank to the shooting tankP = | w . q |
Now that you have
A
,B
, andP
, you have two ways to go: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 addingC*w
to that intersection point.You can find the angle (from
v
) that you must shoot, and it's the inverse tangent ofP/(A+B)
.使用 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