Box2D.Dynamics.World.CreateBody C# (CSharp) Method

CreateBody() public method

create a rigid body given a definition. No reference to the definition is retained.
public CreateBody ( BodyDef def ) : Body
def BodyDef
return Body
        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;
        }

Usage Example

Example #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(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(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);
            }
        }
All Usage Examples Of Box2D.Dynamics.World::CreateBody