public Apple(Fixture connectedFixture, GameContent gameContent, World world)
        {
            this.world = world;
            this.gameContent = gameContent;
            texture = gameContent.apple;

            fallenFromAir = false;

            CreatBody();

            State = LeafState.Grow;

            if (connectedFixture != null)
            {
                this.connectedFixture = connectedFixture;
                AABB aabb; connectedFixture.GetAABB(out aabb);
                body.Position = aabb.GetCenter();

                RevoluteJointDef rjd = new RevoluteJointDef();
                rjd.bodyA = body;
                rjd.bodyB = connectedFixture.GetBody();

                rjd.localAnchorA = Vector2.Zero;
                rjd.localAnchorB = connectedFixture.GetBody().GetLocalPoint(body.Position);

                revoJoint = (RevoluteJoint)world.CreateJoint(rjd);
            }
        }
Esempio n. 2
0
 public SensorInfo(Unit unit, Fixture fixture, int maxSense)
 {
     _unit = unit;
     _fixture = fixture;
     _fixture.SetUserData(this);
     _max = maxSense;
     _sensedUnits = new Unit[maxSense];
     _count = 0;
 }
 protected override void OnPlayerContact(object player, object objB, Fixture fixtureA, Fixture fixtureB)
 {
     base.OnPlayerContact(player, objB, fixtureA, fixtureB);
     if (!roundOver && objB is Target)
     {
         V2DSprite p = (V2DSprite)player;
         int scoredOnPlayer = p.Parent.Index == 0 ? 1 : 0;
         AddPoint(scoredOnPlayer);
     }
 }
        public leaf(Fixture fixture, float rotation, int index, GameContent gameContent)
        {
            this.gameContent = gameContent;
            this.connectedFixture = fixture;
            this.rotation = rotation / 180 * (float)Math.PI;
            oriRotation = this.rotation;

            AABB aabb; fixture.GetAABB(out aabb);
            position = aabb.GetCenter();

            texture = gameContent.leaves[index];
        }
Esempio n. 5
0
        public bool ShouldCollide(Fixture fixtureA, Fixture fixtureB)
        {
            Filter filterA;
            fixtureA.GetFilterData(out filterA);

            Filter filterB;
            fixtureB.GetFilterData(out filterB);

	        if (filterA.groupIndex == filterB.groupIndex && filterA.groupIndex != 0)
	        {
		        return filterA.groupIndex > 0;
	        }

	        bool collide = (filterA.maskBits & filterB.categoryBits) != 0 && (filterA.categoryBits & filterB.maskBits) != 0;
	        
            return collide;
        }
 public void OnPigContact(object pigObj, object objB, Fixture fixtureA, Fixture fixtureB)
 {
     if (!roundOver && objB is Target)
     {
         Target t = (Target)objB;
         if (t.Index < 2)
         {
             pigNeedsReset = true;
             int scoredOnPlayer = t.Index == 0 ? 1 : 0;
             AddPoint(scoredOnPlayer);
         }
         else
         {
             int pt = thrust[t.Index];
             Vector2 pigImpulse = new Vector2(rnd.Next(-pt, pt), -pt);
             pig.body.ApplyLinearImpulse(pigImpulse, pig.body.GetWorldCenter());
         }
     }
 }
        /// <summary>
        /// Creates a fixture and attach it to this body. Use this function if you need
        /// to set some fixture parameters, like friction. Otherwise you can create the
        /// fixture directly from a shape.
        /// If the density is non-zero, this function automatically updates the mass of the body.
        /// Contacts are not created until the next time step.
        /// @warning This function is locked during callbacks.
        /// </summary>
        /// <param name="def">the fixture definition.</param>
        /// <returns></returns>
        public Fixture CreateFixture(FixtureDef def)
        {
            Debug.Assert(_world.IsLocked == false);
            if (_world.IsLocked == true)
            {
                return null;
            }

            Fixture fixture = new Fixture();
            fixture.Create(this, def);

            if ((_flags & BodyFlags.Active) == BodyFlags.Active)
            {
                BroadPhase broadPhase = _world._contactManager._broadPhase;
                fixture.CreateProxy(broadPhase, ref _xf);
            }

            fixture._next = _fixtureList;
            _fixtureList = fixture;
            ++_fixtureCount;

            fixture._body = this;

            // Adjust mass properties if needed.
            if (fixture._density > 0.0f)
            {
                ResetMassData();
            }

            // Let the world know we have a new fixture. This will cause new contacts
            // to be created at the beginning of the next time step.
            _world._flags |= WorldFlags.NewFixture;

            return fixture;
        }
 protected void OnPressButtonContact(object pressButton, object objB, Fixture fixtureA, Fixture fixtureB)
 {
     if (objB is V2DSprite && ((V2DSprite)objB).InstanceName == "item2")
     {
         PressButton pb = (PressButton)pressButton;
         if (!pb.IsDepressed)
         {
             pb.IsDepressed = true;
             if (pb.Index == 1)
             {
                 dropBeam = true;
             }
         }
     }
 }
