Search Unity

Creating, scaling and rotating primitive shapes with controllers

Discussion in 'AR/VR (XR) Discussion' started by themobber, Aug 26, 2017.

  1. themobber

    themobber

    Joined:
    Aug 26, 2017
    Posts:
    1
    Hi everyone,

    I am trying to make something where you stretch out a primitive object like a box with controllers, but I am having difficulty with rotation and orientation. I have made it so on a trigger it creates a box, parents it to a null which is placed at one corner of the box, and is positioned at the triggered controller. Then you press the trigger for the other controller and you can expand the nulled box out (it scales between the two controllers).

    Here is what is happening in the Update (C#), I have been trying out of VR at the moment with two GameObjects, pointA (left controller) and pointB (right controller):

    void Shape_FrameUpdate()
    {
    if (activeShape_null != null)
    {

    activeShape_null.transform.position = pointA.transform.position;

    float newx = activeShape_null.transform.position.x - pointB.transform.position.x;
    float newy = activeShape_null.transform.position.y - pointB.transform.position.y;
    float newz = activeShape_null.transform.position.z - pointB.transform.position.z;
    activeShape_null.transform.localScale = new Vector3(newx, newy, -newz);


    }
    }

    So far this works fine as a start

    Point A is bottom left, Point B is top right... however because I am using x,y,z positions I have this weird world position lock where it always a box facing forward... I'm looking to have it more natural where the rotation of the box is based on the rotation of both the controllers, kind of like how you see it briefly at 25 seconds at this Leap Motion demo...


    I know I haven't added any code for rotation yet, I've tried Quaternion.LookRotation, LookAt and FromToRotation, but I can't seem to get it to work alongside the scaling code which I'm doubting is the right approach...

    Hope it makes sense, any help is much appreciated!
     
  2. SiliconDroid

    SiliconDroid

    Joined:
    Feb 20, 2017
    Posts:
    302
    Hi,

    Probably not exactly what you want, but it may help you derive it:

    This function uniformly scales a cube primitive between two controllers, cube is always aligned between controllers. Call per frame obviously.

    Code (CSharp):
    1.         ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    2.         //    ANIMATE A SCALED CUBE BETWEEN CONTROLLERS, GRAB POINTS ON OPPOSING BOX FACES.
    3.         //  PASS IT TRANSFORM OF AN AXIS ALIGNED CUBE MODEL ( 1M x 1M x 1M ) WITH ITS MODEL ORIGIN IN ITS CENTER.
    4.         //  PASS IN TWO TRANSFORMS OF CONTROLLER POINTS.
    5.         private static Transform tHelper = null;
    6.         private void AnimateStretchyCube(Transform tCube, Transform tConL, Transform tConR)
    7.         {
    8.             //  MAKE HELPER IF NOT ALREADY
    9.             if (!tHelper)
    10.             {
    11.                 GameObject oHelper = new GameObject();
    12.                 oHelper.name = "AnimateStretchyCube_helper";
    13.                 tHelper = oHelper.transform;
    14.             }
    15.  
    16.             //  POS HELPER
    17.             tHelper.position = (tConL.position + tConR.position) * 0.5f;
    18.             tHelper.LookAt(tConR.position, Vector3.up);
    19.  
    20.             //  POS CUBE
    21.             tCube.SetPositionAndRotation (tHelper.position, tHelper.rotation);
    22.  
    23.             //  GET CONTROLLER POS IN CUBESPACE
    24.             Vector3 vConL = tHelper.InverseTransformPoint(tConL.position);
    25.             Vector3 vConR = tHelper.InverseTransformPoint(tConR.position);
    26.  
    27.             //  SCALE CUBE
    28.             float fScale = (tConR.position - tConL.position).magnitude;
    29.             tCube.localScale = new Vector3(fScale, fScale, fScale);
    30.         }
    P.S: use code tags in posts: https://forum.unity3d.com/threads/using-code-tags-properly.143875/
     
    Last edited: Aug 27, 2017
    karthikeyuboosa likes this.
  3. Simp4Siri

    Simp4Siri

    Joined:
    Feb 24, 2021
    Posts:
    1
    Were you able to create the code you wanted to? if so may I please see it I am trying to do something similar except instead of creating the object at the controller im trying to do it where the controller would maniupulate objects far away I feel like your code would be a good starting off point