How to find angle of reflected Ray to match a Point
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):li开发者_开发知识库ne
reflect point : this is what I m looking for :)
Edit:it would be cool to see some "Code" :p
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
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
:
- Find
w
, which is the unit vector in the direction of the wall. - From
w
, findv
, which is the unit vector perpendicular to the wall. Ifw = [x,by]
,v = [-y,x]
. - Find
r_s
, which is the vector from your shooting tank to any known point on your wall. - Find
r_h
, which is the vector from your hit tank to any known point on your wall. - 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
- 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:
- Find
q
, which is the vector from the hit tank to the shooting tank - The distance
P = | w . q |
Now that you have A
, B
, and P
, 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)
.
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
精彩评论