Пример #1
0
        /// <summary>
        /// Initializes a contact.
        /// </summary>
        /// <param name="body1">The first body.</param>
        /// <param name="body2">The second body.</param>
        /// <param name="point1">The collision point in worldspace</param>
        /// <param name="point2">The collision point in worldspace</param>
        /// <param name="n">The normal pointing to body2.</param>
        /// <param name="penetration">The estimated penetration depth.</param>
        public void Initialize(RigidBody body1, RigidBody body2, ref FPVector point1, ref FPVector point2, ref FPVector n,
                               FP penetration, bool newContact, ContactSettings settings)
        {
            this.body1  = body1;  this.body2 = body2;
            this.normal = n; normal.Normalize();
            this.p1     = point1; this.p2 = point2;

            this.newContact = newContact;

            FPVector.Subtract(ref p1, ref body1.position, out relativePos1);
            FPVector.Subtract(ref p2, ref body2.position, out relativePos2);
            FPVector.Transform(ref relativePos1, ref body1.invOrientation, out realRelPos1);
            FPVector.Transform(ref relativePos2, ref body2.invOrientation, out realRelPos2);

            this.initialPen  = penetration;
            this.penetration = penetration;

            body1IsMassPoint = body1.isParticle;
            body2IsMassPoint = body2.isParticle;

            // Material Properties
            if (newContact)
            {
                treatBody1AsStatic = body1.isStatic;
                treatBody2AsStatic = body2.isStatic;

                accumulatedNormalImpulse  = FP.Zero;
                accumulatedTangentImpulse = FP.Zero;

                lostSpeculativeBounce = FP.Zero;

                switch (settings.MaterialCoefficientMixing)
                {
                case ContactSettings.MaterialCoefficientMixingType.TakeMaximum:
                    staticFriction  = FPMath.Max(body1.staticFriction, body2.staticFriction);
                    dynamicFriction = FPMath.Max(body1.staticFriction, body2.staticFriction);
                    restitution     = FPMath.Max(body1.restitution, body2.restitution);
                    break;

                case ContactSettings.MaterialCoefficientMixingType.TakeMinimum:
                    staticFriction  = FPMath.Min(body1.staticFriction, body2.staticFriction);
                    dynamicFriction = FPMath.Min(body1.staticFriction, body2.staticFriction);
                    restitution     = FPMath.Min(body1.restitution, body2.restitution);
                    break;

                case ContactSettings.MaterialCoefficientMixingType.UseAverage:
                    staticFriction  = (body1.staticFriction + body2.staticFriction) * FP.Half;
                    dynamicFriction = (body1.staticFriction + body2.staticFriction) * FP.Half;
                    restitution     = (body1.restitution + body2.restitution) * FP.Half;
                    break;
                }
            }

            this.settings = settings;
        }
Пример #2
0
        /// <summary>
        /// Passes a axis aligned bounding box to the shape where collision
        /// could occour.
        /// </summary>
        /// <param name="box">The bounding box where collision could occur.</param>
        /// <returns>The upper index with which <see cref="SetCurrentShape"/> can be
        /// called.</returns>
        public override int Prepare(ref TSBBox box)
        {
            // simple idea: the terrain is a grid. x and z is the position in the grid.
            // y the height. we know compute the min and max grid-points. All quads
            // between these points have to be checked.

            // including overflow exception prevention

            if (box.min.x < boundings.min.x)
            {
                minX = 0;
            }
            else
            {
                minX = (int)FP.Floor(((box.min.x - sphericalExpansion) / scaleX));
                minX = (int)FPMath.Max(minX, 0);
            }

            if (box.max.x > boundings.max.x)
            {
                maxX = heightsLength0 - 1;
            }
            else
            {
                maxX = (int)FP.Ceiling(((box.max.x + sphericalExpansion) / scaleX));
                maxX = (int)FPMath.Min(maxX, heightsLength0 - 1);
            }

            if (box.min.z < boundings.min.z)
            {
                minZ = 0;
            }
            else
            {
                minZ = (int)FP.Floor(((box.min.z - sphericalExpansion) / scaleZ));
                minZ = (int)FPMath.Max(minZ, 0);
            }

            if (box.max.z > boundings.max.z)
            {
                maxZ = heightsLength1 - 1;
            }
            else
            {
                maxZ = (int)FP.Ceiling((FP)((box.max.z + sphericalExpansion) / scaleZ));
                maxZ = (int)FPMath.Min(maxZ, heightsLength1 - 1);
            }

            numX = maxX - minX;
            numZ = maxZ - minZ;

            // since every quad contains two triangles we multiply by 2.
            return(numX * numZ * 2);
        }
Пример #3
0
            public FP?IntersectsWithRay(FPVector2 origin, FPVector2 direction)
            {
                FP          largestDistance = FPMath.Max(A.Position.x - origin.x, B.Position.x - origin.x) * 2f;
                LineSegment raySegment      = new LineSegment(new Vertex(origin, 0), new Vertex(origin + (direction * largestDistance), 0));

                FPVector2?intersection = FindIntersection(this, raySegment);
                FP?       value        = null;

                if (intersection != null)
                {
                    value = FPVector2.Distance(origin, intersection.Value);
                }

                return(value);
            }
