ComposeSpeed() public static method

public static ComposeSpeed ( System.Vector2 speeds ) : float>.Tuple
speeds System.Vector2
return float>.Tuple
Exemplo n.º 1
0
        private void InelasticCollision(IForwardMovablePhysicalEntity target, IForwardMovablePhysicalEntity source,
                                        float diffAngle)
        {
            Vector2 decomposedSpeedTarget = Utils.DecomposeSpeed(target.ForwardSpeed, target.Direction, diffAngle);
            Vector2 decomposedSpeedSource = Utils.DecomposeSpeed(source.ForwardSpeed, source.Direction, diffAngle);

            float v1 = decomposedSpeedTarget.Y;
            float v2 = decomposedSpeedSource.Y;

            float m1 = target.Weight;
            float m2 = source.Weight;

            float newSpeedTargetAndSource = (m1 * v1 + m2 * v2) / (m1 + m2);

            decomposedSpeedTarget.Y = newSpeedTargetAndSource;
            decomposedSpeedSource.Y = newSpeedTargetAndSource;

            Tuple <float, float> composedSpeedTarget = Utils.ComposeSpeed(decomposedSpeedTarget, diffAngle);
            Tuple <float, float> composedSpeedSource = Utils.ComposeSpeed(decomposedSpeedSource, diffAngle);

            float targetSpeed = composedSpeedTarget.Item1;
            float sourceSpeed = composedSpeedSource.Item1;

            target.ForwardSpeed = targetSpeed > m_collisionChecker.MaximumGameObjectSpeed
                ? m_collisionChecker.MaximumGameObjectSpeed
                : targetSpeed;
            target.Direction    = composedSpeedTarget.Item2;
            source.ForwardSpeed = sourceSpeed > m_collisionChecker.MaximumGameObjectSpeed
                ? m_collisionChecker.MaximumGameObjectSpeed
                : sourceSpeed;
            source.Direction = composedSpeedSource.Item2;
        }
Exemplo n.º 2
0
        private void ElasticCollision(IForwardMovablePhysicalEntity target, IForwardMovablePhysicalEntity source,
                                      float diffAngle)
        {
            Vector2 decomposedSpeedTarget = Utils.DecomposeSpeed(target.ForwardSpeed, target.Direction, diffAngle);
            Vector2 decomposedSpeedSource = Utils.DecomposeSpeed(source.ForwardSpeed, source.Direction, diffAngle);
            float   v1 = decomposedSpeedTarget.Y;
            float   v2 = decomposedSpeedSource.Y;

            float m1 = target.Weight;
            float m2 = source.Weight;

            float originalEnergy = target.ForwardSpeed * target.ForwardSpeed * m1 +
                                   source.ForwardSpeed * source.ForwardSpeed * m2;

            double p   = m1 * v1 + m2 * v2;                               // P = momentum
            double d   = 4.0 * m1 * m1 * m2 * m2 * (v1 - v2) * (v1 - v2); // D = determinant
            float  v2A = (float)((2 * p * m2 + Math.Sqrt(d)) / (2 * (m1 * m2 + m2 * m2)));
            float  v2B = (float)((2 * p * m2 - Math.Sqrt(d)) / (2 * (m1 * m2 + m2 * m2)));

            float newSourceSpeed = v2A;

            if (Math.Abs(v2A - v2) < Math.Abs(v2B - v2)) // one of the results is identity
            {
                newSourceSpeed = v2B;
            }

            float newTargetSpeed = (m1 * v1 + m2 * v2 - m2 * newSourceSpeed) / m1;

            /*
             *  float E2C = v1 * v1 * m1 + v2 * v2 * m2;
             *  float E2D = newTargetSpeed * newTargetSpeed * m1 + newSourceSpeed * newSourceSpeed * m2;
             *  Debug.Assert(Math.Abs(E2C - E2D) < 0.000001); // energy conservation check */

            decomposedSpeedTarget.Y = newTargetSpeed;
            decomposedSpeedSource.Y = newSourceSpeed;
            Tuple <float, float> composedSpeedTarget = Utils.ComposeSpeed(decomposedSpeedTarget, diffAngle);
            Tuple <float, float> composedSpeedSource = Utils.ComposeSpeed(decomposedSpeedSource, diffAngle);

            float targetSpeed = composedSpeedTarget.Item1;
            float sourceSpeed = composedSpeedSource.Item1;
            float finalEnergy = targetSpeed * targetSpeed * m1 + sourceSpeed * sourceSpeed * m2;

            Debug.Assert(Math.Abs(originalEnergy - finalEnergy) < 0.01);

            target.ForwardSpeed = targetSpeed > m_collisionChecker.MaximumGameObjectSpeed
                ? m_collisionChecker.MaximumGameObjectSpeed
                : targetSpeed;
            target.Direction    = composedSpeedTarget.Item2;
            source.ForwardSpeed = sourceSpeed > m_collisionChecker.MaximumGameObjectSpeed
                ? m_collisionChecker.MaximumGameObjectSpeed
                : sourceSpeed;
            source.Direction = composedSpeedSource.Item2;
        }