A rigid body. These are created via World.createBody.
Пример #1
0
 public Fixture()
 {
     m_userData = null;
     m_body = null;
     m_next = null;
     m_proxies = null;
     m_proxyCount = 0;
     m_shape = null;
     m_filter = new Filter();
 }
Пример #2
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>
        /// <param name="body"></param>
        public virtual void destroyBody(Body body)
        {
            Debug.Assert(m_bodyCount > 0);
            Debug.Assert(Locked == false);
            if (Locked)
            {
                return;
            }

            // Delete the attached joints.
            JointEdge je = body.m_jointList;
            while (je != null)
            {
                JointEdge je0 = je;
                je = je.next;
                if (m_destructionListener != null)
                {
                    m_destructionListener.sayGoodbye(je0.joint);
                }

                destroyJoint(je0.joint);

                body.m_jointList = je;
            }
            body.m_jointList = null;

            // Delete the attached contacts.
            ContactEdge ce = body.m_contactList;
            while (ce != null)
            {
                ContactEdge ce0 = ce;
                ce = ce.next;
                m_contactManager.destroy(ce0.contact);
            }
            body.m_contactList = null;

            Fixture f = body.m_fixtureList;
            while (f != null)
            {
                Fixture f0 = f;
                f = f.m_next;

                if (m_destructionListener != null)
                {
                    m_destructionListener.sayGoodbye(f0);
                }

                f0.destroyProxies(m_contactManager.m_broadPhase);
                f0.destroy();
                // TODO djm recycle fixtures (here or in that destroy method)
                body.m_fixtureList = f;
                body.m_fixtureCount -= 1;
            }
            body.m_fixtureList = null;
            body.m_fixtureCount = 0;

            // Remove world body list.
            if (body.m_prev != null)
            {
                body.m_prev.m_next = body.m_next;
            }

            if (body.m_next != null)
            {
                body.m_next.m_prev = body.m_prev;
            }

            if (body == m_bodyList)
            {
                m_bodyList = body.m_next;
            }

            --m_bodyCount;
            // TODO djm recycle body
        }
Пример #3
0
        /// <summary>
        /// create a rigid body given a definition. No reference to the definition is retained.
        /// </summary>
        /// <warning>This function is locked during callbacks.</warning>
        /// <param name="def"></param>
        /// <returns></returns>
        public virtual Body createBody(BodyDef def)
        {
            Debug.Assert(Locked == false);
            if (Locked)
            {
                return null;
            }
            // TODO djm pooling
            Body b = new Body(def, this);

            // add to world doubly linked list
            b.m_prev = null;
            b.m_next = m_bodyList;
            if (m_bodyList != null)
            {
                m_bodyList.m_prev = b;
            }
            m_bodyList = b;
            ++m_bodyCount;

            return b;
        }
Пример #4
0
        /// <summary>
        /// Construct a world object.
        /// </summary>
        /// <param name="gravity">the world gravity vector.</param>
        /// <param name="doSleep">improve performance by not simulating inactive bodies.</param>
        public World(Vec2 gravity, IWorldPool argPool)
        {
            contactStacks = new ContactRegister[ShapeTypesCount][];
            for (int i = 0; i < ShapeTypesCount; i++)
            {
                contactStacks[i] = new ContactRegister[ShapeTypesCount];
            }

            pool = argPool;
            m_destructionListener = null;
            m_debugDraw = null;

            m_bodyList = null;
            m_jointList = null;

            m_bodyCount = 0;
            m_jointCount = 0;

            m_warmStarting = true;
            m_continuousPhysics = true;
            m_subStepping = false;
            m_stepComplete = true;

            m_allowSleep = true;
            m_gravity.set_Renamed(gravity);

            m_flags = CLEAR_FORCES;

            m_inv_dt0 = 0f;

            m_contactManager = new ContactManager(this);
            m_profile = new Profile();

            initializeRegisters();
        }
Пример #5
0
        public Body(BodyDef bd, World world)
        {
            Debug.Assert(bd.position.Valid);
            Debug.Assert(bd.linearVelocity.Valid);
            Debug.Assert(bd.gravityScale >= 0.0f);
            Debug.Assert(bd.angularDamping >= 0.0f);
            Debug.Assert(bd.linearDamping >= 0.0f);

            m_flags = 0;

            if (bd.bullet)
            {
                m_flags |= e_bulletFlag;
            }
            if (bd.fixedRotation)
            {
                m_flags |= e_fixedRotationFlag;
            }
            if (bd.allowSleep)
            {
                m_flags |= e_autoSleepFlag;
            }
            if (bd.awake)
            {
                m_flags |= e_awakeFlag;
            }
            if (bd.active)
            {
                m_flags |= e_activeFlag;
            }

            m_world = world;

            m_xf.p.set_Renamed(bd.position);
            m_xf.q.set_Renamed(bd.angle);

            m_sweep.localCenter.setZero();
            m_sweep.c0.set_Renamed(m_xf.p);
            m_sweep.c.set_Renamed(m_xf.p);
            m_sweep.a0 = bd.angle;
            m_sweep.a = bd.angle;
            m_sweep.alpha0 = 0.0f;

            m_jointList = null;
            m_contactList = null;
            m_prev = null;
            m_next = null;

            m_linearVelocity.set_Renamed(bd.linearVelocity);
            m_angularVelocity = bd.angularVelocity;

            m_linearDamping = bd.linearDamping;
            m_angularDamping = bd.angularDamping;
            m_gravityScale = bd.gravityScale;

            m_force.setZero();
            m_torque = 0.0f;

            m_sleepTime = 0.0f;

            m_type = bd.type;

            if (m_type == BodyType.DYNAMIC)
            {
                m_mass = 1f;
                m_invMass = 1f;
            }
            else
            {
                m_mass = 0f;
                m_invMass = 0f;
            }

            m_I = 0.0f;
            m_invI = 0.0f;

            m_userData = bd.userData;

            m_fixtureList = null;
            m_fixtureCount = 0;
        }