Пример #4
0
            /// <summary>
            /// Iteratively solve this constraint.
            /// </summary>
            public override void Iterate()
            {
                if (skipConstraint)
                {
                    return;
                }

                FP jv = FPVector.Dot(ref body1.linearVelocity, ref jacobian[0]);

                jv += FPVector.Dot(ref body2.linearVelocity, ref jacobian[1]);

                FP softnessScalar = accumulatedImpulse * softnessOverDt;

                FP lambda = -effectiveMass * (jv + bias + softnessScalar);

                if (behavior == DistanceBehavior.LimitMinimumDistance)
                {
                    FP previousAccumulatedImpulse = accumulatedImpulse;
                    accumulatedImpulse = FPMath.Max(accumulatedImpulse + lambda, 0);
                    lambda             = accumulatedImpulse - previousAccumulatedImpulse;
                }
                else if (behavior == DistanceBehavior.LimitMaximumDistance)
                {
                    FP previousAccumulatedImpulse = accumulatedImpulse;
                    accumulatedImpulse = FPMath.Min(accumulatedImpulse + lambda, 0);
                    lambda             = accumulatedImpulse - previousAccumulatedImpulse;
                }
                else
                {
                    accumulatedImpulse += lambda;
                }

                FPVector temp;

                if (!body1.isStatic)
                {
                    FPVector.Multiply(ref jacobian[0], lambda * body1.inverseMass, out temp);
                    FPVector.Add(ref temp, ref body1.linearVelocity, out body1.linearVelocity);
                }

                if (!body2.isStatic)
                {
                    FPVector.Multiply(ref jacobian[1], lambda * body2.inverseMass, out temp);
                    FPVector.Add(ref temp, ref body2.linearVelocity, out body2.linearVelocity);
                }
            }
        /// <summary>
        /// Iteratively solve this constraint.
        /// </summary>
        public override void Iterate()
        {
            if (skipConstraint)
            {
                return;
            }

            FP jv =
                body1.linearVelocity * jacobian[0] +
                body1.angularVelocity * jacobian[1] +
                body2.linearVelocity * jacobian[2] +
                body2.angularVelocity * jacobian[3];

            FP softnessScalar = accumulatedImpulse * softnessOverDt;

            FP lambda = -effectiveMass * (jv + bias + softnessScalar);

            if (behavior == DistanceBehavior.LimitMinimumDistance)
            {
                FP previousAccumulatedImpulse = accumulatedImpulse;
                accumulatedImpulse = FPMath.Max(accumulatedImpulse + lambda, 0);
                lambda             = accumulatedImpulse - previousAccumulatedImpulse;
            }
            else if (behavior == DistanceBehavior.LimitMaximumDistance)
            {
                FP previousAccumulatedImpulse = accumulatedImpulse;
                accumulatedImpulse = FPMath.Min(accumulatedImpulse + lambda, 0);
                lambda             = accumulatedImpulse - previousAccumulatedImpulse;
            }
            else
            {
                accumulatedImpulse += lambda;
            }

            if (!body1.isStatic)
            {
                body1.linearVelocity  += body1.inverseMass * lambda * jacobian[0];
                body1.angularVelocity += FPVector.Transform(lambda * jacobian[1], body1.invInertiaWorld);
            }

            if (!body2.isStatic)
            {
                body2.linearVelocity  += body2.inverseMass * lambda * jacobian[2];
                body2.angularVelocity += FPVector.Transform(lambda * jacobian[3], body2.invInertiaWorld);
            }
        }
        /// <summary>
        /// Hull making.
        /// </summary>
        /// <remarks>Based/Completely from http://www.xbdev.net/physics/MinkowskiDifference/index.php
        /// I don't (100%) see why this should always work.
        /// </remarks>
        /// <param name="triangleList"></param>
        /// <param name="generationThreshold"></param>
        public virtual void MakeHull(ref List <FPVector> triangleList, int generationThreshold)
        {
            FP distanceThreshold = FP.Zero;

            if (generationThreshold < 0)
            {
                generationThreshold = 4;
            }

            Stack <ClipTriangle> activeTriList = new Stack <ClipTriangle>();

            FPVector[] v = new FPVector[] // 6 Array
            {
                new FPVector(-1, 0, 0),
                new FPVector(1, 0, 0),

                new FPVector(0, -1, 0),
                new FPVector(0, 1, 0),

                new FPVector(0, 0, -1),
                new FPVector(0, 0, 1),
            };

            int[,] kTriangleVerts = new int[8, 3] // 8 x 3 Array
            {
                { 5, 1, 3 },
                { 4, 3, 1 },
                { 3, 4, 0 },
                { 0, 5, 3 },

                { 5, 2, 1 },
                { 4, 1, 2 },
                { 2, 0, 4 },
                { 0, 2, 5 }
            };

            for (int i = 0; i < 8; i++)
            {
                ClipTriangle tri = new ClipTriangle();
                tri.n1         = v[kTriangleVerts[i, 0]];
                tri.n2         = v[kTriangleVerts[i, 1]];
                tri.n3         = v[kTriangleVerts[i, 2]];
                tri.generation = 0;
                activeTriList.Push(tri);
            }

            // surfaceTriList
            while (activeTriList.Count > 0)
            {
                ClipTriangle tri = activeTriList.Pop();

                FPVector p1; SupportMapping(ref tri.n1, out p1);
                FPVector p2; SupportMapping(ref tri.n2, out p2);
                FPVector p3; SupportMapping(ref tri.n3, out p3);

                FP d1 = (p2 - p1).sqrMagnitude;
                FP d2 = (p3 - p2).sqrMagnitude;
                FP d3 = (p1 - p3).sqrMagnitude;

                if (FPMath.Max(FPMath.Max(d1, d2), d3) > distanceThreshold && tri.generation < generationThreshold)
                {
                    ClipTriangle tri1 = new ClipTriangle();
                    ClipTriangle tri2 = new ClipTriangle();
                    ClipTriangle tri3 = new ClipTriangle();
                    ClipTriangle tri4 = new ClipTriangle();

                    tri1.generation = tri.generation + 1;
                    tri2.generation = tri.generation + 1;
                    tri3.generation = tri.generation + 1;
                    tri4.generation = tri.generation + 1;

                    tri1.n1 = tri.n1;
                    tri2.n2 = tri.n2;
                    tri3.n3 = tri.n3;

                    FPVector n = FP.Half * (tri.n1 + tri.n2);
                    n.Normalize();

                    tri1.n2 = n;
                    tri2.n1 = n;
                    tri4.n3 = n;

                    n = FP.Half * (tri.n2 + tri.n3);
                    n.Normalize();

                    tri2.n3 = n;
                    tri3.n2 = n;
                    tri4.n1 = n;

                    n = FP.Half * (tri.n3 + tri.n1);
                    n.Normalize();

                    tri1.n3 = n;
                    tri3.n1 = n;
                    tri4.n2 = n;

                    activeTriList.Push(tri1);
                    activeTriList.Push(tri2);
                    activeTriList.Push(tri3);
                    activeTriList.Push(tri4);
                }
                else
                {
                    if (((p3 - p1) % (p2 - p1)).sqrMagnitude > FPMath.Epsilon)
                    {
                        triangleList.Add(p1);
                        triangleList.Add(p2);
                        triangleList.Add(p3);
                    }
                }
            }
        }
