Exemple #1
0
        public LineJoint()
        {
            Body ground = null;
            {
                PolygonShape shape = new PolygonShape();
                shape.SetAsEdge(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));

                BodyDef bd = new BodyDef();
                ground = _world.CreateBody(bd);
                ground.CreateFixture(shape, 0.0f);
            }

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

                BodyDef bd = new BodyDef();
                bd.type = BodyType.Dynamic;
                bd.position = new Vector2(0.0f, 7.0f);
                Body body = _world.CreateBody(bd);
                body.CreateFixture(shape, 1.0f);

                LineJointDef jd = new LineJointDef();
                Vector2 axis = new Vector2(2.0f, 1.0f);
                axis.Normalize();
                jd.Initialize(ground, body, new Vector2(0.0f, 8.5f), axis);
                jd.motorSpeed = 0.0f;
                jd.maxMotorForce = 100.0f;
                jd.enableMotor = true;
                jd.lowerTranslation = -4.0f;
                jd.upperTranslation = 4.0f;
                jd.enableLimit = true;
                _world.CreateJoint(jd);
            }
        }
Exemple #2
0
        public Breakable()
        {
            // Ground body
            {
                BodyDef bd = new BodyDef();
                Body ground = _world.CreateBody(bd);

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

            // Breakable dynamic body
            {
                BodyDef bd = new BodyDef();
                bd.type = BodyType.Dynamic;
                bd.position = new Vector2(0.0f, 40.0f);
                bd.angle = 0.25f * Settings.b2_pi;
                _body1 = _world.CreateBody(bd);

                _shape1 = new PolygonShape();
                _shape1.SetAsBox(0.5f, 0.5f, new Vector2(-0.5f, 0.0f), 0.0f);
                _piece1 = _body1.CreateFixture(_shape1, 1.0f);

                _shape2 = new PolygonShape();
                _shape2.SetAsBox(0.5f, 0.5f, new Vector2(0.5f, 0.0f), 0.0f);
                _piece2 = _body1.CreateFixture(_shape2, 1.0f);
            }

            _break = false;
            _broke = false;
        }
Exemple #3
0
        public ContinuousTest()
        {
        {
			BodyDef bd = new BodyDef();
			bd.position = new Vector2(0.0f, 0.0f);
			Body body = _world.CreateBody(bd);

			PolygonShape shape = new PolygonShape();

			shape.SetAsEdge(new Vector2(-10.0f, 0.0f), new Vector2(10.0f, 0.0f));
			body.CreateFixture(shape, 1.0f);

			shape.SetAsBox(0.2f, 1.0f, new Vector2(0.5f, 1.0f), 0.0f);
            body.CreateFixture(shape, 1.0f);
		}

#if true
		{
			BodyDef bd = new BodyDef();
			bd.type = BodyType.Dynamic;
			bd.position = new Vector2(0.0f, 20.0f);
			//bd.angle = 0.1f;

			PolygonShape shape = new PolygonShape();
			shape.SetAsBox(2.0f, 0.1f);

			_body = _world.CreateBody(bd);
			_body.CreateFixture(shape, 1.0f);

            Launch();
		}
#else
		{
			BodyDef bd = new BodyDef();
			bd.type = BodyType.Dynamic;
			bd.position = new Vector2(0.0f, 0.5f);
			Body body = _world.CreateBody(bd);

			CircleShape shape = new CircleShape();
			shape._p = new Vector2Zero();
			shape._radius = 0.5f;
			body.CreateFixture(shape, 1.0f);

			//bd.bullet = true;
			bd.position = new Vector2(0.0f, 10.0f);
			body = _world.CreateBody(bd);
			body.CreateFixture(shape, 1.0f);
			body.SetLinearVelocity(new Vector2(0.0f, -100.0f));
		}
#endif
        }
Exemple #4
0
        VerticalStack()
        {
            {
                BodyDef bd = new BodyDef();
                Body ground = _world.CreateBody(bd);

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

                shape.SetAsEdge(new Vector2(20.0f, 0.0f), new Vector2(20.0f, 20.0f));
                ground.CreateFixture(shape, 0.0f);
            }

            float[] xs = new float[5]
            {0.0f, -10.0f, -5.0f, 5.0f, 10.0f};

            for (int j = 0; j < (int)StackOptions.e_columnCount; ++j)
            {
                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 < (int)StackOptions.e_rowCount; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.type = BodyType.Dynamic;

                    int n = j * e_rowCount + i;
                    Debug.Assert(n < e_rowCount * e_columnCount);
                    _indices[n] = n;
                    bd.userData = _indices[n];

                    float x = 0.0f;
                    //float x = Rand.RandomFloat-0.02f, 0.02f);
                    //float x = i % 2 == 0 ? -0.025f : 0.025f;
                    bd.position = new Vector2(xs[j] + x, 0.752f + 1.54f * i);
                    Body body = _world.CreateBody(bd);

                    _bodies[n] = body;

                    body.CreateFixture(fd);
                }
            }

            _bullet = null;
        }
Exemple #5
0
        public Prismatic()
        {
            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(2.0f, 0.5f);

                BodyDef bd = new BodyDef();
                bd.BodyType   = BodyType.Dynamic;
                bd.Position   = new Vec2(-10.0f, 10.0f);
                bd.Angle      = 0.5f * (float)Math.PI;
                bd.AllowSleep = false;
                Body body = m_world.CreateBody(bd);
                body.CreateFixture(shape, 5.0f);

                PrismaticJointDef pjd = new PrismaticJointDef();

                // Bouncy limit
                Vec2 axis = new Vec2(2.0f, 1.0f);
                axis.Normalize();
                pjd.Initialize(ground, body, new Vec2(0.0f, 0.0f), axis);

                // Non-bouncy limit
                //pjd.Initialize(ground, body, new Vec2(-10.0f, 10.0f), new Vec2(1.0f, 0.0f));

                pjd.MotorSpeed       = 10.0f;
                pjd.MaxMotorForce    = 10000.0f;
                pjd.EnableMotor      = true;
                pjd.LowerTranslation = 0.0f;
                pjd.UpperTranslation = 20.0f;
                pjd.EnableLimit      = false;

                m_joint = (PrismaticJoint)m_world.CreateJoint(pjd);
            }
        }
Exemple #6
0
        public Prismatic()
        {
            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(2.0f, 0.5f);

                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.Position = new Vec2(-10.0f, 10.0f);
                bd.Angle = 0.5f * (float)Math.PI;
                bd.AllowSleep = false;
                Body body = m_world.CreateBody(bd);
                body.CreateFixture(shape, 5.0f);

                PrismaticJointDef pjd = new PrismaticJointDef();

                // Bouncy limit
                Vec2 axis = new Vec2(2.0f, 1.0f);
                axis.Normalize();
                pjd.Initialize(ground, body, new Vec2(0.0f, 0.0f), axis);

                // Non-bouncy limit
                //pjd.Initialize(ground, body, new Vec2(-10.0f, 10.0f), new Vec2(1.0f, 0.0f));

                pjd.MotorSpeed = 10.0f;
                pjd.MaxMotorForce = 10000.0f;
                pjd.EnableMotor = true;
                pjd.LowerTranslation = 0.0f;
                pjd.UpperTranslation = 20.0f;
                pjd.EnableLimit = false;

                m_joint = (PrismaticJoint)m_world.CreateJoint(pjd);
            }
        }
Exemple #7
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);
                    }
                }
            }
        }
        public PrismaticJointTestBase()
        {
            Body ground;

            {
                var bd = new BodyDef();
                ground = World.CreateBody(bd);

                var shape = new EdgeShape();
                shape.SetTwoSided(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);
            }

            EnableLimit = true;
            EnableMotor = false;
            MotorSpeed  = 10.0f;

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

                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.DynamicBody;
                bd.Position.Set(0.0f, 10.0f);
                bd.Angle      = 0.5f * Settings.Pi;
                bd.AllowSleep = false;
                var body = World.CreateBody(bd);
                body.CreateFixture(shape, 5.0f);

                PrismaticJointDef pjd = new PrismaticJointDef();

                // Horizontal
                pjd.Initialize(ground, body, bd.Position, new Vector2(1.0f, 0.0f));

                pjd.MotorSpeed       = MotorSpeed;
                pjd.MaxMotorForce    = 10000.0f;
                pjd.EnableMotor      = EnableMotor;
                pjd.LowerTranslation = -10.0f;
                pjd.UpperTranslation = 10.0f;
                pjd.EnableLimit      = EnableLimit;

                Joint = (PrismaticJoint)World.CreateJoint(pjd);
            }
        }
Exemple #9
0
        private PrismaticJointTest()
        {
            Body ground;

            {
                BodyDef bd = new BodyDef();
                ground = BodyFactory.CreateFromDef(World, bd);

                EdgeShape shape = new EdgeShape();
                shape.SetTwoSided(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));
                ground.AddFixture(shape);
            }

            _enableLimit = true;
            _enableMotor = false;
            _motorSpeed  = 10.0f;

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

                BodyDef bd = new BodyDef();
                bd.Type       = BodyType.Dynamic;
                bd.Position   = new Vector2(0.0f, 10.0f);
                bd.Angle      = 0.5f * MathConstants.Pi;
                bd.AllowSleep = false;
                Body body = BodyFactory.CreateFromDef(World, bd);
                body.AddFixture(shape);

                PrismaticJointDef pjd = new PrismaticJointDef();

                // Horizontal
                pjd.Initialize(ground, body, bd.Position, new Vector2(1.0f, 0.0f));

                pjd.MotorSpeed       = _motorSpeed;
                pjd.MaxMotorForce    = 10000.0f;
                pjd.EnableMotor      = _enableMotor;
                pjd.LowerTranslation = -10.0f;
                pjd.UpperTranslation = 10.0f;
                pjd.EnableLimit      = _enableLimit;

                _joint = (PrismaticJoint)JointFactory.CreateFromDef(World, pjd);
            }
        }
