Search Unity

Vector3 and Quaternion operators

Discussion in 'Scripting' started by antenna-tree, Feb 23, 2006.

  1. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,324
    I'm having trouble with the syntax of using operators with these 2.

    Can I just get a super simple snippet showing the syntax from someone please (Vector3 operator * for instance). I know I'm just missing something dumb :oops:
     
  2. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    I am not sure if I get what your asking, so if this answer is out to lunch forgive it. ;)

    The Vector3 operator info is just to let you know what operators are overloaded. so the Vector3 operator * just means you can multiply a Vector3 with what they list (floats and vectors) and it will automatically do the proper Vector multiplication with each Vector element. So you don't have to break up the vector to apply the multiplication to each element (x,y,z). So the operators just tell you what the built-in operator functions do and what you can do them with.

    Hope that is what you wanted.
    -Jeremy
     
  3. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Oh, this also means that to multiply a vector by another vector you just:
    Code (csharp):
    1.  
    2. myVector * myOtherVector;
    3.  
    4. //or as we see in the docs:
    5. myFloat * myVector;
    6.  
    -Jeremy
     
  4. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,324
    No, I get an error saying "Operator '*' cannot be used with a left hand side of type 'UnityEngine.Vector3' and a right hand side of type 'UnityEngine.Vector3'" which you say is exactly what the operator should be doing:

    Ha!!! As I'm writing this I just realized I need to use Vector3.Cross... I think? Still confused maybe :roll:
     
  5. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Weird...I will try...I know adding them this way works fine.

    -Jeremy
     
  6. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Weird, I get operator cannot be applied to operands of type UniteEngine.Vector3 in C#. The + and - work though. Docs outdated?

    You could do: x * other.x, y * other.y, z * other.z yourself in a function if you really want to. ;)

    That's weird because it's in the docs and that's how oveloaded operators *should* work.

    -Jeremy.
     
  7. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,324
    Yeah, + and such works, and (V.x * Y.x, V.y * Y.y, V.z * Y.z) works fine, I'm just trying to learn more elegant syntax I guess. Thanks for giving it a go!
     
  8. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    No problem. I am pretty sure this is not intended behaviour or documented correctly, or you and I are just thick. ;) Be good to know as it doesn't make sense for us to write our own math code for stuff like this.

    -Jeremy
     
  9. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,324
    Well my thickness is undisputed, but I'm searching through every example script I have and I can't find a single use of this function so maybe we're both missing something?
     
  10. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    You can't multiply two vectors together. You can only multiply them with floats (=scaling a vector) or by Quaternions (= rotaion and scaling of a vector)

    If you read the documentation for Vector3.operator *, you'll see that there's no Vector3 operator * (Vector3 left, Vector3 right) Either left or rigth side must be a float.
     
  11. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Ok. Would scale work to achieve the same result? Didn't notice it before.

    For my own knowledge, why can't the * operator be overloaded to multiply two vectors? I know operator overloading is kind of limiting in C# but I don't know about C++. You don't have to answer that one, just curious. :)

    -Jeremy
     
  12. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,324
    Yeah, I was starting to feel that this was the case... Jeremy and I both hallucintaed the Vector3 * Vector3 documentation I guess. But why isn't there a method to do this... if I can do (V.x * Y.x, V.y * Y.y, V.z * Y.z) then why not turn it into a single method like Cross?

    Go easy on my programming stupidity :wink:
     
  13. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    BTW antenna, I don't hink cross is what you want. Cross doesn't just multiply the vectors, it bacisally returns a vector perpendicuar to the two sent if I remember correctly. It looks to me like scale is what you want.

    And I am guessing here with my limited knowledge, but I looked around and couldn't find ANY code example of overloading the * for two vectors. Not in any engine math functions, not anything. I think it must have something to do with types or something? I know with C# operator overloading at least one of the variables you send it has to be of a user defined type or class which is weird and limiting. But I don't know about C++

    -Jeremy
     
  14. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    Because there is no single mathematical definition on what multiplying two vectors means. So instead we defined the different multiplication-like operations as Vector3.Cross for the cross product and Vector3.Dot for the dot product and Vector3.Scale for the component-wise scaling operation.
     
  15. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,324
    Yeah, sync1b explained this to me on the IRC tonight. Scale is the trick for what I need to do (I'll try it in the morning), I don't know why I missed this in the docs.

    Thanks guys.
     
  16. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    I missed both scale and the fact that there was no operator defined for two vectors. :eek: A sign I need to get to bed. lol. Thanks freyr.

    -Jeremy