A body definition holds all the data needed to construct a rigid body. You can safely re-use body definitions. Shapes are added to a body after construction.
Пример #1
0
        static void Main(string[] args)
        {
            // Static Body
            Vec2 gravity = new Vec2(0, -10);
            bool doSleep = true;
            World world = new World(gravity);
            world.SleepingAllowed = doSleep;
            BodyDef groundBodyDef = new BodyDef();
            groundBodyDef.position.set_Renamed(0, -10);
            Body groundBody = world.createBody(groundBodyDef);
            PolygonShape groundBox = new PolygonShape();
            groundBox.setAsBox(50, 10);
            groundBody.createFixture(groundBox, 0);

            // Dynamic Body
            BodyDef bodyDef = new BodyDef();
            bodyDef.type = BodyType.DYNAMIC;
            bodyDef.position.set_Renamed(0, 4);
            Body body = world.createBody(bodyDef);
            PolygonShape dynamicBox = new PolygonShape();
            dynamicBox.setAsBox(1, 1);
            FixtureDef fixtureDef = new FixtureDef();
            fixtureDef.shape = dynamicBox;
            fixtureDef.density = 1;
            fixtureDef.friction = 0.3f;
            body.createFixture(fixtureDef);

            // Setup world
            float timeStep = 1.0f / 60.0f;
            int velocityIterations = 6;
            int positionIterations = 2;

            // Run loop
            for (int i = 0; i < 60; ++i)
            {
                world.step(timeStep, velocityIterations, positionIterations);
                Vec2 position = body.Position;
                float angle = body.Angle;
                Console.WriteLine("{0:0.00} {1:0.00} {2:0.00}", position.x, position.y, angle);
            }
        }
Пример #2
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;
        }
Пример #3
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;
        }
Пример #4
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;
        }
Пример #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);

            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;
        }
Пример #6
0
        private void InitializeWorld()
        {
            // Static Body
            Vec2 gravity = new Vec2(0, -10);
            bool doSleep = true;
            world = new World(gravity);
            world.SleepingAllowed = doSleep;
            BodyDef groundBodyDef = new BodyDef();
            groundBodyDef.Position.Set(0, -10);
            Body groundBody = world.CreateBody(groundBodyDef);
            PolygonShape groundBox = new PolygonShape();
            groundBox.SetAsBox(50, 10);
            groundBody.CreateFixture(groundBox, 0);

            {
                // Dynamic Body
                BodyDef bodyDef = new BodyDef();
                bodyDef.Type = BodyType.Dynamic;
                bodyDef.Position.Set(5, 4);
                bodyDef.Angle = (float)(2 * Math.PI / 3);
                Body body = world.CreateBody(bodyDef);
                PolygonShape dynamicBox = new PolygonShape();
                dynamicBox.SetAsBox(1, 1);
                FixtureDef fixtureDef = new FixtureDef();
                fixtureDef.Shape = dynamicBox;
                fixtureDef.Density = 1;
                fixtureDef.Friction = 0.3f;
                body.CreateFixture(fixtureDef);
                Bodies.Add(new BodyAdapter(body));
            }

            {
                // Dynamic Body
                BodyDef bodyDef = new BodyDef();
                bodyDef.Type = BodyType.Dynamic;
                bodyDef.Position.Set(5, 10);
                bodyDef.Angle = (float)(Math.PI / 3);
                Body body = world.CreateBody(bodyDef);
                PolygonShape dynamicBox = new PolygonShape();
                dynamicBox.SetAsBox(1, 1);
                FixtureDef fixtureDef = new FixtureDef();
                fixtureDef.Shape = dynamicBox;
                fixtureDef.Density = 1;
                fixtureDef.Friction = 0.3f;
                body.CreateFixture(fixtureDef);
                Bodies.Add(new BodyAdapter(body));
            }

            {
                // Dynamic Body
                BodyDef bodyDef = new BodyDef();
                bodyDef.Type = BodyType.Dynamic;
                bodyDef.Position.Set(4.5f, 7);
                bodyDef.AngularVelocity = (float)(2 * Math.PI);
                Body body = world.CreateBody(bodyDef);
                PolygonShape dynamicBox = new PolygonShape();
                dynamicBox.SetAsBox(1, 1);
                FixtureDef fixtureDef = new FixtureDef();
                fixtureDef.Shape = dynamicBox;
                fixtureDef.Density = 1;
                fixtureDef.Friction = 0.3f;
                body.CreateFixture(fixtureDef);
                Bodies.Add(new BodyAdapter(body));
            }
        }