Exemple #10
0
        public BulletTest()
        {
            {
                BodyDef bd = new BodyDef();
                bd.Position.Set(0.0f, 0.0f);
                Body body = m_world.CreateBody(bd);

                EdgeShape edge = new EdgeShape();

                edge.Set(new Vec2(-10.0f, 0.0f), new Vec2(10.0f, 0.0f));
                edge.Density = 0;
                body.CreateFixture(edge);

                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(0.2f, 1.0f, new Vec2(0.5f, 1.0f), 0.0f);
                shape.Density = 0;
                body.CreateFixture(shape);
            }

            {
                BodyDef bd = new BodyDef();
                bd.type = BodyType._dynamicBody;
                bd.Position.Set(0.0f, 4.0f);

                PolygonShape box = new PolygonShape();
                box.SetAsBox(2.0f, 0.1f);
                box.Density = 100;

                m_body = m_world.CreateBody(bd);
                m_body.CreateFixture(box);

                box.SetAsBox(0.25f, 0.25f);

                //m_x = RandomFloat(-1.0f, 1.0f);
                m_x = 0.20352793f;
                bd.Position.Set(m_x, 10.0f);
                bd.bullet = true;

                m_bullet = m_world.CreateBody(bd);
                m_bullet.CreateFixture(box);

                m_bullet.SetLinearVelocity(new Vec2(0.0f, -50.0f));
            }
        }
        /// <summary>
        /// Call this to initialize a Behaviour with data supplied in a file.
        /// </summary>
        /// <param name="fileName">The file to load from.</param>
        public override void LoadContent(String fileName)
        {
            base.LoadContent(fileName);

            Int32[] input = { 1, 7, 2, 2, 3, /**/ 15, 16, 14, 16, 17 };
            mMapData = new List<Int32>(input);

            TileMapRenderDefinition def = GameObjectManager.pInstance.pContentManager.Load<TileMapRenderDefinition>(fileName);

            mTexture = GameObjectManager.pInstance.pContentManager.Load<Texture2D>(def.mSpriteFileName);

            mSourceWidth = 16;
            mSourceHeight = 16;

            mMapCountWidth = mTexture.Width / mSourceWidth;
            mMapCountHeight = mTexture.Height / mSourceHeight;

            var shape = new PolygonShape();
            shape.SetAsBox(PhysicsManager.pInstance.ScreenToPhysicalWorld(mSourceWidth / 2 * 5),
                           PhysicsManager.pInstance.ScreenToPhysicalWorld(mSourceHeight / 2 * 2));

            var fd = new FixtureDef();
            fd.shape = shape;
            fd.restitution = 0.0f;
            fd.friction = 0.5f;
            fd.density = 0.0f;

            BodyDef bd = new BodyDef();
            mGround = PhysicsManager.pInstance.pWorld.CreateBody(bd);
            mGround.CreateFixture(fd);

            shape.SetAsBox(PhysicsManager.pInstance.ScreenToPhysicalWorld(mSourceWidth / 2 * 5),
                           PhysicsManager.pInstance.ScreenToPhysicalWorld(mSourceHeight / 2 * 2),
                           PhysicsManager.pInstance.ScreenToPhysicalWorld(new Vector2(mSourceWidth * -5, 0.0f)),
                           0.0f);
            fd = new FixtureDef();
            fd.shape = shape;
            fd.restitution = 0.0f;
            fd.friction = 0.5f;
            fd.density = 0.0f;
            mGround.CreateFixture(fd);

        }
        public MotorJointTest()
        {
            Body ground = null;

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

                EdgeShape shape = new EdgeShape();
                shape.Set(new Vec2(-20.0f, 0.0f), new Vec2(20.0f, 0.0f));

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

                ground.CreateFixture(fd);
            }

            // Define motorized body
            {
                BodyDef bd = new BodyDef();
                bd.type = BodyType._dynamicBody;
                bd.Position.Set(0.0f, 8.0f);
                Body body = m_world.CreateBody(bd);

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

                FixtureDef fd = new FixtureDef();
                fd.shape    = shape;
                fd.friction = 0.6f;
                fd.Density  = 2.0f;
                body.CreateFixture(fd);

                MotorJointDef mjd = new MotorJointDef();
                mjd.Initialize(ground, body);
                mjd.maxForce  = 1000.0f;
                mjd.maxTorque = 1000.0f;
                m_joint       = (MotorJoint)m_world.CreateJoint(mjd);
            }

            m_go   = false;
            m_time = 0.0f;
        }
        public void Step()
        {
            World.Step(1 / 60f, 8, 3);
            if (_count < Count)
            {
                var bd = new BodyDef
                {
                    BodyType = BodyType.DynamicBody,
                    Position = new Vector2(0.0f, 10.0f)
                };
                var body = World.CreateBody(bd);

                var shape = new PolygonShape();
                shape.SetAsBox(0.125f, 0.125f);
                body.CreateFixture(shape, 1.0f);

                ++_count;
            }
        }
Exemple #14
0
        public Body CreateBoxCollider(Body2dComponent component, float x, float y, float hx, float hy)
        {
            var bd = new BodyDef
            {
                BodyType = BodyType.DynamicBody,
                Position = new Vector2(x, y)
            };
            var body = World.CreateBody(bd);

            body.IsBullet = false;
            var shape = new PolygonShape();

            shape.SetAsBox(hx, hy, Vector2.Zero, 0);
            body.CreateFixture(shape, 1.0f);
            body.UserData = component;

            bodyComponents.Add(body, component);
            return(body);
        }
Exemple #15
0
        public DoorButton(Rectangle r, Map m)
            : base(m)
        {
            Body BodyDec = new Body(m.PhysicalWorld);

            BodyDec.BodyType = BodyType.Static;

            PolygonShape S = new PolygonShape(1f);

            S.SetAsBox(r.Width / 2, r.Height / 2);

            Fixture             = BodyDec.CreateFixture(S);
            Fixture.Restitution = 1f;
            Fixture.Friction    = 10f;

            Position = r;

            SetName("DoorButton");
        }
Exemple #16
0
        private Body AddNode(Body parent, Vector2 localAnchor, int depth, float offset, float a)
        {
            var density = 20.0f;
            var h       = new Vector2(0.0f, a);

            var p = parent.GetPosition() + localAnchor - h;

            var bodyDef = new BodyDef();

            bodyDef.BodyType = BodyType.DynamicBody;
            bodyDef.Position = p;
            var body = World.CreateBody(bodyDef);

            var shape = new PolygonShape();

            shape.SetAsBox(0.25f * a, a);
            body.CreateFixture(shape, density);

            if (depth == Depth)
            {
                return(body);
            }

            var a1    = new Vector2(offset, -a);
            var a2    = new Vector2(-offset, -a);
            var body1 = AddNode(body, a1, depth + 1, 0.5f * offset, a);
            var body2 = AddNode(body, a2, depth + 1, 0.5f * offset, a);

            var jointDef = new RevoluteJointDef();

            jointDef.BodyA        = body;
            jointDef.LocalAnchorB = h;

            jointDef.LocalAnchorA = a1;
            jointDef.BodyB        = body1;
            World.CreateJoint(jointDef);

            jointDef.LocalAnchorA = a2;
            jointDef.BodyB        = body2;
            World.CreateJoint(jointDef);

            return(body);
        }
Exemple #17
0
        public Prismatic()
	    {
		    Body ground = null;
		    {
			    BodyDef bd = new BodyDef();
			    ground = _world.CreateBody(bd);

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

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

			    BodyDef bd = new BodyDef();
                bd.type = BodyType.Dynamic;
			    bd.position = new Vector2(-10.0f, 10.0f);
			    bd.angle = 0.5f * (float)Settings.b2_pi;
			    Body body = _world.CreateBody(bd);
			    body.CreateFixture(shape, 5.0f);

                PrismaticJointDef pjd = new PrismaticJointDef();

			    // Bouncy limit
                Vector2 axis = new Vector2(2, 1);
			    axis.Normalize();
			    pjd.Initialize(ground, body, new Vector2(0.0f, 0.0f), axis);

			    // Non-bouncy limit
			    //pjd.Initialize(ground, body, new Vector2(-10.0f, 10.0f), new Vector2(1.0f, 0.0f));

			    pjd.motorSpeed = 10.0f;
			    pjd.maxMotorForce = 1000.0f;
			    pjd.enableMotor = true;
			    pjd.lowerTranslation = 0.0f;
			    pjd.upperTranslation = 20.0f;
			    pjd.enableLimit = true;

			    _joint = (PrismaticJoint)_world.CreateJoint(pjd);
		    }
	    }
        private Body AddNode(Body parent, Vector2 localAnchor, int depth, float offset, float a)
        {
            float   density = 20.0f;
            Vector2 h       = new Vector2(0.0f, a);

            Vector2 p = parent.Position + localAnchor - h;

            BodyDef bodyDef = new BodyDef();

            bodyDef.Type     = BodyType.Dynamic;
            bodyDef.Position = p;
            Body body = BodyFactory.CreateFromDef(World, bodyDef);

            PolygonShape shape = new PolygonShape(density);

            shape.SetAsBox(0.25f * a, a);
            body.AddFixture(shape);

            if (depth == _depth)
            {
                return(body);
            }

            Vector2 a1    = new Vector2(offset, -a);
            Vector2 a2    = new Vector2(-offset, -a);
            Body    body1 = AddNode(body, a1, depth + 1, 0.5f * offset, a);
            Body    body2 = AddNode(body, a2, depth + 1, 0.5f * offset, a);

            RevoluteJointDef jointDef = new RevoluteJointDef();

            jointDef.BodyA        = body;
            jointDef.LocalAnchorB = h;

            jointDef.LocalAnchorA = a1;
            jointDef.BodyB        = body1;
            JointFactory.CreateFromDef(World, jointDef);

            jointDef.LocalAnchorA = a2;
            jointDef.BodyB        = body2;
            JointFactory.CreateFromDef(World, jointDef);

            return(body);
        }
Exemple #19
0
        private PlatformerTest()
        {
            // Ground
            {
                BodyDef bd     = new BodyDef();
                Body    ground = BodyFactory.CreateFromDef(World, bd);

                EdgeShape shape = new EdgeShape();
                shape.SetTwoSided(new Vector2(-20.0f, 0.0f), new Vector2(20.0f, 0.0f));
                ground.AddFixture(shape);
            }

            // Platform
            {
                BodyDef bd = new BodyDef();
                bd.Position = new Vector2(0.0f, 10.0f);
                Body body = BodyFactory.CreateFromDef(World, bd);

                PolygonShape shape = new PolygonShape(0.0f);
                shape.SetAsBox(3.0f, 0.5f);
                _platform = body.AddFixture(shape);

                _bottom = 10.0f - 0.5f;
                _top    = 10.0f + 0.5f;
            }

            // Actor
            {
                BodyDef bd = new BodyDef();
                bd.Type     = BodyType.Dynamic;
                bd.Position = new Vector2(0.0f, 12.0f);
                Body body = BodyFactory.CreateFromDef(World, bd);

                _radius = 0.5f;
                CircleShape shape = new CircleShape(20.0f);
                shape.Radius = _radius;
                _character   = body.AddFixture(shape);

                body.LinearVelocity = new Vector2(0.0f, -50.0f);

                _state = State.e_unknown;
            }
        }
Exemple #20
0
        public Body AddNode(Body parent, Vec2 localAnchor, int depth, float offset, float a)
        {
            Vec2 h = new Vec2(0.0f, a);

            Vec2 p = parent.GetPosition() + localAnchor - h;

            BodyDef bodyDef = new BodyDef();

            bodyDef.type     = BodyType._dynamicBody;
            bodyDef.Position = p;
            Body body = m_world.CreateBody(bodyDef);

            PolygonShape shape = new PolygonShape();

            shape.SetAsBox(0.25f * a, a);
            shape.Density = 20;
            body.CreateFixture(shape);

            if (depth == e_depth)
            {
                return(body);
            }

            Vec2 a1    = new Vec2(offset, -a);
            Vec2 a2    = new Vec2(-offset, -a);
            Body body1 = AddNode(body, a1, depth + 1, 0.5f * offset, a);
            Body body2 = AddNode(body, a2, depth + 1, 0.5f * offset, a);

            RevoluteJointDef jointDef = new RevoluteJointDef();

            jointDef.bodyA        = body;
            jointDef.localAnchorB = h;

            jointDef.localAnchorA = a1;
            jointDef.bodyB        = body1;
            m_world.CreateJoint(jointDef);

            jointDef.localAnchorA = a2;
            jointDef.bodyB        = body2;
            m_world.CreateJoint(jointDef);

            return(body);
        }
