예제 #1
0
        public MovingJoint()
        {
            var capeFixture = new FixtureDef(new PolygonShape(0.08f, 0.4f, (float)(Math.PI)), 0.2f);
            var capeBody = new BodyDef(BodyType.Dynamic, new Vec2(0, 10));

            Body testbody = m_world.CreateBody(capeBody);
            testbody.CreateFixture(capeFixture);

            staticBody = m_world.CreateBody(new BodyDef(BodyType.Static, testbody.WorldCenter + new Vec2(-0.08f / 25, 0.4f)));
            staticBody.CreateFixture(new CircleShape(0.15f), 0);

            {
                RevoluteJointDef rjd = new RevoluteJointDef();
                rjd.Initialize(testbody, staticBody, testbody.WorldCenter + new Vec2(0.0f, 0.4f));
                joint = (RevoluteJoint)m_world.CreateJoint(rjd);
            }

            // build cape
            Body lastBody = testbody;
            for (int i = 0; i < 8; ++i)
            {
                capeBody.Position = new Vec2(capeBody.Position.X, capeBody.Position.Y - 0.8f);

                var nextBody = m_world.CreateBody(capeBody);
                nextBody.CreateFixture(capeFixture);

                var joint = new RevoluteJointDef();
                joint.Initialize(lastBody, nextBody, nextBody.WorldCenter + new Vec2(0.0f, 0.4f));
                m_world.CreateJoint(joint);

                lastBody = nextBody;
            }
        }
예제 #2
0
 /// Initialize the bodies, anchors, and reference angle using a world
 /// anchor point.
 public void Initialize(Body body1, Body body2, Vec2 anchor)
 {
     BodyA = body1;
     BodyB = body2;
     _localAnchorA = BodyA.GetLocalPoint(anchor);
     _localAnchorB = BodyB.GetLocalPoint(anchor);
     _referenceAngle = BodyB.Angle - BodyA.Angle;
 }
예제 #3
0
        public Fixture[] AddToBody(Body body, float density)
        {
            Fixture[] fixtures = new Fixture[_shapes.Count];

            for (int i = 0; i < _shapes.Count; ++i)
                fixtures[i] = body.CreateFixture(_shapes[i], density);

            return fixtures;
        }
예제 #4
0
        /// Main...
        public ElasticBody()
        {
            Program.MainForm.ViewZoom = 0.25f;

            /// Bottom static body
            {
                PolygonShape sd = new PolygonShape();
                sd.SetAsBox(50.0f, 2.0f);
                BodyDef bd = new BodyDef();
                bd.Position = new Vec2(-1.0f, -7.5f);

                m_ground = m_world.CreateBody(bd);
                m_ground.CreateFixture(new FixtureDef(sd, 0.0f, 0.1f, 0.1f));
            }

            /// "Elastic body" 64 bodies - something like a lin. elastic compound
            /// connected via dynamic forces (springs)
            {
                PolygonShape sd = new PolygonShape();
                sd.SetAsBox(width, height);

                FixtureDef sdf = new FixtureDef();
                sdf.Density    = 1.5f;
                sdf.Friction   = 0.01f;
                sdf.Filter.GroupIndex = -1;
                sdf.Shape = sd;
                Vec2 startpoint = new Vec2(0, 0);
                BodyDef    bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.Bullet = false;
              	 			//bd.AllowSleep = false;
                for (int i = 0; i < BodyCountY; ++i)
                {
                    for (int j = 0; j < BodyCountX; ++j)
                    {
                        bd.Position = new Vec2(j*(width*2), 2.51f + (height*2) * i);
                        bd.Position  += startpoint;
                        Body body  = m_world.CreateBody(bd);
                        bodies[BodyCountX*i+j] = body;
                        body.CreateFixture(sdf);
                    }
                }
            }
        }