Пример #6
0
        /// <summary>
        /// This is used to prevent connected bodies from colliding. It may lie, depending on the
        /// collideConnected flag.
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public virtual bool shouldCollide(Body other)
        {
            // At least one body should be dynamic.
            if (m_type != BodyType.DYNAMIC && other.m_type != BodyType.DYNAMIC)
            {
                return false;
            }

            // Does a joint prevent collision?
            for (JointEdge jn = m_jointList; jn != null; jn = jn.next)
            {
                if (jn.other == other)
                {
                    if (jn.joint.m_collideConnected == false)
                    {
                        return false;
                    }
                }
            }

            return true;
        }
Пример #7
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>
        /// <param name="body"></param>
        public void DestroyBody(Body body)
        {
            Debug.Assert(BodyCount > 0);
            Debug.Assert(Locked == false);
            if (Locked)
            {
                return;
            }

            // Delete the attached joints.
            JointEdge je = body.JointList;
            while (je != null)
            {
                JointEdge je0 = je;
                je = je.Next;
                if (DestructionListener != null)
                {
                    DestructionListener.SayGoodbye(je0.Joint);
                }

                DestroyJoint(je0.Joint);

                body.JointList = je;
            }
            body.JointList = null;

            // Delete the attached contacts.
            ContactEdge ce = body.ContactList;
            while (ce != null)
            {
                ContactEdge ce0 = ce;
                ce = ce.Next;
                ContactManager.Destroy(ce0.Contact);
            }
            body.ContactList = null;

            Fixture f = body.FixtureList;
            while (f != null)
            {
                Fixture f0 = f;
                f = f.Next;

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

                f0.DestroyProxies(ContactManager.BroadPhase);
                f0.Destroy();
                // TODO djm recycle fixtures (here or in that destroy method)
                body.FixtureList = f;
                body.FixtureCount -= 1;
            }
            body.FixtureList = null;
            body.FixtureCount = 0;

            // Remove world body list.
            if (body.Prev != null)
            {
                body.Prev.Next = body.Next;
            }

            if (body.Next != null)
            {
                body.Next.Prev = body.Prev;
            }

            if (body == BodyList)
            {
                BodyList = body.Next;
            }

            --BodyCount;
            // TODO djm recycle body
        }
Пример #8
0
        /// <summary>
        /// create a rigid body given a definition. No reference to the definition is retained.
        /// </summary>
        /// <warning>This function is locked during callbacks.</warning>
        /// <param name="def"></param>
        /// <returns></returns>
        public Body CreateBody(BodyDef def)
        {
            Debug.Assert(Locked == false);
            if (Locked)
            {
                return null;
            }
            // TODO djm pooling
            Body b = new Body(def, this);

            // add to world doubly linked list
            b.Prev = null;
            b.Next = BodyList;
            if (BodyList != null)
            {
                BodyList.Prev = b;
            }
            BodyList = b;
            ++BodyCount;

            return b;
        }
Пример #9
0
 public void Add(Body body)
 {
     Debug.Assert(BodyCount < BodyCapacity);
     body.IslandIndex = BodyCount;
     Bodies[BodyCount] = body;
     ++BodyCount;
 }
Пример #10
0
        // We need separation create/destroy functions from the constructor/destructor because
        // the destructor cannot access the allocator (no destructor arguments allowed by C++).
        public void Create(Body body, FixtureDef def)
        {
            UserData = def.UserData;
            Friction = def.Friction;
            Restitution = def.Restitution;

            Body = body;
            Next = null;

            Filter.Set(def.Filter);

            IsSensor = def.IsSensor;

            Shape = def.Shape.Clone();

            // Reserve proxy space
            int childCount = Shape.ChildCount;
            if (Proxies == null)
            {
                Proxies = new FixtureProxy[childCount];
                for (int i = 0; i < childCount; i++)
                {
                    Proxies[i] = new FixtureProxy {Fixture = null, ProxyId = BroadPhase.NULL_PROXY};
                }
            }

            if (Proxies.Length < childCount)
            {
                FixtureProxy[] old = Proxies;
                int newLen = MathUtils.Max(old.Length * 2, childCount);
                Proxies = new FixtureProxy[newLen];
                Array.Copy(old, 0, Proxies, 0, old.Length);
                for (int i = 0; i < newLen; i++)
                {
                    if (i >= old.Length)
                    {
                        Proxies[i] = new FixtureProxy();
                    }
                    Proxies[i].Fixture = null;
                    Proxies[i].ProxyId = BroadPhase.NULL_PROXY;
                }
            }
            ProxyCount = 0;

            m_density = def.Density;
        }
