예제 #1
0
        public void StoreImpulses()
        {
            for (int i = 0; i < _constraintCount; ++i)
            {
                ContactConstraint c = Constraints[i];
                if (c.BodyA.Penetrable || c.BodyB.Penetrable)
                {
                    continue;
                }

                Manifold m = c.Manifold;

                for (int j = 0; j < c.PointCount; ++j)
                {
                    ManifoldPoint          pj = m.Points[j];
                    ContactConstraintPoint cp = c.Points[j];

                    pj.NormalImpulse  = cp.NormalImpulse;
                    pj.TangentImpulse = cp.TangentImpulse;

                    m.Points[j] = pj;
                }

                c.Manifold            = m;
                _contacts[i].Manifold = m;
            }
        }
예제 #2
0
        internal PositionSolverManifold(ref ContactConstraint cc, int index)
        {
            Debug.Assert(cc.PointCount > 0);

            switch (cc.Type)
            {
            case ManifoldType.Circles:
            {
                Vector2 pointA = cc.BodyA.GetWorldPoint(ref cc.LocalPoint);
                Vector2 pointB = cc.BodyB.GetWorldPoint(ref cc.Points[0].LocalPoint);
                if (Vector2.DistanceSquared(pointA, pointB) > Settings.Epsilon * Settings.Epsilon)
                {
                    Normal = pointB - pointA;
                    Normal.Normalize();
                }
                else
                {
                    Normal = new Vector2(1.0f, 0.0f);
                }

                Point      = 0.5f * (pointA + pointB);
                Separation = Vector2.Dot(pointB - pointA, Normal) - cc.Radius;
            }
            break;

            case ManifoldType.FaceA:
            {
                Normal = cc.BodyA.GetWorldVector(ref cc.LocalNormal);
                Vector2 planePoint = cc.BodyA.GetWorldPoint(ref cc.LocalPoint);

                Vector2 clipPoint = cc.BodyB.GetWorldPoint(ref cc.Points[index].LocalPoint);
                Separation = Vector2.Dot(clipPoint - planePoint, Normal) - cc.Radius;
                Point      = clipPoint;
            }
            break;

            case ManifoldType.FaceB:
            {
                Normal = cc.BodyB.GetWorldVector(ref cc.LocalNormal);
                Vector2 planePoint = cc.BodyB.GetWorldPoint(ref cc.LocalPoint);

                Vector2 clipPoint = cc.BodyA.GetWorldPoint(ref cc.Points[index].LocalPoint);
                Separation = Vector2.Dot(clipPoint - planePoint, Normal) - cc.Radius;
                Point      = clipPoint;

                // Ensure normal points from A to B
                Normal = -Normal;
            }
            break;

            default:
                Normal     = Vector2.Zero;
                Point      = Vector2.Zero;
                Separation = 0.0f;
                break;
            }
        }
예제 #3
0
        public static void Solve(ref ContactConstraint cc, int index, out Vector2 normal, out Vector2 point, out float separation)
        {
            Debug.Assert(cc.PointCount > 0);

            switch (cc.Type)
            {
            case ManifoldType.Circles:
            {
                Vector2 pointA = cc.BodyA.GetWorldPoint(ref cc.LocalPoint);
                Vector2 pointB = cc.BodyB.GetWorldPoint(ref cc.Points[0].LocalPoint);
                if (Vector2.DistanceSquared(pointA, pointB) > Settings.Epsilon * Settings.Epsilon)
                {
                    normal = pointB - pointA;
                    normal.Normalize();
                }
                else
                {
                    normal = new Vector2(1.0f, 0.0f);
                }

                point      = 0.5f * (pointA + pointB);
                separation = Vector2.Dot(pointB - pointA, normal) - cc.RadiusA - cc.RadiusB;
            }
            break;

            case ManifoldType.FaceA:
            {
                normal = cc.BodyA.GetWorldVector(ref cc.LocalNormal);
                Vector2 planePoint = cc.BodyA.GetWorldPoint(ref cc.LocalPoint);

                Vector2 clipPoint = cc.BodyB.GetWorldPoint(ref cc.Points[index].LocalPoint);
                separation = Vector2.Dot(clipPoint - planePoint, normal) - cc.RadiusA - cc.RadiusB;
                point      = clipPoint;
            }
            break;

            case ManifoldType.FaceB:
            {
                normal = cc.BodyB.GetWorldVector(ref cc.LocalNormal);
                Vector2 planePoint = cc.BodyB.GetWorldPoint(ref cc.LocalPoint);

                Vector2 clipPoint = cc.BodyA.GetWorldPoint(ref cc.Points[index].LocalPoint);
                separation = Vector2.Dot(clipPoint - planePoint, normal) - cc.RadiusA - cc.RadiusB;
                point      = clipPoint;

                // Ensure normal points from A to B
                normal = -normal;
            }
            break;

            default:
                normal     = Vector2.Zero;
                point      = Vector2.Zero;
                separation = 0.0f;
                break;
            }
        }
        internal PositionSolverManifold(ref ContactConstraint cc, int index)
        {
            Debug.Assert(cc.PointCount > 0);

            switch (cc.Type)
            {
                case ManifoldType.Circles:
                    {
                        Vector2 pointA = cc.BodyA.GetWorldPoint(ref cc.LocalPoint);
                        Vector2 pointB = cc.BodyB.GetWorldPoint(ref cc.Points[0].LocalPoint);
                        if (Vector2.DistanceSquared(pointA, pointB) > Settings.Epsilon * Settings.Epsilon)
                        {
                            Normal = pointB - pointA;
                            Normal.Normalize();
                        }
                        else
                        {
                            Normal = new Vector2(1.0f, 0.0f);
                        }

                        Point = 0.5f * (pointA + pointB);
                        Separation = Vector2.Dot(pointB - pointA, Normal) - cc.Radius;
                    }
                    break;

                case ManifoldType.FaceA:
                    {
                        Normal = cc.BodyA.GetWorldVector(ref cc.LocalNormal);
                        Vector2 planePoint = cc.BodyA.GetWorldPoint(ref cc.LocalPoint);

                        Vector2 clipPoint = cc.BodyB.GetWorldPoint(ref cc.Points[index].LocalPoint);
                        Separation = Vector2.Dot(clipPoint - planePoint, Normal) - cc.Radius;
                        Point = clipPoint;
                    }
                    break;

                case ManifoldType.FaceB:
                    {
                        Normal = cc.BodyB.GetWorldVector(ref cc.LocalNormal);
                        Vector2 planePoint = cc.BodyB.GetWorldPoint(ref cc.LocalPoint);

                        Vector2 clipPoint = cc.BodyA.GetWorldPoint(ref cc.Points[index].LocalPoint);
                        Separation = Vector2.Dot(clipPoint - planePoint, Normal) - cc.Radius;
                        Point = clipPoint;

                        // Ensure normal points from A to B
                        Normal = -Normal;
                    }
                    break;
                default:
                    Normal = Vector2.Zero;
                    Point = Vector2.Zero;
                    Separation = 0.0f;
                    break;
            }
        }
예제 #5
0
        public void WarmStart()
        {
            // Warm start.
            for (int i = 0; i < _constraintCount; ++i)
            {
                ContactConstraint c = Constraints[i];

                Body    bodyA    = c.BodyA;
                Body    bodyB    = c.BodyB;
                float   invMassA = bodyA.InvMass;
                float   invIA    = bodyA.InvI;
                float   invMassB = bodyB.InvMass;
                float   invIB    = bodyB.InvI;
                Vector2 normal   = c.Normal;

#if MATH_OVERLOADS
                Vector2 tangent = MathUtils.Cross(normal, 1.0f);
#else
                Vector2 tangent = new Vector2(normal.Y, -normal.X);
#endif

                for (int j = 0; j < c.PointCount; ++j)
                {
                    ContactConstraintPoint ccp = c.Points[j];
#if MATH_OVERLOADS
                    Vector2 P = ccp.NormalImpulse * normal + ccp.TangentImpulse * tangent;
                    bodyA.AngularVelocityInternal -= invIA * MathUtils.Cross(ccp.rA, P);
                    bodyA.LinearVelocityInternal  -= invMassA * P;
                    bodyB.AngularVelocityInternal += invIB * MathUtils.Cross(ccp.rB, P);
                    bodyB.LinearVelocityInternal  += invMassB * P;
#else
                    Vector2 P = new Vector2(ccp.NormalImpulse * normal.X + ccp.TangentImpulse * tangent.X,
                                            ccp.NormalImpulse * normal.Y + ccp.TangentImpulse * tangent.Y);
                    bodyA.AngularVelocityInternal  -= invIA * (ccp.rA.X * P.Y - ccp.rA.Y * P.X);
                    bodyA.LinearVelocityInternal.X -= invMassA * P.X;
                    bodyA.LinearVelocityInternal.Y -= invMassA * P.Y;
                    bodyB.AngularVelocityInternal  += invIB * (ccp.rB.X * P.Y - ccp.rB.Y * P.X);
                    bodyB.LinearVelocityInternal.X += invMassB * P.X;
                    bodyB.LinearVelocityInternal.Y += invMassB * P.Y;
#endif
                }
            }
        }
        private void PostSolve(Contact contact, ContactConstraint impulse)
        {
            if (!Broken)
            {
                if (Parts.Contains(contact.FixtureA) || Parts.Contains(contact.FixtureB))
                {
                    float maxImpulse = 0.0f;
                    int count = contact.Manifold.PointCount;

                    for (int i = 0; i < count; ++i)
                    {
                        maxImpulse = Math.Max(maxImpulse, impulse.Points[i].NormalImpulse);
                    }

                    if (maxImpulse > Strength)
                    {
                        // Flag the body for breaking.
                        _break = true;
                    }
                }
            }
        }
예제 #7
0
        public void StoreImpulses()
        {
            for (int i = 0; i < this._constraintCount; ++i)
            {
                ContactConstraint c = this.Constraints[i];
                Manifold          m = c.Manifold;

                for (int j = 0; j < c.PointCount; ++j)
                {
                    ManifoldPoint          pj = m.Points[j];
                    ContactConstraintPoint cp = c.Points[j];

                    pj.NormalImpulse  = cp.NormalImpulse;
                    pj.TangentImpulse = cp.TangentImpulse;

                    m.Points[j] = pj;
                }

                c.Manifold = m;
                this._contacts[i].Manifold = m;
            }
        }
예제 #8
0
        public void WarmStart()
        {
            // Warm start.
            for (int i = 0; i < this._constraintCount; ++i)
            {
                ContactConstraint c = this.Constraints[i];

                float tangentx = c.Normal.Y;
                float tangenty = -c.Normal.X;

                for (int j = 0; j < c.PointCount; ++j)
                {
                    ContactConstraintPoint ccp = c.Points[j];
                    float px = ccp.NormalImpulse * c.Normal.X + ccp.TangentImpulse * tangentx;
                    float py = ccp.NormalImpulse * c.Normal.Y + ccp.TangentImpulse * tangenty;
                    c.BodyA.AngularVelocityInternal  -= c.BodyA.InvI * (ccp.rA.X * py - ccp.rA.Y * px);
                    c.BodyA.LinearVelocityInternal.X -= c.BodyA.InvMass * px;
                    c.BodyA.LinearVelocityInternal.Y -= c.BodyA.InvMass * py;
                    c.BodyB.AngularVelocityInternal  += c.BodyB.InvI * (ccp.rB.X * py - ccp.rB.Y * px);
                    c.BodyB.LinearVelocityInternal.X += c.BodyB.InvMass * px;
                    c.BodyB.LinearVelocityInternal.Y += c.BodyB.InvMass * py;
                }
            }
        }
예제 #9
0
        public void WarmStart()
        {
            // Warm start.
            for (int i = 0; i < _constraintCount; ++i)
            {
                ContactConstraint c = Constraints[i];

                float tangentx = c.Normal.y;
                float tangenty = -c.Normal.x;

                for (int j = 0; j < c.PointCount; ++j)
                {
                    ContactConstraintPoint ccp = c.Points[j];
                    float px = ccp.NormalImpulse * c.Normal.x + ccp.TangentImpulse * tangentx;
                    float py = ccp.NormalImpulse * c.Normal.y + ccp.TangentImpulse * tangenty;
                    c.BodyA.AngularVelocityInternal  -= c.BodyA.InvI * (ccp.rA.x * py - ccp.rA.y * px);
                    c.BodyA.LinearVelocityInternal.x -= c.BodyA.InvMass * px;
                    c.BodyA.LinearVelocityInternal.y -= c.BodyA.InvMass * py;
                    c.BodyB.AngularVelocityInternal  += c.BodyB.InvI * (ccp.rB.x * py - ccp.rB.y * px);
                    c.BodyB.LinearVelocityInternal.x += c.BodyB.InvMass * px;
                    c.BodyB.LinearVelocityInternal.y += c.BodyB.InvMass * py;
                }
            }
        }
