Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to do all transformations in world coordinates

Discussion in 'Scripting' started by Steal_lality, Jun 9, 2014.

  1. Steal_lality

    Steal_lality

    Joined:
    May 13, 2014
    Posts:
    5
    Hi,

    I'm trying to develop a game in which you have to solve puzzles (equal to Portal). To solve them you have to use affine transformations on objects.

    That means all transformations shall be done according to the world coordinate system not the local one.

    To show the animation I use iTween. There I can do the translation in worldspace, for the rotation I have an empty GameObject as parent for my cube which rotates, detach the child, rotates back and add the cube as child again.

    My problem is the scaling:

    To scale I generate a 4x4Matrix which I apply on the vertices of the cube. But that doesnt update the position of my cube. So I tried to apply the 4x4Matrix on the position also. But know the cube gets pushed only further away from the origin and the position doesn't match the position of the mesh. See script and picture below.
    In the picture the white sphere is at origin and the cube is initially at (0,5,0), then is moved 3 along x-axis and then scaled by 3 along the x-axis.
    The final position of the cube's transform is: (9,5,0). So that would be right.

    Code (JavaScript):
    1. function scale (x : int, y : int, z: int)
    2. {
    3.     var sMatrix : Matrix4x4;
    4.     sMatrix.SetRow(0, Vector4(x, 0, 0, 0));
    5.     sMatrix.SetRow(1, Vector4(0, y, 0, 0));
    6.     sMatrix.SetRow(2, Vector4(0, 0, z, 0));
    7.     sMatrix.SetRow(3, Vector4(0, 0, 0, 1));
    8.  
    9.     this.transform(cube, sMatrix);
    10. }
    11.  
    12. function transform (gameObject : GameObject, matrix : Matrix4x4)
    13. {
    14.     this.cloneCube();
    15.  
    16.     var mesh : Mesh = gameObject.GetComponent(MeshFilter).mesh;
    17.     var wMatrix : Matrix4x4 = gameObject.transform.localToWorldMatrix;
    18.     var lMatrix : Matrix4x4 = gameObject.transform.worldToLocalMatrix;
    19.     var vert : Vector3[] = mesh.vertices;
    20.     for (var i = 0; i < vert.Length; i++)
    21.     {
    22.         vert[i] = wMatrix.MultiplyPoint3x4(vert[i]);
    23.         vert[i] = matrix.MultiplyPoint3x4(vert[i]);
    24.         vert[i] = lMatrix.MultiplyPoint3x4(vert[i]);
    25.     }
    26.  
    27.     mesh.vertices = vert;
    28.     mesh.RecalculateBounds();
    29.  
    30.     var pos : Vector3 = gameObject.transform.position;
    31.     pos = wMatrix.MultiplyPoint3x4(pos);
    32.     pos = matrix.MultiplyPoint3x4(pos);
    33.     pos = lMatrix.MultiplyPoint3x4(pos);
    34.     gameObject.transform.position = pos;
    35.  
    36. }
    Do you can help me?

    edit: since there were problems with the pic I took it out.
     
    Last edited: Jun 10, 2014
  2. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    (The site you hosted your screenshot on blasted pop-ups all over my screen when I took a look. Needless to say, I shut them all down, posted here, and will move on.)
     
  3. Steal_lality

    Steal_lality

    Joined:
    May 13, 2014
    Posts:
    5
    I'm sorry to hear this. I didn't have any problems, but I erased the image.

    Could you help me anyway? Thanks.
     

    Attached Files: