示例#1
0
        public IEnumerator QuaternionIntervalSnapping()
        {
            // Make a copy of the default quaternion elastic extent.
            var snappingExtent = quatExtent;

            // Set some decent snap points
            snappingExtent.SnapPoints = new Vector3[]
            {
                new Vector3(10, 10, 10)
            };
            // Set a good snap radius.
            snappingExtent.SnapRadius = 5f;

            // Enable interval snapping.
            snappingExtent.RepeatSnapPoints = true;

            // Construct our system.
            QuaternionElasticSystem les = new QuaternionElasticSystem(Quaternion.identity, Quaternion.identity, snappingExtent, elasticProperties);

            // Loop over a range of negative and positive integer multiples of the snap interval.
            for (int j = -3; j < 6; j++)
            {
                // Goal position for the elastic system to seek.
                // We will let the system settle to right next to an integer multiple of the snap point.
                var goalValue = Quaternion.Euler((snappingExtent.SnapPoints[0] * j) - (new Vector3(0.03f, 0.03f, 0.03f)));

                // Let the elastic system come to an equilibrium.
                // No need for yielding for new frames because the elastic system
                // simulates independently from Unity's frame system.
                for (int i = 0; i < 1000; i++)
                {
                    les.ComputeIteration(goalValue, 0.05f);
                }

                // Get the equilibrium value from the system.
                // It should be near the goal value, but slightly pulled towards the snapping point.
                var equilibrium = les.GetCurrentValue();
                Debug.Assert(
                    SignedVectorGreaterThan(equilibrium.eulerAngles, goalValue.eulerAngles),
                    $"Equilibrium should be slightly greater than goal value. Goal: {goalValue:F4}, Current: {equilibrium.eulerAngles:F4}"
                    );
                Debug.Assert(
                    SignedVectorLessThan(equilibrium.eulerAngles, Vector3.one * 360.0f + snappingExtent.SnapPoints[0] * j),
                    $"Equilibrium should still be less than the snapping value."
                    );
            }

            yield return(null);
        }
示例#2
0
        public IEnumerator QuaternionSnapPointSnapping()
        {
            // Make a copy of the default volume elastic extent.
            var snappingExtent = quatExtent;

            // Set some decent snap points
            snappingExtent.SnapPoints = new Vector3[]
            {
                new Vector3(45.0f, 45.0f, 45.0f),
                new Vector3(-45.0f, -45.0f, -45.0f)
            };

            // Construct our system.
            QuaternionElasticSystem les = new QuaternionElasticSystem(Quaternion.identity, Quaternion.identity, snappingExtent, elasticProperties);

            // Goal position for the elastic system to seek.
            // We will let the system settle to right next to one of the snapping point.
            var goalValue = Quaternion.Euler(new Vector3(40, 40, 40));

            // Let the elastic system come to an equilibrium.
            // No need for yielding for new frames because the elastic system
            // simulates independently from Unity's frame system.
            for (int i = 0; i < 1000; i++)
            {
                les.ComputeIteration(goalValue, 0.05f);
            }

            // Get the equilibrium value from the system.
            // It should be near the goal value, but slightly pulled towards the snapping point.
            var equilibrium = les.GetCurrentValue();

            Debug.Assert(
                SignedVectorGreaterThan(equilibrium.eulerAngles, goalValue.eulerAngles),
                $"Equilibrium should be slightly greater than goal value. Goal: {goalValue.eulerAngles:F4}, Current: {equilibrium.eulerAngles:F4}"
                );
            Debug.Assert(
                SignedVectorLessThan(equilibrium.eulerAngles, snappingExtent.SnapPoints[0]),
                $"Equilibrium should still be less than the snapping value"
                );

            // Move the goal value to next to the other snapping point (-45,-45,-45)
            goalValue = Quaternion.Euler(new Vector3(-35, -35, -35));

            // Let the elastic system come to an equilibrium.
            for (int i = 0; i < 1000; i++)
            {
                les.ComputeIteration(goalValue, 0.05f);
            }

            // Get the equilibrium value from the system.
            // It should be near the goal value, but slightly pulled towards the snapping point.
            equilibrium = les.GetCurrentValue();

            Debug.Assert(
                SignedVectorLessThan(equilibrium.eulerAngles, goalValue.eulerAngles),
                $"Equilibrium should be slightly less than goal value. Goal: {goalValue.eulerAngles:F4}, Current: {equilibrium.eulerAngles:F4}"
                );
            Debug.Assert(
                SignedVectorGreaterThan(equilibrium.eulerAngles, snappingExtent.SnapPoints[0]),
                $"Equilibrium should still be greater than the snapping value"
                );

            yield return(null);
        }