Exemple #21
0
        public Prismatic()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = _world.CreateBody(bd);

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

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

                BodyDef bd = new BodyDef();
                bd.type     = BodyType.Dynamic;
                bd.position = new Vector2(-10.0, 10.0);
                bd.angle    = 0.5 * Alt.Box2D.Settings.b2_pi;
                Body body = _world.CreateBody(bd);
                body.CreateFixture(shape, 5.0);

                PrismaticJointDef pjd = new PrismaticJointDef();

                // Bouncy limit
                Vector2 axis = new Vector2(2, 1);
                axis.Normalize();
                pjd.Initialize(ground, body, new Vector2(0.0, 0.0), axis);

                // Non-bouncy limit
                //pjd.Initialize(ground, body, new Vector2(-10.0, 10.0), new Vector2(1.0, 0.0));

                pjd.motorSpeed       = 10.0;
                pjd.maxMotorForce    = 1000.0;
                pjd.enableMotor      = true;
                pjd.lowerTranslation = 0.0;
                pjd.upperTranslation = 20.0;
                pjd.enableLimit      = true;

                _joint = (PrismaticJoint)_world.CreateJoint(pjd);
            }
        }
Exemple #22
0
        public Chain()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = m_world.CreateBody(bd);

                EdgeShape shape = new EdgeShape();
                shape.Set(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f));
                shape.Density = 0;
                ground.CreateFixture(shape);
            }

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

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

                RevoluteJointDef jd = new RevoluteJointDef();
                jd.collideConnected = false;

                const float y        = 25.0f;
                Body        prevBody = ground;
                for (int i = 0; i < 30; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.type = BodyType._dynamicBody;
                    bd.Position.Set(0.5f + i, y);
                    Body body = m_world.CreateBody(bd);
                    body.CreateFixture(fd);

                    Vec2 anchor = new Vec2((float)(i), y);
                    jd.Initialize(prevBody, body, anchor);
                    m_world.CreateJoint(jd);

                    prevBody = body;
                }
            }
        }
Exemple #23
0
        public OneSidedPlatform()
        {
            // Ground
            {
                BodyDef bd = new BodyDef();
                Body ground = _world.CreateBody(bd);

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

            // Platform
            {
                BodyDef bd = new BodyDef();
                bd.position = new Vector2(0.0f, 10.0f);
                Body body = _world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(3.0f, 0.5f);
                _platform = body.CreateFixture(shape, 0.0f);

                _bottom = 10.0f - 0.5f;
                _top = 10.0f + 0.5f;
            }

            // Actor
            {
                BodyDef bd = new BodyDef();
                bd.type = BodyType.Dynamic;
                bd.position = new Vector2(0.0f, 12.0f);
                Body body = _world.CreateBody(bd);

                _radius = 0.5f;
                CircleShape shape = new CircleShape();
                shape._radius = _radius;
                _character = body.CreateFixture(shape, 20.0f);

                body.SetLinearVelocity(new Vector2(0.0f, -50.0f));

                _state = State.e_unknown;
            }
        }
        public MotorJoint()
        {
            Body ground;

            {
                var bd = new BodyDef();
                ground = World.CreateBody(bd);

                var shape = new EdgeShape();
                shape.SetTwoSided(new Vector2(-20.0f, 0.0f), new Vector2(20.0f, 0.0f));

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

                ground.CreateFixture(fd);
            }

            // Define motorized body
            {
                var bd = new BodyDef();
                bd.BodyType = BodyType.DynamicBody;
                bd.Position.Set(0.0f, 8.0f);
                var body = World.CreateBody(bd);

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

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

                var mjd = new MotorJointDef();
                mjd.Initialize(ground, body);
                mjd.MaxForce  = 1000.0f;
                mjd.MaxTorque = 1000.0f;
                _joint        = (Box2DSharp.Dynamics.Joints.MotorJoint)World.CreateJoint(mjd);
            }

            _go   = false;
            _time = 0.0f;
        }
Exemple #25
0
        public OneSidedPlatform()
        {
            // Ground
            {
                BodyDef bd     = new BodyDef();
                Body    ground = _world.CreateBody(bd);

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

            // Platform
            {
                BodyDef bd = new BodyDef();
                bd.position = new Vector2(0.0f, 10.0f);
                Body body = _world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(3.0f, 0.5f);
                _platform = body.CreateFixture(shape, 0.0f);

                _bottom = 10.0f - 0.5f;
                _top    = 10.0f + 0.5f;
            }

            // Actor
            {
                BodyDef bd = new BodyDef();
                bd.type     = BodyType.Dynamic;
                bd.position = new Vector2(0.0f, 12.0f);
                Body body = _world.CreateBody(bd);

                _radius = 0.5f;
                CircleShape shape = new CircleShape();
                shape._radius = _radius;
                _character    = body.CreateFixture(shape, 1.0f);

                body.SetLinearVelocity(new Vector2(0.0f, -50.0f));

                _state = State.e_unknown;
            }
        }
Exemple #26
0
        public Chain()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = _world.CreateBody(bd);

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

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

                FixtureDef fd = new FixtureDef();
                fd.shape    = shape;
                fd.density  = 20.0;
                fd.friction = 0.2;

                RevoluteJointDef jd = new RevoluteJointDef();
                jd.collideConnected = false;

                double y        = 25.0;
                Body   prevBody = ground;
                for (int i = 0; i < 30; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.type     = BodyType.Dynamic;
                    bd.position = new Vector2(0.5f + i, y);
                    Body body = _world.CreateBody(bd);
                    body.CreateFixture(fd);

                    Vector2 anchor = new Vector2((double)i, y);
                    jd.Initialize(prevBody, body, anchor);
                    _world.CreateJoint(jd);

                    prevBody = body;
                }
            }
        }
Exemple #27
0
        public Pyramid()
        {
            {
                BodyDef bd     = new BodyDef();
                Body    ground = m_world.CreateBody(bd);

                EdgeShape shape = new EdgeShape();
                shape.Set(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f));
                shape.Density = 0;
                ground.CreateFixture(shape);
            }

            {
                float        a     = 0.5f;
                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(a, a);
                shape.Density = 5;

                Vec2 x = new Vec2(-7.0f, 0.75f);
                Vec2 y;
                Vec2 deltaX = new Vec2(0.5625f, 1.25f);
                Vec2 deltaY = new Vec2(1.125f, 0.0f);

                for (int i = 0; i < e_count; ++i)
                {
                    y = x;

                    for (int j = i; j < e_count; ++j)
                    {
                        BodyDef bd = new BodyDef();
                        bd.type     = BodyType._dynamicBody;
                        bd.Position = y;
                        Body body = m_world.CreateBody(bd);
                        body.CreateFixture(shape);

                        y += deltaY;
                    }

                    x += deltaX;
                }
            }
        }
Exemple #28
0
        Pulleys()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = _world.CreateBody(bd);

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

            {
                double a = 2.0;
                double b = 4.0;
                double y = 16.0;
                double L = 12.0;

                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(a, b);

                BodyDef bd = new BodyDef();
                bd.type = BodyType.Dynamic;

                bd.position = new Vector2(-10.0, y);
                Body body1 = _world.CreateBody(bd);
                body1.CreateFixture(shape, 5.0);

                bd.position = new Vector2(10.0, y);
                Body body2 = _world.CreateBody(bd);
                body2.CreateFixture(shape, 5.0);

                PulleyJointDef pulleyDef     = new PulleyJointDef();
                Vector2        anchor1       = new Vector2(-10.0, y + b);
                Vector2        anchor2       = new Vector2(10.0, y + b);
                Vector2        groundAnchor1 = new Vector2(-10.0, y + b + L);
                Vector2        groundAnchor2 = new Vector2(10.0, y + b + L);
                pulleyDef.Initialize(body1, body2, groundAnchor1, groundAnchor2, anchor1, anchor2, 2.0f);

                _joint1 = (PulleyJoint)_world.CreateJoint(pulleyDef);
            }
        }
Exemple #29
0
        public Chain()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = _world.CreateBody(bd);

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

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

                FixtureDef fd = new FixtureDef();
                fd.shape = shape;
                fd.density = 20.0f;
                fd.friction = 0.2f;

                RevoluteJointDef jd = new RevoluteJointDef();
                jd.collideConnected = false;

                float y = 25.0f;
                Body prevBody = ground;
                for (int i = 0; i < 30; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.type = BodyType.Dynamic;
                    bd.position = new Vector2(0.5f + i, y);
                    Body body = _world.CreateBody(bd);
                    body.CreateFixture(fd);

                    Vector2 anchor = new Vector2((float)i, y);
                    jd.Initialize(prevBody, body, anchor);
                    _world.CreateJoint(jd);

                    prevBody = body;
                }
            }
        }
Exemple #30
0
        public ConveyorBelt()
        {
            // Ground
            {
                BodyDef bd     = new BodyDef();
                Body    ground = m_world.CreateBody(bd);

                EdgeShape shape = new EdgeShape();
                shape.Set(new Vec2(-20.0f, 0.0f), new Vec2(20.0f, 0.0f));
                shape.Density = 0;
                ground.CreateFixture(shape);
            }

            // Platform
            {
                BodyDef bd = new BodyDef();
                bd.Position.Set(-5.0f, 5.0f);
                Body body = m_world.CreateBody(bd);

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

                FixtureDef fd = new FixtureDef();
                fd.shape    = shape;
                fd.friction = 0.8f;
                m_platform  = body.CreateFixture(fd);
            }

            // Boxes
            for (int i = 0; i < 5; ++i)
            {
                BodyDef bd = new BodyDef();
                bd.type = BodyType._dynamicBody;
                bd.Position.Set(-10.0f + 2.0f * i, 7.0f);
                Body body = m_world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(0.5f, 0.5f);
                shape.Density = 20;
                body.CreateFixture(shape);
            }
        }
Exemple #31
0
        Pulleys()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = _world.CreateBody(bd);

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

            {
                float a = 2.0f;
                float b = 4.0f;
                float y = 16.0f;
                float L = 12.0f;

                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(a, b);

                BodyDef bd = new BodyDef();
                bd.type = BodyType.Dynamic;

                bd.position = new Vector2(-10.0f, y);
                Body body1 = _world.CreateBody(bd);
                body1.CreateFixture(shape, 5.0f);

                bd.position = new Vector2(10.0f, y);
                Body body2 = _world.CreateBody(bd);
                body2.CreateFixture(shape, 5.0f);

                PulleyJointDef pulleyDef = new PulleyJointDef();
                Vector2 anchor1 = new Vector2(-10.0f, y + b);
                Vector2 anchor2 = new Vector2(10.0f, y + b);
                Vector2 groundAnchor1 = new Vector2(-10.0f, y + b + L);
                Vector2 groundAnchor2 = new Vector2(10.0f, y + b + L);
                pulleyDef.Initialize(body1, body2, groundAnchor1, groundAnchor2, anchor1, anchor2, 2.0f);

                _joint1 = (PulleyJoint)_world.CreateJoint(pulleyDef);
            }
        }
        private MotorJointTest()
        {
            Body ground;

            {
                BodyDef bd = new BodyDef();
                ground = BodyFactory.CreateFromDef(World, bd);

                EdgeShape shape = new EdgeShape();
                shape.SetTwoSided(new Vector2(-20.0f, 0.0f), new Vector2(20.0f, 0.0f));

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

                ground.AddFixture(fd);
            }

            // Define motorized body
            {
                BodyDef bd = new BodyDef();
                bd.Type     = BodyType.Dynamic;
                bd.Position = new Vector2(0.0f, 8.0f);
                Body body = BodyFactory.CreateFromDef(World, bd);

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

                FixtureDef fd = new FixtureDef();
                fd.Shape    = shape;
                fd.Friction = 0.6f;
                body.AddFixture(fd);

                MotorJointDef mjd = new MotorJointDef();
                mjd.Initialize(ground, body);
                mjd.MaxForce  = 1000.0f;
                mjd.MaxTorque = 1000.0f;
                _joint        = (MotorJoint)JointFactory.CreateFromDef(World, mjd);
            }

            _go   = false;
            _time = 0.0f;
        }