예제 #10
0
        public void InitializeVelocityConstraints()
        {
            for (int i = 0; i < _constraintCount; ++i)
            {
                ContactConstraint cc = Constraints[i];

                float    radiusA  = cc.RadiusA;
                float    radiusB  = cc.RadiusB;
                Body     bodyA    = cc.BodyA;
                Body     bodyB    = cc.BodyB;
                Manifold manifold = cc.Manifold;

                Vector2 vA = bodyA.LinearVelocity;
                Vector2 vB = bodyB.LinearVelocity;
                float   wA = bodyA.AngularVelocity;
                float   wB = bodyB.AngularVelocity;

                Debug.Assert(manifold.PointCount > 0);

                WorldManifold worldManifold = new WorldManifold(ref manifold, ref bodyA.Xf, radiusA, ref bodyB.Xf, radiusB);

                cc.Normal = worldManifold.Normal;

                for (int j = 0; j < cc.PointCount; ++j)
                {
                    ContactConstraintPoint ccp = cc.Points[j];

                    ccp.rA = worldManifold.Points[j] - bodyA.Sweep.C;
                    ccp.rB = worldManifold.Points[j] - bodyB.Sweep.C;
#if MATH_OVERLOADS
                    float rnA = MathUtils.Cross(ccp.rA, cc.Normal);
                    float rnB = MathUtils.Cross(ccp.rB, cc.Normal);
#else
                    float rnA = ccp.rA.X * cc.Normal.Y - ccp.rA.Y * cc.Normal.X;
                    float rnB = ccp.rB.X * cc.Normal.Y - ccp.rB.Y * cc.Normal.X;
#endif
                    rnA *= rnA;
                    rnB *= rnB;

                    float kNormal = bodyA.InvMass + bodyB.InvMass + bodyA.InvI * rnA + bodyB.InvI * rnB;

                    Debug.Assert(kNormal > Settings.Epsilon);
                    ccp.NormalMass = 1.0f / kNormal;

#if MATH_OVERLOADS
                    Vector2 tangent = MathUtils.Cross(cc.Normal, 1.0f);

                    float rtA = MathUtils.Cross(ccp.rA, tangent);
                    float rtB = MathUtils.Cross(ccp.rB, tangent);
#else
                    Vector2 tangent = new Vector2(cc.Normal.Y, -cc.Normal.X);

                    float rtA = ccp.rA.X * tangent.Y - ccp.rA.Y * tangent.X;
                    float rtB = ccp.rB.X * tangent.Y - ccp.rB.Y * tangent.X;
#endif
                    rtA *= rtA;
                    rtB *= rtB;
                    float kTangent = bodyA.InvMass + bodyB.InvMass + bodyA.InvI * rtA + bodyB.InvI * rtB;

                    Debug.Assert(kTangent > Settings.Epsilon);
                    ccp.TangentMass = 1.0f / kTangent;

                    // Setup a velocity bias for restitution.
                    ccp.VelocityBias = 0.0f;
                    float vRel = Vector2.Dot(cc.Normal, vB + new Vector2(-wB * ccp.rB.Y, wB * ccp.rB.X) - vA - new Vector2(-wA * ccp.rA.Y, wA * ccp.rA.X));
                    if (vRel < -Settings.VelocityThreshold)
                    {
                        ccp.VelocityBias = -cc.Restitution * vRel;
                    }
                }

                // If we have two points, then prepare the block solver.
                if (cc.PointCount == 2)
                {
                    ContactConstraintPoint ccp1 = cc.Points[0];
                    ContactConstraintPoint ccp2 = cc.Points[1];

                    float invMassA = bodyA.InvMass;
                    float invIA    = bodyA.InvI;
                    float invMassB = bodyB.InvMass;
                    float invIB    = bodyB.InvI;

                    float rn1A = MathUtils.Cross(ccp1.rA, cc.Normal);
                    float rn1B = MathUtils.Cross(ccp1.rB, cc.Normal);
                    float rn2A = MathUtils.Cross(ccp2.rA, cc.Normal);
                    float rn2B = MathUtils.Cross(ccp2.rB, cc.Normal);

                    float k11 = invMassA + invMassB + invIA * rn1A * rn1A + invIB * rn1B * rn1B;
                    float k22 = invMassA + invMassB + invIA * rn2A * rn2A + invIB * rn2B * rn2B;
                    float k12 = invMassA + invMassB + invIA * rn1A * rn2A + invIB * rn1B * rn2B;

                    // Ensure a reasonable condition number.
                    const float k_maxConditionNumber = 100.0f;
                    if (k11 * k11 < k_maxConditionNumber * (k11 * k22 - k12 * k12))
                    {
                        // K is safe to invert.
                        cc.K          = new Mat22(new Vector2(k11, k12), new Vector2(k12, k22));
                        cc.NormalMass = cc.K.Inverse;
                    }
                    else
                    {
                        // The constraints are redundant, just use one.
                        // TODO_ERIN use deepest?
                        cc.PointCount = 1;
                    }
                }
            }
        }
예제 #11
0
        private void Report(ContactConstraint[] constraints)
        {
            if (_contactManager == null)
                return;

            for (int i = 0; i < ContactCount; ++i)
            {
                Contact c = _contacts[i];

                c.FixtureA.Body.OnAfterCollision(c.FixtureA, c.FixtureB, c);
                c.FixtureB.Body.OnAfterCollision(c.FixtureB, c.FixtureA, c);

                ContactConstraint cc = constraints[i];
				c.FixtureA.Body.OnPostSolve(c, cc);
				c.FixtureB.Body.OnPostSolve(c, cc);

                if (_contactManager.PostSolve != null)
                    _contactManager.PostSolve(c, cc);
            }
        }
예제 #12
0
 internal void OnPostSolve(Contact contact, ContactConstraint impulse)
 {
     if (this.PostSolve != null)
         this.PostSolve(contact, impulse);
 }
예제 #13
0
		private void body_PostSolve(Contact contact, ContactConstraint impulse)
		{
			int count = contact.Manifold.PointCount;
			for (int i = 0; i < count; ++i)
			{
				if (impulse.Points[i].NormalImpulse != 0.0f || impulse.Points[i].TangentImpulse != 0.0f)
				{
					CollisionData colData = new CollisionData(this.body, impulse, i);
					if (contact.FixtureA.Body == this.body)
						this.eventBuffer.Add(new ColEvent(ColEvent.EventType.PostSolve, contact.FixtureA, contact.FixtureB, colData));
					else
						this.eventBuffer.Add(new ColEvent(ColEvent.EventType.PostSolve, contact.FixtureB, contact.FixtureA, colData));
				}
			}
		}
예제 #14
0
		internal CollisionData(Body localBody, ContactConstraint impulse, int pointIndex)
		{
			if (localBody == impulse.BodyA)
			{
				this.pos = PhysicsUnit.LengthToDuality * (impulse.Points[pointIndex].rA + impulse.BodyA.Position);
				this.normal = impulse.Normal;
				this.normalImpulse = PhysicsUnit.ImpulseToDuality * impulse.Points[pointIndex].NormalImpulse;
				this.tangentImpulse = PhysicsUnit.ImpulseToDuality * impulse.Points[pointIndex].TangentImpulse;
				this.normalMass = PhysicsUnit.MassToDuality * impulse.Points[pointIndex].NormalMass;
				this.tangentMass = PhysicsUnit.MassToDuality * impulse.Points[pointIndex].TangentMass;
			}
			else if (localBody == impulse.BodyB)
			{
				this.pos = PhysicsUnit.LengthToDuality * (impulse.Points[pointIndex].rB + impulse.BodyB.Position);
				this.normal = -impulse.Normal;
				this.normalImpulse = PhysicsUnit.ImpulseToDuality * impulse.Points[pointIndex].NormalImpulse;
				this.tangentImpulse = PhysicsUnit.ImpulseToDuality * impulse.Points[pointIndex].TangentImpulse;
				this.normalMass = PhysicsUnit.MassToDuality * impulse.Points[pointIndex].NormalMass;
				this.tangentMass = PhysicsUnit.MassToDuality * impulse.Points[pointIndex].TangentMass;
			}
			else
				throw new ArgumentException("Local body is not part of the collision", "localBody");
		}
예제 #15
0
        public void PostSolve(Contact contact, ContactConstraint contactConstraint)
        {
            string levelUid = LevelSystem.currentLevelUid;
            List<int> characterEntities = _entityManager.getEntitiesPosessing(levelUid, ComponentType.CharacterMovement);
            int entityAId = (int)contact.FixtureA.Body.UserData;
            int entityBId = (int)contact.FixtureB.Body.UserData;
            CharacterMovementComponent characterMovementComponent = null;
            FixedArray2<Vector2> points;
            Vector2 normal;

            characterMovementComponent = (_entityManager.getComponent(levelUid, entityAId, ComponentType.CharacterMovement) ?? _entityManager.getComponent(levelUid, entityBId, ComponentType.CharacterMovement)) as CharacterMovementComponent;
            if (characterMovementComponent != null)
            {
                if (contact.FixtureA == characterMovementComponent.feetFixture || contact.FixtureB == characterMovementComponent.feetFixture)
                {
                    contact.GetWorldManifold(out normal, out points);
                    characterMovementComponent.collisionNormals.Add(normal);
                    //if (characterMovementComponent.allowJumpResetOnCollision)
                    //    characterMovementComponent.alreadyJumped = false;
                }
            }
        }
예제 #16
0
        public void PostSolve(Contact contact, ContactConstraint impulse)
        {
            for (int i=0; i<Cars.Count; i++){
                if (Cars[i].currentPowerup == Car.powerupBig) Cars[i]._compound.AngularVelocity *= 0.1f;
                if (Cars[i]._compound.FixtureList.Contains(contact.FixtureA) || Cars[i]._compound.FixtureList.Contains(contact.FixtureB))
                {
                    float maxImpulse = 0.0f;
                    int count = contact.Manifold.PointCount;

                    for (int j = 0; j < count; ++j)
                    {
                        maxImpulse = Math.Max(maxImpulse, impulse.Points[j].NormalImpulse);
                        if (Cars[i].currentPowerup == Car.powerupBig) maxImpulse /= 5;
                    }

                    float multiplicator = 1f;
            #if XBOX360
            multiplicator=2f;
            #endif

                    Cars[i].startVibrationTimer(Math.Min(maxImpulse*2f*multiplicator,1.0f));

                    if (maxImpulse > 1)
                    {

                        //randomly choose whether front or back

                        if (Cars[i]._compound.LinearVelocity.Length() > 2.0f)
                        {

                            int sign = 3;
                            if (Random.Next(2) == 1)
                            {
                                sign = -1;
                            }

                            Vector2 carTail = Cars[i]._compound.Position + sign * Cars[i].mDirection * Cars[i].tailOffset * 4;
                            Vector2 carDir = Cars[i].mDirection;
                            Vector2 carDirNormal = Vector2.Normalize(new Vector2(-carDir.Y, carDir.X));
                            int newIndex = Random.Next(collisionsQuotes.Count());
                            stringWriter.addString(collisionsQuotes[newIndex], Cars[i].mColor, maxImpulse / 5f, carTail - carDirNormal, carDirNormal);
                            Cars[i].resetTrail();

                            particleComponent.particleEmitterList[i].Position = Cars[i].Position;
                            particleComponent.particleEmitterList[i].Active = true;

                            // Play sound for car that crashed
                            soundManager.PlaySound(SoundManager.CarCrash);

                        }
                    }
                }
            }
        }
예제 #17
0
        public bool SolvePositionConstraints(float baumgarte)
        {
            float minSeparation = 0.0f;

            for (int i = 0; i < _constraintCount; ++i)
            {
                ContactConstraint c = Constraints[i];

                Body bodyA = c.BodyA;
                Body bodyB = c.BodyB;

                float invMassA = bodyA.Mass * bodyA.InvMass;
                float invIA    = bodyA.Mass * bodyA.InvI;
                float invMassB = bodyB.Mass * bodyB.InvMass;
                float invIB    = bodyB.Mass * bodyB.InvI;

                // Solve normal constraints
                for (int j = 0; j < c.PointCount; ++j)
                {
                    PositionSolverManifold psm = new PositionSolverManifold(ref c, j);
                    Vector2 normal             = psm.Normal;

                    Vector2 point      = psm.Point;
                    float   separation = psm.Separation;

                    Vector2 rA = point - bodyA.Sweep.c;
                    Vector2 rB = point - bodyB.Sweep.c;

                    // Track max constraint error.
                    minSeparation = Math.Min(minSeparation, separation);

                    // Prevent large corrections and allow slop.
                    float C = MathUtils.Clamp(baumgarte * (separation + Settings.LinearSlop),
                                              -Settings.MaxLinearCorrection, 0.0f);

                    // Compute the effective mass.
                    float rnA = MathUtils.Cross(rA, normal);
                    float rnB = MathUtils.Cross(rB, normal);
                    float K   = invMassA + invMassB + invIA * rnA * rnA + invIB * rnB * rnB;

                    // Compute normal impulse
                    float impulse = K > 0.0f ? -C / K : 0.0f;

#if MATH_OVERLOADS
                    Vector2 P = impulse * normal;

                    bodyA.Sweep.c -= invMassA * P;
                    bodyA.Sweep.a -= invIA * MathUtils.Cross(rA, P);

                    bodyB.Sweep.c += invMassB * P;
                    bodyB.Sweep.a += invIB * MathUtils.Cross(rB, P);
#else
                    Vector2 P = new Vector2(impulse * normal.X, impulse * normal.Y);

                    bodyA.Sweep.c.X -= invMassA * P.X;
                    bodyA.Sweep.c.Y -= invMassA * P.Y;
                    bodyA.Sweep.a   -= invIA * (rA.X * P.Y - rA.Y * P.X);

                    bodyB.Sweep.c.X += invMassB * P.X;
                    bodyB.Sweep.c.Y += invMassB * P.Y;
                    bodyB.Sweep.a   += invIB * (rB.X * P.Y - rB.Y * P.X);
#endif
                    bodyA.SynchronizeTransform();
                    bodyB.SynchronizeTransform();
                }
            }

            // We can't expect minSpeparation >= -Settings.b2_linearSlop because we don't
            // push the separation above -Settings.b2_linearSlop.
            return(minSeparation >= -1.5f * Settings.LinearSlop);
        }
 private void PostSolve(Contact contact, ContactConstraint contactConstraint)
 {
     if (contact.IsTouching())
     {
         float maxImpulse = 0.0f;
         for (int i = 0; i < contactConstraint.Manifold.PointCount; ++i)
             maxImpulse = Math.Max(maxImpulse, contactConstraint.Manifold.Points[i].NormalImpulse);
         if (maxImpulse >= 12)
         {
             IGameObject aGameObj = contactConstraint.BodyA.UserData as IGameObject,
                         bGameObj = contactConstraint.BodyB.UserData as IGameObject;
             if (aGameObj != null)
             {
                 aGameObj.Health -= maxImpulse;
                 if (aGameObj.Health <= 0)
                 {
                     RemoveObject(aGameObj);
                 }
             }
             if (bGameObj != null)
             {
                 bGameObj.Health -= maxImpulse;
                 if (bGameObj.Health <= 0)
                 {
                     RemoveObject(bGameObj);
                 }
             }
         }
     }
 }
        public void Reset(Contact[] contacts, int contactCount, float impulseRatio)
        {
            _contacts = contacts;

            _constraintCount = contactCount;

            // grow the array
            if (Constraints == null || Constraints.Length < _constraintCount)
            {
                Constraints = new ContactConstraint[_constraintCount * 2];

                for (int i = 0; i < _constraintCount * 2; i++)
                {
                    Constraints[i] = new ContactConstraint();
                }
            }

            for (int i = 0; i < _constraintCount; ++i)
            {
                Contact contact = contacts[i];

                Fixture fixtureA = contact.FixtureA;
                Fixture fixtureB = contact.FixtureB;
                Shape shapeA = fixtureA.Shape;
                Shape shapeB = fixtureB.Shape;
                float radiusA = shapeA.Radius;
                float radiusB = shapeB.Radius;
                Body bodyA = fixtureA.Body;
                Body bodyB = fixtureB.Body;
                Manifold manifold;
                contact.GetManifold(out manifold);

                float friction = Settings.MixFriction(fixtureA.Friction, fixtureB.Friction);
                float restitution = Settings.MixRestitution(fixtureA.Restitution, fixtureB.Restitution);

                Vector2 vA = bodyA.LinearVelocityInternal;
                Vector2 vB = bodyB.LinearVelocityInternal;
                float wA = bodyA.AngularVelocityInternal;
                float wB = bodyB.AngularVelocityInternal;

                Debug.Assert(manifold.PointCount > 0);

                WorldManifold worldManifold = new WorldManifold(ref manifold, ref bodyA.Xf, radiusA, ref bodyB.Xf,
                                                                radiusB);

                ContactConstraint cc = Constraints[i];
                cc.BodyA = bodyA;
                cc.BodyB = bodyB;
                cc.Manifold = manifold;
                cc.Normal = worldManifold.Normal;
                cc.PointCount = manifold.PointCount;
                cc.Friction = friction;

                cc.LocalNormal = manifold.LocalNormal;
                cc.LocalPoint = manifold.LocalPoint;
                cc.Radius = radiusA + radiusB;
                cc.Type = manifold.Type;

                for (int j = 0; j < cc.PointCount; ++j)
                {
                    ManifoldPoint cp = manifold.Points[j];
                    ContactConstraintPoint ccp = cc.Points[j];

                    ccp.NormalImpulse = impulseRatio * cp.NormalImpulse;
                    ccp.TangentImpulse = impulseRatio * cp.TangentImpulse;

                    ccp.LocalPoint = cp.LocalPoint;

                    ccp.rA = worldManifold.Points[j] - bodyA.Sweep.c;
                    ccp.rB = worldManifold.Points[j] - bodyB.Sweep.c;

            #if MATH_OVERLOADS
                    float rnA = MathUtils.Cross(ccp.rA, cc.Normal);
                    float rnB = MathUtils.Cross(ccp.rB, cc.Normal);
            #else
                    float rnA = ccp.rA.X * cc.Normal.Y - ccp.rA.Y * cc.Normal.X;
                    float rnB = ccp.rB.X * cc.Normal.Y - ccp.rB.Y * cc.Normal.X;
            #endif
                    rnA *= rnA;
                    rnB *= rnB;

                    float kNormal = bodyA.InvMass + bodyB.InvMass + bodyA.InvI * rnA + bodyB.InvI * rnB;

                    Debug.Assert(kNormal > Settings.Epsilon);
                    ccp.NormalMass = 1.0f / kNormal;

            #if MATH_OVERLOADS
                    Vector2 tangent = MathUtils.Cross(cc.Normal, 1.0f);

                    float rtA = MathUtils.Cross(ccp.rA, tangent);
                    float rtB = MathUtils.Cross(ccp.rB, tangent);
            #else
                    Vector2 tangent = new Vector2(cc.Normal.Y, -cc.Normal.X);

                    float rtA = ccp.rA.X * tangent.Y - ccp.rA.Y * tangent.X;
                    float rtB = ccp.rB.X * tangent.Y - ccp.rB.Y * tangent.X;
            #endif
                    rtA *= rtA;
                    rtB *= rtB;
                    float kTangent = bodyA.InvMass + bodyB.InvMass + bodyA.InvI * rtA + bodyB.InvI * rtB;

                    Debug.Assert(kTangent > Settings.Epsilon);
                    ccp.TangentMass = 1.0f / kTangent;

                    // Setup a velocity bias for restitution.
                    ccp.VelocityBias = 0.0f;
                    float vRel = Vector2.Dot(cc.Normal,
                                             vB + MathUtils.Cross(wB, ccp.rB) - vA - MathUtils.Cross(wA, ccp.rA));
                    if (vRel < -Settings.VelocityThreshold)
                    {
                        ccp.VelocityBias = -restitution * vRel;
                    }
                }

                // If we have two points, then prepare the block solver.
                if (cc.PointCount == 2)
                {
                    ContactConstraintPoint ccp1 = cc.Points[0];
                    ContactConstraintPoint ccp2 = cc.Points[1];

                    float invMassA = bodyA.InvMass;
                    float invIA = bodyA.InvI;
                    float invMassB = bodyB.InvMass;
                    float invIB = bodyB.InvI;

                    float rn1A = MathUtils.Cross(ccp1.rA, cc.Normal);
                    float rn1B = MathUtils.Cross(ccp1.rB, cc.Normal);
                    float rn2A = MathUtils.Cross(ccp2.rA, cc.Normal);
                    float rn2B = MathUtils.Cross(ccp2.rB, cc.Normal);

                    float k11 = invMassA + invMassB + invIA * rn1A * rn1A + invIB * rn1B * rn1B;
                    float k22 = invMassA + invMassB + invIA * rn2A * rn2A + invIB * rn2B * rn2B;
                    float k12 = invMassA + invMassB + invIA * rn1A * rn2A + invIB * rn1B * rn2B;

                    // Ensure a reasonable condition number.
                    const float k_maxConditionNumber = 100.0f;
                    if (k11 * k11 < k_maxConditionNumber * (k11 * k22 - k12 * k12))
                    {
                        // K is safe to invert.
                        cc.K = new Mat22(new Vector2(k11, k12), new Vector2(k12, k22));
                        cc.NormalMass = cc.K.Inverse;
                    }
                    else
                    {
                        // The constraints are redundant, just use one.
                        // TODO_ERIN use deepest?
                        cc.PointCount = 1;
                    }
                }

                if (fixtureA.PostSolve != null)
                    fixtureA.PostSolve(cc);

                if (fixtureB.PostSolve != null)
                    fixtureB.PostSolve(cc);
            }
        }
예제 #20
0
파일: Island.cs 프로젝트: scastle/Solitude
        private void Report(ContactConstraint[] constraints)
        {
            if (_contactManager == null)
            {
                return;
            }

            for (int i = 0; i < ContactCount; ++i)
            {
                Contact c = _contacts[i];

                ContactConstraint cc = constraints[i];

                ContactImpulse impulse = new ContactImpulse();
                for (int j = 0; j < cc.PointCount; ++j)
                {
                    impulse.NormalImpulses[j] = cc.Points[j].NormalImpulse;
                    impulse.TangentImpulses[j] = cc.Points[j].TangentImpulse;
                }

                _contactManager.PostSolve(c, ref impulse);
            }
        }
예제 #21
0
파일: Physics.cs 프로젝트: BaldMan82/iGL
 public void PostSolve(Contact contact, ContactConstraint impulse)
 {
 }
예제 #22
0
파일: Island.cs 프로젝트: mumumumu/SpeedUp
        private void Report(ContactConstraint[] constraints)
        {
            if (_contactManager == null)
                return;

            for (int i = 0; i < ContactCount && _contacts[i].FixtureA!=null&&_contacts[i].FixtureB!=null; ++i)
            {
                Contact c = _contacts[i];

                if (c.FixtureA.AfterCollision != null)
                    c.FixtureA.AfterCollision(c.FixtureA, c.FixtureB, c);

                if (c.FixtureB.AfterCollision != null)
                    c.FixtureB.AfterCollision(c.FixtureB, c.FixtureA, c);

                if (_contactManager.PostSolve != null)
                {
                    ContactConstraint cc = constraints[i];

                    _contactManager.PostSolve(c, cc);
                }
            }
        }