Esempio n. 9
0
        protected override void OnWalkerContact(object player, object objB, Fixture fixtureA, Fixture fixtureB)
        {
            base.OnWalkerContact(player, objB, fixtureA, fixtureB);

            if (objB is PressButton)
            {
                PressButton p = (PressButton)objB;
                if (!p.IsDepressed)
                {
                    p.IsDepressed = true;
                    signalHit = p.Index;
                }
            }
            else if (objB is TeleportDoor)
            {
                TeleportDoor td = (TeleportDoor)objB;
                if (td.Index % 2 == 0)
                {
                    teleportIndex = td.Index;
                    walker.directionWalking = -walker.directionWalking;
                }
            }
            else if (objB is V2DSprite && ((V2DSprite)objB).Parent is Trampoline)
            {
                Walker p = (Walker)player;

                float ls = p.body.GetLinearVelocity().LengthSquared();

                if (ls > 400f)
                {
                    ((Trampoline)((V2DSprite)objB).Parent).Bounce();
                }
                else if (ls > 200f)
                {
                    ((Trampoline)((V2DSprite)objB).Parent).BounceSmall();
                }

                if (ls > 1000f)
                {
                    p.body.SetLinearVelocity(p.body.GetLinearVelocity() * .8f);
                }
            }
        }
Esempio n. 10
0
        internal void Reset(Fixture fA, int indexA, Fixture fB, int indexB)
        {
            _flags = ContactFlags.Enabled;

	        _fixtureA = fA;
	        _fixtureB = fB;

            _indexA = indexA;
            _indexB = indexB;

	        _manifold._pointCount = 0;

	        _prev = null;
	        _next = null;

	        _nodeA.Contact = null;
	        _nodeA.Prev = null;
	        _nodeA.Next = null;
	        _nodeA.Other = null;

	        _nodeB.Contact = null;
	        _nodeB.Prev = null;
	        _nodeB.Next = null;
	        _nodeB.Other = null;

            _toiCount = 0;
        }
Esempio n. 11
0
        protected override void OnWalkerContact(object player, object objB, Fixture fixtureA, Fixture fixtureB)
        {
            base.OnWalkerContact(player, objB, fixtureA, fixtureB);

            if (objB is V2DSprite)
            {
                if (((V2DSprite)objB).Parent is Trampoline)
                {
                    EndSequence();

                    Walker p = (Walker)player;
                    if (p.Visible)
                    {
                        float ls = p.body.GetLinearVelocity().LengthSquared();

                        if (ls > 400f)
                        {
                            ((Trampoline)((V2DSprite)objB).Parent).Bounce();
                        }
                        else if (ls > 200f)
                        {
                            ((Trampoline)((V2DSprite)objB).Parent).BounceSmall();
                        }

                        if (ls > 1000f)
                        {
                            p.body.SetLinearVelocity(p.body.GetLinearVelocity() * .8f);
                        }
                    }
                    int index = ((V2DSprite)objB).Parent.Index;
                    pianoKey[index].PlayNote();

                    noteStack.Push(index);
                    CheckForWin();
                    SetHint();
                }
            }
        }