Пример #7
0
        /// <summary>
        /// PrepareForIteration has to be called before <see cref="Iterate"/>.
        /// </summary>
        /// <param name="timestep">The timestep of the simulation.</param>
        public void PrepareForIteration(FP timestep)
        {
            FP dvx, dvy, dvz;

            dvx = (body2.angularVelocity.y * relativePos2.z) - (body2.angularVelocity.z * relativePos2.y) + body2.linearVelocity.x;
            dvy = (body2.angularVelocity.z * relativePos2.x) - (body2.angularVelocity.x * relativePos2.z) + body2.linearVelocity.y;
            dvz = (body2.angularVelocity.x * relativePos2.y) - (body2.angularVelocity.y * relativePos2.x) + body2.linearVelocity.z;

            dvx = dvx - (body1.angularVelocity.y * relativePos1.z) + (body1.angularVelocity.z * relativePos1.y) - body1.linearVelocity.x;
            dvy = dvy - (body1.angularVelocity.z * relativePos1.x) + (body1.angularVelocity.x * relativePos1.z) - body1.linearVelocity.y;
            dvz = dvz - (body1.angularVelocity.x * relativePos1.y) + (body1.angularVelocity.y * relativePos1.x) - body1.linearVelocity.z;

            FP kNormal = FP.Zero;

            FPVector rantra = FPVector.zero;

            if (!treatBody1AsStatic)
            {
                kNormal += body1.inverseMass;

                if (!body1IsMassPoint)
                {
                    // JVector.Cross(ref relativePos1, ref normal, out rantra);
                    rantra.x = (relativePos1.y * normal.z) - (relativePos1.z * normal.y);
                    rantra.y = (relativePos1.z * normal.x) - (relativePos1.x * normal.z);
                    rantra.z = (relativePos1.x * normal.y) - (relativePos1.y * normal.x);

                    // JVector.Transform(ref rantra, ref body1.invInertiaWorld, out rantra);
                    FP num0 = ((rantra.x * body1.invInertiaWorld.M11) + (rantra.y * body1.invInertiaWorld.M21)) + (rantra.z * body1.invInertiaWorld.M31);
                    FP num1 = ((rantra.x * body1.invInertiaWorld.M12) + (rantra.y * body1.invInertiaWorld.M22)) + (rantra.z * body1.invInertiaWorld.M32);
                    FP num2 = ((rantra.x * body1.invInertiaWorld.M13) + (rantra.y * body1.invInertiaWorld.M23)) + (rantra.z * body1.invInertiaWorld.M33);

                    rantra.x = num0; rantra.y = num1; rantra.z = num2;

                    //JVector.Cross(ref rantra, ref relativePos1, out rantra);
                    num0 = (rantra.y * relativePos1.z) - (rantra.z * relativePos1.y);
                    num1 = (rantra.z * relativePos1.x) - (rantra.x * relativePos1.z);
                    num2 = (rantra.x * relativePos1.y) - (rantra.y * relativePos1.x);

                    rantra.x = num0; rantra.y = num1; rantra.z = num2;
                }
            }

            FPVector rbntrb = FPVector.zero;

            if (!treatBody2AsStatic)
            {
                kNormal += body2.inverseMass;

                if (!body2IsMassPoint)
                {
                    // JVector.Cross(ref relativePos1, ref normal, out rantra);
                    rbntrb.x = (relativePos2.y * normal.z) - (relativePos2.z * normal.y);
                    rbntrb.y = (relativePos2.z * normal.x) - (relativePos2.x * normal.z);
                    rbntrb.z = (relativePos2.x * normal.y) - (relativePos2.y * normal.x);

                    // JVector.Transform(ref rantra, ref body1.invInertiaWorld, out rantra);
                    FP num0 = ((rbntrb.x * body2.invInertiaWorld.M11) + (rbntrb.y * body2.invInertiaWorld.M21)) + (rbntrb.z * body2.invInertiaWorld.M31);
                    FP num1 = ((rbntrb.x * body2.invInertiaWorld.M12) + (rbntrb.y * body2.invInertiaWorld.M22)) + (rbntrb.z * body2.invInertiaWorld.M32);
                    FP num2 = ((rbntrb.x * body2.invInertiaWorld.M13) + (rbntrb.y * body2.invInertiaWorld.M23)) + (rbntrb.z * body2.invInertiaWorld.M33);

                    rbntrb.x = num0; rbntrb.y = num1; rbntrb.z = num2;

                    //JVector.Cross(ref rantra, ref relativePos1, out rantra);
                    num0 = (rbntrb.y * relativePos2.z) - (rbntrb.z * relativePos2.y);
                    num1 = (rbntrb.z * relativePos2.x) - (rbntrb.x * relativePos2.z);
                    num2 = (rbntrb.x * relativePos2.y) - (rbntrb.y * relativePos2.x);

                    rbntrb.x = num0; rbntrb.y = num1; rbntrb.z = num2;
                }
            }

            if (!treatBody1AsStatic)
            {
                kNormal += rantra.x * normal.x + rantra.y * normal.y + rantra.z * normal.z;
            }
            if (!treatBody2AsStatic)
            {
                kNormal += rbntrb.x * normal.x + rbntrb.y * normal.y + rbntrb.z * normal.z;
            }

            massNormal = FP.One / kNormal;

            FP num = dvx * normal.x + dvy * normal.y + dvz * normal.z;

            tangent.x = dvx - normal.x * num;
            tangent.y = dvy - normal.y * num;
            tangent.z = dvz - normal.z * num;

            num = tangent.x * tangent.x + tangent.y * tangent.y + tangent.z * tangent.z;

            if (num != FP.Zero)
            {
                num        = FP.Sqrt(num);
                tangent.x /= num;
                tangent.y /= num;
                tangent.z /= num;
            }

            FP kTangent = FP.Zero;

            if (treatBody1AsStatic)
            {
                rantra.MakeZero();
            }
            else
            {
                kTangent += body1.inverseMass;

                if (!body1IsMassPoint)
                {
                    // JVector.Cross(ref relativePos1, ref normal, out rantra);
                    rantra.x = (relativePos1.y * tangent.z) - (relativePos1.z * tangent.y);
                    rantra.y = (relativePos1.z * tangent.x) - (relativePos1.x * tangent.z);
                    rantra.z = (relativePos1.x * tangent.y) - (relativePos1.y * tangent.x);

                    // JVector.Transform(ref rantra, ref body1.invInertiaWorld, out rantra);
                    FP num0 = ((rantra.x * body1.invInertiaWorld.M11) + (rantra.y * body1.invInertiaWorld.M21)) + (rantra.z * body1.invInertiaWorld.M31);
                    FP num1 = ((rantra.x * body1.invInertiaWorld.M12) + (rantra.y * body1.invInertiaWorld.M22)) + (rantra.z * body1.invInertiaWorld.M32);
                    FP num2 = ((rantra.x * body1.invInertiaWorld.M13) + (rantra.y * body1.invInertiaWorld.M23)) + (rantra.z * body1.invInertiaWorld.M33);

                    rantra.x = num0; rantra.y = num1; rantra.z = num2;

                    //JVector.Cross(ref rantra, ref relativePos1, out rantra);
                    num0 = (rantra.y * relativePos1.z) - (rantra.z * relativePos1.y);
                    num1 = (rantra.z * relativePos1.x) - (rantra.x * relativePos1.z);
                    num2 = (rantra.x * relativePos1.y) - (rantra.y * relativePos1.x);

                    rantra.x = num0; rantra.y = num1; rantra.z = num2;
                }
            }

            if (treatBody2AsStatic)
            {
                rbntrb.MakeZero();
            }
            else
            {
                kTangent += body2.inverseMass;

                if (!body2IsMassPoint)
                {
                    // JVector.Cross(ref relativePos1, ref normal, out rantra);
                    rbntrb.x = (relativePos2.y * tangent.z) - (relativePos2.z * tangent.y);
                    rbntrb.y = (relativePos2.z * tangent.x) - (relativePos2.x * tangent.z);
                    rbntrb.z = (relativePos2.x * tangent.y) - (relativePos2.y * tangent.x);

                    // JVector.Transform(ref rantra, ref body1.invInertiaWorld, out rantra);
                    FP num0 = ((rbntrb.x * body2.invInertiaWorld.M11) + (rbntrb.y * body2.invInertiaWorld.M21)) + (rbntrb.z * body2.invInertiaWorld.M31);
                    FP num1 = ((rbntrb.x * body2.invInertiaWorld.M12) + (rbntrb.y * body2.invInertiaWorld.M22)) + (rbntrb.z * body2.invInertiaWorld.M32);
                    FP num2 = ((rbntrb.x * body2.invInertiaWorld.M13) + (rbntrb.y * body2.invInertiaWorld.M23)) + (rbntrb.z * body2.invInertiaWorld.M33);

                    rbntrb.x = num0; rbntrb.y = num1; rbntrb.z = num2;

                    //JVector.Cross(ref rantra, ref relativePos1, out rantra);
                    num0 = (rbntrb.y * relativePos2.z) - (rbntrb.z * relativePos2.y);
                    num1 = (rbntrb.z * relativePos2.x) - (rbntrb.x * relativePos2.z);
                    num2 = (rbntrb.x * relativePos2.y) - (rbntrb.y * relativePos2.x);

                    rbntrb.x = num0; rbntrb.y = num1; rbntrb.z = num2;
                }
            }

            if (!treatBody1AsStatic)
            {
                kTangent += FPVector.Dot(ref rantra, ref tangent);
            }
            if (!treatBody2AsStatic)
            {
                kTangent += FPVector.Dot(ref rbntrb, ref tangent);
            }
            massTangent = FP.One / kTangent;

            restitutionBias = lostSpeculativeBounce;

            speculativeVelocity = FP.Zero;

            FP relNormalVel = normal.x * dvx + normal.y * dvy + normal.z * dvz; //JVector.Dot(ref normal, ref dv);

            if (Penetration > settings.allowedPenetration)
            {
                restitutionBias = settings.bias * (FP.One / timestep) * FPMath.Max(FP.Zero, Penetration - settings.allowedPenetration);
                restitutionBias = FPMath.Clamp(restitutionBias, FP.Zero, settings.maximumBias);
                //  body1IsMassPoint = body2IsMassPoint = false;
            }


            FP timeStepRatio = timestep / lastTimeStep;

            accumulatedNormalImpulse  *= timeStepRatio;
            accumulatedTangentImpulse *= timeStepRatio;

            {
                // Static/Dynamic friction
                FP relTangentVel     = -(tangent.x * dvx + tangent.y * dvy + tangent.z * dvz);
                FP tangentImpulse    = massTangent * relTangentVel;
                FP maxTangentImpulse = -staticFriction * accumulatedNormalImpulse;

                if (tangentImpulse < maxTangentImpulse)
                {
                    friction = dynamicFriction;
                }
                else
                {
                    friction = staticFriction;
                }
            }

            FPVector impulse;

            // Simultaneos solving and restitution is simply not possible
            // so fake it a bit by just applying restitution impulse when there
            // is a new contact.

            /*if (relNormalVel < -FP.One && newContact)
             * {
             *  restitutionBias = TSMath.Max(-restitution * relNormalVel, restitutionBias);
             * }*/

            restitutionBias = FPMath.Max(-restitution * relNormalVel, restitutionBias);

            // Speculative Contacts!
            // if the penetration is negative (which means the bodies are not already in contact, but they will
            // be in the future) we store the current bounce bias in the variable 'lostSpeculativeBounce'
            // and apply it the next frame, when the speculative contact was already solved.
            if (penetration < -settings.allowedPenetration)
            {
                speculativeVelocity = penetration / timestep;

                lostSpeculativeBounce = restitutionBias;
                restitutionBias       = FP.Zero;
            }
            else
            {
                lostSpeculativeBounce = FP.Zero;
            }

            impulse.x = normal.x * accumulatedNormalImpulse + tangent.x * accumulatedTangentImpulse;
            impulse.y = normal.y * accumulatedNormalImpulse + tangent.y * accumulatedTangentImpulse;
            impulse.z = normal.z * accumulatedNormalImpulse + tangent.z * accumulatedTangentImpulse;

            if (!treatBody1AsStatic)
            {
                body1.linearVelocity.x -= (impulse.x * body1.inverseMass);
                body1.linearVelocity.y -= (impulse.y * body1.inverseMass);
                body1.linearVelocity.z -= (impulse.z * body1.inverseMass);

                if (!body1IsMassPoint)
                {
                    FP num0, num1, num2;
                    num0 = relativePos1.y * impulse.z - relativePos1.z * impulse.y;
                    num1 = relativePos1.z * impulse.x - relativePos1.x * impulse.z;
                    num2 = relativePos1.x * impulse.y - relativePos1.y * impulse.x;

                    FP num3 =
                        (((num0 * body1.invInertiaWorld.M11) +
                          (num1 * body1.invInertiaWorld.M21)) +
                         (num2 * body1.invInertiaWorld.M31));
                    FP num4 =
                        (((num0 * body1.invInertiaWorld.M12) +
                          (num1 * body1.invInertiaWorld.M22)) +
                         (num2 * body1.invInertiaWorld.M32));
                    FP num5 =
                        (((num0 * body1.invInertiaWorld.M13) +
                          (num1 * body1.invInertiaWorld.M23)) +
                         (num2 * body1.invInertiaWorld.M33));

                    body1.angularVelocity.x -= num3;
                    body1.angularVelocity.y -= num4;
                    body1.angularVelocity.z -= num5;
                }
            }

            if (!treatBody2AsStatic)
            {
                body2.linearVelocity.x += (impulse.x * body2.inverseMass);
                body2.linearVelocity.y += (impulse.y * body2.inverseMass);
                body2.linearVelocity.z += (impulse.z * body2.inverseMass);

                if (!body2IsMassPoint)
                {
                    FP num0, num1, num2;
                    num0 = relativePos2.y * impulse.z - relativePos2.z * impulse.y;
                    num1 = relativePos2.z * impulse.x - relativePos2.x * impulse.z;
                    num2 = relativePos2.x * impulse.y - relativePos2.y * impulse.x;

                    FP num3 =
                        (((num0 * body2.invInertiaWorld.M11) +
                          (num1 * body2.invInertiaWorld.M21)) +
                         (num2 * body2.invInertiaWorld.M31));
                    FP num4 =
                        (((num0 * body2.invInertiaWorld.M12) +
                          (num1 * body2.invInertiaWorld.M22)) +
                         (num2 * body2.invInertiaWorld.M32));
                    FP num5 =
                        (((num0 * body2.invInertiaWorld.M13) +
                          (num1 * body2.invInertiaWorld.M23)) +
                         (num2 * body2.invInertiaWorld.M33));

                    body2.angularVelocity.x += num3;
                    body2.angularVelocity.y += num4;
                    body2.angularVelocity.z += num5;
                }
            }

            lastTimeStep = timestep;

            newContact = false;
        }
        /// <summary>
        /// PrepareForIteration has to be called before <see cref="Iterate"/>.
        /// </summary>
        /// <param name="timestep">The timestep of the simulation.</param>
        public void PrepareForIteration(FP timestep)
        {
            FPVector dv      = CalculateRelativeVelocity();
            FP       kNormal = FP.Zero;

            FPVector rantra = FPVector.zero;

            if (!treatBody1AsStatic)
            {
                kNormal += body1.inverseMass;

                if (!body1IsMassPoint)
                {
                    FPVector.Cross(ref relativePos1, ref normal, out rantra);
                    FPVector.Transform(ref rantra, ref body1.invInertiaWorld, out rantra);
                    FPVector.Cross(ref rantra, ref relativePos1, out rantra);
                }
            }

            FPVector rbntrb = FPVector.zero;

            if (!treatBody2AsStatic)
            {
                kNormal += body2.inverseMass;

                if (!body2IsMassPoint)
                {
                    FPVector.Cross(ref relativePos2, ref normal, out rbntrb);
                    FPVector.Transform(ref rbntrb, ref body2.invInertiaWorld, out rbntrb);
                    FPVector.Cross(ref rbntrb, ref relativePos2, out rbntrb);
                }
            }

            if (!treatBody1AsStatic)
            {
                kNormal += FPVector.Dot(ref rantra, ref normal);
            }

            if (!treatBody2AsStatic)
            {
                kNormal += FPVector.Dot(ref rbntrb, ref normal);
            }

            massNormal = FP.One / kNormal;

            tangent = dv - FPVector.Dot(dv, normal) * normal;
            tangent.Normalize();

            FP kTangent = FP.Zero;

            if (treatBody1AsStatic)
            {
                rantra.MakeZero();
            }
            else
            {
                kTangent += body1.inverseMass;

                if (!body1IsMassPoint)
                {
                    FPVector.Cross(ref relativePos1, ref normal, out rantra);
                    FPVector.Transform(ref rantra, ref body1.invInertiaWorld, out rantra);
                    FPVector.Cross(ref rantra, ref relativePos1, out rantra);
                }
            }

            if (treatBody2AsStatic)
            {
                rbntrb.MakeZero();
            }
            else
            {
                kTangent += body2.inverseMass;

                if (!body2IsMassPoint)
                {
                    FPVector.Cross(ref relativePos2, ref tangent, out rbntrb);
                    FPVector.Transform(ref rbntrb, ref body2.invInertiaWorld, out rbntrb);
                    FPVector.Cross(ref rbntrb, ref relativePos2, out rbntrb);
                }
            }

            if (!treatBody1AsStatic)
            {
                kTangent += FPVector.Dot(ref rantra, ref tangent);
            }
            if (!treatBody2AsStatic)
            {
                kTangent += FPVector.Dot(ref rbntrb, ref tangent);
            }

            massTangent = FP.One / kTangent;

            restitutionBias = lostSpeculativeBounce;

            speculativeVelocity = FP.Zero;

            FP relNormalVel = FPVector.Dot(ref normal, ref dv);

            if (Penetration > settings.allowedPenetration)
            {
                restitutionBias = settings.bias * (FP.One / timestep) * FPMath.Max(FP.Zero, Penetration - settings.allowedPenetration);
                restitutionBias = FPMath.Clamp(restitutionBias, FP.Zero, settings.maximumBias);
                //  body1IsMassPoint = body2IsMassPoint = false;
            }


            FP timeStepRatio = timestep / lastTimeStep;

            accumulatedNormalImpulse  *= timeStepRatio;
            accumulatedTangentImpulse *= timeStepRatio;

            {
                // Static/Dynamic friction
                FP relTangentVel     = -FPVector.Dot(ref tangent, ref dv);
                FP tangentImpulse    = massTangent * relTangentVel;
                FP maxTangentImpulse = -staticFriction * accumulatedNormalImpulse;

                if (tangentImpulse < maxTangentImpulse)
                {
                    friction = dynamicFriction;
                }
                else
                {
                    friction = staticFriction;
                }
            }

            FPVector impulse;

            // Simultaneos solving and restitution is simply not possible
            // so fake it a bit by just applying restitution impulse when there
            // is a new contact.
            if (relNormalVel < -FP.One && newContact)
            {
                restitutionBias = FPMath.Max(-restitution * relNormalVel, restitutionBias);
            }

            // Speculative Contacts!
            // if the penetration is negative (which means the bodies are not already in contact, but they will
            // be in the future) we store the current bounce bias in the variable 'lostSpeculativeBounce'
            // and apply it the next frame, when the speculative contact was already solved.
            if (penetration < -settings.allowedPenetration)
            {
                speculativeVelocity = penetration / timestep;

                lostSpeculativeBounce = restitutionBias;
                restitutionBias       = FP.Zero;
            }
            else
            {
                lostSpeculativeBounce = FP.Zero;
            }

            impulse = normal * accumulatedNormalImpulse + tangent * accumulatedTangentImpulse;
            ApplyImpulse(ref impulse);

            lastTimeStep = timestep;

            newContact = false;
        }