예제 #23
0
        private static void Solve(ContactConstraint cc, int index, out Vector2 normal, out Vector2 point,
                                  out float separation)
        {
            Debug.Assert(cc.PointCount > 0);

            normal = Vector2.Zero;

            switch (cc.Type)
            {
                case ManifoldType.Circles:
                    {
                        Vector2 pointA = cc.BodyA.GetWorldPoint(ref cc.LocalPoint);
                        Vector2 pointB = cc.BodyB.GetWorldPoint(ref cc.Points[0].LocalPoint);
                        float a = (pointA.X - pointB.X) * (pointA.X - pointB.X) +
                                  (pointA.Y - pointB.Y) * (pointA.Y - pointB.Y);
                        if (a > Settings.Epsilon * Settings.Epsilon)
                        {
                            Vector2 normalTmp = pointB - pointA;
                            float factor = 1f / (float)Math.Sqrt(normalTmp.X * normalTmp.X + normalTmp.Y * normalTmp.Y);
                            normal.X = normalTmp.X * factor;
                            normal.Y = normalTmp.Y * factor;
                        }
                        else
                        {
                            normal.X = 1;
                            normal.Y = 0;
                        }

                        point = 0.5f * (pointA + pointB);
                        separation = (pointB.X - pointA.X) * normal.X + (pointB.Y - pointA.Y) * normal.Y - cc.RadiusA -
                                     cc.RadiusB;
                    }
                    break;

                case ManifoldType.FaceA:
                    {
                        normal = cc.BodyA.GetWorldVector(ref cc.LocalNormal);
                        Vector2 planePoint = cc.BodyA.GetWorldPoint(ref cc.LocalPoint);
                        Vector2 clipPoint = cc.BodyB.GetWorldPoint(ref cc.Points[index].LocalPoint);
                        separation = (clipPoint.X - planePoint.X) * normal.X + (clipPoint.Y - planePoint.Y) * normal.Y -
                                     cc.RadiusA - cc.RadiusB;
                        point = clipPoint;
                    }
                    break;

                case ManifoldType.FaceB:
                    {
                        normal = cc.BodyB.GetWorldVector(ref cc.LocalNormal);
                        Vector2 planePoint = cc.BodyB.GetWorldPoint(ref cc.LocalPoint);

                        Vector2 clipPoint = cc.BodyA.GetWorldPoint(ref cc.Points[index].LocalPoint);
                        separation = (clipPoint.X - planePoint.X) * normal.X + (clipPoint.Y - planePoint.Y) * normal.Y -
                                     cc.RadiusA - cc.RadiusB;
                        point = clipPoint;

                        // Ensure normal points from A to B
                        normal = -normal;
                    }
                    break;
                default:
                    point = Vector2.Zero;
                    separation = 0.0f;
                    break;
            }
        }
        private void PostSolve(Contact contact, ContactConstraint impulse)
        {
            spriteA = (Sprite)contact.FixtureA.UserData;
            spriteB = (Sprite)contact.FixtureB.UserData;

            //code optimization
            if (spriteA.SpriteType == Sprite.Type.Terrain && spriteB.SpriteType == Sprite.Type.Terrain) return;

            //relativeVelocity = new Vector2(Math.Abs(contact.FixtureA.Body.LinearVelocity.X - contact.FixtureB.Body.LinearVelocity.X), Math.Abs(contact.FixtureA.Body.LinearVelocity.Y - contact.FixtureB.Body.LinearVelocity.Y));
            relativeVelocity = contact.FixtureA.Body.LinearVelocity - contact.FixtureB.Body.LinearVelocity;
            if (relativeVelocity.Length() < 1) damage = 0;
            else damage = GetDamage(impulse, spriteA, spriteB);

            switch (spriteA.SpriteType)
            {
                case Sprite.Type.Terrain:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage);
                        TerrainCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.Block:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage);
                        BlockCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.StaticBlock:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage);
                        StaticBlockCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.CannonBall:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage);
                        CannonBallCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.Fruit:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage);
                        FruitCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.Veggie:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage);
                        VeggieCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.Explosive:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage);
                        TNTCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.Switch:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage);
                        SwitchCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.Fan:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage);
                        FanCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.Gremlin:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage);
                        GremlinCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.Bird:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage);
                        BirdCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.Bat:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage);
                        BatCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.Fish:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage);
                        FishCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.Spider:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage);
                        SpiderCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.Snowman:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage);
                        SnowmanCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.Snowball:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage);
                        SnowballCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.FruitShot:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage);
                        FruitShotCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.VeggieShot:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage);
                        VeggieShotCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.ExplosiveShot:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage);
                        ExplosiveShotCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.CannonballShot:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage);
                        CannonBallShotCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.Boss:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage, contact.FixtureA.IsSensor);
                        BossCollision(this, postContactArgs);
                        return;
                    }
                default:
                    break;
            }

            switch (spriteB.SpriteType)
            {
                case Sprite.Type.Terrain:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage);
                        TerrainCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.Block:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage);
                        BlockCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.StaticBlock:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage);
                        StaticBlockCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.CannonBall:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage);
                        CannonBallCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.Fruit:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage);
                        FruitCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.Veggie:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage);
                        VeggieCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.Explosive:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage);
                        TNTCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.Switch:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage);
                        SwitchCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.Fan:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage);
                        FanCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.Gremlin:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage);
                        GremlinCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.Bird:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage);
                        BirdCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.Bat:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage);
                        BatCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.Fish:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage);
                        FishCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.Spider:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage);
                        SpiderCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.Snowman:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage);
                        SnowmanCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.Snowball:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage);
                        SnowballCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.FruitShot:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage);
                        FruitShotCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.VeggieShot:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage);
                        VeggieShotCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.ExplosiveShot:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage);
                        ExplosiveShotCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.CannonballShot:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage);
                        CannonBallShotCollision(this, postContactArgs);
                        return;
                    }
                case Sprite.Type.Boss:
                    {
                        PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage,contact.FixtureB.IsSensor);
                        BossCollision(this, postContactArgs);
                        return;
                    }
                default:
                    break;
            }

            return;
        }
예제 #25
0
        public void Reset(Contact[] contacts, int contactCount, float impulseRatio, bool warmstarting)
        {
            _contacts = contacts;

            _constraintCount = contactCount;

            // grow the array
            if (Constraints == null || Constraints.Length < _constraintCount)
            {
                Constraints = new ContactConstraint[_constraintCount * 2];

                for (int i = 0; i < Constraints.Length; i++)
                {
                    Constraints[i] = new ContactConstraint();
                }
            }

            // Initialize position independent portions of the constraints.
            for (int i = 0; i < _constraintCount; ++i)
            {
                Contact contact = contacts[i];

                Fixture fixtureA = contact.FixtureA;
                Fixture fixtureB = contact.FixtureB;
                Shape shapeA = fixtureA.Shape;
                Shape shapeB = fixtureB.Shape;
                float radiusA = shapeA.Radius;
                float radiusB = shapeB.Radius;
                Body bodyA = fixtureA.Body;
                Body bodyB = fixtureB.Body;
                Manifold manifold = contact.Manifold;

                Debug.Assert(manifold.PointCount > 0);

                ContactConstraint cc = Constraints[i];
                cc.Friction = Settings.MixFriction(fixtureA.Friction, fixtureB.Friction);
                cc.Restitution = Settings.MixRestitution(fixtureA.Restitution, fixtureB.Restitution);
                cc.BodyA = bodyA;
                cc.BodyB = bodyB;
                cc.Manifold = manifold;
                cc.Normal = Vector2.Zero;
                cc.PointCount = manifold.PointCount;

                cc.LocalNormal = manifold.LocalNormal;
                cc.LocalPoint = manifold.LocalPoint;
                cc.RadiusA = radiusA;
                cc.RadiusB = radiusB;
                cc.Type = manifold.Type;

                for (int j = 0; j < cc.PointCount; ++j)
                {
                    ManifoldPoint cp = manifold.Points[j];
                    ContactConstraintPoint ccp = cc.Points[j];

                    if (warmstarting)
                    {
                        ccp.NormalImpulse = impulseRatio * cp.NormalImpulse;
                        ccp.TangentImpulse = impulseRatio * cp.TangentImpulse;
                    }
                    else
                    {
                        ccp.NormalImpulse = 0.0f;
                        ccp.TangentImpulse = 0.0f;
                    }

                    ccp.LocalPoint = cp.LocalPoint;
                    ccp.rA = Vector2.Zero;
                    ccp.rB = Vector2.Zero;
                    ccp.NormalMass = 0.0f;
                    ccp.TangentMass = 0.0f;
                    ccp.VelocityBias = 0.0f;
                }

                cc.K.SetZero();
                cc.NormalMass.SetZero();
            }
        }
예제 #26
0
        private static void Solve(ContactConstraint cc, int index, out Vector2 normal, out Vector2 point,
                                  out float separation)
        {
            Debug.Assert(cc.PointCount > 0);

            normal = Vector2.Zero;

            switch (cc.Type)
            {
            case ManifoldType.Circles:
            {
                Vector2 pointA = cc.BodyA.GetWorldPoint(ref cc.LocalPoint);
                Vector2 pointB = cc.BodyB.GetWorldPoint(ref cc.Points[0].LocalPoint);
                float   a      = (pointA.X - pointB.X) * (pointA.X - pointB.X) +
                                 (pointA.Y - pointB.Y) * (pointA.Y - pointB.Y);
                if (a > Settings.Epsilon * Settings.Epsilon)
                {
                    Vector2 normalTmp = pointB - pointA;
                    float   factor    = 1f / (float)Math.Sqrt(normalTmp.X * normalTmp.X + normalTmp.Y * normalTmp.Y);
                    normal.X = normalTmp.X * factor;
                    normal.Y = normalTmp.Y * factor;
                }
                else
                {
                    normal.X = 1;
                    normal.Y = 0;
                }

                point      = 0.5f * (pointA + pointB);
                separation = (pointB.X - pointA.X) * normal.X + (pointB.Y - pointA.Y) * normal.Y - cc.RadiusA -
                             cc.RadiusB;
            }
            break;

            case ManifoldType.FaceA:
            {
                normal = cc.BodyA.GetWorldVector(ref cc.LocalNormal);
                Vector2 planePoint = cc.BodyA.GetWorldPoint(ref cc.LocalPoint);
                Vector2 clipPoint  = cc.BodyB.GetWorldPoint(ref cc.Points[index].LocalPoint);
                separation = (clipPoint.X - planePoint.X) * normal.X + (clipPoint.Y - planePoint.Y) * normal.Y -
                             cc.RadiusA - cc.RadiusB;
                point = clipPoint;
            }
            break;

            case ManifoldType.FaceB:
            {
                normal = cc.BodyB.GetWorldVector(ref cc.LocalNormal);
                Vector2 planePoint = cc.BodyB.GetWorldPoint(ref cc.LocalPoint);

                Vector2 clipPoint = cc.BodyA.GetWorldPoint(ref cc.Points[index].LocalPoint);
                separation = (clipPoint.X - planePoint.X) * normal.X + (clipPoint.Y - planePoint.Y) * normal.Y -
                             cc.RadiusA - cc.RadiusB;
                point = clipPoint;

                // Ensure normal points from A to B
                normal = -normal;
            }
            break;

            default:
                point      = Vector2.Zero;
                separation = 0.0f;
                break;
            }
        }