Esempio n. 12
0
        void DrawShape(Fixture fixture, Transform xf, Color color)
        {
            switch (fixture.ShapeType)
            {
                case ShapeType.Circle:
                    {
                        CircleShape circle = (CircleShape)fixture.GetShape();

                        Vector2 center = MathUtils.Multiply(ref xf, circle._p);
                        float radius = circle._radius;
                        Vector2 axis = xf.R.col1;

                        DebugDraw.DrawSolidCircle(center, radius, axis, color);
                    }
                    break;

                case ShapeType.Polygon:
                    {
                        PolygonShape poly = (PolygonShape)fixture.GetShape();
                        int vertexCount = poly._vertexCount;
                        Debug.Assert(vertexCount <= Settings.b2_maxPolygonVertices);
                        FixedArray8<Vector2> vertices = new FixedArray8<Vector2>();

                        for (int i = 0; i < vertexCount; ++i)
                        {
                            vertices[i] = MathUtils.Multiply(ref xf, poly._vertices[i]);
                        }

                        DebugDraw.DrawSolidPolygon(ref vertices, vertexCount, color);
                    }
                    break;
            }
        }
        protected override void OnPlayerContact(object player, object objB, Fixture fixtureA, Fixture fixtureB)
        {
            base.OnPlayerContact(player, objB, fixtureA, fixtureB);
            if (!roundOver && objB is Target)
            {
                PlayerFeet pf = (PlayerFeet)player;
                int scoredOnPlayer = pf.Parent.Index == 0 ? 1 : 0;

                V2DSprite head = ((HeadPlayer)pf.Parent).head;
                int pt = thrust[scoredOnPlayer];
                Vector2 impulse = new Vector2(rnd.Next(-pt, pt), -pt);
                head.body.ApplyLinearImpulse(impulse, head.body.GetWorldCenter());
            }
        }
        public float NearestFixtureDistance(Vector2 mousePos, float nearestDistance)
        {
            nearestFixture = null;

            for (Fixture f = body.GetFixtureList(); f != null; f = f.GetNext())
            {
                // the fixture have a joint
                if (f.GetUserData() is Branch) continue;

                AABB aabb;
                f.GetAABB(out aabb);
                Vector2 center = aabb.GetCenter();
                float d = Vector2.Distance(center, mousePos);

                if (d < nearestDistance)
                {
                    nearestDistance = d;
                    nearestFixture = f;
                }
            }

            return nearestDistance;
        }
        public void CutDown(Fixture cutDownFixture)
        {
            List<Fixture> destroyFix = new List<Fixture>();
            for (Fixture f = body.GetFixtureList(); f != null; f = f.GetNext())
            {
                destroyFix.Add(f);
                if (f == cutDownFixture) break;
            }

            fixtureCount -= destroyFix.Count;
            foreach (Fixture f in destroyFix)
            {
                object o = f.GetUserData();
                if (o is Branch)
                {
                    body.GetWorld().DestroyJoint(((Branch)o).revoJoint);
                    ((Branch)o).revoJoint = null;
                }

                body.DestroyFixture(f);
            }

            // Destroy Body if all fixture are destroyed
            if (fixtureCount == 0) body.GetWorld().DestroyBody(body);

            newGrow = true;
        }
        internal static Contact Create(Fixture fixtureA, Fixture fixtureB)
        {
            ShapeType type1 = fixtureA.ShapeType;
            ShapeType type2 = fixtureB.ShapeType;

            Debug.Assert(ShapeType.Unknown < type1 && type1 < ShapeType.TypeCount);
            Debug.Assert(ShapeType.Unknown < type2 && type2 < ShapeType.TypeCount);

            Contact c;
            var pool = fixtureA._body._world._contactPool;
            if (pool.Count > 0)
            {
                c = pool.Dequeue();
                if (type1 >= type2)
                {
                    c.Reset(fixtureA, fixtureB);
                }
                else
                {
                    c.Reset(fixtureB, fixtureA);
                }
            }
            else
            {
                if (type1 >= type2)
                {
                    c = new Contact(fixtureA, fixtureB);
                }
                else
                {
                    c = new Contact(fixtureB, fixtureA);
                }
            }

            c._type = Contact.s_registers[(int)type1, (int)type2];

            return c;
        }
 internal Contact(Fixture fA, Fixture fB)
 {
     Reset(fA, fB);
 }
Esempio n. 18
0
        protected override void OnWalkerContact(object player, object objB, Fixture fixtureA, Fixture fixtureB)
        {
            base.OnWalkerContact(player, objB, fixtureA, fixtureB);

            if (objB is PressButton)
            {
                PressButton pb = (PressButton)objB;
                if (!pb.IsDepressed)
                {
                    pb.IsDepressed = true;
                    if (pb.Index == 0)
                    {
                        openDoor = true;
                    }
                }
            }
        }
Esempio n. 19
0
        /// <summary>
        /// Destroy a fixture. This removes the fixture from the broad-phase and
        /// destroys all contacts associated with this fixture. This will	
        /// automatically adjust the mass of the body if the body is dynamic and the
        /// fixture has positive density.
        /// All fixtures attached to a body are implicitly destroyed when the body is destroyed.
        /// @warning This function is locked during callbacks.
        /// </summary>
        /// <param name="fixture">the fixture to be removed.</param>
        public void DestroyFixture(Fixture fixture)
        {
            Debug.Assert(_world.IsLocked == false);
            if (_world.IsLocked == true)
            {
                return;
            }

            Debug.Assert(fixture._body == this);

            // Remove the fixture from this body's singly linked list.
            Debug.Assert(_fixtureCount > 0);
            Fixture node = _fixtureList;
            bool found = false;
            while (node != null)
            {
                if (node == fixture)
                {
                    _fixtureList = fixture._next;
                    found = true;
                    break;
                }

                node = node._next;
            }

            // You tried to remove a shape that is not attached to this body.
            Debug.Assert(found);

            // Destroy any contacts associated with the fixture.
            ContactEdge edge = _contactList;
            while (edge != null)
            {
                Contact c = edge.Contact;
                edge = edge.Next;

                Fixture fixtureA = c.GetFixtureA();
                Fixture fixtureB = c.GetFixtureB();

                if (fixture == fixtureA || fixture == fixtureB)
                {
                    // This destroys the contact and removes it from
                    // this body's contact list.
                    _world._contactManager.Destroy(c);
                }
            }

            if ((_flags & BodyFlags.Active) == BodyFlags.Active)
            {
                Debug.Assert(fixture._proxyId != BroadPhase.NullProxy);

                BroadPhase broadPhase = _world._contactManager._broadPhase;
                fixture.DestroyProxy(broadPhase);
            }
            else
            {
                Debug.Assert(fixture._proxyId == BroadPhase.NullProxy);
            }

            fixture.Destroy();
            fixture._body = null;
            fixture._next = null;

            --_fixtureCount;

            ResetMassData();
        }