예제 #5
0
        public Body[] GenerateBodies(World world, Vec2 basePosition, FixtureDef def)
        {
            if (_vecs.Count <= 1)
                throw new ArgumentOutOfRangeException("Vecs");

            Body[] bodies = new Body[_vecs.Count - 1];

            for (int i = 0; i < _vecs.Count - 1; ++i)
            {
                PolygonShape edge = new PolygonShape(_vecs[i], _vecs[i + 1]);

                BodyDef bd = new BodyDef();
                bd.Position = basePosition;

                bodies[i] = world.CreateBody(bd);
                bodies[i].CreateFixture(new FixtureDef(edge, 0, def.Restitution, def.Friction, false, def.UserData));
            }

            return bodies;
        }
예제 #6
0
        public ShapeEditing()
        {
            {
                BodyDef bd = new BodyDef();
                Body ground = m_world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.SetAsEdge(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);
            }

            {
                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.Position = new Vec2(0.0f, 10.0f);
                m_body = m_world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(4.0f, 4.0f, new Vec2(0.0f, 0.0f), 0.0f);
                m_fixture1 = m_body.CreateFixture(shape, 10.0f);
                m_fixture2 = null;
            }
        }
예제 #7
0
        public TitleState(StateStack stack, Context context)
            : base(stack, context)
        {
            starttimer = new Clock();
            starttimer.Restart();

            titletext = new Text();
            titletext.Scale = new Vector2f(2, 2);
            titletext.DisplayedString = "PIXELTASIM";
            titletext.Font = mContext.mFonts.get(FontID.Title);
            titletext.centerOrigin();
            titletext.Position = new Vector2f(mContext.mWindow.Size.X/2, mContext.mWindow.Size.Y / 8);
            titletext.Color = Color.Black;

            square = new RectangleShape(new Vector2f(UnitConverter.toPixScale(0.5f), UnitConverter.toPixScale(0.5f)));
            square.centerOrigin();
            square.FillColor = Color.Red;
            square.OutlineColor = Color.Black;
            square.OutlineThickness = 1;

            b2World = new World(new Vec2(0, 10), false);
            BodyDef b2Def = new BodyDef();
            b2Def.BodyType = BodyType.Dynamic;
            b2Def.Position = new Vec2(UnitConverter.toSimScale(mContext.mWindow.Size.X / 2), UnitConverter.toSimScale(mContext.mWindow.Size.Y / 8*3));
            b2Body = b2World.CreateBody(b2Def);

            PolygonShape dynamicbox = new PolygonShape();
            dynamicbox.SetAsBox(UnitConverter.toSimScale(32), UnitConverter.toSimScale(32));

            FixtureDef fixDef = new FixtureDef();
            fixDef.Shape = dynamicbox;
            fixDef.Density = 1;
            fixDef.Friction = 0.3f;
            b2Body.CreateFixture(fixDef);

            square.Position = new Vector2f(UnitConverter.toPixScale(b2Body.Position.X), UnitConverter.toPixScale(b2Body.Position.Y));
        }
