Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Let's say a vector starts at Point P and points in the direction of v. How do I make sure that this vector exactly reaches the line? Its end should be exactly on the line, and not cross it.

Illustration of a vector exactly meeting a line

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

You should calculate the intersection point (IP) between the two lines.

      (X - Ax)     (Y - Ay)
EQ1 = --------  =  --------   =>   (X-Ax) * (By-Ay) - (Y - Ay) * (Bx - Ax) = 0;
      (Bx - Ax)    (By - Ay)

      (X - Px)     (Y - Py)
EQ2 = --------  =  --------   =>   (X-Px) * (Vx) - (Y - Py) * (Vy) = 0;
         (Vx)        (Vy)

Solve the two equations and you get IP (X,Y).

Maybe you'd need to check that IP is between A and B... before continue...

Then you build a new line with this equation:

  (P + V) = P + t*(IP-P)   => t = V / (IP - P) 

   t = (IP.X-P.X != 0) 
        ? (V.X / (IP.X - P.X)) 
        : (V.Y / (IP.Y - P.Y));

  if t>1 then (P+V) has overpassed the line

EDIT:

enter image description here

Remember:

1) You have to calculate IP. the intersection point
2) You should check that IP is between A and B
3) You had the vector V, and now have the vector (IP-P),
4) You can choose betwwen two options:
   a) Now you can compare the vector lenghts to now if V is greater that (IP-P)
   b) Calculate t as I described before...
           if t==0 => V=(0,0);  
           if t=1 => V=(IP-P)
           if (t>0 && t<1) length(V) < length(IP-P)
           if (t>1) length(V) > length(IP-P)

enter image description here

share|improve this answer
    
It looks nice. Is it possible that to include a solved example. It will very helpful –  user960567 Apr 18 '12 at 9:16
    
beat me to it.. +1 –  teodron Apr 18 '12 at 9:17
    
@Blau, Please help. I have not understand your formula. P + t*(IP-P). I think it is a vector and you are checking it as scaler –  user960567 Apr 18 '12 at 10:48
1  
Maybe this will help: alienryderflex.com/intersect –  Roy T. Apr 18 '12 at 10:58
    
@RoyT. It did'nt help. –  user960567 Apr 18 '12 at 11:19
show 12 more comments

You should:

  1. Find an (X, Y) point of intersection of your line and a support line for the vector.
  2. Extend your vector by the distance from its end to than point you've found.

Example:

  1. Find the (X, Y) point: solve the

    (X - Ax) * (By - Ay) - (Y - Ay) * (Bx - Ax) = 0

    and

    (X - Px) * Vx - (Y - Py) * Vy = 0

    equations.

  2. Your new vector will be Your old vector (both components) plus YVN * Dist;

    • where YVN is Your old vector normalized (one-unit length);
    • where Dist is distance between N and V;
    • where N is your newfoundland point.
share|improve this answer
    
Is it possible that you please with an example. Like Blau demostrate with an example. –  user960567 Apr 18 '12 at 12:29
    
@user960567 okay. –  AgentFire Apr 18 '12 at 12:51
    
I can't extend the vector. Vector is fixed. I can only change the starting Point P. –  user960567 Apr 18 '12 at 12:52
    
@user960567 okay now? –  AgentFire Apr 18 '12 at 13:10
    
I said that I cannot change vector –  user960567 Apr 18 '12 at 14:41
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.