Esempio n. 20
0
        //protected void OnSeesawContact(object player, object objB)
        //{
        //    if(objB is
        //}
        protected override void OnWalkerContact(object player, object objB, Fixture fixtureA, Fixture fixtureB)
        {
            base.OnWalkerContact(player, objB, fixtureA, fixtureB);

            if (objB is PressButton)
            {
                PressButton pb = (PressButton)objB;
                if (!pb.IsDepressed)
                {
                    pb.IsDepressed = true;
                    if (pb.Index == 0)
                    {
                        smasher[0].SetMotorSpeed(-3);
                        smasher[0].EnableMotor(true);
                        smasher[1].SetMotorSpeed(4);
                        smasher[1].EnableMotor(true);
                    }
                    else if (pb.Index == 1)
                    {
                        openDoor = true;
                        smasher[0].EnableMotor(false);
                        smasher[1].EnableMotor(false);
                    }
                }
            }
            else if (objB is DisplayObject)
            {
                if (((DisplayObject)objB).InstanceName == "elevatorBeam")
                {
                    needsTeleport = true;
                    if (stage != null)
                    {
                        stage.audio.PlaySound(Sfx.teleport);
                    }
                }
            }
        }
Esempio n. 21
0
        protected override void OnWalkerContact(object player, object objB, Fixture fixtureA, Fixture fixtureB)
        {
            base.OnWalkerContact(player, objB, fixtureA, fixtureB);

            if (objB is PressButton)
            {
                PressButton pb = (PressButton)objB;
                if (!pb.IsDepressed)
                {
                    pb.IsDepressed = true;
                    destroyJoint = true;
                }
            }
        }
Esempio n. 22
0
        protected override void OnWalkerContact(object player, object objB, Fixture fixtureA, Fixture fixtureB)
        {
            base.OnWalkerContact(player, objB, fixtureA, fixtureB);

            if (objB is PressButton)
            {
                PressButton p = (PressButton)objB;
                if(!p.IsDepressed)
                {
                    p.IsDepressed = true;
                    switch(p.Index)
                    {
                        case 0:
                            pj[1]._enableMotor = true;
                            pj[1]._motorSpeed = -slowLift;
                            break;
                        case 1:
                            pj[2]._enableMotor = true;
                            pj[2]._motorSpeed = slowLift;
                            toggleElevator = true;
                            pressButton[5].IsDepressed = false;
                            break;
                        case 2:
                            pj[5]._enableMotor = true;
                            pj[5]._motorSpeed = fastLift;
                            pj[6]._enableMotor = true;
                            pj[6]._motorSpeed = fastLift;
                            break;
                        case 3:
                            pj[3]._enableMotor = true;
                            pj[3]._motorSpeed = -slowLift;
                            break;
                        case 4:
                            rCircle._enableLimit = false;
                            rCircle._enableMotor = true;
                            rCircle._motorSpeed = -1;
                            EnableMotorSound(true);
                            break;
                        case 5:
                            pj[2]._enableLimit = true;
                            pj[2]._enableMotor = true;
                            pj[2]._motorSpeed = -slowLift;
                            pressButton[1].IsDepressed = false;
                            break;
                    }
                }
            }
            else if (objB is TeleportDoor)
            {
                TeleportDoor td = (TeleportDoor)objB;
                if (td.Index % 2 == 0)
                {
                    teleportIndex = td.Index;
                }
            }
            else if (objB is V2DSprite && ((V2DSprite)objB).Parent is Trampoline)
            {
                Walker p = (Walker)player;

                float ls = p.body.GetLinearVelocity().LengthSquared();

                if (ls > 400f)
                {
                    ((Trampoline)((V2DSprite)objB).Parent).Bounce();
                }
                else if (ls > 200f)
                {
                    ((Trampoline)((V2DSprite)objB).Parent).BounceSmall();
                }

                if (ls > 1000f)
                {
                    p.body.SetLinearVelocity(p.body.GetLinearVelocity() * .8f);
                }
            }
        }
        // Broad-phase callback.
        internal void AddPair(Fixture proxyUserDataA, Fixture proxyUserDataB)
        {
            Fixture fixtureA = proxyUserDataA;
            Fixture fixtureB = proxyUserDataB;

            Body bodyA = fixtureA.GetBody();
            Body bodyB = fixtureB.GetBody();

            // Are the fixtures on the same body?
            if (bodyA == bodyB)
            {
                return;
            }

            // Does a contact already exist?
            ContactEdge edge = bodyB.GetContactList();
            while (edge != null)
            {
                if (edge.Other == bodyA)
                {
                    Fixture fA = edge.Contact.GetFixtureA();
                    Fixture fB = edge.Contact.GetFixtureB();
                    if (fA == fixtureA && fB == fixtureB)
                    {
                        // A contact already exists.
                        return;
                    }

                    if (fA == fixtureB && fB == fixtureA)
                    {
                        // A contact already exists.
                        return;
                    }
                }

                edge = edge.Next;
            }

            // Does a joint override collision? Is at least one body dynamic?
            if (bodyB.ShouldCollide(bodyA) == false)
            {
                return;
            }

            // Check user filtering.
            if (ContactFilter.ShouldCollide(fixtureA, fixtureB) == false)
            {
                return;
            }

            // Call the factory.
            Contact c = Contact.Create(fixtureA, fixtureB);

            // Contact creation may swap fixtures.
            fixtureA = c.GetFixtureA();
            fixtureB = c.GetFixtureB();
            bodyA = fixtureA.GetBody();
            bodyB = fixtureB.GetBody();

            // Insert into the world.
            c._prev = null;
            c._next = _contactList;
            if (_contactList != null)
            {
                _contactList._prev = c;
            }
            _contactList = c;

            // Connect to island graph.

            // Connect to body A
            c._nodeA.Contact = c;
            c._nodeA.Other = bodyB;

            c._nodeA.Prev = null;
            c._nodeA.Next = bodyA._contactList;
            if (bodyA._contactList != null)
            {
                bodyA._contactList.Prev = c._nodeA;
            }
            bodyA._contactList = c._nodeA;

            // Connect to body B
            c._nodeB.Contact = c;
            c._nodeB.Other = bodyA;

            c._nodeB.Prev = null;
            c._nodeB.Next = bodyB._contactList;
            if (bodyB._contactList != null)
            {
                bodyB._contactList.Prev = c._nodeB;
            }
            bodyB._contactList = c._nodeB;

            ++_contactCount;
        }