예제 #8
0
파일: Car.cs 프로젝트: RubisetCie/box2c
        public Car()
        {
            {	// car body
                PolygonShape poly1 = new PolygonShape(), poly2 = new PolygonShape();

                // bottom half
                poly1.Vertices = new Vec2[]
                {
                    new Vec2(2.2f,-0.74f),
                    new Vec2(2.2f,-0.2f),
                    new Vec2(1.0f,0),
                    new Vec2(-2.2f,0),
                    new Vec2(-2.2f,-0.74f)
                };

                FixtureDef fixture1 = new FixtureDef();

                fixture1.Filter.GroupIndex = -1;
                fixture1.Shape = poly1;
                fixture1.Density		= 20.0f;
                fixture1.Friction		= 0.68f;

                // top half
                poly2.Vertices = new Vec2[]
                {
                    new Vec2(1.0f,0),
                    new Vec2(0.5f,0.74f),
                    new Vec2(-1.3f,0.7f),
                    new Vec2(-1.7f,0),
                };

                FixtureDef fixture2 = new FixtureDef();

                fixture2.Filter.GroupIndex = -1;
                fixture2.Shape = poly2;
                fixture2.Density		= 5.0f;
                fixture2.Friction		= 0.68f;

                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.Position = new Vec2(0, 0);

                m_vehicle = m_world.CreateBody(bd);
                m_vehicle.CreateFixture(fixture1);
                m_vehicle.CreateFixture(fixture2);
            }

            {	// vehicle wheels
                CircleShape circ = new CircleShape();
                circ.Radius = 0.58608f;

                FixtureDef wheelFix = new FixtureDef();
                wheelFix.Shape = circ;
                wheelFix.Density = 40.0f;
                wheelFix.Friction = 0.8f;
                wheelFix.Filter.GroupIndex = -1;

                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.AllowSleep = false;
                bd.Position = new Vec2(1.2f, -0.8f);

                m_rightWheel = m_world.CreateBody(bd);
                m_rightWheel.CreateFixture(wheelFix);

                bd.Position = new Vec2(-1.2f, -0.8f);
                m_leftWheel = m_world.CreateBody(bd);
                m_leftWheel.CreateFixture(wheelFix);
            }

            {	// join wheels to chassis
                RevoluteJointDef jd = new RevoluteJointDef();
                jd.Initialize(m_vehicle, m_leftWheel, m_leftWheel.WorldCenter);
                jd.CollideConnected = false;
                jd.EnableMotor = true;
                jd.MaxMotorTorque = 10.0f;
                jd.MotorSpeed = 0.0f;
                m_leftJoint = (RevoluteJoint)m_world.CreateJoint(jd);

                jd.Initialize(m_vehicle, m_rightWheel, m_rightWheel.WorldCenter);
                jd.CollideConnected = false;
                m_rightJoint = (RevoluteJoint)m_world.CreateJoint(jd);
            }
        }
예제 #9
0
        public BodyTypes()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = m_world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.SetAsEdge(new Vec2(-20.0f, 0.0f), new Vec2(20.0f, 0.0f));

                FixtureDef fd = new FixtureDef();
                fd.Shape = shape;

                ground.CreateFixture(fd);
            }

            // Define attachment
            {
                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.Position = new Vec2(0.0f, 3.0f);
                m_attachment = m_world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(0.5f, 2.0f);
                m_attachment.CreateFixture(shape, 2.0f);
            }

            // Define platform
            {
                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.Position = new Vec2(-4.0f, 5.0f);
                m_platform = m_world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(0.5f, 4.0f, new Vec2(4.0f, 0.0f), 0.5f * (float)Math.PI);

                FixtureDef fd = new FixtureDef();
                fd.Shape = shape;
                fd.Friction = 0.6f;
                fd.Density = 2.0f;
                m_platform.CreateFixture(fd);

                RevoluteJointDef rjd = new RevoluteJointDef();
                rjd.Initialize(m_attachment, m_platform, new Vec2(0.0f, 5.0f));
                rjd.MaxMotorTorque = 50.0f;
                rjd.EnableMotor = true;
                m_world.CreateJoint(rjd);

                PrismaticJointDef pjd = new PrismaticJointDef();
                pjd.Initialize(ground, m_platform, new Vec2(0.0f, 5.0f), new Vec2(1.0f, 0.0f));

                pjd.MaxMotorForce = 1000.0f;
                pjd.EnableMotor = true;
                pjd.LowerTranslation = -10.0f;
                pjd.UpperTranslation = 10.0f;
                pjd.EnableLimit = true;

                m_world.CreateJoint(pjd);

                m_speed = 3.0f;
            }

            // Create a payload
            {
                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.Position = new Vec2(0.0f, 8.0f);
                m_payload = m_world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(0.75f, 0.75f);

                FixtureDef fd = new FixtureDef();
                fd.Shape = shape;
                fd.Friction = 0.6f;
                fd.Density = 2.0f;

                m_payload.CreateFixture(fd);
            }
        }
