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
Exemplo n.º 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);
            }
        }
Exemplo n.º 2
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));
            }
        }