예제 #27
0
        public void SolveVelocityConstraints()
        {
            for (int i = 0; i < _constraintCount; ++i)
            {
                ContactConstraint c = Constraints[i];
                Body    bodyA       = c.BodyA;
                Body    bodyB       = c.BodyB;
                float   wA          = bodyA.AngularVelocityInternal;
                float   wB          = bodyB.AngularVelocityInternal;
                Vector2 vA          = bodyA.LinearVelocityInternal;
                Vector2 vB          = bodyB.LinearVelocityInternal;
                float   invMassA    = bodyA.InvMass;
                float   invIA       = bodyA.InvI;
                float   invMassB    = bodyB.InvMass;
                float   invIB       = bodyB.InvI;
                Vector2 normal      = c.Normal;

#if MATH_OVERLOADS
                Vector2 tangent = MathUtils.Cross(normal, 1.0f);
#else
                Vector2 tangent = new Vector2(normal.Y, -normal.X);
#endif
                float friction = c.Friction;

                Debug.Assert(c.PointCount == 1 || c.PointCount == 2);

                // Solve tangent constraints
                for (int j = 0; j < c.PointCount; ++j)
                {
                    ContactConstraintPoint ccp = c.Points[j];

#if MATH_OVERLOADS
                    // Relative velocity at contact
                    Vector2 dv = vB + MathUtils.Cross(wB, ccp.rB) - vA - MathUtils.Cross(wA, ccp.rA);

                    // Compute tangent force
                    float vt = Vector2.Dot(dv, tangent);
#else
                    // Relative velocity at contact
                    Vector2 dv = new Vector2(vB.X + (-wB * ccp.rB.Y) - vA.X - (-wA * ccp.rA.Y),
                                             vB.Y + (wB * ccp.rB.X) - vA.Y - (wA * ccp.rA.X));

                    // Compute tangent force
                    float vt = dv.X * tangent.X + dv.Y * tangent.Y;
#endif
                    float lambda = ccp.TangentMass * (-vt);

                    // MathUtils.Clamp the accumulated force
                    float maxFriction = friction * ccp.NormalImpulse;
                    float newImpulse  = MathUtils.Clamp(ccp.TangentImpulse + lambda, -maxFriction, maxFriction);
                    lambda = newImpulse - ccp.TangentImpulse;

#if MATH_OVERLOADS
                    // Apply contact impulse
                    Vector2 P = lambda * tangent;

                    vA -= invMassA * P;
                    wA -= invIA * MathUtils.Cross(ccp.rA, P);

                    vB += invMassB * P;
                    wB += invIB * MathUtils.Cross(ccp.rB, P);
#else
                    // Apply contact impulse
                    Vector2 P = new Vector2(lambda * tangent.X, lambda * tangent.Y);

                    vA.X -= invMassA * P.X;
                    vA.Y -= invMassA * P.Y;
                    wA   -= invIA * (ccp.rA.X * P.Y - ccp.rA.Y * P.X);

                    vB.X += invMassB * P.X;
                    vB.Y += invMassB * P.Y;
                    wB   += invIB * (ccp.rB.X * P.Y - ccp.rB.Y * P.X);
#endif
                    ccp.TangentImpulse = newImpulse;
                }

                // Solve normal constraints
                if (c.PointCount == 1)
                {
                    ContactConstraintPoint ccp = c.Points[0];

#if MATH_OVERLOADS
                    // Relative velocity at contact
                    Vector2 dv = vB + MathUtils.Cross(wB, ccp.rB) - vA - MathUtils.Cross(wA, ccp.rA);

                    // Compute normal impulse
                    float vn     = Vector2.Dot(dv, normal);
                    float lambda = -ccp.NormalMass * (vn - ccp.VelocityBias);

                    // MathUtils.Clamp the accumulated impulse
                    float newImpulse = Math.Max(ccp.NormalImpulse + lambda, 0.0f);
                    lambda = newImpulse - ccp.NormalImpulse;

                    // Apply contact impulse
                    Vector2 P = lambda * normal;
                    vA -= invMassA * P;
                    wA -= invIA * MathUtils.Cross(ccp.rA, P);

                    vB += invMassB * P;
                    wB += invIB * MathUtils.Cross(ccp.rB, P);
#else
                    // Relative velocity at contact
                    Vector2 dv = new Vector2(vB.X + (-wB * ccp.rB.Y) - vA.X - (-wA * ccp.rA.Y),
                                             vB.Y + (wB * ccp.rB.X) - vA.Y - (wA * ccp.rA.X));

                    // Compute normal impulse
                    float vn     = dv.X * normal.X + dv.Y * normal.Y;
                    float lambda = -ccp.NormalMass * (vn - ccp.VelocityBias);

                    // Clamp the accumulated impulse
                    float newImpulse = Math.Max(ccp.NormalImpulse + lambda, 0.0f);
                    lambda = newImpulse - ccp.NormalImpulse;

                    // Apply contact impulse
                    var P = new Vector2(lambda * normal.X, lambda * normal.Y);

                    vA.X -= invMassA * P.X;
                    vA.Y -= invMassA * P.Y;
                    wA   -= invIA * (ccp.rA.X * P.Y - ccp.rA.Y * P.X);

                    vB.X += invMassB * P.X;
                    vB.Y += invMassB * P.Y;
                    wB   += invIB * (ccp.rB.X * P.Y - ccp.rB.Y * P.X);
#endif
                    ccp.NormalImpulse = newImpulse;
                }
                else
                {
                    // Block solver developed in collaboration with Dirk Gregorius (back in 01/07 on Box2D_Lite).
                    // Build the mini LCP for this contact patch
                    //
                    // vn = A * x + b, vn >= 0, , vn >= 0, x >= 0 and vn_i * x_i = 0 with i = 1..2
                    //
                    // A = J * W * JT and J = ( -n, -r1 x n, n, r2 x n )
                    // b = vn_0 - velocityBias
                    //
                    // The system is solved using the "Total enumeration method" (s. Murty). The complementary constraint vn_i * x_i
                    // implies that we must have in any solution either vn_i = 0 or x_i = 0. So for the 2D contact problem the cases
                    // vn1 = 0 and vn2 = 0, x1 = 0 and x2 = 0, x1 = 0 and vn2 = 0, x2 = 0 and vn1 = 0 need to be tested. The first valid
                    // solution that satisfies the problem is chosen.
                    //
                    // In order to account of the accumulated impulse 'a' (because of the iterative nature of the solver which only requires
                    // that the accumulated impulse is clamped and not the incremental impulse) we change the impulse variable (x_i).
                    //
                    // Substitute:
                    //
                    // x = x' - a
                    //
                    // Plug into above equation:
                    //
                    // vn = A * x + b
                    //    = A * (x' - a) + b
                    //    = A * x' + b - A * a
                    //    = A * x' + b'
                    // b' = b - A * a;

                    ContactConstraintPoint cp1 = c.Points[0];
                    ContactConstraintPoint cp2 = c.Points[1];

                    Vector2 a = new Vector2(cp1.NormalImpulse, cp2.NormalImpulse);
                    Debug.Assert(a.X >= 0.0f && a.Y >= 0.0f);

#if MATH_OVERLOADS
                    // Relative velocity at contact
                    Vector2 dv1 = vB + MathUtils.Cross(wB, cp1.rB) - vA - MathUtils.Cross(wA, cp1.rA);
                    Vector2 dv2 = vB + MathUtils.Cross(wB, cp2.rB) - vA - MathUtils.Cross(wA, cp2.rA);

                    // Compute normal velocity
                    float vn1 = Vector2.Dot(dv1, normal);
                    float vn2 = Vector2.Dot(dv2, normal);

                    Vector2 b = new Vector2(vn1 - cp1.VelocityBias, vn2 - cp2.VelocityBias);
                    b -= MathUtils.Multiply(ref c.K, a);
#else
                    // Relative velocity at contact
                    Vector2 dv1 = new Vector2(vB.X + (-wB * cp1.rB.Y) - vA.X - (-wA * cp1.rA.Y),
                                              vB.Y + (wB * cp1.rB.X) - vA.Y - (wA * cp1.rA.X));
                    Vector2 dv2 = new Vector2(vB.X + (-wB * cp2.rB.Y) - vA.X - (-wA * cp2.rA.Y),
                                              vB.Y + (wB * cp2.rB.X) - vA.Y - (wA * cp2.rA.X));

                    // Compute normal velocity
                    float vn1 = dv1.X * normal.X + dv1.Y * normal.Y;
                    float vn2 = dv2.X * normal.X + dv2.Y * normal.Y;

                    Vector2 b = new Vector2(vn1 - cp1.VelocityBias, vn2 - cp2.VelocityBias);
                    b -= MathUtils.Multiply(ref c.K, ref a); // Inlining didn't help for the multiply.
#endif
                    while (true)
                    {
                        //
                        // Case 1: vn = 0
                        //
                        // 0 = A * x' + b'
                        //
                        // Solve for x':
                        //
                        // x' = - inv(A) * b'
                        //
                        Vector2 x = -MathUtils.Multiply(ref c.NormalMass, ref b);

                        if (x.X >= 0.0f && x.Y >= 0.0f)
                        {
#if MATH_OVERLOADS
                            // Resubstitute for the incremental impulse
                            Vector2 d = x - a;

                            // Apply incremental impulse
                            Vector2 P1 = d.X * normal;
                            Vector2 P2 = d.Y * normal;
                            vA -= invMassA * (P1 + P2);
                            wA -= invIA * (MathUtils.Cross(cp1.rA, P1) + MathUtils.Cross(cp2.rA, P2));

                            vB += invMassB * (P1 + P2);
                            wB += invIB * (MathUtils.Cross(cp1.rB, P1) + MathUtils.Cross(cp2.rB, P2));
#else
                            // Resubstitute for the incremental impulse
                            Vector2 d = new Vector2(x.X - a.X, x.Y - a.Y);

                            // Apply incremental impulse
                            Vector2 P1  = new Vector2(d.X * normal.X, d.X * normal.Y);
                            Vector2 P2  = new Vector2(d.Y * normal.X, d.Y * normal.Y);
                            Vector2 P12 = new Vector2(P1.X + P2.X, P1.Y + P2.Y);

                            vA.X -= invMassA * P12.X;
                            vA.Y -= invMassA * P12.Y;
                            wA   -= invIA * ((cp1.rA.X * P1.Y - cp1.rA.Y * P1.X) + (cp2.rA.X * P2.Y - cp2.rA.Y * P2.X));

                            vB.X += invMassB * P12.X;
                            vB.Y += invMassB * P12.Y;
                            wB   += invIB * ((cp1.rB.X * P1.Y - cp1.rB.Y * P1.X) + (cp2.rB.X * P2.Y - cp2.rB.Y * P2.X));
#endif
                            // Accumulate
                            cp1.NormalImpulse = x.X;
                            cp2.NormalImpulse = x.Y;

#if B2_DEBUG_SOLVER
                            float k_errorTol = 1e-3f;

                            // Postconditions
                            dv1 = vB + MathUtils.Cross(wB, cp1.rB) - vA - MathUtils.Cross(wA, cp1.rA);
                            dv2 = vB + MathUtils.Cross(wB, cp2.rB) - vA - MathUtils.Cross(wA, cp2.rA);

                            // Compute normal velocity
                            vn1 = Vector2.Dot(dv1, normal);
                            vn2 = Vector2.Dot(dv2, normal);

                            Debug.Assert(MathUtils.Abs(vn1 - cp1.velocityBias) < k_errorTol);
                            Debug.Assert(MathUtils.Abs(vn2 - cp2.velocityBias) < k_errorTol);
#endif
                            break;
                        }

                        //
                        // Case 2: vn1 = 0 and x2 = 0
                        //
                        //   0 = a11 * x1' + a12 * 0 + b1'
                        // vn2 = a21 * x1' + a22 * 0 + b2'
                        //
                        x.X = -cp1.NormalMass * b.X;
                        x.Y = 0.0f;
                        vn1 = 0.0f;
                        vn2 = c.K.col1.Y * x.X + b.Y;

                        if (x.X >= 0.0f && vn2 >= 0.0f)
                        {
#if MATH_OVERLOADS
                            // Resubstitute for the incremental impulse
                            Vector2 d = x - a;

                            // Apply incremental impulse
                            Vector2 P1 = d.X * normal;
                            Vector2 P2 = d.Y * normal;
                            vA -= invMassA * (P1 + P2);
                            wA -= invIA * (MathUtils.Cross(cp1.rA, P1) + MathUtils.Cross(cp2.rA, P2));

                            vB += invMassB * (P1 + P2);
                            wB += invIB * (MathUtils.Cross(cp1.rB, P1) + MathUtils.Cross(cp2.rB, P2));
#else
                            // Resubstitute for the incremental impulse
                            Vector2 d = new Vector2(x.X - a.X, x.Y - a.Y);

                            // Apply incremental impulse
                            Vector2 P1  = new Vector2(d.X * normal.X, d.X * normal.Y);
                            Vector2 P2  = new Vector2(d.Y * normal.X, d.Y * normal.Y);
                            Vector2 P12 = new Vector2(P1.X + P2.X, P1.Y + P2.Y);

                            vA.X -= invMassA * P12.X;
                            vA.Y -= invMassA * P12.Y;
                            wA   -= invIA * ((cp1.rA.X * P1.Y - cp1.rA.Y * P1.X) + (cp2.rA.X * P2.Y - cp2.rA.Y * P2.X));

                            vB.X += invMassB * P12.X;
                            vB.Y += invMassB * P12.Y;
                            wB   += invIB * ((cp1.rB.X * P1.Y - cp1.rB.Y * P1.X) + (cp2.rB.X * P2.Y - cp2.rB.Y * P2.X));
#endif
                            // Accumulate
                            cp1.NormalImpulse = x.X;
                            cp2.NormalImpulse = x.Y;

#if B2_DEBUG_SOLVER
                            // Postconditions
                            dv1 = vB + MathUtils.Cross(wB, cp1.rB) - vA - MathUtils.Cross(wA, cp1.rA);

                            // Compute normal velocity
                            vn1 = Vector2.Dot(dv1, normal);

                            Debug.Assert(MathUtils.Abs(vn1 - cp1.velocityBias) < k_errorTol);
#endif
                            break;
                        }


                        //
                        // Case 3: vn2 = 0 and x1 = 0
                        //
                        // vn1 = a11 * 0 + a12 * x2' + b1'
                        //   0 = a21 * 0 + a22 * x2' + b2'
                        //
                        x.X = 0.0f;
                        x.Y = -cp2.NormalMass * b.Y;
                        vn1 = c.K.col2.X * x.Y + b.X;
                        vn2 = 0.0f;

                        if (x.Y >= 0.0f && vn1 >= 0.0f)
                        {
#if MATH_OVERLOADS
                            // Resubstitute for the incremental impulse
                            Vector2 d = x - a;

                            // Apply incremental impulse
                            Vector2 P1 = d.X * normal;
                            Vector2 P2 = d.Y * normal;
                            vA -= invMassA * (P1 + P2);
                            wA -= invIA * (MathUtils.Cross(cp1.rA, P1) + MathUtils.Cross(cp2.rA, P2));

                            vB += invMassB * (P1 + P2);
                            wB += invIB * (MathUtils.Cross(cp1.rB, P1) + MathUtils.Cross(cp2.rB, P2));
#else
                            // Resubstitute for the incremental impulse
                            Vector2 d = new Vector2(x.X - a.X, x.Y - a.Y);

                            // Apply incremental impulse
                            Vector2 P1  = new Vector2(d.X * normal.X, d.X * normal.Y);
                            Vector2 P2  = new Vector2(d.Y * normal.X, d.Y * normal.Y);
                            Vector2 P12 = new Vector2(P1.X + P2.X, P1.Y + P2.Y);

                            vA.X -= invMassA * P12.X;
                            vA.Y -= invMassA * P12.Y;
                            wA   -= invIA * ((cp1.rA.X * P1.Y - cp1.rA.Y * P1.X) + (cp2.rA.X * P2.Y - cp2.rA.Y * P2.X));

                            vB.X += invMassB * P12.X;
                            vB.Y += invMassB * P12.Y;
                            wB   += invIB * ((cp1.rB.X * P1.Y - cp1.rB.Y * P1.X) + (cp2.rB.X * P2.Y - cp2.rB.Y * P2.X));
#endif
                            // Accumulate
                            cp1.NormalImpulse = x.X;
                            cp2.NormalImpulse = x.Y;

#if B2_DEBUG_SOLVER
                            // Postconditions
                            dv2 = vB + MathUtils.Cross(wB, cp2.rB) - vA - MathUtils.Cross(wA, cp2.rA);

                            // Compute normal velocity
                            vn2 = Vector2.Dot(dv2, normal);

                            Debug.Assert(MathUtils.Abs(vn2 - cp2.velocityBias) < k_errorTol);
#endif
                            break;
                        }

                        //
                        // Case 4: x1 = 0 and x2 = 0
                        //
                        // vn1 = b1
                        // vn2 = b2;
                        x.X = 0.0f;
                        x.Y = 0.0f;
                        vn1 = b.X;
                        vn2 = b.Y;

                        if (vn1 >= 0.0f && vn2 >= 0.0f)
                        {
#if MATH_OVERLOADS
                            // Resubstitute for the incremental impulse
                            Vector2 d = x - a;

                            // Apply incremental impulse
                            Vector2 P1 = d.X * normal;
                            Vector2 P2 = d.Y * normal;
                            vA -= invMassA * (P1 + P2);
                            wA -= invIA * (MathUtils.Cross(cp1.rA, P1) + MathUtils.Cross(cp2.rA, P2));

                            vB += invMassB * (P1 + P2);
                            wB += invIB * (MathUtils.Cross(cp1.rB, P1) + MathUtils.Cross(cp2.rB, P2));
#else
                            // Resubstitute for the incremental impulse
                            Vector2 d = new Vector2(x.X - a.X, x.Y - a.Y);

                            // Apply incremental impulse
                            Vector2 P1  = new Vector2(d.X * normal.X, d.X * normal.Y);
                            Vector2 P2  = new Vector2(d.Y * normal.X, d.Y * normal.Y);
                            Vector2 P12 = new Vector2(P1.X + P2.X, P1.Y + P2.Y);

                            vA.X -= invMassA * P12.X;
                            vA.Y -= invMassA * P12.Y;
                            wA   -= invIA * ((cp1.rA.X * P1.Y - cp1.rA.Y * P1.X) + (cp2.rA.X * P2.Y - cp2.rA.Y * P2.X));

                            vB.X += invMassB * P12.X;
                            vB.Y += invMassB * P12.Y;
                            wB   += invIB * ((cp1.rB.X * P1.Y - cp1.rB.Y * P1.X) + (cp2.rB.X * P2.Y - cp2.rB.Y * P2.X));
#endif
                            // Accumulate
                            cp1.NormalImpulse = x.X;
                            cp2.NormalImpulse = x.Y;

                            break;
                        }

                        // No solution, give up. This is hit sometimes, but it doesn't seem to matter.
                        break;
                    }
                }

                bodyA.LinearVelocityInternal  = vA;
                bodyA.AngularVelocityInternal = wA;
                bodyB.LinearVelocityInternal  = vB;
                bodyB.AngularVelocityInternal = wB;
            }
        }