예제 #10
0
 public void Initialize(Body bodyA, Body bodyB, Vec2 anchor)
 {
     BodyA = bodyA;
     BodyB = bodyB;
     _localAnchorA = bodyA.GetLocalPoint(anchor);
     _localAnchorB = bodyB.GetLocalPoint(anchor);
     _referenceAngle = bodyB.Angle - bodyA.Angle;
 }
예제 #11
0
파일: Test.cs 프로젝트: RubisetCie/box2c
        public Test()
        {
            Vec2 gravity;
            gravity = new Vec2(0.0f, -10.0f);
            bool doSleep = true;
            m_world = new World(gravity, doSleep);
            m_bomb = null;
            m_textLine = 30;
            m_mouseJoint = null;
            m_pointCount = 0;

            m_destructionListener.test = this;
            m_world.DestructionListener = m_destructionListener;
            m_world.ContactListener = this;
            m_world.DebugDraw = m_debugDraw;

            m_bombSpawning = false;

            m_stepCount = 0;

            BodyDef bodyDef = new BodyDef();
            m_groundBody = m_world.CreateBody(bodyDef);
            m_groundBody.UserData = "Ground";
        }
예제 #12
0
파일: Test.cs 프로젝트: RubisetCie/box2c
        public void LaunchBomb(Vec2 position, Vec2 velocity)
        {
            if (m_bomb != null)
            {
                m_world.DestroyBody(m_bomb);
                m_bomb = null;
            }

            m_bomb = m_world.CreateBody(new BodyDef(BodyType.Dynamic, position, 0.0f, velocity, true));
            m_bomb.Mass = 5;

            m_bomb.CreateFixture(new FixtureDef(new CircleShape(0.3f), 20.0f));
        }
예제 #13
0
파일: Bridge.cs 프로젝트: RubisetCie/box2c
        public Bridge()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = m_world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.SetAsEdge(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);
            }

            {
                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(0.5f, 0.125f);

                FixtureDef fd = new FixtureDef();
                fd.Shape = shape;
                fd.Density = 20.0f;
                fd.Friction = 0.2f;

                RevoluteJointDef jd = new RevoluteJointDef();

                Body prevBody = ground;
                for (int i = 0; i < e_count; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.BodyType = BodyType.Dynamic;
                    bd.Position = new Vec2(-14.5f + 1.0f * i, 5.0f);
                    Body body = m_world.CreateBody(bd);
                    body.CreateFixture(fd);

                    Vec2 anchor = new Vec2(-15.0f + 1.0f * i, 5.0f);
                    jd.Initialize(prevBody, body, anchor);
                    m_world.CreateJoint(jd);

                    if (i == (e_count >> 1))
                        m_middle = body;

                    prevBody = body;
                }

                jd.Initialize(prevBody, ground, new Vec2(-15.0f + 1.0f * e_count, 5.0f));
                m_world.CreateJoint(jd);
            }

            for (int i = 0; i < 2; ++i)
            {
                Vec2[] vertices = new Vec2[3];
                vertices[0] = new Vec2(-0.5f, 0.0f);
                vertices[1] = new Vec2(0.5f, 0.0f);
                vertices[2] = new Vec2(0.0f, 1.5f);

                PolygonShape shape = new PolygonShape();
                shape.Vertices = vertices;

                FixtureDef fd = new FixtureDef();
                fd.Shape = shape;
                fd.Density = 1.0f;

                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.Position = new Vec2(-8.0f + 8.0f * i, 12.0f);
                Body body = m_world.CreateBody(bd);
                body.CreateFixture(fd);
            }

            for (int i = 0; i < 3; ++i)
            {
                CircleShape shape = new CircleShape();
                shape.Radius = 0.5f;

                FixtureDef fd = new FixtureDef();
                fd.Shape = shape;
                fd.Density = 1.0f;

                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.Position = new Vec2(-6.0f + 6.0f * i, 10.0f);
                Body body = m_world.CreateBody(bd);
                body.CreateFixture(fd);
            }
        }
