views:

68

answers:

2

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

+1  A: 

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

JAB
thanx JAB.I'm going to see if I can implement this...and looking for other responses.
+3  A: 

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).

Justin L.
+1 for being simpler/easier than my answer.
JAB
thanx Justin..if you have any resource " code or pseudocode" would be nice to see it.