예제 #28
0
        // Sequential position solver for position constraints.
        public bool SolvePositionConstraintsTOI(float baumgarte, Body toiBodyA, Body toiBodyB)
        {
            float minSeparation = 0.0f;

            for (int i = 0; i < _constraintCount; ++i)
            {
                ContactConstraint c = Constraints[i];
                Body bodyA          = c.BodyA;
                Body bodyB          = c.BodyB;

                float massA = 0.0f;
                if (bodyA == toiBodyA || bodyA == toiBodyB)
                {
                    massA = bodyA.Mass;
                }

                float massB = 0.0f;
                if (bodyB == toiBodyA || bodyB == toiBodyB)
                {
                    massB = bodyB.Mass;
                }

                float invMassA = bodyA.Mass * bodyA.InvMass;
                float invIA    = bodyA.Mass * bodyA.InvI;
                float invMassB = bodyB.Mass * bodyB.InvMass;
                float invIB    = bodyB.Mass * bodyB.InvI;

                // Solve normal constraints
                for (int j = 0; j < c.PointCount; ++j)
                {
                    Vector2 normal;
                    Vector2 point;
                    float   separation;

                    PositionSolverManifold.Solve(ref c, j, out normal, out point, out separation);

                    Vector2 rA; //= point - bodyA.Sweep.C);
                    Vector2.Subtract(ref point, ref bodyA.Sweep.C, out rA);
                    Vector2 rB; // = point - bodyB.Sweep.C;
                    Vector2.Subtract(ref point, ref bodyB.Sweep.C, out rB);

                    // Track max constraint error.
                    minSeparation = Math.Min(minSeparation, separation);

                    // Prevent large corrections and allow slop.
                    float C = MathUtils.Clamp(baumgarte * (separation + Settings.LinearSlop), -Settings.MaxLinearCorrection, 0.0f);

                    // Compute the effective mass.
                    float rnA = rA.X * normal.Y - rA.Y * normal.X;
                    float rnB = rB.X * normal.Y - rB.Y * normal.X;
                    float K   = invMassA + invMassB + invIA * rnA * rnA + invIB * rnB * rnB;

                    // Compute normal impulse
                    float impulse = K > 0.0f ? -C / K : 0.0f;

                    Vector2 P = impulse * normal;

                    bodyA.Sweep.C -= invMassA * P;
                    bodyA.Sweep.A -= invIA * (rA.X * P.Y - rA.Y * P.X);
                    bodyA.SynchronizeTransform();

                    bodyB.Sweep.C += invMassB * P;
                    bodyB.Sweep.A += invIB * (rB.X * P.Y - rB.Y * P.X);
                    bodyB.SynchronizeTransform();
                }
            }

            // We can't expect minSpeparation >= -b2_linearSlop because we don't
            // push the separation above -b2_linearSlop.
            return(minSeparation >= -1.5f * Settings.LinearSlop);
        }
예제 #29
0
        public void Reset(Contact[] contacts, int contactCount, float impulseRatio)
        {
            _contacts = contacts;

            _constraintCount = contactCount;

            // grow the array
            if (Constraints == null || Constraints.Length < _constraintCount)
            {
                Constraints = new ContactConstraint[_constraintCount * 2];

                for (int i = 0; i < _constraintCount * 2; i++)
                {
                    Constraints[i] = new ContactConstraint();
                }
            }

            for (int i = 0; i < _constraintCount; ++i)
            {
                Contact contact = contacts[i];

                Fixture  fixtureA = contact.FixtureA;
                Fixture  fixtureB = contact.FixtureB;
                Shape    shapeA   = fixtureA.Shape;
                Shape    shapeB   = fixtureB.Shape;
                float    radiusA  = shapeA.Radius;
                float    radiusB  = shapeB.Radius;
                Body     bodyA    = fixtureA.Body;
                Body     bodyB    = fixtureB.Body;
                Manifold manifold;
                contact.GetManifold(out manifold);

                float friction    = Settings.MixFriction(fixtureA.Friction, fixtureB.Friction);
                float restitution = Settings.MixRestitution(fixtureA.Restitution, fixtureB.Restitution);

                Vector2 vA = bodyA.LinearVelocityInternal;
                Vector2 vB = bodyB.LinearVelocityInternal;
                float   wA = bodyA.AngularVelocityInternal;
                float   wB = bodyB.AngularVelocityInternal;

                Debug.Assert(manifold.PointCount > 0);

                WorldManifold worldManifold = new WorldManifold(ref manifold, ref bodyA.Xf, radiusA, ref bodyB.Xf,
                                                                radiusB);

                ContactConstraint cc = Constraints[i];
                cc.BodyA      = bodyA;
                cc.BodyB      = bodyB;
                cc.Manifold   = manifold;
                cc.Normal     = worldManifold.Normal;
                cc.PointCount = manifold.PointCount;
                cc.Friction   = friction;

                cc.LocalNormal = manifold.LocalNormal;
                cc.LocalPoint  = manifold.LocalPoint;
                cc.Radius      = radiusA + radiusB;
                cc.Type        = manifold.Type;

                for (int j = 0; j < cc.PointCount; ++j)
                {
                    ManifoldPoint          cp  = manifold.Points[j];
                    ContactConstraintPoint ccp = cc.Points[j];

                    ccp.NormalImpulse  = impulseRatio * cp.NormalImpulse;
                    ccp.TangentImpulse = impulseRatio * cp.TangentImpulse;

                    ccp.LocalPoint = cp.LocalPoint;

                    ccp.rA = worldManifold.Points[j] - bodyA.Sweep.c;
                    ccp.rB = worldManifold.Points[j] - bodyB.Sweep.c;

#if MATH_OVERLOADS
                    float rnA = MathUtils.Cross(ccp.rA, cc.Normal);
                    float rnB = MathUtils.Cross(ccp.rB, cc.Normal);
#else
                    float rnA = ccp.rA.X * cc.Normal.Y - ccp.rA.Y * cc.Normal.X;
                    float rnB = ccp.rB.X * cc.Normal.Y - ccp.rB.Y * cc.Normal.X;
#endif
                    rnA *= rnA;
                    rnB *= rnB;

                    float kNormal = bodyA.InvMass + bodyB.InvMass + bodyA.InvI * rnA + bodyB.InvI * rnB;

                    Debug.Assert(kNormal > Settings.Epsilon);
                    ccp.NormalMass = 1.0f / kNormal;

#if MATH_OVERLOADS
                    Vector2 tangent = MathUtils.Cross(cc.Normal, 1.0f);

                    float rtA = MathUtils.Cross(ccp.rA, tangent);
                    float rtB = MathUtils.Cross(ccp.rB, tangent);
#else
                    Vector2 tangent = new Vector2(cc.Normal.Y, -cc.Normal.X);

                    float rtA = ccp.rA.X * tangent.Y - ccp.rA.Y * tangent.X;
                    float rtB = ccp.rB.X * tangent.Y - ccp.rB.Y * tangent.X;
#endif
                    rtA *= rtA;
                    rtB *= rtB;
                    float kTangent = bodyA.InvMass + bodyB.InvMass + bodyA.InvI * rtA + bodyB.InvI * rtB;

                    Debug.Assert(kTangent > Settings.Epsilon);
                    ccp.TangentMass = 1.0f / kTangent;

                    // Setup a velocity bias for restitution.
                    ccp.VelocityBias = 0.0f;
                    float vRel = Vector2.Dot(cc.Normal,
                                             vB + MathUtils.Cross(wB, ccp.rB) - vA - MathUtils.Cross(wA, ccp.rA));
                    if (vRel < -Settings.VelocityThreshold)
                    {
                        ccp.VelocityBias = -restitution * vRel;
                    }
                }

                // If we have two points, then prepare the block solver.
                if (cc.PointCount == 2)
                {
                    ContactConstraintPoint ccp1 = cc.Points[0];
                    ContactConstraintPoint ccp2 = cc.Points[1];

                    float invMassA = bodyA.InvMass;
                    float invIA    = bodyA.InvI;
                    float invMassB = bodyB.InvMass;
                    float invIB    = bodyB.InvI;

                    float rn1A = MathUtils.Cross(ccp1.rA, cc.Normal);
                    float rn1B = MathUtils.Cross(ccp1.rB, cc.Normal);
                    float rn2A = MathUtils.Cross(ccp2.rA, cc.Normal);
                    float rn2B = MathUtils.Cross(ccp2.rB, cc.Normal);

                    float k11 = invMassA + invMassB + invIA * rn1A * rn1A + invIB * rn1B * rn1B;
                    float k22 = invMassA + invMassB + invIA * rn2A * rn2A + invIB * rn2B * rn2B;
                    float k12 = invMassA + invMassB + invIA * rn1A * rn2A + invIB * rn1B * rn2B;

                    // Ensure a reasonable condition number.
                    const float k_maxConditionNumber = 100.0f;
                    if (k11 * k11 < k_maxConditionNumber * (k11 * k22 - k12 * k12))
                    {
                        // K is safe to invert.
                        cc.K          = new Mat22(new Vector2(k11, k12), new Vector2(k12, k22));
                        cc.NormalMass = cc.K.Inverse;
                    }
                    else
                    {
                        // The constraints are redundant, just use one.
                        // TODO_ERIN use deepest?
                        cc.PointCount = 1;
                    }
                }

                if (fixtureA.PostSolve != null)
                {
                    fixtureA.PostSolve(cc);
                }

                if (fixtureB.PostSolve != null)
                {
                    fixtureB.PostSolve(cc);
                }
            }
        }
        public int GetDamage(ContactConstraint impulse, Sprite spriteA, Sprite spriteB)
        {
            float maxImpulse = 0;
            int damage = 0;
            for (int i = 0; i < impulse.Manifold.PointCount; ++i)
            {
                maxImpulse = Math.Max(0, impulse.Manifold.Points[i].NormalImpulse);
            }
            damage = (int)((maxImpulse - 1f) * GameSettings.DamageMultiplier); //the -1 reduces bumping around and may need to be adjusted)

            if (damage > spriteA.HitPoints) damage = spriteA.HitPoints;
            if (damage > spriteB.HitPoints) damage = spriteB.HitPoints;

            if (damage < 0) return 0;
            else return damage;
        }