예제 #14
0
        public TheoJansen()
        {
            m_offset = new Vec2(0.0f, 8.0f);
            m_motorSpeed = 2.0f;
            m_motorOn = true;
            Vec2 pivot = new Vec2(0.0f, 0.8f);

            // Ground
            {
                BodyDef bd = new BodyDef();
                Body ground = m_world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.SetAsEdge(new Vec2(-50.0f, 0.0f), new Vec2(50.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);

                shape.SetAsEdge(new Vec2(-50.0f, 0.0f), new Vec2(-50.0f, 10.0f));
                ground.CreateFixture(shape, 0.0f);

                shape.SetAsEdge(new Vec2(50.0f, 0.0f), new Vec2(50.0f, 10.0f));
                ground.CreateFixture(shape, 0.0f);

                const float groundHeight = 0.19f;
                const float groundHeight2 = groundHeight * 2;

                shape.SetAsBox(10, groundHeight, new Vec2(-14, (groundHeight / 2)), 0);
                ground.CreateFixture(shape, 0.0f);

                shape.SetAsBox(8, groundHeight, new Vec2(-14, (groundHeight / 2) + groundHeight2), 0);
                ground.CreateFixture(shape, 0.0f);

                shape.SetAsBox(6, groundHeight, new Vec2(-14, (groundHeight / 2) + groundHeight2 + groundHeight2), 0);
                ground.CreateFixture(shape, 0.0f);

                shape.SetAsBox(4, groundHeight, new Vec2(-14, (groundHeight / 2) + groundHeight2 + groundHeight2 + groundHeight2), 0);
                ground.CreateFixture(shape, 0.0f);

                shape.SetAsBox(2, groundHeight, new Vec2(-14, (groundHeight / 2) + groundHeight2 + groundHeight2 + groundHeight2 + groundHeight2), 0);
                ground.CreateFixture(shape, 0.0f);
            }

            // Balls
            for (int i = 0; i < 25; ++i)
            {
                CircleShape shape = new CircleShape();
                shape.Radius = 0.25f;

                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.Position = new Vec2(-40.0f + 2.0f * i, 3f);

                Body body = m_world.CreateBody(bd);
                body.CreateFixture(shape, 1.0f);
            }

            // Chassis
            {
                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(2.5f, 1.0f);

                FixtureDef sd = new FixtureDef();
                sd.Density = 1.0f;
                sd.Shape = shape;
                sd.Filter.GroupIndex = -1;
                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.Position = pivot + m_offset;
                m_chassis = m_world.CreateBody(bd);
                m_chassis.CreateFixture(sd);
            }

            {
                CircleShape shape = new CircleShape();
                shape.Radius = 1.6f;

                FixtureDef sd = new FixtureDef();
                sd.Density = 1.0f;
                sd.Shape = shape;
                sd.Filter.GroupIndex = -1;
                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.Position = pivot + m_offset;
                m_wheel = m_world.CreateBody(bd);
                m_wheel.CreateFixture(sd);
            }

            {
                RevoluteJointDef jd = new RevoluteJointDef();
                jd.Initialize(m_wheel, m_chassis, pivot + m_offset);
                jd.CollideConnected = false;
                jd.MotorSpeed = m_motorSpeed;
                jd.MaxMotorTorque = 400.0f;
                jd.EnableMotor = m_motorOn;
                m_motorJoint = (RevoluteJoint)m_world.CreateJoint(jd);
            }

            Vec2 wheelAnchor = pivot + new Vec2(0.0f, -0.8f);

            CreateLeg(-1.0f, wheelAnchor);
            CreateLeg(1.0f, wheelAnchor);

            m_wheel.SetTransform(m_wheel.Position, 120.0f * (float)Math.PI / 180.0f);
            CreateLeg(-1.0f, wheelAnchor);
            CreateLeg(1.0f, wheelAnchor);

            m_wheel.SetTransform(m_wheel.Position, -120.0f * (float)Math.PI / 180.0f);
            CreateLeg(-1.0f, wheelAnchor);
            CreateLeg(1.0f, wheelAnchor);
        }
예제 #15
0
 /// Initialize the bodies, anchors, axis, and reference angle using the world
 /// anchor and world axis.
 public void Initialize(Body bodyA, Body bodyB, Vec2 anchor, Vec2 axis)
 {
     BodyA = bodyA;
     BodyB = bodyB;
     _localAnchorA = bodyA.GetLocalPoint(anchor);
     _localAnchorB = bodyB.GetLocalPoint(anchor);
     _localAxisA = bodyA.GetLocalVector(axis);
 }
예제 #16
0
        /// Initialize the bodies, anchors, lengths, max lengths, and ratio using the world anchors.
        public void Initialize(Body bodyA, Body bodyB,
						Vec2 groundAnchorA, Vec2 groundAnchorB,
						Vec2 anchorA, Vec2 anchorB,
						float ratio)
        {
            BodyA = bodyA;
            BodyB = bodyB;
            _groundAnchorA = groundAnchorA;
            _groundAnchorB = groundAnchorB;
            _localAnchorA = bodyA.GetLocalPoint(anchorA);
            _localAnchorB = bodyB.GetLocalPoint(anchorB);
            Vec2 d1 = anchorA - groundAnchorA;
            _lengthA = d1.Length();
            Vec2 d2 = anchorB - groundAnchorB;
            _lengthB = d2.Length();
            _ratio = ratio;

            if (!(ratio > float.Epsilon))
                throw new ArgumentException("Ratio is too small");

            float C = _lengthA + ratio * _lengthB;
            _maxLengthA = C - ratio * b2_minPulleyLength;
            _maxLengthB = (C - b2_minPulleyLength) / ratio;
        }
예제 #17
0
파일: World.cs 프로젝트: RubisetCie/box2c
 public void DestroyBody(Body body)
 {
     NativeMethods.b2world_destroybody(_worldPtr, body.BodyPtr);
 }
예제 #18
0
 /// Add a spring force
 void AddSpringForce(Body bA, Vec2 localA, Body bB, Vec2 localB, float k, float friction, float desiredDist)
 {
     Vec2 pA = bA.GetWorldPoint(localA);
     Vec2 pB = bB.GetWorldPoint(localB);
     Vec2 diff = pB - pA;
     //Find velocities of attach points
     Vec2 vA = bA.LinearVelocity - (bA.GetWorldVector(localA).Cross(bA.AngularVelocity));
     Vec2 vB = bB.LinearVelocity - (bB.GetWorldVector(localB).Cross(bB.AngularVelocity));
     Vec2 vdiff = vB-vA;
     float dx = diff.Normalize(); //normalizes diff and puts length into dx
     float vrel = vdiff.X*diff.X + vdiff.Y*diff.Y;
     float forceMag = -k*(dx-desiredDist) - friction*vrel;
     diff *= forceMag; // diff *= forceMag
     bB.ApplyForce(diff, bA.GetWorldPoint(localA));
     diff *= -1.0f;
     bA.ApplyForce(diff, bB.GetWorldPoint(localB));
 }
예제 #19
0
 /// Initialize the bodies, anchors, and length using the world
 /// anchors.
 public void Initialize(Body bodyA, Body bodyB,
     Vec2 anchorA, Vec2 anchorB)
 {
     BodyA = bodyA;
     BodyB = bodyB;
     _localAnchorA = bodyA.GetLocalPoint(anchorA);
     _localAnchorB = bodyB.GetLocalPoint(anchorB);
     Vec2 d = anchorB - anchorA;
     _length = d.Length();
 }
예제 #20
0
        public ApplyForce()
        {
            m_world.Gravity = new Vec2(0.0f, 0.0f);

            const float k_restitution = 0.4f;

            Body ground;
            {
                BodyDef bd = new BodyDef();
                bd.Position = new Vec2(0.0f, 20.0f);
                ground = m_world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();

                FixtureDef sd = new FixtureDef();
                sd.Shape = shape;
                sd.Density = 0.0f;
                sd.Restitution = k_restitution;

                // Left vertical
                shape.SetAsEdge(new Vec2(-20.0f, -20.0f), new Vec2(-20.0f, 20.0f));
                ground.CreateFixture(sd);

                // Right vertical
                shape.SetAsEdge(new Vec2(20.0f, -20.0f), new Vec2(20.0f, 20.0f));
                ground.CreateFixture(sd);

                // Top horizontal
                shape.SetAsEdge(new Vec2(-20.0f, 20.0f), new Vec2(20.0f, 20.0f));
                ground.CreateFixture(sd);

                // Bottom horizontal
                shape.SetAsEdge(new Vec2(-20.0f, -20.0f), new Vec2(20.0f, -20.0f));
                ground.CreateFixture(sd);
            }

            {
                Transform xf1 = new Transform();
                xf1.R = new Mat22(0.3524f * (float)Math.PI);
                xf1.Position = (xf1.R * new Vec2(1.0f, 0.0f));

                Vec2[] vertices = new Vec2[3]
                {
                    (xf1 * new Vec2(-1.0f, 0.0f)),
                    (xf1 * new Vec2(1.0f, 0.0f)),
                    (xf1 * new Vec2(0.0f, 0.5f))
                };

                PolygonShape poly1 = new PolygonShape(vertices);

                FixtureDef sd1 = new FixtureDef();
                sd1.Shape = poly1;
                sd1.Density = 4.0f;

                Transform xf2 = new Transform();
                xf2.R = new Mat22(-0.3524f * (float)Math.PI);
                xf2.Position = (xf2.R * new Vec2(-1.0f, 0.0f));

                vertices = new Vec2[]
                {
                    (xf2 * new Vec2(-1.0f, 0.0f)),
                    (xf2 * new Vec2(1.0f, 0.0f)),
                    (xf2 * new Vec2(0.0f, 0.5f))
                };

                PolygonShape poly2 = new PolygonShape(vertices);

                FixtureDef sd2 = new FixtureDef();
                sd2.Shape = poly2;
                sd2.Density = 2.0f;

                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.AngularDamping = 5.0f;
                bd.LinearDamping = 0.1f;

                bd.Position = new Vec2(0.0f, 2.0f);
                bd.Angle = (float)Math.PI;
                bd.AllowSleep = false;
                m_body = m_world.CreateBody(bd);
                m_body.CreateFixture(sd1);
                m_body.CreateFixture(sd2);
            }

            {
                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(0.5f, 0.5f);

                FixtureDef fd = new FixtureDef();
                fd.Shape = shape;
                fd.Density = 1.0f;
                fd.Friction = 0.3f;

                for (int i = 0; i < 10; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.BodyType = BodyType.Dynamic;

                    bd.Position = new Vec2(0.0f, 5.0f + 1.54f * i);
                    Body body = m_world.CreateBody(bd);

                    body.CreateFixture(fd);

                    float gravity = 10.0f;
                    float I = body.Inertia;
                    float mass = body.Mass;

                    // For a circle: I = 0.5 * m * r * r ==> r = sqrt(2 * I / m)
                    float radius = (float)Math.Sqrt(2.0f * I / mass);

                    FrictionJointDef jd = new FrictionJointDef();
                    jd.LocalAnchorA = jd.LocalAnchorB = Vec2.Empty;
                    jd.BodyA = ground;
                    jd.BodyB = body;
                    jd.CollideConnected = true;
                    jd.MaxForce = mass * gravity;
                    jd.MaxTorque = mass * radius * gravity;

                    m_world.CreateJoint(jd);
                }
            }
        }