Esempio n. 24
0
        protected override void OnWalkerContact(object player, object objB, Fixture fixtureA, Fixture fixtureB)
        {
            base.OnWalkerContact(player, objB, fixtureA, fixtureB);

            if (objB is TeleportDoor)
            {
                TeleportDoor td = (TeleportDoor)objB;
                if (td.Index == 0)
                {
                    needsTeleport = true;
                }
            }
        }
Esempio n. 25
0
        // Advance a dynamic body to its first time of contact
        // and adjust the position to ensure clearance.
        void SolveTOI(Body body)
        {
            // Find the minimum contact.
            Contact toiContact = null;
            float   toi        = 1.0f;
            Body    toiOther   = null;
            bool    found;
            int     count;
            int     iter = 0;

            bool bullet = body.IsBullet;

            // Iterate until all contacts agree on the minimum TOI. We have
            // to iterate because the TOI algorithm may skip some intermediate
            // collisions when objects rotate through each other.
            do
            {
                count = 0;
                found = false;
                for (ContactEdge ce = body._contactList; ce != null; ce = ce.Next)
                {
                    if (ce.Contact == toiContact)
                    {
                        continue;
                    }

                    Body     other = ce.Other;
                    BodyType type  = other.GetType();

                    // Only bullets perform TOI with dynamic bodies.
                    if (bullet == true)
                    {
                        // Bullets only perform TOI with bodies that have their TOI resolved.
                        if ((other._flags & BodyFlags.Toi) == 0)
                        {
                            continue;
                        }

                        // No repeated hits on non-static bodies
                        if (type != BodyType.Static && (ce.Contact._flags & ContactFlags.BulletHit) != 0)
                        {
                            continue;
                        }
                    }
                    else if (type == BodyType.Dynamic)
                    {
                        continue;
                    }

                    // Check for a disabled contact.
                    Contact contact = ce.Contact;
                    if (contact.IsEnabled() == false)
                    {
                        continue;
                    }

                    // Prevent infinite looping.
                    if (contact._toiCount > 10)
                    {
                        continue;
                    }

                    Fixture fixtureA = contact._fixtureA;
                    Fixture fixtureB = contact._fixtureB;
                    int     indexA   = contact._indexA;
                    int     indexB   = contact._indexB;

                    // Cull sensors.
                    if (fixtureA.IsSensor() || fixtureB.IsSensor())
                    {
                        continue;
                    }

                    Body bodyA = fixtureA._body;
                    Body bodyB = fixtureB._body;

                    // Compute the time of impact in interval [0, minTOI]
                    TOIInput input = new TOIInput();
                    input.proxyA.Set(fixtureA.GetShape(), indexA);
                    input.proxyB.Set(fixtureB.GetShape(), indexB);
                    input.sweepA = bodyA._sweep;
                    input.sweepB = bodyB._sweep;
                    input.tMax   = toi;

                    TOIOutput output;
                    TimeOfImpact.CalculateTimeOfImpact(out output, ref input);

                    if (output.State == TOIOutputState.Touching && output.t < toi)
                    {
                        toiContact = contact;
                        toi        = output.t;
                        toiOther   = other;
                        found      = true;
                    }

                    ++count;
                }

                ++iter;
            } while (found && count > 1 && iter < 50);

            if (toiContact == null)
            {
                body.Advance(1.0f);
                return;
            }

            Sweep backup = body._sweep;

            body.Advance(toi);
            toiContact.Update(_contactManager.ContactListener);
            if (toiContact.IsEnabled() == false)
            {
                // Contact disabled. Backup and recurse.
                body._sweep = backup;
                SolveTOI(body);
            }

            ++toiContact._toiCount;

            // Update all the valid contacts on this body and build a contact island.
            count = 0;
            for (ContactEdge ce = body._contactList; (ce != null) && (count < Settings.b2_maxTOIContacts); ce = ce.Next)
            {
                Body     other = ce.Other;
                BodyType type  = other.GetType();

                // Only perform correction with static bodies, so the
                // body won't get pushed out of the world.
                if (type == BodyType.Dynamic)
                {
                    continue;
                }

                // Check for a disabled contact.
                Contact contact = ce.Contact;
                if (contact.IsEnabled() == false)
                {
                    continue;
                }

                Fixture fixtureA = contact._fixtureA;
                Fixture fixtureB = contact._fixtureB;

                // Cull sensors.
                if (fixtureA.IsSensor() || fixtureB.IsSensor())
                {
                    continue;
                }

                // The contact likely has some new contact points. The listener
                // gives the user a chance to disable the contact.
                if (contact != toiContact)
                {
                    contact.Update(_contactManager.ContactListener);
                }

                // Did the user disable the contact?
                if (contact.IsEnabled() == false)
                {
                    // Skip this contact.
                    continue;
                }

                if (contact.IsTouching() == false)
                {
                    continue;
                }

                _toiContacts[count] = contact;
                ++count;
            }

            // Reduce the TOI body's overlap with the contact island.
            _toiSolver.Initialize(_toiContacts, count, body);

            float k_toiBaumgarte = 0.75f;

            //bool solved = false;
            for (int i = 0; i < 20; ++i)
            {
                bool contactsOkay = _toiSolver.Solve(k_toiBaumgarte);
                if (contactsOkay)
                {
                    //solved = true;
                    break;
                }
            }

            if (toiOther.GetType() != BodyType.Static)
            {
                toiContact._flags |= ContactFlags.BulletHit;
            }
        }