Exemple #33
0
        public Pulleys()
        {
            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);
            }

            {
                float a = 2.0f;
                float b = 4.0f;
                float y = 16.0f;
                float L = 12.0f;

                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(a, b);

                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;

                bd.Position = new Vec2(-10.0f, y);
                Body body1 = m_world.CreateBody(bd);
                body1.CreateFixture(shape, 5.0f);

                bd.Position = new Vec2(10.0f, y);
                Body body2 = m_world.CreateBody(bd);
                body2.CreateFixture(shape, 5.0f);

                PulleyJointDef pulleyDef     = new PulleyJointDef();
                Vec2           anchor1       = new Vec2(-10.0f, y + b);
                Vec2           anchor2       = new Vec2(10.0f, y + b);
                Vec2           groundAnchor1 = new Vec2(-10.0f, y + b + L);
                Vec2           groundAnchor2 = new Vec2(10.0f, y + b + L);
                pulleyDef.Initialize(body1, body2, groundAnchor1, groundAnchor2, anchor1, anchor2, 2.0f);

                m_joint1 = (PulleyJoint)m_world.CreateJoint(pulleyDef);
            }
        }
Exemple #34
0
        public Wheel(World world)
        {
            //_mGroundAreas = new List<WaterArea>();

            Body = new Body(world)
            {
                BodyType = BodyType.Dynamic, IsSensor = true, UserData = this
            };

            var shape = new PolygonShape(1);

            shape.SetAsBox(ConvertUnits.ToMeters(0.03125f), ConvertUnits.ToMeters(0.125f));
            var fixture = Body.CreateFixture(shape);

            fixture.UserData = this;
            Body.UserData    = this;

            _mCurrentTraction = 1;
            _mCurrentDrag     = 1;
        }
Exemple #35
0
        private ChainTest()
        {
            Body ground;
            {
                BodyDef bd = new BodyDef();
                ground = BodyFactory.CreateFromDef(World, bd);

                EdgeShape shape = new EdgeShape(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));
                ground.AddFixture(shape);
            }

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

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

                RevoluteJointDef jd = new RevoluteJointDef();
                jd.CollideConnected = false;

                const float y        = 25.0f;
                Body        prevBody = ground;
                for (int i = 0; i < 30; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.Type     = BodyType.Dynamic;
                    bd.Position = new Vector2(0.5f + i, y);
                    Body body = BodyFactory.CreateFromDef(World, bd);

                    body.AddFixture(fd);

                    Vector2 anchor = new Vector2(i, y);
                    jd.Initialize(prevBody, body, anchor);
                    JointFactory.CreateFromDef(World, jd);

                    prevBody = body;
                }
            }
        }
Exemple #36
0
        protected override void Create()
        {
            {
                var bd = new BodyDef();
                bd.Position.Set(0.0f, 0.0f);
                var body = World.CreateBody(bd);

                var edge = new EdgeShape();

                edge.Set(new Vector2(-10.0f, 0.0f), new Vector2(10.0f, 0.0f));
                body.CreateFixture(edge, 0.0f);

                var shape = new PolygonShape();
                shape.SetAsBox(0.2f, 1.0f, new Vector2(0.5f, 1.0f), 0.0f);
                body.CreateFixture(shape, 0.0f);
            }

            {
                var bd = new BodyDef();
                bd.BodyType = BodyType.DynamicBody;
                bd.Position.Set(0.0f, 4.0f);

                var box = new PolygonShape();
                box.SetAsBox(2.0f, 0.1f);

                _body = World.CreateBody(bd);
                _body.CreateFixture(box, 1.0f);

                box.SetAsBox(0.25f, 0.25f);

                //m_x = RandomFloat(-1.0f, 1.0f);
                _x          = 0.20352793f;
                bd.Position = new Vector2(_x, 10.0f);
                bd.Bullet   = true;

                _bullet = World.CreateBody(bd);
                _bullet.CreateFixture(box, 100.0f);

                _bullet.SetLinearVelocity(new Vector2(0.0f, -50.0f));
            }
        }
Exemple #37
0
        /// <summary>
        /// 创建一个矩形Body
        /// </summary>
        /// <param name="x">坐标 X</param>
        /// <param name="y">坐标 Y</param>
        /// <param name="width">矩形的宽</param>
        /// <param name="height">矩形的高</param>
        /// <param name="bodyType">物体类型,默认是DynamicBody</param>
        /// <param name="isSensor">是否不参加物理模拟,默认否</param>
        /// <returns></returns>
        public Body CreateRectangleBody(float x, float y, float width, float height, BodyType bodyType = BodyType.DynamicBody, bool isSensor = false)
        {
            var bodyDef = CreateBodyDef(x, y, bodyType);

            var body      = world.CreateBody(bodyDef);
            var bodyShape = new PolygonShape();

            bodyShape.SetAsBox(width / 2, height / 2);

            var fixtureDef = new FixtureDef
            {
                Shape    = bodyShape,
                Density  = 1.0f,
                Friction = 0.3f,
                IsSensor = isSensor
            };

            body.CreateFixture(fixtureDef);

            return(body);
        }
Exemple #38
0
        public OneSidedPlatform()
        {
            // Ground
            {
                BodyDef bd = new BodyDef();
                Body ground = m_world.CreateBody(bd);

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

            // Platform
            {
                BodyDef bd = new BodyDef();
                bd.Position = new Vec2(0.0f, 10.0f);
                Body body = m_world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(3.0f, 0.5f);
                m_platform = body.CreateFixture(shape, 0.0f);

                m_top = 10.0f + 0.5f;
            }

            // Actor
            {
                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.Position = new Vec2(0.0f, 12.0f);
                Body body = m_world.CreateBody(bd);

                m_radius = 0.5f;
                CircleShape shape = new CircleShape();
                shape.Radius = m_radius;
                m_character = body.CreateFixture(shape, 20.0f);

                body.LinearVelocity = new Vec2(0.0f, -50.0f);
            }
        }
Exemple #39
0
        private void CreateBoxes()
        {
            if (_body1 != null)
            {
                World.RemoveBody(_body1);
                _body1 = null;
            }

            if (_body2 != null)
            {
                World.RemoveBody(_body2);
                _body2 = null;
            }

            {
                BodyDef bd = new BodyDef();
                bd.Type       = BodyType.Dynamic;
                bd.Position   = new Vector2(8.0f, 2.6f) + _offset1;
                bd.AllowSleep = false;
                _body1        = BodyFactory.CreateFromDef(World, bd);

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

                _body1.AddFixture(shape);
            }

            {
                BodyDef bd = new BodyDef();
                bd.Type       = BodyType.Dynamic;
                bd.Position   = new Vector2(8.0f, 2.6f) + _offset2;
                bd.AllowSleep = false;
                _body2        = BodyFactory.CreateFromDef(World, bd);

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

                _body2.AddFixture(shape);
            }
        }
Exemple #40
0
        public ConveyorBelt()
        {
            // Ground
            {
                var bd     = new BodyDef();
                var ground = World.CreateBody(bd);

                var shape = new EdgeShape();
                shape.SetTwoSided(new Vector2(-20.0f, 0.0f), new Vector2(20.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);
            }

            // Platform
            {
                var bd = new BodyDef();
                bd.Position.Set(-5.0f, 5.0f);
                var body = World.CreateBody(bd);

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

                var fd = new FixtureDef();
                fd.Shape    = shape;
                fd.Friction = 0.8f;
                _platform   = body.CreateFixture(fd);
            }

            // Boxes
            for (var i = 0; i < 5; ++i)
            {
                var bd = new BodyDef();
                bd.BodyType = BodyType.DynamicBody;
                bd.Position.Set(-10.0f + 2.0f * i, 7.0f);
                var body = World.CreateBody(bd);

                var shape = new PolygonShape();
                shape.SetAsBox(0.5f, 0.5f);
                body.CreateFixture(shape, 20.0f);
            }
        }
        public OneSidePlatform()
        {
            // Ground
            {
                var bd     = new BodyDef();
                var ground = World.CreateBody(bd);

                var shape = new EdgeShape();
                shape.Set(new Vector2(-20.0f, 0.0f), new Vector2(20.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);
            }

            // Platform
            {
                var bd = new BodyDef();
                bd.Position.Set(0.0f, 10.0f);
                var body = World.CreateBody(bd);

                var shape = new PolygonShape();
                shape.SetAsBox(3.0f, 0.5f);
                _platform = body.CreateFixture(shape, 0.0f);

                _top = 10.0f + 0.5f;
            }

            // Actor
            {
                var bd = new BodyDef();
                bd.BodyType = BodyType.DynamicBody;
                bd.Position.Set(0.0f, 12.0f);
                var body = World.CreateBody(bd);

                _radius = 0.5f;
                var shape = new CircleShape();
                shape.Radius = _radius;
                _character   = body.CreateFixture(shape, 20.0f);

                body.SetLinearVelocity(new Vector2(0.0f, -50.0f));
            }
        }
Exemple #42
0
        protected override void Create()
        {
            {
                var bd     = new BodyDef();
                var ground = World.CreateBody(bd);

                var shape = new EdgeShape();
                shape.Set(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);
            }

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

                var     x = new Vector2(-7.0f, 0.75f);
                Vector2 y;
                var     deltaX = new Vector2(0.5625f, 1.25f);
                var     deltaY = new Vector2(1.125f, 0.0f);

                for (var i = 0; i < Count; ++i)
                {
                    y = x;

                    for (var j = i; j < Count; ++j)
                    {
                        var bd = new BodyDef();
                        bd.BodyType = BodyType.DynamicBody;
                        bd.Position = y;
                        var body = World.CreateBody(bd);
                        body.CreateFixture(shape, 5.0f);

                        y += deltaY;
                    }

                    x += deltaX;
                }
            }
        }
Exemple #43
0
        public Pyramid()
        {
            {
                BodyDef bd = new BodyDef();
                Body ground = _world.CreateBody(bd);

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

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

                Vector2 x = new Vector2(-7.0f, 0.75f);
                Vector2 y;
                Vector2 deltaX = new Vector2(0.5625f, 1.25f);
                Vector2 deltaY = new Vector2(1.125f, 0.0f);

                for (int i = 0; i < e_count; ++i)
                {
                    y = x;

                    for (int j = i; j < e_count; ++j)
                    {
                        BodyDef bd = new BodyDef();
                        bd.type = BodyType.Dynamic;
                        bd.position = y;
                        Body body = _world.CreateBody(bd);
                        body.CreateFixture(shape, 5.0f);

                        y += deltaY;
                    }

                    x += deltaX;
                }
            }
        }
Exemple #44
0
        public ShapeEditing()
	    {
		    {
			    BodyDef bd = new BodyDef();
			    Body ground = _world.CreateBody(bd);

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

            BodyDef bodydef = new BodyDef();
            bodydef.type = BodyType.Dynamic;
		    bodydef.position = new Vector2(0.0f, 10.0f);
		    _body = _world.CreateBody(bodydef);

		    PolygonShape shape2 = new PolygonShape();
		    shape2.SetAsBox(4.0f, 4.0f, new Vector2(0.0f, 0.0f), 0.0f);
		    _fixture1 = _body.CreateFixture(shape2, 10.0f);

		    _fixture2 = null;
	    }
Exemple #45
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;
            }
        }
Exemple #46
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));
        }
