Search Unity

Finding the interesection of a direction vector and a position vector!

Discussion in 'Scripting' started by mQe, Feb 14, 2018.

  1. mQe

    mQe

    Joined:
    Jul 22, 2012
    Posts:
    57
    I have a direction vector and a position vector, i need to calculate a new position where they intersect.

    Basically, i need |A| cos from this diagram :
    http://www.falstad.com/dotproduct/

    It works using Mathf.Cos in unity, but unfortunately Mathf.Cos only goes from -1 to +1. So my position gets clamped which is not desired. How to solve this? :)
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    You're not being precise, so it's hard to know what you mean. (Vectors represent points in (in this case) 2D space; they can't "intersect" though they might happen to be equal.)

    The link you pointed to is illustrating a dot product. If that's what you need, just use Vector2.Dot. Mathf.Cos goes from -1 to +1 because that is what cosine, the mathematical function, really does. If you understand what cosine means, you'll see that it's impossible for it to have any other value. So I can't imagine what you want from it.

    So! Why don't you back up and explain what it is you're trying to do?
     
  3. mQe

    mQe

    Joined:
    Jul 22, 2012
    Posts:
    57
    I know what a vector is :p
    In this case it's 3D, but whatever.
    What I mean by intersection is if you have a direction vector, you make an imaginary perpendicular line from a position vector and end up with a new position vector. That's what I need, I'm not sure how to explain it any other way =/
     
  4. mQe

    mQe

    Joined:
    Jul 22, 2012
    Posts:
    57
    The dot product gives me even more problems when it's limited to 0 to 1.
    This is a fairly common thing to calculate so I'm probably just explaining it in a retarded way.
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    OK, so far we have: "you have a direction vector, you make an imaginary perpendicular line from a position vector and end up with a new position vector."

    This imaginary line: it's perpendicular to what? The position vector, or the direction vector? And then what exactly do you do with it to get a new position vector? There are obviously an infinite number of points on any line... which of them do you want?

    I see two options: either put more effort into explaining what you want mathematically (which will mean being actually precise with your terms, and possibly drawing a picture); or back up and explain what it is you're trying to actually accomplish.

    I guarantee you are not trying to accomplish picking a second position vector on some line that's perpendicular to something in some way related to some other direction vector. What you're trying to do is fire bullets at an angle to the direction you're facing, or find where something dropped from your spaceship would hit the terrain, or something like that. And you believe this ill-defined mathematical operation will do it for you (and you might be right). But if you explain to us what it is you're trying to accomplish, we'll either be able to understand the math you can't explain, or suggest a completely different way to approach it.
     
  6. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,519
    You need two directions, not a position. If you must use the position then you need an origin point to get the direction to that position so you can get the dot product from two directions.

    The direction can be any large value, it's an offset in spacial units from the origin (0,0,0 in math) and the magnitude of the vector is the length. Normally directions are normalized, eg locked to a total magnitude of 1, so all values of that direction value added up will total 1.

    The point of the dot product is to basically say "if direction A is the total opposite of B, then i'll give you -1 back, if they are pointing the exact same direction, I'll give you 1 back - but if say, A points UP and B points Right then I'll give you back 0".

    It's basically a normalized percentage of how far along the A direction you have to go on it in order to get to the intersection on B.

    I think you need to understand what the math is going to give you back, then you'll better understand how to get the information it needs.
     
  7. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Just had a thought: could it be you're trying to describe the vector projection of A onto B (where A and B are Vector2Ds)?

    In that case, it's just Vector2.Dot(A, B) if B is normalized (has length 1). If it's not normalized, then use Vector2.Dot(A, B.normalized).
     
  8. mQe

    mQe

    Joined:
    Jul 22, 2012
    Posts:
    57
    Thanks for the totally beginner trigonometry examples :p
    You got it right in the end though, it's just a projection. I just didn't know what it was called, sorry about the terrible explanation ( although Cos did actually got me pretty close ).

    Or you can use Vector3.Projection which does exactly what I want :)
     
  9. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Glad you got it figured out. I realize now you never even told us if you were working in 2D or 3D... I had assumed 2D, or yeah, I would have suggested Vector3.Project. :) (Of course Vector3.Project will work for 2D too, with appropriate typecasting...)
     
  10. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,997
    What version of Unity are you using? If it's the most current, you may need Unity Pro. Oh, wait, I'm seeing that those weasels at Unity Co. are trying to cover up their incompetence by claiming cosin is only supposed to go from -1 to 1, otherwise we can look in the Asset Store. Typical.