Esempio n. 26
0
 internal Contact(Fixture fA, int indexA, Fixture fB, int indexB)
 {
     Reset(fA, indexA, fB, indexB);
 }
Esempio n. 27
0
        /// <summary> Call this to draw shapes and other debug draw data.
        /// </summary>
        public void DrawDebugData()
        {
            if (DebugDraw == null)
            {
                return;
            }

            DebugDrawFlags flags = DebugDraw.Flags;

            if ((flags & DebugDrawFlags.Shape) == DebugDrawFlags.Shape)
            {
                for (Body b = _bodyList; b != null; b = b.GetNext())
                {
                    Transform xf;
                    b.GetTransform(out xf);
                    for (Fixture f = b.GetFixtureList(); f != null; f = f.GetNext())
                    {
                        if (b.IsActive() == false)
                        {
                            DrawShape(f, xf, new Color(0.5f, 0.5f, 0.3f));
                        }
                        else if (b.GetType() == BodyType.Static)
                        {
                            DrawShape(f, xf, new Color(0.5f, 0.9f, 0.5f));
                        }
                        else if (b.GetType() == BodyType.Kinematic)
                        {
                            DrawShape(f, xf, new Color(0.5f, 0.5f, 0.9f));
                        }
                        else if (b.IsAwake() == false)
                        {
                            DrawShape(f, xf, new Color(0.6f, 0.6f, 0.6f));
                        }
                        else
                        {
                            DrawShape(f, xf, new Color(0.9f, 0.7f, 0.7f));
                        }
                    }
                }
            }

            if ((flags & DebugDrawFlags.Joint) == DebugDrawFlags.Joint)
            {
                for (Joint j = _jointList; j != null; j = j.GetNext())
                {
                    DrawJoint(j);
                }
            }

            if ((flags & DebugDrawFlags.Pair) == DebugDrawFlags.Pair)
            {
                Color color = new Color(0.3f, 0.9f, 0.9f);
                for (Contact c = _contactManager._contactList; c != null; c = c.GetNext())
                {
                    /*
                     * Fixture fixtureA = c.GetFixtureA();
                     * Fixture fixtureB = c.GetFixtureB();
                     *
                     * AABB aabbA;
                     * AABB aabbB;
                     * fixtureA.GetAABB(out aabbA);
                     * fixtureB.GetAABB(out aabbB);
                     *
                     * Vector2 cA = aabbA.GetCenter();
                     * Vector2 cB = aabbB.GetCenter();
                     *
                     * DebugDraw.DrawSegment(cA, cB, color);
                     */
                }
            }

            if ((flags & DebugDrawFlags.AABB) == DebugDrawFlags.AABB)
            {
                Color      color = new Color(0.9f, 0.3f, 0.9f);
                BroadPhase bp    = _contactManager._broadPhase;

                for (Body b = _bodyList; b != null; b = b.GetNext())
                {
                    if (b.IsActive() == false)
                    {
                        continue;
                    }

                    for (Fixture f = b.GetFixtureList(); f != null; f = f.GetNext())
                    {
                        for (int i = 0; i < f._proxyCount; ++i)
                        {
                            FixtureProxy proxy = f._proxies[i];
                            AABB         aabb;
                            bp.GetFatAABB(proxy.proxyId, out aabb);
                            FixedArray8 <Vector2> vs = new FixedArray8 <Vector2>();
                            vs[0] = new Vector2(aabb.lowerBound.X, aabb.lowerBound.Y);
                            vs[1] = new Vector2(aabb.upperBound.X, aabb.lowerBound.Y);
                            vs[2] = new Vector2(aabb.upperBound.X, aabb.upperBound.Y);
                            vs[3] = new Vector2(aabb.lowerBound.X, aabb.upperBound.Y);

                            DebugDraw.DrawPolygon(ref vs, 4, color);
                        }
                    }
                }
            }

            if ((flags & DebugDrawFlags.CenterOfMass) == DebugDrawFlags.CenterOfMass)
            {
                for (Body b = _bodyList; b != null; b = b.GetNext())
                {
                    Transform xf;
                    b.GetTransform(out xf);
                    xf.Position = b.GetWorldCenter();
                    DebugDraw.DrawTransform(ref xf);
                }
            }
        }
