Esempio n. 1
0
        public IEnumerator VolumeIntervalSnapping()
        {
            // Make a copy of the default volume elastic extent.
            var snappingExtent = volumeExtent;

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

            // Expand the bounds a bit
            snappingExtent.StretchBounds = new Bounds(Vector3.zero, 2.0f * Vector3.one);

            // Set a good snap radius.
            snappingExtent.SnapRadius = 0.1f;

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

            // Construct our system.
            VolumeElasticSystem les = new VolumeElasticSystem(Vector3.zero, Vector3.zero, 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 = (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, goalValue),
                    $"Equilibrium should be slightly greater than goal value. Goal: {goalValue.ToString("F4")}, Current: {equilibrium.ToString("F4")}"
                    );
                Debug.Assert(
                    SignedVectorLessThan(equilibrium, snappingExtent.SnapPoints[0] * j),
                    $"Equilibrium should still be less than the snapping value. Equilibrium"
                    );
            }

            yield return(null);
        }
Esempio n. 2
0
        public IEnumerator VolumeSnapPointSnapping()
        {
            // Make a copy of the default volume elastic extent.
            var snappingExtent = volumeExtent;

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

            snappingExtent.SnapRadius = 0.5f;

            // Construct our system.
            VolumeElasticSystem les = new VolumeElasticSystem(Vector3.zero, Vector3.zero, 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 = new Vector3(0.3f, 0.3f, 0.3f);

            // 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, goalValue),
                $"Equilibrium should be slightly greater than goal value. Goal: {goalValue}, Current: {equilibrium}"
                );
            Debug.Assert(
                SignedVectorLessThan(equilibrium, snappingExtent.SnapPoints[1]),
                $"Equilibrium should still be less than the snapping value"
                );

            // Move the goal value to next to the other snapping point (-0.5,-0.5,-0.5)
            goalValue = new Vector3(-0.4f, -0.4f, -0.4f);

            // 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, goalValue),
                $"Equilibrium should be slightly less than goal value. Goal: {goalValue}, Current: {equilibrium}"
                );
            Debug.Assert(
                SignedVectorGreaterThan(equilibrium, snappingExtent.SnapPoints[0]),
                $"Equilibrium should still be greater than the snapping value"
                );

            yield return(null);
        }
Esempio n. 3
0
        public IEnumerator VolumeBoundsForce()
        {
            // VolumeExtent is configured with bounds centered at (0,0,0), with size (1,1,1)
            VolumeElasticSystem ves = new VolumeElasticSystem(Vector3.zero, Vector3.zero, volumeExtent, elasticProperties);

            // Goal position for the elastic system to stretch towards.
            // Will be beyond the configured bounds!
            var goalValue = 15.0f * Vector3.one;

            // 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 < 50; i++)
            {
                ves.ComputeIteration(goalValue, 0.1f);
            }

            // Get the equilibrium value from the system.
            var equilibrium = ves.GetCurrentValue();

            Debug.Assert(
                SignedVectorLessThan(equilibrium, goalValue),
                $"Stretching beyond max limit should result in equilibrium value less than goal value, equilibrium: {equilibrium}"
                );

            // Compute one small iteration, covering 50 milliseconds.
            var newValue = ves.ComputeIteration(equilibrium, 0.05f);

            // The system should have shrunk back towards the endpoint.
            Debug.Assert(
                SignedVectorLessThan(equilibrium, goalValue),
                $"Elastic system should have contracted towards endpoint when released, actual value: {newValue}, equilibrium: {equilibrium}"
                );
            Debug.Assert(
                SignedVectorLessThan(ves.GetCurrentVelocity(), Vector3.zero),
                $"Elastic system should now have negative velocity, actual velocity: {ves.GetCurrentVelocity()}"
                );

            // Compute one more small iteration (50 milliseconds)
            var secondNewValue = ves.ComputeIteration(equilibrium, 0.05f);

            // The system should have shrunk back towards the endpoint.
            Debug.Assert(
                secondNewValue.magnitude < equilibrium.magnitude,
                $"Elastic system should have contracted towards endpoint when released, actual value: {secondNewValue}, equilibrium: {equilibrium}"
                );
            Debug.Assert(
                secondNewValue.magnitude < newValue.magnitude,
                $"Elastic system should have contracted further towards endpoint, new value: {secondNewValue}, last value: {newValue}"
                );
            Debug.Assert(
                ves.GetCurrentVelocity().x < 0.0f &&
                ves.GetCurrentVelocity().y < 0.0f &&
                ves.GetCurrentVelocity().z < 0.0f,
                $"Elastic system should still have negative velocity, actual velocity: {ves.GetCurrentVelocity()}"
                );

            // Now, we test pulling the elastic negative, and performing similar checks.
            goalValue = -5.0f * Vector3.one;

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

            // Get the equilibrium value from the system.
            equilibrium = ves.GetCurrentValue();
            Debug.Assert(
                SignedVectorGreaterThan(equilibrium, goalValue),
                $"Stretching beyond minimum limit should result in equilibrium value greater than goal value, equilibrium: {equilibrium}"
                );

            // Compute one small iteration, covering 50 milliseconds.
            newValue = ves.ComputeIteration(equilibrium, 0.05f);

            // The system should have shrunk back towards the endpoint.
            Debug.Assert(
                SignedVectorGreaterThan(newValue, equilibrium),
                $"Elastic system should have contracted towards endpoint when released, actual value: {newValue}, equilibrium: {equilibrium}"
                );
            Debug.Assert(
                SignedVectorGreaterThan(ves.GetCurrentVelocity(), Vector3.zero),
                $"Elastic system should now have positive velocity, actual velocity: {ves.GetCurrentVelocity()}"
                );

            // Compute one more small iteration (50 milliseconds)
            secondNewValue = ves.ComputeIteration(equilibrium, 0.05f);

            // The system should have shrunk back towards the endpoint.
            Debug.Assert(
                SignedVectorGreaterThan(secondNewValue, equilibrium),
                $"Elastic system should have contracted towards endpoint when released, actual value: {secondNewValue}, equilibrium: {equilibrium}"
                );
            Debug.Assert(
                SignedVectorGreaterThan(secondNewValue, newValue),
                $"Elastic system should have contracted further towards endpoint, new value: {secondNewValue}, last value: {newValue}"
                );
            Debug.Assert
                (SignedVectorGreaterThan(ves.GetCurrentVelocity(), Vector3.zero),
                $"Elastic system should still have positive velocity, actual velocity: {ves.GetCurrentVelocity()}"
                );

            yield return(null);
        }