Exemple #47
0
        public ApplyForce()
        {
            _world.Gravity = new Vector2(0.0f, 0.0f);

		    const float k_restitution = 0.4f;

            Body ground;
		    {
			    BodyDef bd = new BodyDef();
			    bd.position = new Vector2(0.0f, 20.0f);
			    ground = _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 Vector2(-20.0f, -20.0f), new Vector2(-20.0f, 20.0f));
			    ground.CreateFixture(sd);

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

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

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

		    {
                Transform xf1 = new Transform();
			    xf1.R.Set(0.3524f * (float)Settings.b2_pi);
			    xf1.Position = MathUtils.Multiply(ref xf1.R, new Vector2(1.0f, 0.0f));

			    Vector2[] vertices = new Vector2[3];
			    vertices[0] = MathUtils.Multiply(ref xf1, new Vector2(-1.0f, 0.0f));
			    vertices[1] = MathUtils.Multiply(ref xf1, new Vector2(1.0f, 0.0f));
			    vertices[2] = MathUtils.Multiply(ref xf1, new Vector2(0.0f, 0.5f));
    			
			    PolygonShape poly1 = new PolygonShape();
			    poly1.Set(vertices, 3);

			    FixtureDef sd1 = new FixtureDef();
			    sd1.shape = poly1;
			    sd1.density = 4.0f;

                Transform xf2 = new Transform();
                xf2.R.Set(-0.3524f * (float)Settings.b2_pi);
			    xf2.Position = MathUtils.Multiply(ref xf2.R, new Vector2(-1.0f, 0.0f));

			    vertices[0] = MathUtils.Multiply(ref xf2, new Vector2(-1.0f, 0.0f));
			    vertices[1] = MathUtils.Multiply(ref xf2, new Vector2(1.0f, 0.0f));
			    vertices[2] = MathUtils.Multiply(ref xf2, new Vector2(0.0f, 0.5f));
    			
			    PolygonShape poly2 = new PolygonShape();
			    poly2.Set(vertices, 3);

                FixtureDef sd2 = new FixtureDef();
			    sd2.shape = poly2;
			    sd2.density = 2.0f;

			    BodyDef bd = new BodyDef();
                bd.type = BodyType.Dynamic;
			    bd.angularDamping = 5.0f;
			    bd.linearDamping = 0.1f;

			    bd.position = new Vector2(0.0f, 2.0f);
                bd.angle = (float)Settings.b2_pi;
                bd.allowSleep = false;
			    _body = _world.CreateBody(bd);
			    _body.CreateFixture(sd1);
			    _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.type = BodyType.Dynamic;

                    bd.position = new Vector2(0.0f, 5.0f + 1.54f * i);
                    Body body = _world.CreateBody(bd);

                    body.CreateFixture(fd);

                    float gravity = 10.0f;
                    float I = body.GetInertia();
                    float mass = body.GetMass();

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

                    FrictionJointDef jd = new FrictionJointDef();
                    jd.localAnchorA = Vector2.Zero;
                    jd.localAnchorB = Vector2.Zero;
                    jd.bodyA = ground;
                    jd.bodyB = body;
                    jd.collideConnected = true;
                    jd.maxForce = mass * gravity;
                    jd.maxTorque = mass * radius * gravity;

                    _world.CreateJoint(jd);
                }
            }
        }
Exemple #48
0
        public CompoundShapes()
        {
            {
                BodyDef bd = new BodyDef();
                bd.position = new Vector2(0.0f, 0.0f);
                Body body = _world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.SetAsEdge(new Vector2(50.0f, 0.0f), new Vector2(-50.0f, 0.0f));

                body.CreateFixture(shape, 0.0f);
            }

            {
                CircleShape circle1 = new CircleShape();
                circle1._radius = 0.5f;
                circle1._p = new Vector2(-0.5f, 0.5f);

                CircleShape circle2 = new CircleShape();
                circle2._radius = 0.5f;
                circle2._p = new Vector2(0.5f, 0.5f);

                for (int i = 0; i < 10; ++i)
                {
                    float x = Rand.RandomFloat(-0.1f, 0.1f);
                    BodyDef bd = new BodyDef();
                    bd.type = BodyType.Dynamic;
                    bd.position = new Vector2(x + 5.0f, 1.05f + 2.5f * i);
                    bd.angle = Rand.RandomFloat(-(float)Settings.b2_pi, (float)Settings.b2_pi);
                    Body body = _world.CreateBody(bd);
                    body.CreateFixture(circle1, 2.0f);
                    body.CreateFixture(circle2, 0.0f);
                }
            }

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

                PolygonShape polygon2 = new PolygonShape();
                polygon2.SetAsBox(0.25f, 0.5f, new Vector2(0.0f, -0.5f), 0.5f * (float)Settings.b2_pi);

                for (int i = 0; i < 10; ++i)
                {
                    float x = Rand.RandomFloat(-0.1f, 0.1f);
                    BodyDef bd = new BodyDef();
                    bd.type = BodyType.Dynamic;
                    bd.position = new Vector2(x - 5.0f, 1.05f + 2.5f * i);
                    bd.angle = Rand.RandomFloat(-(float)Settings.b2_pi, (float)Settings.b2_pi);
                    Body body = _world.CreateBody(bd);
                    body.CreateFixture(polygon1, 2.0f);
                    body.CreateFixture(polygon2, 2.0f);
                }
            }

            {
                Transform xf1 = new Transform();
                xf1.R.Set(0.3524f * (float)Settings.b2_pi);
                xf1.Position = MathUtils.Multiply(ref xf1.R, new Vector2(1.0f, 0.0f));

                Vector2[] vertices = new Vector2[3];

                PolygonShape triangle1 = new PolygonShape();
                vertices[0] = MathUtils.Multiply(ref xf1, new Vector2(-1.0f, 0.0f));
                vertices[1] = MathUtils.Multiply(ref xf1, new Vector2(1.0f, 0.0f));
                vertices[2] = MathUtils.Multiply(ref xf1, new Vector2(0.0f, 0.5f));
                triangle1.Set(vertices, 3);

                Transform xf2 = new Transform();
                xf2.R.Set(-0.3524f * (float)Settings.b2_pi);
                xf2.Position = MathUtils.Multiply(ref xf2.R, new Vector2(-1.0f, 0.0f));

                PolygonShape triangle2 = new PolygonShape();
                vertices[0] = MathUtils.Multiply(ref xf2, new Vector2(-1.0f, 0.0f));
                vertices[1] = MathUtils.Multiply(ref xf2, new Vector2(1.0f, 0.0f));
                vertices[2] = MathUtils.Multiply(ref xf2, new Vector2(0.0f, 0.5f));
                triangle2.Set(vertices, 3);

                for (int i = 0; i < 10; ++i)
                {
                    float x = Rand.RandomFloat(-0.1f, 0.1f);
                    BodyDef bd = new BodyDef();
                    bd.type = BodyType.Dynamic;
                    bd.position = new Vector2(x, 2.05f + 2.5f * i);
                    bd.angle = 0.0f;
                    Body body = _world.CreateBody(bd);
                    body.CreateFixture(triangle1, 2.0f);
                    body.CreateFixture(triangle2, 2.0f);
                }
            }

            {
                PolygonShape bottom = new PolygonShape();
                bottom.SetAsBox( 1.5f, 0.15f );

                PolygonShape left = new PolygonShape();
                left.SetAsBox(0.15f, 2.7f, new Vector2(-1.45f, 2.35f), 0.2f);

                PolygonShape right = new PolygonShape();
                right.SetAsBox(0.15f, 2.7f, new Vector2(1.45f, 2.35f), -0.2f);

                BodyDef bd = new BodyDef();
                bd.type = BodyType.Dynamic;
                bd.position = new Vector2( 0.0f, 2.0f );
                Body body = _world.CreateBody(bd);
                body.CreateFixture(bottom, 4.0f);
                body.CreateFixture(left, 4.0f);
                body.CreateFixture(right, 4.0f);
            }
        }
        public CollisionFiltering()
        {
            // Ground body
            {
                PolygonShape shape = new PolygonShape();
                shape.SetAsEdge(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f));

                FixtureDef sd = new FixtureDef();
                sd.Shape = shape;
                sd.Friction = 0.3f;

                BodyDef bd = new BodyDef();
                Body ground = m_world.CreateBody(bd);
                ground.CreateFixture(sd);
            }

            // Small triangle
            Vec2[] vertices = new Vec2[3];
            vertices[0] = new Vec2(-1.0f, 0.0f);
            vertices[1] = new Vec2(1.0f, 0.0f);
            vertices[2] = new Vec2(0.0f, 2.0f);

            PolygonShape polygon = new PolygonShape(vertices);

            FixtureDef triangleShapeDef = new FixtureDef();
            triangleShapeDef.Shape = polygon;
            triangleShapeDef.Density = 1.0f;

            triangleShapeDef.Filter = new FilterData(k_triangleCategory, k_triangleMask, k_smallGroup);

            BodyDef triangleBodyDef = new BodyDef();
            triangleBodyDef.BodyType = BodyType.Dynamic;
            triangleBodyDef.Position = new Vec2(-5.0f, 2.0f);

            Body body1 = m_world.CreateBody(triangleBodyDef);
            body1.CreateFixture(triangleShapeDef);

            // Large triangle (recycle definitions)
            vertices[0] *= 2.0f;
            vertices[1] *= 2.0f;
            vertices[2] *= 2.0f;
            polygon.Vertices = vertices;
            triangleShapeDef.Filter = new FilterData(triangleShapeDef.Filter.CategoryBits, triangleShapeDef.Filter.MaskBits, k_largeGroup);
            triangleBodyDef.Position = new Vec2(-5.0f, 6.0f);
            triangleBodyDef.FixedRotation = true; // look at me!

            Body body2 = m_world.CreateBody(triangleBodyDef);
            body2.CreateFixture(triangleShapeDef);

            {
                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.Position = new Vec2(-5.0f, 10.0f);
                Body body = m_world.CreateBody(bd);

                PolygonShape p = new PolygonShape();
                p.SetAsBox(0.5f, 1.0f);
                body.CreateFixture(p, 1.0f);

                PrismaticJointDef jd = new PrismaticJointDef();
                jd.BodyA = body2;
                jd.BodyB = body;
                jd.EnableLimit = true;
                jd.LocalAnchorA = new Vec2(0.0f, 4.0f);
                jd.LocalAnchorB = Vec2.Empty;
                jd.LocalAxis = new Vec2(0.0f, 1.0f);
                jd.LowerTranslation = -1.0f;
                jd.UpperTranslation = 1.0f;

                m_world.CreateJoint(jd);
            }

            // Small box
            polygon.SetAsBox(1.0f, 0.5f);
            FixtureDef boxShapeDef = new FixtureDef();
            boxShapeDef.Shape = polygon;
            boxShapeDef.Density = 1.0f;
            boxShapeDef.Restitution = 0.1f;

            boxShapeDef.Filter = new FilterData(k_boxCategory, k_boxMask, k_smallGroup);

            BodyDef boxBodyDef = new BodyDef();
            boxBodyDef.BodyType = BodyType.Dynamic;
            boxBodyDef.Position = new Vec2(0.0f, 2.0f);

            Body body3 = m_world.CreateBody(boxBodyDef);
            body3.CreateFixture(boxShapeDef);

            // Large box (recycle definitions)
            polygon.SetAsBox(2.0f, 1.0f);
            boxShapeDef.Filter = new FilterData(k_boxCategory, k_boxMask, k_largeGroup);
            boxBodyDef.Position = new Vec2(0.0f, 6.0f);

            Body body4 = m_world.CreateBody(boxBodyDef);
            body4.CreateFixture(boxShapeDef);

            // Small circle
            CircleShape circle = new CircleShape();
            circle.Radius = 1.0f;

            FixtureDef circleShapeDef = new FixtureDef();
            circleShapeDef.Shape = circle;
            circleShapeDef.Density = 1.0f;

            circleShapeDef.Filter = new FilterData(k_circleCategory, k_circleMask, k_smallGroup);

            BodyDef circleBodyDef = new BodyDef();
            circleBodyDef.BodyType = BodyType.Dynamic;
            circleBodyDef.Position = new Vec2(5.0f, 2.0f);

            Body body5 = m_world.CreateBody(circleBodyDef);
            body5.CreateFixture(circleShapeDef);

            // Large circle
            circle.Radius *= 2.0f;
            circleShapeDef.Filter = new FilterData(k_circleCategory, k_circleMask, k_largeGroup);
            circleBodyDef.Position = new Vec2(5.0f, 6.0f);

            Body body6 = m_world.CreateBody(circleBodyDef);
            body6.CreateFixture(circleShapeDef);
        }