Esempio n. 28
0
        internal static Contact Create(Fixture fixtureA, int indexA, Fixture fixtureB, int indexB)
        {
            ShapeType type1 = fixtureA.ShapeType;
	        ShapeType type2 = fixtureB.ShapeType;

	        Debug.Assert(ShapeType.Unknown < type1 && type1 < ShapeType.TypeCount);
            Debug.Assert(ShapeType.Unknown < type2 && type2 < ShapeType.TypeCount);

            Contact c;
            var pool = fixtureA._body._world._contactPool;
            if (pool.Count > 0)
            {
                c = pool.Dequeue();
                if ((type1 >= type2 || (type1 == ShapeType.Edge && type2 == ShapeType.Polygon))
                    &&
                    !(type2 == ShapeType.Edge && type1 == ShapeType.Polygon))
                {
                    c.Reset(fixtureA, indexA, fixtureB, indexB);
                }
                else
                {
                    c.Reset(fixtureB, indexB, fixtureA, indexA);
                }
            }
            else
            {
                // Edge+Polygon is non-symetrical due to the way Erin handles collision type registration.
                if ((type1 >= type2 || (type1 == ShapeType.Edge && type2 == ShapeType.Polygon))
                    && 
                    !(type2 == ShapeType.Edge && type1 == ShapeType.Polygon))
                {
                    c = new Contact(fixtureA, indexA, fixtureB, indexB);
                }
                else
                {
                    c = new Contact(fixtureB, indexB, fixtureA, indexA);
                }
            }

            c._type = Contact.s_registers[(int)type1, (int)type2];

            return c;
        }
        public bool RayCollide(object userData, Fixture fixture)
        {
            // By default, cast userData as a fixture, and then collide if the shapes would collide
            if (userData == null)
            {
                return true;
            }

            return ShouldCollide((Fixture)userData, fixture);
        }
Esempio n. 30
0
        protected virtual void OnWalkerContact(object player, object objB, Fixture fixtureA, Fixture fixtureB)
        {
            walker.BeginContact();

            if (objB is WinTarget)
            {
                if (targetAnim != null)
                {
                    walker.StopAllSounds();
                    walker.DestroyView();
                    DestroyAfterUpdate(walker);

                    if (stage != null)
                    {
                        stage.audio.PlaySound(Sfx.snore);
                    }
                    targetAnim.GotoAndPlay(1);
                    targetAnim.PlayheadWrap += new V2DRuntime.Display.AnimationEvent(win_PlayheadWrap);
                }
            }
            if (stage != null)
            {
                Cue c = stage.audio.PlaySound(Sfx.smallHit);
                c.SetVariable("Volume", .6f);
            }
        }
