Search Unity

3 axis vectors to 3 vector3's. Need math help.

Discussion in 'Scripting' started by ZO5KmUG6R, Nov 27, 2014.

  1. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Hi! There's this game I'm loading a position file from, but the positions are as follows:

    Vector xAxis;
    Vector yAxis;
    Vector zAxis;

    These vectors define scale, and rotation. Can anyone help me convert these type of values to Unity's eulerAngles and localScale?
     
  2. OpticalOverride

    OpticalOverride

    Joined:
    Jan 13, 2013
    Posts:
    161
    Did you mean "float" instead of "Vector"? What data is contained in this arbitrary "Vector" data type from your file? It doesn't exist in Unity. If xAxis/yAxis/zAxis are just a single number with a decimal point (a float), then just use either of the following lines of code:

    gameObject.transform.eulerAngles = new Vector3( xAxis, yAxis, zAxis );
    gameObject.transform.localScale = new Vector3( xAxis, yAxis, zAxis );

    Are the "Vector"s in your data file objects or single numbers? There simply isn't enough information in your post (or improper information) to give a direct answer.
     
  3. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Oh right. The Vector is 3 floats. X Y Z. It gets the scale and rotation from those 3 vectors. Kind of an odd way, but I'd like to know how to get it into Unity's scale/rot format.
     
  4. OpticalOverride

    OpticalOverride

    Joined:
    Jan 13, 2013
    Posts:
    161
    So the "Vector" is really just a Vector3 from Unity? Or do you mean "Vector" defines each the x,y,z separately, as floats? Either way, that's not odd at all, it is defined in a similar way in Unity. A Vector3 in Unity is simply an object that contains x/y/z floats that can be used to descibe a game object's position/rotation/scale. Simply grab the 3 floats from the file that correspond to either the rotation or scale, and use the code I described above (transform.eulerAngles or transform.localScale, respectively). That code is in C#.

    If it's still unclear, maybe I'm not understanding the problem. Perhaps post some code or output from the file and I can help further.
     
  5. ErrorX

    ErrorX

    Joined:
    May 21, 2014
    Posts:
    22
    What for data are you loading a model? It can be a triangle?

    To get a rotation of triagle is this:
    Quaternion rotation = Quaternion.LookRotation (Vector3.Cross(xAxis - yAxis, zAxis - xAxis).normalized);
    float scale = Vector3.Cross(xAxis - yAxis,zAxis - xAxis).magnitude; //I dont think this line will work

    It can also be a matrix?

    Matrix4x4 matrix = new Matrix4x4();
    matrix.SetColumn(0, xAxis);
    matrix.SetColumn(1, yAxis);
    matrix.SetColumn(2, zAxis);
    vertor3 MyNewPoint = matrix.MultiplyPoint3x4(MyLocalVector);
     
    Last edited: Nov 28, 2014
  6. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    I've taken a while and thought out how 2 explain it

    There are 3 vector3's that define
    a)Rotation
    b)Scale

    of

    a)X-axis
    b)Y-axis
    c)Z-axis

    X axis : X Y Z, 3 floats, they define rotation and scale.
    etc

    So I want to know how to turn these into Unity values for scale and rotation
     
  7. OpticalOverride

    OpticalOverride

    Joined:
    Jan 13, 2013
    Posts:
    161
    I'm really not understanding this... only 1 float is required to define the rotation on a single axis.

    OK, I'm going to leave you with this: an explanation of localScale and eulerAngles in Unity.

    Unity has a data type called a 'Vector3', that can hold 3 floats (hence, Vector3). localScale and eulerAngles are also both of type Vector3 and will describe to the Transform component of a game object the scale (locally of course) and the rotation (the 3 floats in the Vector3 correspond to the angle, in degrees, around each of the X/Y/Z axis for it's transform component). I'm not sure what your data is corresponding to from this arbitrary game in this arbitrary data type from your file, but if you can determine any of the previously mentioned information from your data file, that's is how Unity will interpret them. Good luck!

    For future reference, a better way to explain a problem is often to show some code/research/thoroughly explain the issue (in your case, what is this file/game/outcome/etc.). To me, I can't seem to understand the context of the issue, or what your data pertains to... Sorry, I can't be of help.
     
  8. OpticalOverride

    OpticalOverride

    Joined:
    Jan 13, 2013
    Posts:
    161
    After writing my last post, I think I may understand what you're trying to say.

    Does your file contain rotation/scale matrices? I haven't worked with these very often (made a plugin for the Razer Hydra -> Unity, which I believe was where I needed a similar approach). Can't directly help you with this, but here's a couple of links that may be of help or get you on the right track.

    http://en.wikipedia.org/wiki/Rotation_matrix

    http://en.wikipedia.org/wiki/Scaling_(geometry)

    Good luck!
     
  9. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    I know all about Vector3's and all, This is actually the only thing stumping me.

    The 3 floats define a direction, not a rotation, my bad there.
    They also define scale by just making that direction "longer". I just don't know how to take the XYZ direction and split it from the scale.
     
  10. OpticalOverride

    OpticalOverride

    Joined:
    Jan 13, 2013
    Posts:
    161
    Have you simply tried putting these 3 floats into eulerAngles/localScale/etc. and seeing what happens? Due to the lack of explanation about the data itself, I don't think you're even exactly sure what these 3 floats are in the file.. Beyond this suggestion, I can't help much more, sorry.
     
  11. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    ErrorX posted the most helpful answer here. It was a matrix indeed, I wasn't even realizing it! Now I have the Matrix and code to transform it to a Transform component