Exemple #50
0
        public SliderCrank()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = _world.CreateBody(bd);

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

            {
                Body prevBody = ground;

                // Define crank.
                {
                    PolygonShape shape = new PolygonShape();
                    shape.SetAsBox(0.5f, 2.0f);

                    BodyDef bd = new BodyDef();
                    bd.type = BodyType.Dynamic;
                    bd.position = new Vector2(0.0f, 7.0f);
                    Body body = _world.CreateBody(bd);
                    body.CreateFixture(shape, 2.0f);

                    RevoluteJointDef rjd = new RevoluteJointDef();
                    rjd.Initialize(prevBody, body, new Vector2(0.0f, 5.0f));
                    rjd.motorSpeed = 1.0f * (float)Settings.b2_pi;
                    rjd.maxMotorTorque = 10000.0f;
                    rjd.enableMotor = true;
                    _joint1 = (RevoluteJoint)_world.CreateJoint(rjd);

                    prevBody = body;
                }

                // Define follower.
                {
                    PolygonShape shape = new PolygonShape();
                    shape.SetAsBox(0.5f, 4.0f);

                    BodyDef bd = new BodyDef();
                    bd.type = BodyType.Dynamic;
                    bd.position = new Vector2(0.0f, 13.0f);
                    Body body = _world.CreateBody(bd);
                    body.CreateFixture(shape, 2.0f);

                    RevoluteJointDef rjd3 = new RevoluteJointDef();
                    rjd3.Initialize(prevBody, body, new Vector2(0.0f, 9.0f));
                    rjd3.enableMotor = false;
                    _world.CreateJoint(rjd3);

                    prevBody = body;
                }

                // Define piston
                {
                    PolygonShape shape = new PolygonShape();
                    shape.SetAsBox(1.5f, 1.5f);

                    BodyDef bd = new BodyDef();
                    bd.type = BodyType.Dynamic;
                    bd.position = new Vector2(0.0f, 17.0f);
                    Body body = _world.CreateBody(bd);
                    body.CreateFixture(shape, 2.0f);

                    RevoluteJointDef rjd2 = new RevoluteJointDef();
                    rjd2.Initialize(prevBody, body, new Vector2(0.0f, 17.0f));
                    _world.CreateJoint(rjd2);

                    PrismaticJointDef pjd = new PrismaticJointDef();
                    pjd.Initialize(ground, body, new Vector2(0.0f, 17.0f), new Vector2(0.0f, 1.0f));

                    pjd.maxMotorForce = 1000.0f;
                    pjd.enableMotor = true;

                    _joint2 = (PrismaticJoint)_world.CreateJoint(pjd);
                }

                // Create a payload
                {
                    PolygonShape shape = new PolygonShape();
                    shape.SetAsBox(1.5f, 1.5f);

                    BodyDef bd = new BodyDef();
                    bd.type = BodyType.Dynamic;
                    bd.position = new Vector2(0.0f, 23.0f);
                    Body body = _world.CreateBody(bd);
                    body.CreateFixture(shape, 2.0f);
                }
            }
        }
Exemple #51
0
        TheoJansen()
        {
            _offset = new Vector2(0.0f, 8.0f);
            _motorSpeed = 2.0f;
            _motorOn = true;
            Vector2 pivot = new Vector2(0.0f, 0.8f);

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

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

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

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

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

                BodyDef bd = new BodyDef();
                bd.type = BodyType.Dynamic;
                bd.position = new Vector2(-40.0f + 2.0f * i, 0.5f);

                Body body = _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.type = BodyType.Dynamic;
                bd.position = pivot + _offset;
                _chassis = _world.CreateBody(bd);
                _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.type = BodyType.Dynamic;
                bd.position = pivot + _offset;
                _wheel = _world.CreateBody(bd);
                _wheel.CreateFixture(sd);
            }

            {
                RevoluteJointDef jd = new RevoluteJointDef();
                jd.Initialize(_wheel, _chassis, pivot + _offset);
                jd.collideConnected = false;
                jd.motorSpeed = _motorSpeed;
                jd.maxMotorTorque = 400.0f;
                jd.enableMotor = _motorOn;
                _motorJoint = (RevoluteJoint)_world.CreateJoint(jd);
            }

            Vector2 wheelAnchor;

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

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

            _wheel.SetTransform(_wheel.GetPosition(), 120.0f * (float)Settings.b2_pi / 180.0f);
            CreateLeg(-1.0f, wheelAnchor);
            CreateLeg(1.0f, wheelAnchor);

            _wheel.SetTransform(_wheel.GetPosition(), -120.0f * (float)Settings.b2_pi / 180.0f);
            CreateLeg(-1.0f, wheelAnchor);
            CreateLeg(1.0f, wheelAnchor);
        }
Exemple #52
0
        public VaryingFriction()
        {
            {
                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);
            }

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

                BodyDef bd = new BodyDef();
                bd.Position = new Vec2(-4.0f, 22.0f);
                bd.Angle = -0.25f;

                Body ground = m_world.CreateBody(bd);
                ground.CreateFixture(shape, 0.0f);
            }

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

                BodyDef bd = new BodyDef();
                bd.Position = new Vec2(10.5f, 19.0f);

                Body ground = m_world.CreateBody(bd);
                ground.CreateFixture(shape, 0.0f);
            }

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

                BodyDef bd = new BodyDef();
                bd.Position = new Vec2(4.0f, 14.0f);
                bd.Angle = 0.25f;

                Body ground = m_world.CreateBody(bd);
                ground.CreateFixture(shape, 0.0f);
            }

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

                BodyDef bd = new BodyDef();
                bd.Position = new Vec2(-10.5f, 11.0f);

                Body ground = m_world.CreateBody(bd);
                ground.CreateFixture(shape, 0.0f);
            }

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

                BodyDef bd = new BodyDef();
                bd.Position = new Vec2(-4.0f, 6.0f);
                bd.Angle = -0.25f;

                Body ground = m_world.CreateBody(bd);
                ground.CreateFixture(shape, 0.0f);
            }

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

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

                float[] friction = {0.75f, 0.5f, 0.35f, 0.1f, 0.0f};

                for (int i = 0; i < 5; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.BodyType = BodyType.Dynamic;
                    bd.Position = new Vec2(-15.0f + 4.0f * i, 28.0f);
                    Body body = m_world.CreateBody(bd);

                    fd.Friction = friction[i];
                    body.CreateFixture(fd);
                }
            }
        }
        public CharacterCollision()
        {
            // Ground body
            {
                BodyDef bd = new BodyDef();
                Body ground = m_world.CreateBody(bd);

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

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

                PolygonShape shape = new PolygonShape();
                shape.SetAsEdge(new Vec2(-8.0f, 1.0f), new Vec2(-6.0f, 1.0f));
                ground.CreateFixture(shape, 0.0f);
                shape.SetAsEdge(new Vec2(-6.0f, 1.0f), new Vec2(-4.0f, 1.0f));
                ground.CreateFixture(shape, 0.0f);
                shape.SetAsEdge(new Vec2(-4.0f, 1.0f), new Vec2(-2.0f, 1.0f));
                ground.CreateFixture(shape, 0.0f);
            }

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

                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(1.0f, 1.0f, new Vec2(4.0f, 3.0f), 0.0f);
                ground.CreateFixture(shape, 0.0f);
                shape.SetAsBox(1.0f, 1.0f, new Vec2(6.0f, 3.0f), 0.0f);
                ground.CreateFixture(shape, 0.0f);
                shape.SetAsBox(1.0f, 1.0f, new Vec2(8.0f, 3.0f), 0.0f);
                ground.CreateFixture(shape, 0.0f);
            }

            // Square made from edges notice how the edges are shrunk to account
            // for the polygon radius. This makes it so the square character does
            // not get snagged. However, ray casts can now go through the cracks.
            {
                BodyDef bd = new BodyDef();
                Body ground = m_world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
                float d = 2.0f * Box2DSettings.b2_polygonRadius;
                shape.SetAsEdge(new Vec2(-1.0f + d, 3.0f), new Vec2(1.0f - d, 3.0f));
                ground.CreateFixture(shape, 0.0f);
                shape.SetAsEdge(new Vec2(1.0f, 3.0f + d), new Vec2(1.0f, 5.0f - d));
                ground.CreateFixture(shape, 0.0f);
                shape.SetAsEdge(new Vec2(1.0f - d, 5.0f), new Vec2(-1.0f + d, 5.0f));
                ground.CreateFixture(shape, 0.0f);
                shape.SetAsEdge(new Vec2(-1.0f, 5.0f - d), new Vec2(-1.0f, 3.0f + d));
                ground.CreateFixture(shape, 0.0f);
            }

            // Square character
            {
                BodyDef bd = new BodyDef();
                bd.Position = new Vec2(-3.0f, 5.0f);
                bd.BodyType = BodyType.Dynamic;
                bd.FixedRotation = true;
                bd.AllowSleep = false;

                Body body = m_world.CreateBody(bd);

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

                FixtureDef fd = new FixtureDef();
                fd.Shape = shape;
                fd.Density = 20.0f;
                body.CreateFixture(fd);
            }

            // Hexagon character
            {
                BodyDef bd = new BodyDef();
                bd.Position = new Vec2(-5.0f, 5.0f);
                bd.BodyType = BodyType.Dynamic;
                bd.FixedRotation = true;
                bd.AllowSleep = false;

                Body body = m_world.CreateBody(bd);

                float angle = 0.0f;
                float delta = (float)Math.PI / 3.0f;

                Vec2[] vertices = new Vec2[6];
                for (int i = 0; i < 6; ++i)
                {
                    vertices[i] = new Vec2(0.5f * (float)Math.Cos(angle), 0.5f * (float)Math.Sin(angle));
                    angle += delta;
                }

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

                FixtureDef fd = new FixtureDef();
                fd.Shape = shape;
                fd.Density = 20.0f;
                body.CreateFixture(fd);
            }

            // Circle character
            {
                BodyDef bd = new BodyDef();
                bd.Position = new Vec2(3.0f, 5.0f);
                bd.BodyType = BodyType.Dynamic;
                bd.FixedRotation = true;
                bd.AllowSleep = false;

                Body body = m_world.CreateBody(bd);

                CircleShape shape = new CircleShape();
                shape.Radius = 0.5f;

                FixtureDef fd = new FixtureDef();
                fd.Shape = shape;
                fd.Density = 20.0f;
                body.CreateFixture(fd);
            }
        }
