Beispiel #1
0
        static bool apply2Dsprings(GameObject target, Constraint constraint, Event ui)
        {
            int springIndex = ui.springIndex;

            // apply springs to gui object. returns true if it did anything.
            bool    result = false;
            Vector3 anchor = target.GetComponent <RectTransform>().anchoredPosition;

            if (constraint.edgeSprings)
            {
                result = true;

                if (anchor.x < constraint.edgeSpringMin.x)
                {
                    anchor.x = Mathf.SmoothDamp(anchor.x, constraint.edgeSpringMin.x, ref ui.eVel0, 0.15f);
                }
                else if (anchor.x > constraint.edgeSpringMax.x)
                {
                    anchor.x = Mathf.SmoothDamp(anchor.x, constraint.edgeSpringMax.x, ref ui.eVel1, 0.15f);
                }

                if (anchor.y < constraint.edgeSpringMin.y)
                {
                    anchor.y = Mathf.SmoothDamp(anchor.y, constraint.edgeSpringMin.y, ref ui.eVel2, 0.15f);
                }
                else if (anchor.y > constraint.edgeSpringMax.y)
                {
                    anchor.y = Mathf.SmoothDamp(anchor.y, constraint.edgeSpringMax.y, ref ui.eVel3, 0.15f);
                }

                float maxVel = Mathf.Max(Mathf.Abs(ui.eVel0), Mathf.Abs(ui.eVel1), Mathf.Abs(ui.eVel2), Mathf.Abs(ui.eVel3));

                if (maxVel < 1f)
                {
                    // maxvel 0 implies springs not actually doing anything, so return should be false
                    result = false;
                }
            }

            if (constraint.springs)
            {
                result = true;
                int   closestSpring = -1;
                float closest       = 9999999999f;



                if (springIndex >= 0)
                {
                    closestSpring = springIndex;
                }
                else
                {
                    for (int i = 0; i < constraint.springPositions.Length; i++)
                    {
                        // find nearest spring

                        Vector3 theSpringPosition = constraint.springPositions[i];
                        float   distance          = Vector2.Distance(anchor, theSpringPosition);
                        if (distance < closest)
                        {
                            closest       = distance;
                            closestSpring = i;
                        }
                    }
                }

                //float eVel0 = 0;
                //float eVel1 = 0;

                anchor.x = Mathf.SmoothDamp(anchor.x, constraint.springPositions[closestSpring].x, ref ui.eVel0, 0.15f);
                anchor.y = Mathf.SmoothDamp(anchor.y, constraint.springPositions[closestSpring].y, ref ui.eVel1, 0.15f);

                float maxVel = Mathf.Max(Mathf.Abs(ui.eVel0), Mathf.Abs(ui.eVel1));

                //              float maxVel = Mathf.Max (eVel0, eVel1);
                //              if (maxVel > 0f && maxVel < 1f) {

                if (maxVel < 1f)
                {
                    // Note that speed is per second. So 1 pixel per second. Maybe look at dx dy since last time.
                    // Also, last is not per se deltatime ago.

                    // maxvel 0 implies springs not actually doing anything, so return should be false
                    result = false;
                }
            }
            target.GetComponent <RectTransform>().anchoredPosition = anchor;

            return(result);
        }