Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Shortest way to check if 2 Vector3's are orthogonal in world

Discussion in 'Scripting' started by BenVenNL, Nov 18, 2019.

  1. BenVenNL

    BenVenNL

    Joined:
    Oct 14, 2019
    Posts:
    56
    I have two Vector3's, I need to know if they are otrthogonal positioned on the world axis.

    Is there a build in functionality?

    Or do I compare

    if (.x & .y of both Vector3's are equal OR .y & .z of both Vector3's are equal OR .z & .x of both Vector3's are equal )

    What is the quickest way to do this?
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    2,474
    Yoreki likes this.
  3. BenVenNL

    BenVenNL

    Joined:
    Oct 14, 2019
    Posts:
    56
  4. JasonBricco

    JasonBricco

    Joined:
    Jul 15, 2013
    Posts:
    936
    You can think of normalized world positions as direction vectors. They're directions from the origin (0, 0, 0). Normalizing means to make the vector unit length (length 1).

    Then, the dot product between the unit vectors will return 0 if they are perpendicular.
     
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    7,352
    positions aren't orthogonal, only directions.

    You may mean the direction from origin to a position... in which case, your position vector IS a directional vector (basically the direction and magnitude from origin to that position).

    Honestly when you say:
    I honestly don't know what you're trying to ask.

    You want to know if your 2 vectors are orthogonal on the world axis?
    What is the world axis? There are 3 axes in a 3d world... do you mean origin? What are you saying here?
    Are you asking if the 2 vectors are orthogonal to this "world axis", or are you asking if they're orthogonal to each other? (in which case, why bother mentioning the world axis?)
     
  6. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    2,474
    two positions can't be orthogonal- it needs to two lines (i.e directions). which lines do you want to compare?
     
  7. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,278
    Well, if this pseudo-code is an accurate representation of the intent:
    then it looks like they want to test whether line passing through the 2 given points is parallel to any world axis--or, equivalently, orthogonal to the plane of the other two axes. (This is more restrictive than being orthogonal to an axis: any line parallel to one axis is also orthogonal to the other two, but a line can be orthogonal to an axis without being parallel to any.)

    If that's the goal, your proposed pseudocode looks like a reasonable approach to me.

    If you had to compare to an arbitrary direction vector instead of a world axis, you'd have to do something like: subtract the points, normalize the result, and take a dot product between that and the reference direction vector. But if you know your comparison directions happen to be coordinate axes, then looking at the individual coordinates is probably easier.

    Of course, testing floating-point-anything for exact equality is generally a bad idea, so you should probably test if they are approximately parallel. Which means you should test whether the coordinates of your points are approximately equal on each axis.
     
unityunity