Exemple #54
0
        public Dominos()
        {
            Body b1 = null;
            {
                PolygonShape shape = new PolygonShape();
                shape.SetAsEdge(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));

                BodyDef bd = new BodyDef();
                b1 = _world.CreateBody(bd);
                b1.CreateFixture(shape, 0.0f);
            }

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

                BodyDef bd = new BodyDef();
                bd.position = new Vector2(-1.5f, 10.0f);
                Body ground = _world.CreateBody(bd);
                ground.CreateFixture(shape, 0.0f);
            }

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

                FixtureDef fd = new FixtureDef();
                fd.shape = shape;
                fd.density = 20.0f;
                fd.friction = 0.1f;

                for (int i = 0; i < 10; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.type = BodyType.Dynamic;
                    bd.position = new Vector2(-6.0f + 1.0f * i, 11.25f);
                    Body body = _world.CreateBody(bd);
                    body.CreateFixture(fd);
                }
            }

            {
                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(7.0f, 0.25f, Vector2.Zero, 0.3f);

                BodyDef bd = new BodyDef();
                bd.position = new Vector2(1.0f, 6.0f);
                Body ground = _world.CreateBody(bd);
                ground.CreateFixture(shape, 0.0f);
            }

            Body b2 = null;
            {
                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(0.25f, 1.5f);

                BodyDef bd = new BodyDef();
                bd.position = new Vector2(-7.0f, 4.0f);
                b2 = _world.CreateBody(bd);
                b2.CreateFixture(shape, 0.0f);
            }

            Body b3 = null;
            {
                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(6.0f, 0.125f);

                BodyDef bd = new BodyDef();
                bd.type = BodyType.Dynamic;
                bd.position = new Vector2(-0.9f, 1.0f);
                bd.angle = -0.15f;

                b3 = _world.CreateBody(bd);
                b3.CreateFixture(shape, 10.0f);
            }

            RevoluteJointDef jd = new RevoluteJointDef();
            Vector2 anchor;

            anchor = new Vector2(-2.0f, 1.0f);
            jd.Initialize(b1, b3, anchor);
            jd.collideConnected = true;
            _world.CreateJoint(jd);

            Body b4 = null;
            {
                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(0.25f, 0.25f);

                BodyDef bd = new BodyDef();
                bd.type = BodyType.Dynamic;
                bd.position = new Vector2(-10.0f, 15.0f);
                b4 = _world.CreateBody(bd);
                b4.CreateFixture(shape, 10.0f);
            }

            anchor = new Vector2(-7.0f, 15.0f);
            jd.Initialize(b2, b4, anchor);
            _world.CreateJoint(jd);

            Body b5 = null;
            {
                BodyDef bd = new BodyDef();
                bd.type = BodyType.Dynamic;
                bd.position = new Vector2(6.5f, 3.0f);
                b5 = _world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
                FixtureDef fd = new FixtureDef();

                fd.shape = shape;
                fd.density = 10.0f;
                fd.friction = 0.1f;

                shape.SetAsBox(1.0f, 0.1f, new Vector2(0.0f, -0.9f), 0.0f);
                b5.CreateFixture(fd);

                shape.SetAsBox(0.1f, 1.0f, new Vector2(-0.9f, 0.0f), 0.0f);
                b5.CreateFixture(fd);

                shape.SetAsBox(0.1f, 1.0f, new Vector2(0.9f, 0.0f), 0.0f);
                b5.CreateFixture(fd);
            }

            anchor = new Vector2(6.0f, 2.0f);
            jd.Initialize(b1, b5, anchor);
            _world.CreateJoint(jd);

            Body b6 = null;
            {
                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(1.0f, 0.1f);

                BodyDef bd = new BodyDef();
                bd.type = BodyType.Dynamic;
                bd.position = new Vector2(6.5f, 4.1f);
                b6 = _world.CreateBody(bd);
                b6.CreateFixture(shape, 30.0f);
            }

            anchor = new Vector2(7.5f, 4.0f);
            jd.Initialize(b5, b6, anchor);
            _world.CreateJoint(jd);

            Body b7 = null;
            {
                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(0.1f, 1.0f);

                BodyDef bd = new BodyDef();
                bd.type = BodyType.Dynamic;
                bd.position = new Vector2(7.4f, 1.0f);

                b7 = _world.CreateBody(bd);
                b7.CreateFixture(shape, 10.0f);
            }

            DistanceJointDef djd = new DistanceJointDef();
            djd.bodyA = b3;
            djd.bodyB = b7;
            djd.localAnchorA = new Vector2(6.0f, 0.0f);
            djd.localAnchorB = new Vector2(0.0f, -1.0f);
            Vector2 d = djd.bodyB.GetWorldPoint(djd.localAnchorB) - djd.bodyA.GetWorldPoint(djd.localAnchorA);
            djd.length = d.Length();
            _world.CreateJoint(djd);

            {
                float radius = 0.2f;

                CircleShape shape = new CircleShape();
                shape._radius = radius;

                for (int i = 0; i < 4; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.type = BodyType.Dynamic;
                    bd.position = new Vector2(5.9f + 2.0f * radius * i, 2.4f);
                    Body body = _world.CreateBody(bd);
                    body.CreateFixture(shape, 10.0f);
                }
            }
        }
        public CollisionFiltering()
        {
            // Ground body
            {
                PolygonShape shape = new PolygonShape();
                shape.SetAsEdge(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));

                FixtureDef sd = new FixtureDef();
                sd.shape = shape;
                sd.friction = 0.3f;

                BodyDef bd = new BodyDef();
                Body ground = _world.CreateBody(bd);
                ground.CreateFixture(sd);
            }

            // Small triangle
            Vector2[] vertices = new Vector2[3];
            vertices[0] = new Vector2(-1.0f, 0.0f);
            vertices[1] = new Vector2(1.0f, 0.0f);
            vertices[2] = new Vector2(0.0f, 2.0f);
            PolygonShape polygon = new PolygonShape();
            polygon.Set(vertices, 3);

            FixtureDef triangleShapeDef = new FixtureDef();
            triangleShapeDef.shape = polygon;
            triangleShapeDef.density = 1.0f;

            triangleShapeDef.filter.groupIndex = k_smallGroup;
            triangleShapeDef.filter.categoryBits = k_triangleCategory;
            triangleShapeDef.filter.maskBits = k_triangleMask;

            BodyDef triangleBodyDef = new BodyDef();
            triangleBodyDef.type = BodyType.Dynamic;
            triangleBodyDef.position = new Vector2(-5.0f, 2.0f);

            Body body1 = _world.CreateBody(triangleBodyDef);
            body1.CreateFixture(triangleShapeDef);

            // Large triangle (recycle definitions)
            vertices[0] *= 2.0f;
            vertices[1] *= 2.0f;
            vertices[2] *= 2.0f;
            polygon.Set(vertices, 3);
            triangleShapeDef.filter.groupIndex = k_largeGroup;
            triangleBodyDef.position = new Vector2(-5.0f, 6.0f);
            triangleBodyDef.fixedRotation = true; // look at me!

            Body body2 = _world.CreateBody(triangleBodyDef);
            body2.CreateFixture(triangleShapeDef);

            {
                BodyDef bd = new BodyDef();
                bd.type = BodyType.Dynamic;
                bd.position = new Vector2(-5.0f, 10.0f);
                Body body = _world.CreateBody(bd);

                PolygonShape p = new PolygonShape();
                p.SetAsBox(0.5f, 1.0f);
                body.CreateFixture(p, 1.0f);

                PrismaticJointDef jd = new PrismaticJointDef();
                jd.bodyA = body2;
                jd.bodyB = body;
                jd.enableLimit = true;
                jd.localAnchorA = new Vector2(0.0f, 4.0f);
                jd.localAnchorB = Vector2.Zero;
                jd.localAxis1 = new Vector2(0.0f, 1.0f);
                jd.lowerTranslation = -1.0f;
                jd.upperTranslation = 1.0f;

                _world.CreateJoint(jd);
            }

            // Small box
            polygon.SetAsBox(1.0f, 0.5f);
            FixtureDef boxShapeDef = new FixtureDef();
            boxShapeDef.shape = polygon;
            boxShapeDef.density = 1.0f;
            boxShapeDef.restitution = 0.1f;

            boxShapeDef.filter.groupIndex = k_smallGroup;
            boxShapeDef.filter.categoryBits = k_boxCategory;
            boxShapeDef.filter.maskBits = k_boxMask;

            BodyDef boxBodyDef = new BodyDef();
            boxBodyDef.type = BodyType.Dynamic;
            boxBodyDef.position = new Vector2(0.0f, 2.0f);

            Body body3 = _world.CreateBody(boxBodyDef);
            body3.CreateFixture(boxShapeDef);

            // Large box (recycle definitions)
            polygon.SetAsBox(2.0f, 1.0f);
            boxShapeDef.filter.groupIndex = k_largeGroup;
            boxBodyDef.position = new Vector2(0.0f, 6.0f);

            Body body4 = _world.CreateBody(boxBodyDef);
            body4.CreateFixture(boxShapeDef);

            // Small circle
            CircleShape circle = new CircleShape();
            circle._radius = 1.0f;

            FixtureDef circleShapeDef = new FixtureDef();
            circleShapeDef.shape = circle;
            circleShapeDef.density = 1.0f;

            circleShapeDef.filter.groupIndex = k_smallGroup;
            circleShapeDef.filter.categoryBits = k_circleCategory;
            circleShapeDef.filter.maskBits = k_circleMask;

            BodyDef circleBodyDef = new BodyDef();
            circleBodyDef.type = BodyType.Dynamic;
            circleBodyDef.position = new Vector2(5.0f, 2.0f);

            Body body5 = _world.CreateBody(circleBodyDef);
            body5.CreateFixture(circleShapeDef);

            // Large circle
            circle._radius *= 2.0f;
            circleShapeDef.filter.groupIndex = k_largeGroup;
            circleBodyDef.position = new Vector2(5.0f, 6.0f);

            Body body6 = _world.CreateBody(circleBodyDef);
            body6.CreateFixture(circleShapeDef);
        }
