Search Unity

[Strange Behaviour] Scale Object while grab a childObject

Discussion in 'Physics' started by SchChris, Jul 24, 2017.

  1. SchChris

    SchChris

    Joined:
    May 11, 2017
    Posts:
    5
    Hello Guys,

    following is the starting position:
    I instantiate a cube with childObjects on it to represent the corners, the edges and the surface. I can grab the childObject and Move the ViveController to Scale the object to the distance between the grabbedChild and his oppositeObject.
    This works really fine for a cube and a sphere. But if I try this on a cylinder or a cone, it behaviour is strange:
    The opposite surface is wandering to the surface of the grabbedChild without moving the controller.

    Below you can see the functions which are neccessary for this:
    In the Update from the ParenObject i calculate the distance between grabbedChild and his Opposite and give it to the ScaleObject-Function:

    #Code
    void Update
    {
    ....

    Debug.Log("GrabbedFrame have VRTK_InteractableObject and is Grabbed");
    newPositionGrabbedFrame = GrabbedFrame.transform.localPosition;

    #region Calculate Distance

    foreach (GameObject item in MyFrames)
    {
    if (item.name != "FrameBox")
    item.GetComponent<Frame>().CalcDistanceToOppositeFrame();
    }

    //frameDistance = Vector3.Distance(newPositionGrabbedFrame, oppositeFrame.transform.localPosition);
    // get distance between grabbedFrame and his opposite
    scaleDirection = -(oppositeFrame.transform.localPosition - newPositionGrabbedFrame);

    //get only positive Direction values
    if (scaleDirection.x < 0)
    scaleDirection.x *= -1;

    if (scaleDirection.y < 0)
    scaleDirection.y *= -1;

    if (scaleDirection.z < 0)
    scaleDirection.z *= -1;

    // make sure, that the y coords from the grabbedFrame not lower than the opposite values
    // otherwise set minimalScaleFactor as distance
    // x axis
    if (scaleDirection.x < minimumScaleFactor)
    scaleDirection.x = minimumScaleFactor;

    // y Axis
    if (scaleDirection.y < minimumScaleFactor)
    scaleDirection.y = minimumScaleFactor;

    // z axis
    if (scaleDirection.z < minimumScaleFactor)
    scaleDirection.z = minimumScaleFactor;

    frameDistanceX = scaleDirection.x;
    frameDistanceY = scaleDirection.y;
    frameDistanceZ = scaleDirection.z;

    #endregion Calculate Distance

    #region Scaling
    //Debug.Log("isForcedToUngrab: " + isForcedToUngrab);
    if (!isForcedToUngrab && scaleDirection.x != frameDistanceStartX && scaleDirection.y != frameDistanceStartY && scaleDirection.z != frameDistanceStartZ)
    ScaleObject(frameDistanceX, frameDistanceY, frameDistanceZ);

    #endregion Scaling
    }

    private void ScaleObject(float _newScaleX, float _newScaleY, float _newScaleZ)
    {
    Debug.Log("ScaleObject");
    #region Calculation

    // Calculate new Position to give Values to RePositionObject in lateUpdate
    frameDistance = -(oppositeFrame.transform.localPosition - newPositionGrabbedFrame);

    newPos = ((frameDistance / 2) + oppositeFrame.transform.localPosition);

    #region x Axis

    if (!freezeInfo.FreezeX)
    {
    // make sure, that scaleValue is not lower than minimumScale
    if (ScalingObject.transform.localScale.x <= minimumScaleFactor)
    newPos.x = visualStartPos.x;
    //newPos.x = ScalingObject.transform.localPosition.x;

    newScaleForX = _newScaleX;

    // make sure, that scaleValue is not lower than minimumScale
    if (newScaleForX <= minimumScaleFactor && newScaleForX > 0)
    newScaleForX = minimumScaleFactor;


    }
    else
    {
    newPos.x = visualStartPos.x;
    //newPos.x = ScalingObject.transform.localPosition.x;
    newScaleForX = originalScaleVisual.x;
    }

    #endregion x Axis

    #region y Axis

    if (!freezeInfo.FreezeY)
    {
    if (ScalingObject.transform.localScale.y <= minimumScaleFactor)
    newPos.y = visualStartPos.y;
    //newPos.y = ScalingObject.transform.localPosition.y;

    newScaleForY = _newScaleY;

    if (newScaleForY <= minimumScaleFactor && newScaleForY > 0)
    newScaleForY = minimumScaleFactor;

    if (frameParent.name == "Torus")
    {
    if (ScalingObject.transform.localScale.y <= minimumScaleFactorTorus)
    newPos.y = visualStartPos.y;

    if (newScaleForY <= minimumScaleFactorTorus && newScaleForY > 0)
    newScaleForY = minimumScaleFactorTorus;

    Debug.Log("NewScaleY of Torus: " + newScaleForY);
    }

    }
    else
    {
    newPos.y = visualStartPos.y;
    //newPos.y = ScalingObject.transform.localPosition.y;
    newScaleForY = originalScaleVisual.y;
    }

    #endregion y Axis

    #region z Axis

    if (!freezeInfo.FreezeZ)
    {

    if (ScalingObject.transform.localScale.z <= minimumScaleFactor)
    newPos.z = visualStartPos.z;
    //newPos.z = ScalingObject.transform.localPosition.z;

    newScaleForZ = _newScaleZ;

    if (newScaleForZ <= minimumScaleFactor && newScaleForZ > 0)
    newScaleForZ = minimumScaleFactor;
    }
    else
    {
    newPos.z = visualStartPos.z;
    //newPos.z = ScalingObject.transform.localPosition.z;
    newScaleForZ = originalScaleVisual.z;
    }

    #endregion z Axis

    #endregion Calculation

    // Set new scale
    newScaleVisual = new Vector3(newScaleForX, newScaleForY, newScaleForZ);

    #region Force to ungrab Frame


    // X Values
    if (newScaleForX <= minimumScaleFactor/* && frameDistanceStartX > minimumScaleFactor*/ && newScaleForX > 0)
    {
    Debug.Log("Do unforce because X reached minimum");
    ForceToUngrabFrame();
    ScalingObject.transform.localScale = new Vector3(minimumScaleFactor, ScalingObject.transform.localScale.y, ScalingObject.transform.localScale.z);
    }
    //Y Values
    else if (newScaleForY <= minimumScaleFactor /*&& frameDistanceStartY != minimumScaleFactor*/ && newScaleForY > 0)
    {
    Debug.Log("Do unforce because Y reached minimum");
    ForceToUngrabFrame();
    ScalingObject.transform.localScale = new Vector3(ScalingObject.transform.localScale.x, minimumScaleFactor, ScalingObject.transform.localScale.z);
    }
    // Z Values
    else if (newScaleForZ <= minimumScaleFactor /*&& frameDistanceStartZ > minimumScaleFactor */&& newScaleForZ > 0)
    {
    Debug.Log("Do unforce because Z reached minimum");
    ForceToUngrabFrame();
    ScalingObject.transform.localScale = new Vector3(ScalingObject.transform.localScale.x, ScalingObject.transform.localScale.y, minimumScaleFactor);
    }
    else
    {
    // Reposition Object
    //RePositionObject(newPos.x, newPos.y, newPos.z);
    // Check if the current Position different from the new one
    Debug.Log("Repos Object");
    if (ScalingObject.transform.localPosition != newPos)
    ScalingObject.transform.localPosition = newPos;

    Debug.Log("Rescale Object.");
    // Check if current Scale is different from the new one
    if (scalingObject.transform.localScale != newScaleVisual)
    ScalingObject.transform.localScale = newScaleVisual;

    createFrameAroundPrimitive.RepositionFrames();
    }


    #endregion


    }
    #Code

    I don't get it why the cone and cylinder are scaling negativ without moving the grabbedChild. It doesn't Scale negative if i grab the side edges and surfaces but when I grab the front and back.

    Hopefully you have an idea for this strange behaviour, I don't see the reason for this ...


    Greetings, Chris aka SchChris
     
  2. SchChris

    SchChris

    Joined:
    May 11, 2017
    Posts:
    5
    Fixed it whilst i freeze the oppositeChild too. It is not really pretty but it works. Maybe one of you have an idea why this trouble is appearing at all