예제 #31
0
        public void InitializeVelocityConstraints()
        {
            for (int i = 0; i < this._constraintCount; ++i)
            {
                ContactConstraint cc = this.Constraints[i];

                float    radiusA  = cc.RadiusA;
                float    radiusB  = cc.RadiusB;
                Body     bodyA    = cc.BodyA;
                Body     bodyB    = cc.BodyB;
                Manifold manifold = cc.Manifold;

                Vector2 vA = bodyA.LinearVelocity;
                Vector2 vB = bodyB.LinearVelocity;
                float   wA = bodyA.AngularVelocity;
                float   wB = bodyB.AngularVelocity;

                Debug.Assert(manifold.PointCount > 0);
                FixedArray2 <Vector2> points;

                Collision.Collision.GetWorldManifold(ref manifold, ref bodyA.Xf, radiusA, ref bodyB.Xf, radiusB,
                                                     out cc.Normal, out points);
                Vector2 tangent = new Vector2(cc.Normal.Y, -cc.Normal.X);

                for (int j = 0; j < cc.PointCount; ++j)
                {
                    ContactConstraintPoint ccp = cc.Points[j];

                    ccp.rA = points[j] - bodyA.Sweep.C;
                    ccp.rB = points[j] - bodyB.Sweep.C;

                    float rnA = ccp.rA.X * cc.Normal.Y - ccp.rA.Y * cc.Normal.X;
                    float rnB = ccp.rB.X * cc.Normal.Y - ccp.rB.Y * cc.Normal.X;
                    rnA *= rnA;
                    rnB *= rnB;

                    float kNormal = bodyA.InvMass + bodyB.InvMass + bodyA.InvI * rnA + bodyB.InvI * rnB;

                    Debug.Assert(kNormal > Settings.Epsilon);
                    ccp.NormalMass = 1.0f / kNormal;

                    float rtA = ccp.rA.X * tangent.Y - ccp.rA.Y * tangent.X;
                    float rtB = ccp.rB.X * tangent.Y - ccp.rB.Y * tangent.X;

                    rtA *= rtA;
                    rtB *= rtB;
                    float kTangent = bodyA.InvMass + bodyB.InvMass + bodyA.InvI * rtA + bodyB.InvI * rtB;

                    Debug.Assert(kTangent > Settings.Epsilon);
                    ccp.TangentMass = 1.0f / kTangent;

                    // Setup a velocity bias for restitution.
                    ccp.VelocityBias = 0.0f;
                    float vRel = cc.Normal.X * (vB.X + -wB * ccp.rB.Y - vA.X - -wA * ccp.rA.Y) +
                                 cc.Normal.Y * (vB.Y + wB * ccp.rB.X - vA.Y - wA * ccp.rA.X);
                    if (vRel < -Settings.VelocityThreshold)
                    {
                        ccp.VelocityBias = -cc.Restitution * vRel;
                    }
                }

                // If we have two points, then prepare the block solver.
                if (cc.PointCount == 2)
                {
                    ContactConstraintPoint ccp1 = cc.Points[0];
                    ContactConstraintPoint ccp2 = cc.Points[1];

                    float invMassA = bodyA.InvMass;
                    float invIA    = bodyA.InvI;
                    float invMassB = bodyB.InvMass;
                    float invIB    = bodyB.InvI;

                    float rn1A = ccp1.rA.X * cc.Normal.Y - ccp1.rA.Y * cc.Normal.X;
                    float rn1B = ccp1.rB.X * cc.Normal.Y - ccp1.rB.Y * cc.Normal.X;
                    float rn2A = ccp2.rA.X * cc.Normal.Y - ccp2.rA.Y * cc.Normal.X;
                    float rn2B = ccp2.rB.X * cc.Normal.Y - ccp2.rB.Y * cc.Normal.X;

                    float k11 = invMassA + invMassB + invIA * rn1A * rn1A + invIB * rn1B * rn1B;
                    float k22 = invMassA + invMassB + invIA * rn2A * rn2A + invIB * rn2B * rn2B;
                    float k12 = invMassA + invMassB + invIA * rn1A * rn2A + invIB * rn1B * rn2B;

                    // Ensure a reasonable condition number.
                    const float k_maxConditionNumber = 100.0f;
                    if (k11 * k11 < k_maxConditionNumber * (k11 * k22 - k12 * k12))
                    {
                        // K is safe to invert.
                        cc.K.Col1.X = k11;
                        cc.K.Col1.Y = k12;
                        cc.K.Col2.X = k12;
                        cc.K.Col2.Y = k22;

                        float a = cc.K.Col1.X, b = cc.K.Col2.X, c = cc.K.Col1.Y, d = cc.K.Col2.Y;
                        float det = a * d - b * c;
                        if (det != 0.0f)
                        {
                            det = 1.0f / det;
                        }

                        cc.NormalMass.Col1.X = det * d;
                        cc.NormalMass.Col1.Y = -det * c;
                        cc.NormalMass.Col2.X = -det * b;
                        cc.NormalMass.Col2.Y = det * a;
                    }
                    else
                    {
                        // The constraints are redundant, just use one.
                        // TODO_ERIN use deepest?
                        cc.PointCount = 1;
                    }
                }
            }
        }
예제 #32
0
파일: Test.cs 프로젝트: Ratel13/cocos2d-xna
 protected virtual void PostSolve(Contact contact, ContactConstraint impulse)
 {
 }
예제 #33
0
        public void SolveVelocityConstraints()
        {
            for (int i = 0; i < this._constraintCount; ++i)
            {
                ContactConstraint c  = this.Constraints[i];
                float             wA = c.BodyA.AngularVelocityInternal;
                float             wB = c.BodyB.AngularVelocityInternal;

                float tangentx = c.Normal.Y;
                float tangenty = -c.Normal.X;

                float friction = c.Friction;

                Debug.Assert(c.PointCount == 1 || c.PointCount == 2);

                // Solve tangent constraints
                for (int j = 0; j < c.PointCount; ++j)
                {
                    ContactConstraintPoint ccp = c.Points[j];
                    float lambda = ccp.TangentMass *
                                   -((c.BodyB.LinearVelocityInternal.X + (-wB * ccp.rB.Y) -
                                      c.BodyA.LinearVelocityInternal.X - (-wA * ccp.rA.Y)) * tangentx +
                                     (c.BodyB.LinearVelocityInternal.Y + (wB * ccp.rB.X) -
                                      c.BodyA.LinearVelocityInternal.Y - (wA * ccp.rA.X)) * tangenty);

                    // MathUtils.Clamp the accumulated force
                    float maxFriction = friction * ccp.NormalImpulse;
                    float newImpulse  = Math.Max(-maxFriction, Math.Min(ccp.TangentImpulse + lambda, maxFriction));
                    lambda = newImpulse - ccp.TangentImpulse;

                    // Apply contact impulse
                    float px = lambda * tangentx;
                    float py = lambda * tangenty;

                    c.BodyA.LinearVelocityInternal.X -= c.BodyA.InvMass * px;
                    c.BodyA.LinearVelocityInternal.Y -= c.BodyA.InvMass * py;
                    wA -= c.BodyA.InvI * (ccp.rA.X * py - ccp.rA.Y * px);

                    c.BodyB.LinearVelocityInternal.X += c.BodyB.InvMass * px;
                    c.BodyB.LinearVelocityInternal.Y += c.BodyB.InvMass * py;
                    wB += c.BodyB.InvI * (ccp.rB.X * py - ccp.rB.Y * px);

                    ccp.TangentImpulse = newImpulse;
                }

                // Solve normal constraints
                if (c.PointCount == 1)
                {
                    ContactConstraintPoint ccp = c.Points[0];

                    // Relative velocity at contact
                    // Compute normal impulse
                    float lambda = -ccp.NormalMass *
                                   ((c.BodyB.LinearVelocityInternal.X + (-wB * ccp.rB.Y) -
                                     c.BodyA.LinearVelocityInternal.X - (-wA * ccp.rA.Y)) * c.Normal.X +
                                    (c.BodyB.LinearVelocityInternal.Y + (wB * ccp.rB.X) -
                                     c.BodyA.LinearVelocityInternal.Y -
                                     (wA * ccp.rA.X)) * c.Normal.Y - ccp.VelocityBias);

                    // Clamp the accumulated impulse
                    float newImpulse = Math.Max(ccp.NormalImpulse + lambda, 0.0f);
                    lambda = newImpulse - ccp.NormalImpulse;

                    // Apply contact impulse
                    float px = lambda * c.Normal.X;
                    float py = lambda * c.Normal.Y;

                    c.BodyA.LinearVelocityInternal.X -= c.BodyA.InvMass * px;
                    c.BodyA.LinearVelocityInternal.Y -= c.BodyA.InvMass * py;
                    wA -= c.BodyA.InvI * (ccp.rA.X * py - ccp.rA.Y * px);

                    c.BodyB.LinearVelocityInternal.X += c.BodyB.InvMass * px;
                    c.BodyB.LinearVelocityInternal.Y += c.BodyB.InvMass * py;
                    wB += c.BodyB.InvI * (ccp.rB.X * py - ccp.rB.Y * px);

                    ccp.NormalImpulse = newImpulse;
                }
                else
                {
                    // Block solver developed in collaboration with Dirk Gregorius (back in 01/07 on Box2D_Lite).
                    // Build the mini LCP for this contact patch
                    //
                    // vn = A * x + b, vn >= 0, , vn >= 0, x >= 0 and vn_i * x_i = 0 with i = 1..2
                    //
                    // A = J * W * JT and J = ( -n, -r1 x n, n, r2 x n )
                    // b = vn_0 - velocityBias
                    //
                    // The system is solved using the "Total enumeration method" (s. Murty). The complementary constraint vn_i * x_i
                    // implies that we must have in any solution either vn_i = 0 or x_i = 0. So for the 2D contact problem the cases
                    // vn1 = 0 and vn2 = 0, x1 = 0 and x2 = 0, x1 = 0 and vn2 = 0, x2 = 0 and vn1 = 0 need to be tested. The first valid
                    // solution that satisfies the problem is chosen.
                    //
                    // In order to account of the accumulated impulse 'a' (because of the iterative nature of the solver which only requires
                    // that the accumulated impulse is clamped and not the incremental impulse) we change the impulse variable (x_i).
                    //
                    // Substitute:
                    //
                    // x = x' - a
                    //
                    // Plug into above equation:
                    //
                    // vn = A * x + b
                    //    = A * (x' - a) + b
                    //    = A * x' + b - A * a
                    //    = A * x' + b'
                    // b' = b - A * a;

                    ContactConstraintPoint cp1 = c.Points[0];
                    ContactConstraintPoint cp2 = c.Points[1];

                    float ax = cp1.NormalImpulse;
                    float ay = cp2.NormalImpulse;
                    Debug.Assert(ax >= 0.0f && ay >= 0.0f);

                    // Relative velocity at contact
                    // Compute normal velocity
                    float vn1 = (c.BodyB.LinearVelocityInternal.X + (-wB * cp1.rB.Y) - c.BodyA.LinearVelocityInternal.X -
                                 (-wA * cp1.rA.Y)) * c.Normal.X +
                                (c.BodyB.LinearVelocityInternal.Y + (wB * cp1.rB.X) - c.BodyA.LinearVelocityInternal.Y -
                                 (wA * cp1.rA.X)) * c.Normal.Y;
                    float vn2 = (c.BodyB.LinearVelocityInternal.X + (-wB * cp2.rB.Y) - c.BodyA.LinearVelocityInternal.X -
                                 (-wA * cp2.rA.Y)) * c.Normal.X +
                                (c.BodyB.LinearVelocityInternal.Y + (wB * cp2.rB.X) - c.BodyA.LinearVelocityInternal.Y -
                                 (wA * cp2.rA.X)) * c.Normal.Y;

                    float bx = vn1 - cp1.VelocityBias - (c.K.Col1.X * ax + c.K.Col2.X * ay);
                    float by = vn2 - cp2.VelocityBias - (c.K.Col1.Y * ax + c.K.Col2.Y * ay);

                    float xx = -(c.NormalMass.Col1.X * bx + c.NormalMass.Col2.X * by);
                    float xy = -(c.NormalMass.Col1.Y * bx + c.NormalMass.Col2.Y * by);

                    while (true)
                    {
                        //
                        // Case 1: vn = 0
                        //
                        // 0 = A * x' + b'
                        //
                        // Solve for x':
                        //
                        // x' = - inv(A) * b'
                        //
                        if (xx >= 0.0f && xy >= 0.0f)
                        {
                            // Resubstitute for the incremental impulse
                            float dx = xx - ax;
                            float dy = xy - ay;

                            // Apply incremental impulse
                            float p1x = dx * c.Normal.X;
                            float p1y = dx * c.Normal.Y;

                            float p2x = dy * c.Normal.X;
                            float p2y = dy * c.Normal.Y;

                            float p12x = p1x + p2x;
                            float p12y = p1y + p2y;

                            c.BodyA.LinearVelocityInternal.X -= c.BodyA.InvMass * p12x;
                            c.BodyA.LinearVelocityInternal.Y -= c.BodyA.InvMass * p12y;
                            wA -= c.BodyA.InvI * ((cp1.rA.X * p1y - cp1.rA.Y * p1x) + (cp2.rA.X * p2y - cp2.rA.Y * p2x));

                            c.BodyB.LinearVelocityInternal.X += c.BodyB.InvMass * p12x;
                            c.BodyB.LinearVelocityInternal.Y += c.BodyB.InvMass * p12y;
                            wB += c.BodyB.InvI * ((cp1.rB.X * p1y - cp1.rB.Y * p1x) + (cp2.rB.X * p2y - cp2.rB.Y * p2x));

                            // Accumulate
                            cp1.NormalImpulse = xx;
                            cp2.NormalImpulse = xy;

                            break;
                        }

                        //
                        // Case 2: vn1 = 0 and x2 = 0
                        //
                        //   0 = a11 * x1' + a12 * 0 + b1'
                        // vn2 = a21 * x1' + a22 * 0 + b2'
                        //
                        xx  = -cp1.NormalMass * bx;
                        xy  = 0.0f;
                        vn1 = 0.0f;
                        vn2 = c.K.Col1.Y * xx + by;

                        if (xx >= 0.0f && vn2 >= 0.0f)
                        {
                            // Resubstitute for the incremental impulse
                            float dx = xx - ax;
                            float dy = xy - ay;

                            // Apply incremental impulse
                            float p1x = dx * c.Normal.X;
                            float p1y = dx * c.Normal.Y;

                            float p2x = dy * c.Normal.X;
                            float p2y = dy * c.Normal.Y;

                            float p12x = p1x + p2x;
                            float p12y = p1y + p2y;

                            c.BodyA.LinearVelocityInternal.X -= c.BodyA.InvMass * p12x;
                            c.BodyA.LinearVelocityInternal.Y -= c.BodyA.InvMass * p12y;
                            wA -= c.BodyA.InvI * ((cp1.rA.X * p1y - cp1.rA.Y * p1x) + (cp2.rA.X * p2y - cp2.rA.Y * p2x));

                            c.BodyB.LinearVelocityInternal.X += c.BodyB.InvMass * p12x;
                            c.BodyB.LinearVelocityInternal.Y += c.BodyB.InvMass * p12y;
                            wB += c.BodyB.InvI * ((cp1.rB.X * p1y - cp1.rB.Y * p1x) + (cp2.rB.X * p2y - cp2.rB.Y * p2x));

                            // Accumulate
                            cp1.NormalImpulse = xx;
                            cp2.NormalImpulse = xy;

                            break;
                        }


                        //
                        // Case 3: vn2 = 0 and x1 = 0
                        //
                        // vn1 = a11 * 0 + a12 * x2' + b1'
                        //   0 = a21 * 0 + a22 * x2' + b2'
                        //
                        xx  = 0.0f;
                        xy  = -cp2.NormalMass * by;
                        vn1 = c.K.Col2.X * xy + bx;
                        vn2 = 0.0f;

                        if (xy >= 0.0f && vn1 >= 0.0f)
                        {
                            // Resubstitute for the incremental impulse
                            float dx = xx - ax;
                            float dy = xy - ay;

                            // Apply incremental impulse
                            float p1x = dx * c.Normal.X;
                            float p1y = dx * c.Normal.Y;

                            float p2x = dy * c.Normal.X;
                            float p2y = dy * c.Normal.Y;

                            float p12x = p1x + p2x;
                            float p12y = p1y + p2y;

                            c.BodyA.LinearVelocityInternal.X -= c.BodyA.InvMass * p12x;
                            c.BodyA.LinearVelocityInternal.Y -= c.BodyA.InvMass * p12y;
                            wA -= c.BodyA.InvI * ((cp1.rA.X * p1y - cp1.rA.Y * p1x) + (cp2.rA.X * p2y - cp2.rA.Y * p2x));

                            c.BodyB.LinearVelocityInternal.X += c.BodyB.InvMass * p12x;
                            c.BodyB.LinearVelocityInternal.Y += c.BodyB.InvMass * p12y;
                            wB += c.BodyB.InvI * ((cp1.rB.X * p1y - cp1.rB.Y * p1x) + (cp2.rB.X * p2y - cp2.rB.Y * p2x));

                            // Accumulate
                            cp1.NormalImpulse = xx;
                            cp2.NormalImpulse = xy;

                            break;
                        }

                        //
                        // Case 4: x1 = 0 and x2 = 0
                        //
                        // vn1 = b1
                        // vn2 = b2;
                        xx  = 0.0f;
                        xy  = 0.0f;
                        vn1 = bx;
                        vn2 = by;

                        if (vn1 >= 0.0f && vn2 >= 0.0f)
                        {
                            // Resubstitute for the incremental impulse
                            float dx = xx - ax;
                            float dy = xy - ay;

                            // Apply incremental impulse
                            float p1x = dx * c.Normal.X;
                            float p1y = dx * c.Normal.Y;

                            float p2x = dy * c.Normal.X;
                            float p2y = dy * c.Normal.Y;

                            float p12x = p1x + p2x;
                            float p12y = p1y + p2y;

                            c.BodyA.LinearVelocityInternal.X -= c.BodyA.InvMass * p12x;
                            c.BodyA.LinearVelocityInternal.Y -= c.BodyA.InvMass * p12y;
                            wA -= c.BodyA.InvI * ((cp1.rA.X * p1y - cp1.rA.Y * p1x) + (cp2.rA.X * p2y - cp2.rA.Y * p2x));

                            c.BodyB.LinearVelocityInternal.X += c.BodyB.InvMass * p12x;
                            c.BodyB.LinearVelocityInternal.Y += c.BodyB.InvMass * p12y;
                            wB += c.BodyB.InvI * ((cp1.rB.X * p1y - cp1.rB.Y * p1x) + (cp2.rB.X * p2y - cp2.rB.Y * p2x));

                            // Accumulate
                            cp1.NormalImpulse = xx;
                            cp2.NormalImpulse = xy;

                            break;
                        }

                        // No solution, give up. This is hit sometimes, but it doesn't seem to matter.
                        break;
                    }
                }

                c.BodyA.AngularVelocityInternal = wA;
                c.BodyB.AngularVelocityInternal = wB;
            }
        }
        protected override void PostSolve(Contact contact, ContactConstraint impulse)
        {
            if (_broke)
            {
                // The body already broke.
                return;
            }

            // Should the body break?
            float maxImpulse = 0.0f;
            Manifold manifold;
            contact.GetManifold(out manifold);

            for (int i = 0; i < manifold.PointCount; ++i)
            {
                maxImpulse = Math.Max(maxImpulse, impulse.Points[i].NormalImpulse);
            }

            if (maxImpulse > 40.0f)
            {
                // Flag the body for breaking.
                _break = true;
            }
        }