Exemple #56
0
        public Cantilever()
        {
            Body ground = null;
		    {
                BodyDef bd = new BodyDef();
			    ground = _world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
			    shape.SetAsEdge(new Vector2(-40.0f, 0.0f), new Vector2(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;

                WeldJointDef jd = new WeldJointDef();

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

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

				    prevBody = body;
			    }
		    }

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

                FixtureDef fd = new FixtureDef();
			    fd.shape = shape;
			    fd.density = 20.0f;

                WeldJointDef jd = new WeldJointDef();

			    Body prevBody = ground;
			    for (int i = 0; i < e_count; ++i)
			    {
                    BodyDef bd = new BodyDef();
				    bd.type = BodyType.Dynamic;
				    bd.position = new Vector2(-14.5f + 1.0f * i, 15.0f);
				    bd.inertiaScale = 10.0f;
				    Body body = _world.CreateBody(bd);
				    body.CreateFixture(fd);

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

				    prevBody = body;
			    }
		    }

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

                FixtureDef fd = new FixtureDef();
			    fd.shape = shape;
			    fd.density = 20.0f;

                WeldJointDef jd = new WeldJointDef();

			    Body prevBody = ground;
			    for (int i = 0; i < e_count; ++i)
			    {
                    BodyDef bd = new BodyDef();
				    bd.type = BodyType.Dynamic;
				    bd.position = new Vector2(-4.5f + 1.0f * i, 5.0f);
				    Body body = _world.CreateBody(bd);
				    body.CreateFixture(fd);

				    if (i > 0)
				    {
					    Vector2 anchor = new Vector2(-5.0f + 1.0f * i, 5.0f);
					    jd.Initialize(prevBody, body, anchor);
					    _world.CreateJoint(jd);
				    }

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

                FixtureDef fd = new FixtureDef();
			    fd.shape = shape;
			    fd.density = 20.0f;

                WeldJointDef jd = new WeldJointDef();

			    Body prevBody = ground;
			    for (int i = 0; i < e_count; ++i)
			    {
                    BodyDef bd = new BodyDef();
				    bd.type = BodyType.Dynamic;
				    bd.position = new Vector2(5.5f + 1.0f * i, 10.0f);
				    bd.inertiaScale = 10.0f;
				    Body body = _world.CreateBody(bd);
				    body.CreateFixture(fd);

				    if (i > 0)
				    {
					    Vector2 anchor = new Vector2(5.0f + 1.0f * i, 10.0f);
					    jd.Initialize(prevBody, body, anchor);
					    _world.CreateJoint(jd);
				    }

				    prevBody = body;
			    }
		    }

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

                PolygonShape shape = new PolygonShape();
			    shape.Set(vertices, 3);

                FixtureDef fd = new FixtureDef();
			    fd.shape = shape;
			    fd.density = 1.0f;

                BodyDef bd = new BodyDef();
			    bd.type = BodyType.Dynamic;
			    bd.position = new Vector2(-8.0f + 8.0f * i, 12.0f);
			    Body body = _world.CreateBody(bd);
			    body.CreateFixture(fd);
		    }

            for (int i = 0; i < 2; ++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.type = BodyType.Dynamic;
			    bd.position = new Vector2(-6.0f + 6.0f * i, 10.0f);
			    Body body = _world.CreateBody(bd);
			    body.CreateFixture(fd);
		    }
        }
Exemple #57
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);
                }
            }
        }
Exemple #58
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);
            }
        }
Exemple #59
0
        public Web()
        {
            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.5f);

                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;

                bd.Position = new Vec2(-5.0f, 5.0f);
                m_bodies[0] = m_world.CreateBody(bd);
                m_bodies[0].CreateFixture(shape, 5.0f);

                bd.Position = new Vec2(5.0f, 5.0f);
                m_bodies[1] = m_world.CreateBody(bd);
                m_bodies[1].CreateFixture(shape, 5.0f);

                bd.Position = new Vec2(5.0f, 15.0f);
                m_bodies[2] = m_world.CreateBody(bd);
                m_bodies[2].CreateFixture(shape, 5.0f);

                bd.Position = new Vec2(-5.0f, 15.0f);
                m_bodies[3] = m_world.CreateBody(bd);
                m_bodies[3].CreateFixture(shape, 5.0f);

                DistanceJointDef jd = new DistanceJointDef();
                Vec2 p1, p2, d;

                jd.FrequencyHz = 4.0f;
                jd.DampingRatio = 0.5f;

                jd.BodyA = ground;
                jd.BodyB = m_bodies[0];
                jd.LocalAnchorA = new Vec2(-10.0f, 0.0f);
                jd.LocalAnchorB = new Vec2(-0.5f, -0.5f);
                p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA);
                p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB);
                d = p2 - p1;
                jd.Length = d.Length();
                m_joints[0] = m_world.CreateJoint(jd);

                jd.BodyA = ground;
                jd.BodyB = m_bodies[1];
                jd.LocalAnchorA = new Vec2(10.0f, 0.0f);
                jd.LocalAnchorB = new Vec2(0.5f, -0.5f);
                p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA);
                p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB);
                d = p2 - p1;
                jd.Length = d.Length();
                m_joints[1] = m_world.CreateJoint(jd);

                jd.BodyA = ground;
                jd.BodyB = m_bodies[2];
                jd.LocalAnchorA = new Vec2(10.0f, 20.0f);
                jd.LocalAnchorB = new Vec2(0.5f, 0.5f);
                p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA);
                p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB);
                d = p2 - p1;
                jd.Length = d.Length();
                m_joints[2] = m_world.CreateJoint(jd);

                jd.BodyA = ground;
                jd.BodyB = m_bodies[3];
                jd.LocalAnchorA = new Vec2(-10.0f, 20.0f);
                jd.LocalAnchorB = new Vec2(-0.5f, 0.5f);
                p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA);
                p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB);
                d = p2 - p1;
                jd.Length = d.Length();
                m_joints[3] = m_world.CreateJoint(jd);

                jd.BodyA = m_bodies[0];
                jd.BodyB = m_bodies[1];
                jd.LocalAnchorA = new Vec2(0.5f, 0.0f);
                jd.LocalAnchorB = new Vec2(-0.5f, 0.0f);;
                p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA);
                p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB);
                d = p2 - p1;
                jd.Length = d.Length();
                m_joints[4] = m_world.CreateJoint(jd);

                jd.BodyA = m_bodies[1];
                jd.BodyB = m_bodies[2];
                jd.LocalAnchorA = new Vec2(0.0f, 0.5f);
                jd.LocalAnchorB = new Vec2(0.0f, -0.5f);
                p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA);
                p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB);
                d = p2 - p1;
                jd.Length = d.Length();
                m_joints[5] = m_world.CreateJoint(jd);

                jd.BodyA = m_bodies[2];
                jd.BodyB = m_bodies[3];
                jd.LocalAnchorA = new Vec2(-0.5f, 0.0f);
                jd.LocalAnchorB = new Vec2(0.5f, 0.0f);
                p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA);
                p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB);
                d = p2 - p1;
                jd.Length = d.Length();
                m_joints[6] = m_world.CreateJoint(jd);

                jd.BodyA = m_bodies[3];
                jd.BodyB = m_bodies[0];
                jd.LocalAnchorA = new Vec2(0.0f, -0.5f);
                jd.LocalAnchorB = new Vec2(0.0f, 0.5f);
                p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA);
                p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB);
                d = p2 - p1;
                jd.Length = d.Length();
                m_joints[7] = m_world.CreateJoint(jd);
            }
        }
        public CollisionProcessing()
        {
            // Ground body
            {
                PolygonShape shape = new PolygonShape();
                shape.SetAsEdge(new Vector2(-50.0f, 0.0f), new Vector2(50.0f, 0.0f));

                FixtureDef sd = new FixtureDef();
                sd.shape = shape;

                BodyDef bd = new BodyDef();
                Body ground = _world.CreateBody(bd);
                ground.CreateFixture(sd);
            }

            float xLo = -5.0f, xHi = 5.0f;
            float yLo = 2.0f, yHi = 35.0f;

            // Small triangle
            Vector2[] vertices = new Vector2[3];
            vertices[0] = new Vector2(-1.0f, 0.0f);
            vertices[1] = new Vector2(1.0f, 0.0f);
            vertices[2] = new Vector2(0.0f, 2.0f);

            PolygonShape polygon = new PolygonShape();
            polygon.Set(vertices, 3);

            FixtureDef triangleShapeDef = new FixtureDef();
            triangleShapeDef.shape = polygon;
            triangleShapeDef.density = 1.0f;

            BodyDef triangleBodyDef = new BodyDef();
            triangleBodyDef.type = BodyType.Dynamic;
            triangleBodyDef.position = new Vector2(Rand.RandomFloat(xLo, xHi), Rand.RandomFloat(yLo, yHi));

            Body body1 = _world.CreateBody(triangleBodyDef);
            body1.CreateFixture(triangleShapeDef);

            // Large triangle (recycle definitions)
            vertices[0] *= 2.0f;
            vertices[1] *= 2.0f;
            vertices[2] *= 2.0f;
            polygon.Set(vertices, 3);

            triangleBodyDef.position = new Vector2(Rand.RandomFloat(xLo, xHi), Rand.RandomFloat(yLo, yHi));

            Body body2 = _world.CreateBody(triangleBodyDef);
            body2.CreateFixture(triangleShapeDef);

            // Small box
            polygon.SetAsBox(1.0f, 0.5f);

            FixtureDef boxShapeDef = new FixtureDef();
            boxShapeDef.shape = polygon;
            boxShapeDef.density = 1.0f;

            BodyDef boxBodyDef = new BodyDef();
            boxBodyDef.type = BodyType.Dynamic;
            boxBodyDef.position = new Vector2(Rand.RandomFloat(xLo, xHi), Rand.RandomFloat(yLo, yHi));

            Body body3 = _world.CreateBody(boxBodyDef);
            body3.CreateFixture(boxShapeDef);

            // Large box (recycle definitions)
            polygon.SetAsBox(2.0f, 1.0f);
            boxBodyDef.position = new Vector2(Rand.RandomFloat(xLo, xHi), Rand.RandomFloat(yLo, yHi));

            Body body4 = _world.CreateBody(boxBodyDef);
            body4.CreateFixture(boxShapeDef);

            // Small circle
            CircleShape circle = new CircleShape();
            circle._radius = 1.0f;

            FixtureDef circleShapeDef = new FixtureDef();
            circleShapeDef.shape = circle;
            circleShapeDef.density = 1.0f;

            BodyDef circleBodyDef = new BodyDef();
            circleBodyDef.type = BodyType.Dynamic;
            circleBodyDef.position = new Vector2(Rand.RandomFloat(xLo, xHi), Rand.RandomFloat(yLo, yHi));

            Body body5 = _world.CreateBody(circleBodyDef);
            body5.CreateFixture(circleShapeDef);

            // Large circle
            circle._radius *= 2.0f;
            circleBodyDef.position = new Vector2(Rand.RandomFloat(xLo, xHi), Rand.RandomFloat(yLo, yHi));

            Body body6 = _world.CreateBody(circleBodyDef);
            body6.CreateFixture(circleShapeDef);
        }