Пример #11
0
        // We need separation create/destroy functions from the constructor/destructor because
        // the destructor cannot access the allocator (no destructor arguments allowed by C++).
        public virtual void create(Body body, FixtureDef def)
        {
            m_userData = def.userData;
            m_friction = def.friction;
            m_restitution = def.restitution;

            m_body = body;
            m_next = null;

            m_filter.set_Renamed(def.filter);

            m_isSensor = def.isSensor;

            m_shape = def.shape.Clone();

            // Reserve proxy space
            int childCount = m_shape.ChildCount;
            if (m_proxies == null)
            {
                m_proxies = new FixtureProxy[childCount];
                for (int i = 0; i < childCount; i++)
                {
                    m_proxies[i] = new FixtureProxy();
                    m_proxies[i].fixture = null;
                    m_proxies[i].proxyId = BroadPhase.NULL_PROXY;
                }
            }

            if (m_proxies.Length < childCount)
            {
                FixtureProxy[] old = m_proxies;
                int newLen = MathUtils.max(old.Length * 2, childCount);
                m_proxies = new FixtureProxy[newLen];
                Array.Copy(old, 0, m_proxies, 0, old.Length);
                for (int i = 0; i < newLen; i++)
                {
                    if (i >= old.Length)
                    {
                        m_proxies[i] = new FixtureProxy();
                    }
                    m_proxies[i].fixture = null;
                    m_proxies[i].proxyId = BroadPhase.NULL_PROXY;
                }
            }
            m_proxyCount = 0;

            m_density = def.density;
        }
Пример #12
0
        public Body(BodyDef bd, World world)
        {
            Debug.Assert(bd.Position.Valid);
            Debug.Assert(bd.LinearVelocity.Valid);
            Debug.Assert(bd.GravityScale >= 0.0f);
            Debug.Assert(bd.AngularDamping >= 0.0f);
            Debug.Assert(bd.LinearDamping >= 0.0f);

            Flags = TypeFlags.None;

            if (bd.Bullet)
            {
                Flags |= TypeFlags.Bullet;
            }
            if (bd.FixedRotation)
            {
                Flags |= TypeFlags.FixedRotation;
            }
            if (bd.AllowSleep)
            {
                Flags |= TypeFlags.AutoSleep;
            }
            if (bd.Awake)
            {
                Flags |= TypeFlags.Awake;
            }
            if (bd.Active)
            {
                Flags |= TypeFlags.Active;
            }

            World = world;

            Xf.P.Set(bd.Position);
            Xf.Q.Set(bd.Angle);

            Sweep.LocalCenter.SetZero();
            Sweep.C0.Set(Xf.P);
            Sweep.C.Set(Xf.P);
            Sweep.A0 = bd.Angle;
            Sweep.A = bd.Angle;
            Sweep.Alpha0 = 0.0f;

            JointList = null;
            ContactList = null;
            Prev = null;
            Next = null;

            m_linearVelocity.Set(bd.LinearVelocity);
            m_angularVelocity = bd.AngularVelocity;

            LinearDamping = bd.LinearDamping;
            AngularDamping = bd.AngularDamping;
            GravityScale = bd.GravityScale;

            Force.SetZero();
            Torque = 0.0f;

            SleepTime = 0.0f;

            m_type = bd.Type;

            if (m_type == BodyType.Dynamic)
            {
                Mass = 1f;
                InvMass = 1f;
            }
            else
            {
                Mass = 0f;
                InvMass = 0f;
            }

            I = 0.0f;
            InvI = 0.0f;

            UserData = bd.UserData;

            FixtureList = null;
            FixtureCount = 0;
        }
Пример #13
0
        /// <summary>
        /// This is used to prevent connected bodies from colliding. It may lie, depending on the
        /// collideConnected flag.
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public bool ShouldCollide(Body other)
        {
            // At least one body should be dynamic.
            if (m_type != BodyType.Dynamic && other.m_type != BodyType.Dynamic)
            {
                return false;
            }

            // Does a joint prevent collision?
            for (JointEdge jn = JointList; jn != null; jn = jn.Next)
            {
                if (jn.Other == other)
                {
                    if (jn.Joint.CollideConnected == false)
                    {
                        return false;
                    }
                }
            }

            return true;
        }
Пример #14
0
 public BodyAdapter(Body body)
 {
     this.body = body;
 }
Пример #15
0
 public virtual void add(Body body)
 {
     Debug.Assert(m_bodyCount < m_bodyCapacity);
     body.m_islandIndex = m_bodyCount;
     m_bodies[m_bodyCount] = body;
     ++m_bodyCount;
 }