Esempio n. 31
0
        protected override void OnWalkerContact(object player, object objB, Fixture fixtureA, Fixture fixtureB)
        {
            base.OnWalkerContact(player, objB, fixtureA, fixtureB);

            if (objB is PressButton)
            {
                PressButton p = (PressButton)objB;
                if (!p.IsDepressed)
                {
                    p.IsDepressed = true;
                    switch (p.Index)
                    {
                        case 0:
                            pj[0]._enableMotor = true;
                            pj[0]._motorSpeed = -slowLift;
                            oscMotor = false;
                            break;
                        case 1:
                            pj[0]._enableMotor = true;
                            pj[0]._motorSpeed = slowLift;
                            oscMotor = false;
                            break;
                    }
                }
            }
            else if (objB is TeleportDoor)
            {
                TeleportDoor td = (TeleportDoor)objB;
                if (td.Index == 0)
                {
                    needsTeleport = true;
                }
            }
            else if (objB is V2DSprite && ((V2DSprite)objB).Parent is Trampoline)
            {
                Walker p = (Walker)player;

                float ls = p.body.GetLinearVelocity().LengthSquared();

                if (ls > 400f)
                {
                    ((Trampoline)((V2DSprite)objB).Parent).Bounce();
                }
                else if (ls > 200f)
                {
                    ((Trampoline)((V2DSprite)objB).Parent).BounceSmall();
                }

                if (ls > 1000f)
                {
                    p.body.SetLinearVelocity(p.body.GetLinearVelocity() * .8f);
                }
            }
        }
        public Atom(Symbol symbol, Vector2 position, GameContent gameContent, World world)
        {
            this.gameContent = gameContent;
            this.world = world;

            if (symbol == Symbol.Ra) eye = EyeState.Angry;

            this.symbol = symbol;
            this.symbolStr = symbol.ToString();
            symbolCenter = gameContent.symbolFont.MeasureString(this.symbolStr);
            symbolCenter.X *= 0.5f;
            symbolCenter.Y *= 0.92f;

            bondsLeft = (int)symbol;

            BodyDef bd = new BodyDef();
            bd.type = BodyType.Dynamic;
            bd.position = this.position = position / gameContent.b2Scale;
            bd.bullet = true;
            body = world.CreateBody(bd);
            body.SetUserData(this);

            CircleShape cs = new CircleShape();
            cs._radius = gameContent.atomRadius / gameContent.b2Scale;

            FixtureDef fd = new FixtureDef();
            fd.shape = cs;
            fd.restitution = 0.2f;
            fd.friction = 0.5f;

            fixture = body.CreateFixture(fd);

            electroShockAnimation = new Animation(gameContent.electroShock, 3, 0.1f, true, new Vector2(0.5f, 0.5f));

            radiationSmoke = new RadiationSmoke(gameContent, this);

            // Collide only with Ground but not with itself and bonded Filter
            mouseFilter = new Filter();
            mouseFilter.categoryBits = 0x0002; mouseFilter.maskBits = 0x0001; mouseFilter.groupIndex = -2;

            // Collide with every thing
            atomFilter = new Filter();
            atomFilter.categoryBits = 0x0001; atomFilter.maskBits = 0x0001; atomFilter.groupIndex = 1;

            fixture.SetFilterData(ref atomFilter);

            SetMode(false, false);
        }
Esempio n. 33
0
        /// <summary> Destroy a rigid body given a definition. No reference to the definition
        /// is retained. This function is locked during callbacks.
        /// </summary> <warning> This automatically deletes all associated shapes and joints.
        /// </warning> <warning> This function is locked during callbacks.
        /// </warning>
        public void DestroyBody(Body b)
        {
            Debug.Assert(_bodyCount > 0);
            Debug.Assert(!IsLocked);
            if (IsLocked)
            {
                return;
            }

            // Delete the attached joints.
            JointEdge je = b._jointList;

            while (je != null)
            {
                JointEdge je0 = je;
                je = je.Next;

                if (DestructionListener != null)
                {
                    DestructionListener.SayGoodbye(je0.Joint);
                }

                DestroyJoint(je0.Joint);
            }
            b._jointList = null;

            // Delete the attached contacts.
            ContactEdge ce = b._contactList;

            while (ce != null)
            {
                ContactEdge ce0 = ce;
                ce = ce.Next;
                _contactManager.Destroy(ce0.Contact);
            }
            b._contactList = null;

            // Delete the attached fixtures. This destroys broad-phase proxies.
            Fixture f = b._fixtureList;

            while (f != null)
            {
                Fixture f0 = f;
                f = f._next;

                if (DestructionListener != null)
                {
                    DestructionListener.SayGoodbye(f0);
                }

                f0.DestroyProxies(_contactManager._broadPhase);
                f0.Destroy();
            }
            b._fixtureList  = null;
            b._fixtureCount = 0;

            // Remove world body list.
            if (b._prev != null)
            {
                b._prev._next = b._next;
            }

            if (b._next != null)
            {
                b._next._prev = b._prev;
            }

            if (b == _bodyList)
            {
                _bodyList = b._next;
            }

            --_bodyCount;
        }