예제 #35
0
        public bool SolvePositionConstraints(float baumgarte)
        {
            float minSeparation = 0.0f;

            for (int i = 0; i < this._constraintCount; ++i)
            {
                ContactConstraint c = this.Constraints[i];

                Body bodyA = c.BodyA;
                Body bodyB = c.BodyB;

                float invMassA = bodyA.Mass * bodyA.InvMass;
                float invIA    = bodyA.Mass * bodyA.InvI;
                float invMassB = bodyB.Mass * bodyB.InvMass;
                float invIB    = bodyB.Mass * bodyB.InvI;

                // Solve normal constraints
                for (int j = 0; j < c.PointCount; ++j)
                {
                    Vector2 normal;
                    Vector2 point;
                    float   separation;

                    Solve(c, j, out normal, out point, out separation);

                    float rax = point.X - bodyA.Sweep.C.X;
                    float ray = point.Y - bodyA.Sweep.C.Y;

                    float rbx = point.X - bodyB.Sweep.C.X;
                    float rby = point.Y - bodyB.Sweep.C.Y;

                    // Track max constraint error.
                    minSeparation = Math.Min(minSeparation, separation);

                    // Prevent large corrections and allow slop.
                    float C = Math.Max(-Settings.MaxLinearCorrection,
                                       Math.Min(baumgarte * (separation + Settings.LinearSlop), 0.0f));

                    // Compute the effective mass.
                    float rnA = rax * normal.Y - ray * normal.X;
                    float rnB = rbx * normal.Y - rby * normal.X;
                    float K   = invMassA + invMassB + invIA * rnA * rnA + invIB * rnB * rnB;

                    // Compute normal impulse
                    float impulse = K > 0.0f ? -C / K : 0.0f;

                    float px = impulse * normal.X;
                    float py = impulse * normal.Y;

                    bodyA.Sweep.C.X -= invMassA * px;
                    bodyA.Sweep.C.Y -= invMassA * py;
                    bodyA.Sweep.A   -= invIA * (rax * py - ray * px);

                    bodyB.Sweep.C.X += invMassB * px;
                    bodyB.Sweep.C.Y += invMassB * py;
                    bodyB.Sweep.A   += invIB * (rbx * py - rby * px);

                    bodyA.SynchronizeTransform();
                    bodyB.SynchronizeTransform();
                }
            }

            // We can't expect minSpeparation >= -Settings.b2_linearSlop because we don't
            // push the separation above -Settings.b2_linearSlop.
            return(minSeparation >= -1.5f * Settings.LinearSlop);
        }
        private void PostSolve(ContactConstraint contactConstraint)
        {
            if (!Broken)
            {
                float maxImpulse = 0.0f;
                for (int i = 0; i < contactConstraint.Manifold.PointCount; ++i)
                {
                    maxImpulse = Math.Max(maxImpulse, contactConstraint.Manifold.Points[0].NormalImpulse);
                    maxImpulse = Math.Max(maxImpulse, contactConstraint.Manifold.Points[1].NormalImpulse);
                }

                if (maxImpulse > Strength)
                {
                    // Flag the body for breaking.
                    _break = true;
                }
            }
        }
예제 #37
0
        public void Reset(Contact[] contacts, int contactCount, float impulseRatio, bool warmstarting)
        {
            this._contacts = contacts;

            this._constraintCount = contactCount;

            // grow the array
            if (this.Constraints == null || this.Constraints.Length < this._constraintCount)
            {
                this.Constraints = new ContactConstraint[this._constraintCount * 2];

                for (int i = 0; i < this.Constraints.Length; i++)
                {
                    this.Constraints[i] = new ContactConstraint();
                }
            }

            // Initialize position independent portions of the constraints.
            for (int i = 0; i < this._constraintCount; ++i)
            {
                Contact contact = contacts[i];

                Fixture  fixtureA = contact.FixtureA;
                Fixture  fixtureB = contact.FixtureB;
                Shape    shapeA   = fixtureA.Shape;
                Shape    shapeB   = fixtureB.Shape;
                float    radiusA  = shapeA.Radius;
                float    radiusB  = shapeB.Radius;
                Body     bodyA    = fixtureA.Body;
                Body     bodyB    = fixtureB.Body;
                Manifold manifold = contact.Manifold;

                Debug.Assert(manifold.PointCount > 0);

                ContactConstraint cc = this.Constraints[i];
                cc.Friction    = Settings.MixFriction(fixtureA.Friction, fixtureB.Friction);
                cc.Restitution = Settings.MixRestitution(fixtureA.Restitution, fixtureB.Restitution);
                cc.BodyA       = bodyA;
                cc.BodyB       = bodyB;
                cc.Manifold    = manifold;
                cc.Normal      = Vector2.Zero;
                cc.PointCount  = manifold.PointCount;

                cc.LocalNormal = manifold.LocalNormal;
                cc.LocalPoint  = manifold.LocalPoint;
                cc.RadiusA     = radiusA;
                cc.RadiusB     = radiusB;
                cc.Type        = manifold.Type;

                for (int j = 0; j < cc.PointCount; ++j)
                {
                    ManifoldPoint          cp  = manifold.Points[j];
                    ContactConstraintPoint ccp = cc.Points[j];

                    if (warmstarting)
                    {
                        ccp.NormalImpulse  = impulseRatio * cp.NormalImpulse;
                        ccp.TangentImpulse = impulseRatio * cp.TangentImpulse;
                    }
                    else
                    {
                        ccp.NormalImpulse  = 0.0f;
                        ccp.TangentImpulse = 0.0f;
                    }

                    ccp.LocalPoint   = cp.LocalPoint;
                    ccp.rA           = Vector2.Zero;
                    ccp.rB           = Vector2.Zero;
                    ccp.NormalMass   = 0.0f;
                    ccp.TangentMass  = 0.0f;
                    ccp.VelocityBias = 0.0f;
                }

                cc.K.SetZero();
                cc.NormalMass.SetZero();
            }
        }
예제 #38
0
 private void PostSolveHandler(Contact contact, ContactConstraint impulse)
 {
     var gob1 = ((CollisionArea)contact.FixtureA.UserData).Owner;
     var gob2 = ((CollisionArea)contact.FixtureB.UserData).Owner;
     var eventKey = new CollisionEventKey(gob1, gob2);
     var previousMaxImpulse = 0f;
     _collisionImpulses.TryGetValue(eventKey, out previousMaxImpulse);
     _collisionImpulses[eventKey] = Math.Max(previousMaxImpulse, impulse.Points.Take(impulse.PointCount).Max(p => p.NormalImpulse));
 }