Пример #9
0
    public void ApplyForces(MoveInfo move)
    {
        if (freeze)
        {
            return;
        }

        controlScript.normalizedJumpArc = (Fix64)1 - ((verticalForce + verticalTotalForce) / (verticalTotalForce * 2));


        Fix64 appliedFriction = (moveDirection != 0 || controlScript.myInfo.physics.highMovingFriction) ?
                                UFE.config.selectedStage._groundFriction : controlScript.myInfo.physics._friction;


        if (move != null && move.ignoreFriction)
        {
            appliedFriction = 0;
        }

        if (controlScript.activePullIn != null)
        {
            worldTransform.position = FPVector.Lerp(worldTransform.position,
                                                    controlScript.activePullIn.position,
                                                    UFE.fixedDeltaTime * controlScript.activePullIn.speed);

            if (controlScript.activePullIn.forceStand && !IsGrounded())
            {
                ForceGrounded();
            }

            if (FPVector.Distance(controlScript.activePullIn.position, worldTransform.position) <= controlScript.activePullIn._targetDistance ||
                controlScript.currentSubState != SubStates.Stunned)
            {
                controlScript.activePullIn = null;
            }
        }
        else
        {
            if (!IsGrounded())
            {
                appliedFriction = 0;
                if (verticalForce == 0)
                {
                    verticalForce = -.1;
                }
            }

            if (horizontalForce != 0 && !isTakingOff)
            {
                if (horizontalForce > 0)
                {
                    horizontalForce -= appliedFriction * UFE.fixedDeltaTime;
                    horizontalForce  = FPMath.Max(0, horizontalForce);
                }
                else if (horizontalForce < 0)
                {
                    horizontalForce += appliedFriction * UFE.fixedDeltaTime;
                    horizontalForce  = FPMath.Min(0, horizontalForce);
                }

                Fix64 leftCameraBounds  = opWorldTransform.position.x - (UFE.config.cameraOptions._maxDistance / 2);
                Fix64 rightCameraBounds = opWorldTransform.position.x + (UFE.config.cameraOptions._maxDistance / 2);

                bool bouncingOnCamera = false;
                if (controlScript.currentHit != null &&
                    controlScript.currentHit.bounceOnCameraEdge &&
                    (worldTransform.position.x <= leftCameraBounds ||
                     worldTransform.position.x >= rightCameraBounds))
                {
                    bouncingOnCamera = true;
                }


                if (wallBounceTimes < UFE.config.wallBounceOptions._maximumBounces &&
                    controlScript.currentSubState == SubStates.Stunned &&
                    controlScript.currentState != PossibleStates.Down &&
                    UFE.config.wallBounceOptions.bounceForce != Sizes.None &&
                    FPMath.Abs(horizontalForce) >= UFE.config.wallBounceOptions._minimumBounceForce &&
                    (worldTransform.position.x <= UFE.config.selectedStage._leftBoundary ||
                     worldTransform.position.x >= UFE.config.selectedStage._rightBoundary || bouncingOnCamera) &&
                    controlScript.currentHit != null && controlScript.currentHit.wallBounce &&
                    !isWallBouncing)
                {
                    if (controlScript.currentHit.overrideForcesOnWallBounce)
                    {
                        if (controlScript.currentHit.resetWallBounceHorizontalPush)
                        {
                            horizontalForce = 0;
                        }
                        if (controlScript.currentHit.resetWallBounceVerticalPush)
                        {
                            verticalForce = 0;
                        }

                        Fix64 addedH = -controlScript.currentHit._wallBouncePushForce.x;
                        Fix64 addedV = controlScript.currentHit._wallBouncePushForce.y;

                        AddForce(new FPVector(addedH, addedV, 0), controlScript.mirror);
                    }
                    else
                    {
                        if (UFE.config.wallBounceOptions.bounceForce == Sizes.Small)
                        {
                            horizontalForce /= -1.4;
                        }
                        else if (UFE.config.wallBounceOptions.bounceForce == Sizes.Medium)
                        {
                            horizontalForce /= -1.2;
                        }
                        else if (UFE.config.wallBounceOptions.bounceForce == Sizes.High)
                        {
                            horizontalForce *= -1;
                        }
                    }

                    wallBounceTimes++;

                    if (verticalForce > 0 || !IsGrounded())
                    {
                        if (moveSetScript.basicMoves.airWallBounce.animMap[0].clip != null)
                        {
                            controlScript.currentHitAnimation = moveSetScript.basicMoves.airWallBounce.name;
                        }
                    }
                    else
                    {
                        if (controlScript.currentHit.knockOutOnWallBounce)
                        {
                            moveSetScript.PlayBasicMove(moveSetScript.basicMoves.standingWallBounceKnockdown);
                            controlScript.currentHitAnimation = moveSetScript.basicMoves.standingWallBounceKnockdown.name;
                        }
                        else
                        {
                            moveSetScript.PlayBasicMove(moveSetScript.basicMoves.standingWallBounce);
                            controlScript.currentHitAnimation = moveSetScript.basicMoves.standingWallBounce.name;
                        }
                    }

                    if (UFE.config.wallBounceOptions.bouncePrefab != null)
                    {
                        GameObject pTemp = UFE.SpawnGameObject(UFE.config.wallBounceOptions.bouncePrefab, transform.position, Quaternion.identity, Mathf.RoundToInt(UFE.config.wallBounceOptions.bounceKillTime * UFE.config.fps));
                        pTemp.transform.rotation = UFE.config.wallBounceOptions.bouncePrefab.transform.rotation;
                        if (UFE.config.wallBounceOptions.sticky)
                        {
                            pTemp.transform.parent = transform;
                        }
                        //pTemp.transform.localPosition = Vector3.zero;
                    }

                    if (UFE.config.wallBounceOptions.shakeCamOnBounce)
                    {
                        controlScript.shakeCameraDensity = UFE.config.wallBounceOptions._shakeDensity;
                    }

                    UFE.PlaySound(UFE.config.wallBounceOptions.bounceSound);
                    isWallBouncing = true;
                }

                worldTransform.Translate((horizontalForce * UFE.fixedDeltaTime), 0, 0);
            }

            if (move == null || (move != null && !move.ignoreGravity))
            {
                if ((verticalForce < 0 && !IsGrounded()) || verticalForce > 0)
                {
                    verticalForce -= appliedGravity * UFE.fixedDeltaTime;
                    worldTransform.Translate((moveDirection * UFE.fixedDeltaTime) * controlScript.myInfo.physics._jumpDistance, (verticalForce * UFE.fixedDeltaTime), 0);
                }
                else if (verticalForce < 0 &&
                         IsGrounded() &&
                         controlScript.currentSubState != SubStates.Stunned)
                {
                    verticalForce = 0;
                }
            }
        }

        Fix64 minDist = opWorldTransform.position.x - UFE.config.cameraOptions._maxDistance;
        Fix64 maxDist = opWorldTransform.position.x + UFE.config.cameraOptions._maxDistance;

        worldTransform.position = new FPVector(FPMath.Clamp(worldTransform.position.x, minDist, maxDist), worldTransform.position.y, worldTransform.position.z);

        worldTransform.position = new FPVector(
            FPMath.Clamp(worldTransform.position.x,
                         UFE.config.selectedStage._leftBoundary,
                         UFE.config.selectedStage._rightBoundary),
            FPMath.Max(worldTransform.position.y, UFE.config.selectedStage._groundHeight),
            worldTransform.position.z);

        if (controlScript.currentState == PossibleStates.Down)
        {
            return;
        }

        if (IsGrounded() && controlScript.currentState != PossibleStates.Down)
        {
            if (verticalTotalForce != 0)
            {
                if (groundBounceTimes < UFE.config.groundBounceOptions._maximumBounces &&
                    controlScript.currentSubState == SubStates.Stunned &&
                    UFE.config.groundBounceOptions.bounceForce != Sizes.None &&
                    verticalForce <= -UFE.config.groundBounceOptions._minimumBounceForce &&
                    controlScript.currentHit.groundBounce)
                {
                    if (controlScript.currentHit.overrideForcesOnGroundBounce)
                    {
                        if (controlScript.currentHit.resetGroundBounceHorizontalPush)
                        {
                            horizontalForce = 0;
                        }
                        if (controlScript.currentHit.resetGroundBounceVerticalPush)
                        {
                            verticalForce = 0;
                        }

                        Fix64 addedH = controlScript.currentHit._groundBouncePushForce.x;
                        Fix64 addedV = controlScript.currentHit._groundBouncePushForce.y;

                        AddForce(new FPVector(addedH, addedV, 0), controlScript.mirror);
                    }
                    else
                    {
                        if (UFE.config.groundBounceOptions.bounceForce == Sizes.Small)
                        {
                            AddForce(new FPVector(0, (-verticalForce / 2.4), 0), 1);
                        }
                        else if (UFE.config.groundBounceOptions.bounceForce == Sizes.Medium)
                        {
                            AddForce(new FPVector(0, (-verticalForce / 1.8), 0), 1);
                        }
                        else if (UFE.config.groundBounceOptions.bounceForce == Sizes.High)
                        {
                            AddForce(new FPVector(0, (-verticalForce / 1.2), 0), 1);
                        }
                    }

                    groundBounceTimes++;

                    if (!isGroundBouncing)
                    {
                        controlScript.stunTime += airTime + UFE.config.knockDownOptions.air._knockedOutTime;

                        if (moveSetScript.basicMoves.groundBounce.animMap[0].clip != null)
                        {
                            controlScript.currentHitAnimation = moveSetScript.basicMoves.groundBounce.name;
                            moveSetScript.PlayBasicMove(moveSetScript.basicMoves.groundBounce);
                        }

                        if (UFE.config.groundBounceOptions.bouncePrefab != null)
                        {
                            GameObject pTemp = UFE.SpawnGameObject(UFE.config.groundBounceOptions.bouncePrefab, transform.position, Quaternion.identity, Mathf.RoundToInt(UFE.config.groundBounceOptions.bounceKillTime * UFE.config.fps));
                            pTemp.transform.rotation = UFE.config.groundBounceOptions.bouncePrefab.transform.rotation;
                            if (UFE.config.groundBounceOptions.sticky)
                            {
                                pTemp.transform.parent = transform;
                            }
                            //pTemp.transform.localPosition = Vector3.zero;
                        }
                        if (UFE.config.groundBounceOptions.shakeCamOnBounce)
                        {
                            controlScript.shakeCameraDensity = UFE.config.groundBounceOptions._shakeDensity;
                        }
                        UFE.PlaySound(UFE.config.groundBounceOptions.bounceSound);
                        isGroundBouncing = true;
                    }
                    return;
                }
                verticalTotalForce          = 0;
                airTime                     = 0;
                moveSetScript.totalAirMoves = 0;
                currentAirJumps             = 0;

                BasicMoveInfo airAnimation  = null;
                string        downAnimation = "";

                isGroundBouncing  = false;
                groundBounceTimes = 0;

                Fix64 animationSpeed = 0;
                Fix64 delayTime      = 0;
                if (controlScript.currentMove != null && controlScript.currentMove.hitAnimationOverride)
                {
                    return;
                }
                if (controlScript.currentSubState == SubStates.Stunned)
                {
                    if (moveSetScript.IsAnimationPlaying(moveSetScript.basicMoves.airRecovery.name))
                    {
                        controlScript.stunTime     = 0;
                        controlScript.currentState = PossibleStates.Stand;
                    }
                    else
                    {
                        controlScript.stunTime = UFE.config.knockDownOptions.air._knockedOutTime + UFE.config.knockDownOptions.air._standUpTime;

                        // Hit Clips
                        if (moveSetScript.IsAnimationPlaying(moveSetScript.basicMoves.getHitKnockBack.name) &&
                            moveSetScript.basicMoves.getHitKnockBack.animMap[1].clip != null)
                        {
                            airAnimation  = moveSetScript.basicMoves.getHitKnockBack;
                            downAnimation = moveSetScript.GetAnimationString(airAnimation, 2);
                        }
                        else if (moveSetScript.IsAnimationPlaying(moveSetScript.basicMoves.getHitHighKnockdown.name) &&
                                 moveSetScript.basicMoves.getHitHighKnockdown.animMap[1].clip != null)
                        {
                            airAnimation           = moveSetScript.basicMoves.getHitHighKnockdown;
                            downAnimation          = moveSetScript.GetAnimationString(airAnimation, 2);
                            controlScript.stunTime = UFE.config.knockDownOptions.high._knockedOutTime + UFE.config.knockDownOptions.high._standUpTime;
                        }
                        else if (moveSetScript.IsAnimationPlaying(moveSetScript.basicMoves.getHitMidKnockdown.name) &&
                                 moveSetScript.basicMoves.getHitMidKnockdown.animMap[1].clip != null)
                        {
                            airAnimation           = moveSetScript.basicMoves.getHitMidKnockdown;
                            downAnimation          = moveSetScript.GetAnimationString(airAnimation, 2);
                            controlScript.stunTime = UFE.config.knockDownOptions.highLow._knockedOutTime + UFE.config.knockDownOptions.highLow._standUpTime;
                        }
                        else if (moveSetScript.IsAnimationPlaying(moveSetScript.basicMoves.getHitSweep.name) &&
                                 moveSetScript.basicMoves.getHitSweep.animMap[1].clip != null)
                        {
                            airAnimation           = moveSetScript.basicMoves.getHitSweep;
                            downAnimation          = moveSetScript.GetAnimationString(airAnimation, 2);
                            controlScript.stunTime = UFE.config.knockDownOptions.sweep._knockedOutTime + UFE.config.knockDownOptions.sweep._standUpTime;
                        }
                        else if (moveSetScript.IsAnimationPlaying(moveSetScript.basicMoves.getHitCrumple.name) &&
                                 moveSetScript.basicMoves.getHitCrumple.animMap[1].clip != null)
                        {
                            airAnimation  = moveSetScript.basicMoves.getHitCrumple;
                            downAnimation = moveSetScript.GetAnimationString(airAnimation, 2);

                            // Stage Clips
                        }
                        else if (moveSetScript.IsAnimationPlaying(moveSetScript.basicMoves.standingWallBounceKnockdown.name) &&
                                 moveSetScript.basicMoves.standingWallBounceKnockdown.animMap[1].clip != null)
                        {
                            airAnimation           = moveSetScript.basicMoves.standingWallBounceKnockdown;
                            downAnimation          = moveSetScript.GetAnimationString(airAnimation, 2);
                            controlScript.stunTime = UFE.config.knockDownOptions.wallbounce._knockedOutTime + UFE.config.knockDownOptions.wallbounce._standUpTime;
                        }
                        else if (moveSetScript.IsAnimationPlaying(moveSetScript.basicMoves.airWallBounce.name) &&
                                 moveSetScript.basicMoves.airWallBounce.animMap[1].clip != null)
                        {
                            airAnimation           = moveSetScript.basicMoves.airWallBounce;
                            downAnimation          = moveSetScript.GetAnimationString(airAnimation, 2);
                            controlScript.stunTime = UFE.config.knockDownOptions.wallbounce._knockedOutTime + UFE.config.knockDownOptions.wallbounce._standUpTime;

                            // Fall Clips
                        }
                        else if (moveSetScript.IsAnimationPlaying(moveSetScript.basicMoves.fallingFromAirHit.name) &&
                                 moveSetScript.basicMoves.fallingFromAirHit.animMap[1].clip != null)
                        {
                            airAnimation  = moveSetScript.basicMoves.fallingFromAirHit;
                            downAnimation = moveSetScript.GetAnimationString(airAnimation, 2);
                        }
                        else if (moveSetScript.IsAnimationPlaying(moveSetScript.basicMoves.fallingFromGroundBounce.name) &&
                                 moveSetScript.basicMoves.fallingFromGroundBounce.animMap[1].clip != null)
                        {
                            airAnimation  = moveSetScript.basicMoves.fallingFromGroundBounce;
                            downAnimation = moveSetScript.GetAnimationString(airAnimation, 2);
                        }
                        else
                        {
                            if (moveSetScript.basicMoves.fallDown.animMap[0].clip == null)
                            {
                                Debug.LogError("Fall Down From Air Hit animation not found! Make sure you have it set on Character -> Basic Moves -> Fall Down From Air Hit");
                            }

                            airAnimation  = moveSetScript.basicMoves.fallDown;
                            downAnimation = moveSetScript.GetAnimationString(airAnimation, 1);
                        }

                        controlScript.currentState = PossibleStates.Down;
                    }
                }
                else if (controlScript.currentState != PossibleStates.Stand)
                {
                    if (moveSetScript.basicMoves.landing.animMap[0].clip != null &&
                        (controlScript.currentMove == null ||
                         (controlScript.currentMove != null && controlScript.currentMove.cancelMoveWheLanding)))
                    {
                        controlScript.isAirRecovering = false;
                        airAnimation  = moveSetScript.basicMoves.landing;
                        moveDirection = 0;
                        isLanding     = true;
                        controlScript.KillCurrentMove();
                        delayTime = (Fix64)controlScript.myInfo.physics.landingDelay / (Fix64)UFE.config.fps;
                        UFE.DelaySynchronizedAction(ResetLanding, delayTime);

                        if (airAnimation.autoSpeed)
                        {
                            animationSpeed = moveSetScript.GetAnimationLength(airAnimation.name) / delayTime;
                        }
                    }

                    if (controlScript.currentState != PossibleStates.Crouch)
                    {
                        controlScript.currentState = PossibleStates.Stand;
                    }
                }

                if (airAnimation != null)
                {
                    if (downAnimation != "")
                    {
                        moveSetScript.PlayBasicMove(airAnimation, downAnimation);
                    }
                    else
                    {
                        moveSetScript.PlayBasicMove(airAnimation);
                    }

                    if (animationSpeed != 0)
                    {
                        moveSetScript.SetAnimationSpeed(airAnimation.name, animationSpeed);
                    }
                }
            }

            if (controlScript.currentSubState != SubStates.Stunned &&
                !controlScript.isBlocking && !controlScript.blockStunned &&
                move == null &&
                !isTakingOff &&
                !isLanding &&
                controlScript.currentState == PossibleStates.Stand)
            {
                if (moveDirection > 0 && controlScript.mirror == -1 ||
                    moveDirection < 0 && controlScript.mirror == 1)
                {
                    if (moveSetScript.basicMoves.moveForward.animMap[0].clip == null)
                    {
                        Debug.LogError("Move Forward animation not found! Make sure you have it set on Character -> Basic Moves -> Move Forward");
                    }
                    if (!moveSetScript.IsAnimationPlaying(moveSetScript.basicMoves.moveForward.name))
                    {
                        moveSetScript.PlayBasicMove(moveSetScript.basicMoves.moveForward);
                    }
                }
                else if (moveDirection > 0 && controlScript.mirror == 1 ||
                         moveDirection < 0 && controlScript.mirror == -1)
                {
                    if (moveSetScript.basicMoves.moveBack.animMap[0].clip == null)
                    {
                        Debug.LogError("Move Back animation not found! Make sure you have it set on Character -> Basic Moves -> Move Back");
                    }
                    if (!moveSetScript.IsAnimationPlaying(moveSetScript.basicMoves.moveBack.name))
                    {
                        moveSetScript.PlayBasicMove(moveSetScript.basicMoves.moveBack);
                    }
                }
            }
        }
        else if (verticalForce > 0 || !IsGrounded())
        {
            if (move != null && controlScript.currentState == PossibleStates.Stand)
            {
                controlScript.currentState = PossibleStates.NeutralJump;
            }
            if (move == null && verticalForce / verticalTotalForce > 0 && verticalForce / verticalTotalForce <= 1)
            {
                if (isGroundBouncing)
                {
                    return;
                }

                if (moveDirection == 0)
                {
                    controlScript.currentState = PossibleStates.NeutralJump;
                }
                else
                {
                    if (moveDirection > 0 && controlScript.mirror == -1 ||
                        moveDirection < 0 && controlScript.mirror == 1)
                    {
                        controlScript.currentState = PossibleStates.ForwardJump;
                    }

                    if (moveDirection > 0 && controlScript.mirror == 1 ||
                        moveDirection < 0 && controlScript.mirror == -1)
                    {
                        controlScript.currentState = PossibleStates.BackJump;
                    }
                }

                BasicMoveInfo airAnimation = moveSetScript.basicMoves.jumpStraight;
                if (controlScript.currentSubState == SubStates.Stunned)
                {
                    if (isWallBouncing && moveSetScript.basicMoves.airWallBounce.animMap[0].clip != null)
                    {
                        airAnimation = moveSetScript.basicMoves.airWallBounce;
                    }
                    else if (moveSetScript.basicMoves.getHitKnockBack.animMap[0].clip != null &&
                             FPMath.Abs(horizontalForce) > UFE.config.comboOptions._knockBackMinForce &&
                             UFE.config.comboOptions._knockBackMinForce > 0)
                    {
                        airAnimation = moveSetScript.basicMoves.getHitKnockBack;
                        airTime     *= (Fix64)2;
                    }
                    else
                    {
                        if (moveSetScript.basicMoves.getHitAir.animMap[0].clip == null)
                        {
                            Debug.LogError("Get Hit Air animation not found! Make sure you have it set on Character -> Basic Moves -> Get Hit Air");
                        }

                        airAnimation = moveSetScript.basicMoves.getHitAir;
                    }
                    if (overrideStunAnimation != null)
                    {
                        airAnimation = overrideStunAnimation;
                    }
                }
                else if (controlScript.isAirRecovering &&
                         (moveSetScript.basicMoves.airRecovery.animMap[0].clip != null))
                {
                    airAnimation = moveSetScript.basicMoves.airRecovery;
                }
                else
                {
                    if (moveSetScript.basicMoves.jumpForward.animMap[0].clip != null && controlScript.currentState == PossibleStates.ForwardJump)
                    {
                        airAnimation = moveSetScript.basicMoves.jumpForward;
                    }
                    else if (moveSetScript.basicMoves.jumpBack.animMap[0].clip != null && controlScript.currentState == PossibleStates.BackJump)
                    {
                        airAnimation = moveSetScript.basicMoves.jumpBack;
                    }
                    else
                    {
                        if (moveSetScript.basicMoves.jumpStraight.animMap[0].clip == null)
                        {
                            Debug.LogError("Jump animation not found! Make sure you have it set on Character -> Basic Moves -> Jump Straight");
                        }

                        airAnimation = moveSetScript.basicMoves.jumpStraight;
                    }
                }

                if (!overrideAirAnimation && !moveSetScript.IsAnimationPlaying(airAnimation.name))
                {
                    moveSetScript.PlayBasicMove(airAnimation);

                    if (airAnimation.autoSpeed)
                    {
                        moveSetScript.SetAnimationNormalizedSpeed(airAnimation.name, (moveSetScript.GetAnimationLength(airAnimation.name) / airTime));
                    }
                }
            }
            else if (move == null && verticalForce / verticalTotalForce <= 0)
            {
                BasicMoveInfo airAnimation = moveSetScript.basicMoves.fallStraight;
                if (isGroundBouncing && moveSetScript.basicMoves.fallingFromGroundBounce.animMap[0].clip != null)
                {
                    airAnimation = moveSetScript.basicMoves.fallingFromGroundBounce;
                }
                else if (isWallBouncing && moveSetScript.basicMoves.airWallBounce.animMap[0].clip != null)
                {
                    airAnimation = moveSetScript.basicMoves.airWallBounce;
                }
                else
                {
                    if (controlScript.currentSubState == SubStates.Stunned)
                    {
                        if (moveSetScript.basicMoves.getHitKnockBack.animMap[0].clip != null &&
                            FPMath.Abs(horizontalForce) > UFE.config.comboOptions._knockBackMinForce &&
                            UFE.config.comboOptions._knockBackMinForce > 0)
                        {
                            airAnimation = moveSetScript.basicMoves.getHitKnockBack;
                        }
                        else
                        {
                            airAnimation = moveSetScript.basicMoves.getHitAir;
                            if (moveSetScript.basicMoves.fallingFromAirHit.animMap[0].clip != null)
                            {
                                airAnimation = moveSetScript.basicMoves.fallingFromAirHit;
                            }
                            else if (moveSetScript.basicMoves.getHitAir.animMap[0].clip == null)
                            {
                                Debug.LogError("Air Juggle animation not found! Make sure you have it set on Character -> Basic Moves -> Air Juggle");
                            }
                        }
                        if (overrideStunAnimation != null)
                        {
                            airAnimation = overrideStunAnimation;
                        }
                    }
                    else if (controlScript.isAirRecovering &&
                             (moveSetScript.basicMoves.airRecovery.animMap[0].clip != null))
                    {
                        airAnimation = moveSetScript.basicMoves.airRecovery;
                    }
                    else
                    {
                        if (moveSetScript.basicMoves.fallForward.animMap[0].clip != null && controlScript.currentState == PossibleStates.ForwardJump)
                        {
                            airAnimation = moveSetScript.basicMoves.fallForward;
                        }
                        else if (moveSetScript.basicMoves.fallBack.animMap[0].clip != null && controlScript.currentState == PossibleStates.BackJump)
                        {
                            airAnimation = moveSetScript.basicMoves.fallBack;
                        }
                        else
                        {
                            if (moveSetScript.basicMoves.fallStraight.animMap[0].clip == null)
                            {
                                Debug.LogError("Fall animation not found! Make sure you have it set on Character -> Basic Moves -> Fall Straight");
                            }

                            airAnimation = moveSetScript.basicMoves.fallStraight;
                        }
                    }
                }

                if (!overrideAirAnimation && !moveSetScript.IsAnimationPlaying(airAnimation.name))
                {
                    moveSetScript.PlayBasicMove(airAnimation);

                    if (airAnimation.autoSpeed)
                    {
                        moveSetScript.SetAnimationNormalizedSpeed(airAnimation.name, (moveSetScript.GetAnimationLength(airAnimation.name) / airTime));
                    }
                }
            }
        }
        if (horizontalForce == 0 && verticalForce == 0)
        {
            moveDirection = 0;
        }
    }