コード例 #1
0
        /// <summary>
        /// Creates the fixed revolute joint.
        /// </summary>
        /// <param name="world">The world.</param>
        /// <param name="body">The body.</param>
        /// <param name="bodyAnchor">The body anchor.</param>
        /// <param name="worldAnchor">The world anchor.</param>
        /// <returns></returns>
        public static FixedRevoluteJoint CreateFixedRevoluteJoint(World world, Body body, Vector2 bodyAnchor, Vector2 worldAnchor)
        {
            FixedRevoluteJoint fixedRevoluteJoint = new FixedRevoluteJoint(body, bodyAnchor, worldAnchor);

            world.AddJoint(fixedRevoluteJoint);
            return(fixedRevoluteJoint);
        }
コード例 #2
0
        public FixedRevoluteJoint CreateFixedRevoluteJoint(PhysicsSimulator physicsSimulator, Body body, Vector2 anchor)
        {
            FixedRevoluteJoint revoluteJoint = CreateFixedRevoluteJoint(body, anchor);

            physicsSimulator.Add(revoluteJoint);
            return(revoluteJoint);
        }
コード例 #3
0
        internal void RotateSelected(float p)
        {
            float   radians      = p * (float)Math.PI / (120 * 48);
            Matrix  rot          = Matrix.CreateRotationZ(radians);
            Vector3 centerOfMass = getGroupPosition(multipleSelect.CheckedItems);
            Matrix  toCenter     = Matrix.CreateTranslation(-centerOfMass);
            Matrix  andBack      = Matrix.CreateTranslation(centerOfMass);

            Matrix total = toCenter * rot * andBack;

            foreach (object o in multipleSelect.CheckedItems)
            {
                if (o is Body || o is FixedRevoluteJoint)
                {
                    if (o is Body)
                    {
                        Body b = (Body)o;
                        if (farseerManager.world.BodyList.Contains(b))
                        {
                            b.Position  = Vector2.Transform(b.Position, total);
                            b.Rotation += radians;
                        }
                    }
                    else if (o is FixedRevoluteJoint)
                    {
                        FixedRevoluteJoint j = (FixedRevoluteJoint)o;
                        j.WorldAnchorB = Vector2.Transform(j.WorldAnchorB, total);
                    }
                }
            }
        }
コード例 #4
0
        public Chain(Vector2 pathBeginning, Vector2 pathEnd, Vector2 revolutePosition1, bool useSecondRevolute, Vector2 revolutePosition2, int numberOfChainlinks, Body revoluteBody, bool breakable)
        {
            chainTexture = ScreenManager.Content.Load <Texture2D>("Enemies/kette");
            origin       = new Vector2(chainTexture.Width / 2, chainTexture.Height / 2);

            //Chain start / end
            Path path = new Path();

            path.Add(pathBeginning);
            path.Add(pathEnd);

            //A single chainlink
            PolygonShape shape = new PolygonShape(PolygonTools.CreateRectangle(0.125f, 0.6f), 20);

            //Use PathFactory to create all the chainlinks based on the chainlink created before.
            chainLinks = PathManager.EvenlyDistributeShapesAlongPath(PhysicsGameScreen.World, path, shape, BodyType.Dynamic, numberOfChainlinks);

            foreach (Body chainLink in chainLinks)
            {
                foreach (Fixture f in chainLink.FixtureList)
                {
                    f.Friction = 0.1f;
                }
            }

            //Fix the first chainlink to a Body
            FixedRevoluteJoint fixedJoint = new FixedRevoluteJoint(chainLinks[0], Vector2.Zero, revolutePosition1);

            PhysicsGameScreen.World.AddJoint(fixedJoint);


            RevoluteJoint fixedJoint2 = new RevoluteJoint(chainLinks[numberOfChainlinks -= 2], revoluteBody, new Vector2(chainLinks[numberOfChainlinks -= 2].LocalCenter.X - 0.125f,
                                                                                                                         chainLinks[numberOfChainlinks -= 2].LocalCenter.Y + 0.6f), revolutePosition2);

            PhysicsGameScreen.World.AddJoint(fixedJoint2);

            //Attach all the chainlinks together with a revolute joint
            List <RevoluteJoint> joints = PathManager.AttachBodiesWithRevoluteJoint(PhysicsGameScreen.World, chainLinks,
                                                                                    new Vector2(0, -0.6f),
                                                                                    new Vector2(0, 0.6f),
                                                                                    false, false);



            if (breakable == true)
            {
                //The chain is breakable
                for (int i = 0; i < joints.Count; i++)
                {
                    RevoluteJoint r = joints[i];

                    r.Breakpoint = 10000f;
                    //if (r.Broke lower bridge
                }
            }
        }
コード例 #5
0
        public FixedRevoluteJoint CreateFixedRevoluteJoint(Body body, Vector2 anchor)
        {
            if (body.isStatic)
            {
                throw new InvalidOperationException("Fixed joints cannot be created on static bodies");
            }
            FixedRevoluteJoint revoluteJoint = new FixedRevoluteJoint(body, anchor);

            return(revoluteJoint);
        }
コード例 #6
0
        /// <exception cref="InvalidOperationException">Fixed joints cannot be created on static bodies</exception>
        public FixedRevoluteJoint CreateFixedRevoluteJoint(Body body, Vector2 anchor)
        {
            FixedRevoluteJoint revoluteJoint = new FixedRevoluteJoint(body, anchor);

            if (body.isStatic)
            {
                //throw new InvalidOperationException("Fixed joints cannot be created on static bodies");
                revoluteJoint.Enabled = false; // if you create a joint of a static body it is created as disabled.
            }
            return(revoluteJoint);
        }
コード例 #7
0
        public override Object PlacePhysicsObject(Microsoft.Xna.Framework.Vector2 position, FarseerPhysics.Dynamics.World world)
        {
            List <Fixture> list = world.TestPointAll(position);


            if (pin.Checked && list.Count > 0)
            {
                FixedRevoluteJoint j = new FixedRevoluteJoint(list[0].Body, list[0].Body.GetLocalPoint(position), position);
                if (motorEnabled.Checked)
                {
                    j.MotorEnabled = true;
                    float speed;
                    float maxTorque;
                    if (float.TryParse(motorSpeed.Text, out speed))
                    {
                        j.MotorSpeed = speed;
                    }
                    if (float.TryParse(motorTorque.Text, out maxTorque))
                    {
                        j.MaxMotorTorque = maxTorque;
                    }
                }

                world.AddJoint(j);
                return(j);
            }

            if (list.Count > 1)
            {
                RevoluteJoint j = new RevoluteJoint(list[0].Body, list[1].Body, list[0].Body.GetLocalPoint(position), list[1].Body.GetLocalPoint(position));
                if (motorEnabled.Checked)
                {
                    j.MotorEnabled = true;
                    float speed;
                    float maxTorque;
                    if (float.TryParse(motorSpeed.Text, out speed))
                    {
                        j.MotorSpeed = speed;
                    }
                    if (float.TryParse(motorTorque.Text, out maxTorque))
                    {
                        j.MaxMotorTorque = maxTorque;
                    }
                }
                world.AddJoint(j);
                return(j);
            }

            return(base.PlacePhysicsObject(position, world));
        }
コード例 #8
0
        private GearsTest()
        {
            {
                // First circle
                CircleShape circle1 = new CircleShape(1.0f, 5);
                Body        body1   = BodyFactory.CreateBody(World);
                body1.BodyType = BodyType.Dynamic;
                body1.Position = new Vector2(-3.0f, 12.0f);
                body1.CreateFixture(circle1);

                // Second circle
                CircleShape circle2 = new CircleShape(2.0f, 5);
                Body        body2   = BodyFactory.CreateBody(World);
                body2.BodyType = BodyType.Dynamic;
                body2.Position = new Vector2(0.0f, 12.0f);
                body2.CreateFixture(circle2);

                // Rectangle
                Vertices     box        = PolygonTools.CreateRectangle(0.5f, 5.0f);
                PolygonShape polygonBox = new PolygonShape(box, 5);
                Body         body3      = BodyFactory.CreateBody(World);
                body3.BodyType = BodyType.Dynamic;
                body3.Position = new Vector2(2.5f, 12.0f);
                body3.CreateFixture(polygonBox);

                // Fix first circle
                _joint1 = new FixedRevoluteJoint(body1, Vector2.Zero, body1.Position);
                World.AddJoint(_joint1);

                // Fix second circle
                _joint2 = new FixedRevoluteJoint(body2, Vector2.Zero, body2.Position);
                World.AddJoint(_joint2);

                // Fix rectangle
                _joint3              = new FixedPrismaticJoint(body3, body3.Position, new Vector2(0.0f, 1.0f));
                _joint3.LowerLimit   = -5.0f;
                _joint3.UpperLimit   = 5.0f;
                _joint3.LimitEnabled = true;
                World.AddJoint(_joint3);

                // Attach first and second circle together with a gear joint
                _joint4 = new GearJoint(_joint1, _joint2, circle2.Radius / circle1.Radius);
                World.AddJoint(_joint4);

                // Attach second and rectangle together with a gear joint
                _joint5 = new GearJoint(_joint2, _joint3, -1.0f / circle2.Radius);
                World.AddJoint(_joint5);
            }
        }
コード例 #9
0
        public void switchClamped()
        {
            if (clampedJoint != null)
            {
                game.world.RemoveJoint(clampedJoint);
            }

            freeFlight = false;

            Body oldCurrentClamped = currentClamped;

            clampedJoint = JointFactory.CreateFixedRevoluteJoint(game.world, notClamped, -r, notClamped.GetWorldPoint(-r));
            setupClampedJoint();

            currentClamped = notClamped;
            notClamped     = oldCurrentClamped;
        }
コード例 #10
0
        public void addSpinningDeath()
        {
            Body    b   = new Body(world);
            Fixture rec = FixtureFactory.AttachRectangle(20, 2, 1, Vector2.Zero, b);

            b.Position = new Vector2(10, 0);

            rec.Body.BodyType = BodyType.Dynamic;
            FarseerTextures.ApplyTexture(rec, FarseerTextures.TextureType.Normal);

            FixedRevoluteJoint joint = new FixedRevoluteJoint(rec.Body, Vector2.Zero, new Vector2(20, 0));

            joint.MotorEnabled   = true;
            joint.MotorSpeed     = 6;
            joint.MaxMotorTorque = 10000;
            joint.MotorTorque    = 10000;
            world.AddJoint(joint);
        }
コード例 #11
0
ファイル: ChainTest.cs プロジェクト: womandroid/cocos2d-xna
        private ChainTest()
        {
            //Ground
            BodyFactory.CreateEdge(World, new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));

            //Chain start / end
            Path path = new Path();

            path.Add(new Vector2(0, 25));
            path.Add(new Vector2(40, 25));

            //A single chainlink
            PolygonShape shape = new PolygonShape(PolygonTools.CreateRectangle(0.125f, 0.6f), 20);

            //Use PathFactory to create all the chainlinks based on the chainlink created before.
            List <Body> chainLinks = PathManager.EvenlyDistributeShapesAlongPath(World, path, shape, BodyType.Dynamic, 30);

            foreach (Body chainLink in chainLinks)
            {
                foreach (Fixture f in chainLink.FixtureList)
                {
                    f.Friction = 0.2f;
                }
            }

            //Fix the first chainlink to the world
            FixedRevoluteJoint fixedJoint = new FixedRevoluteJoint(chainLinks[0], Vector2.Zero, chainLinks[0].Position);

            World.AddJoint(fixedJoint);

            //Attach all the chainlinks together with a revolute joint
            List <RevoluteJoint> joints = PathManager.AttachBodiesWithRevoluteJoint(World, chainLinks,
                                                                                    new Vector2(0, -0.6f),
                                                                                    new Vector2(0, 0.6f),
                                                                                    false, false);

            //The chain is breakable
            for (int i = 0; i < joints.Count; i++)
            {
                RevoluteJoint r = joints[i];
                r.Breakpoint = 10000f;
            }
        }
コード例 #12
0
        private void DrawRevoluteJoints(SpriteBatch spriteBatch)
        {
            for (int i = 0; i < _physicsSimulator.JointList.Count; i++)
            {
                if (_physicsSimulator.JointList[i] is FixedRevoluteJoint)
                {
                    FixedRevoluteJoint fixedRevoluteJoint = (FixedRevoluteJoint)_physicsSimulator.JointList[i];
                    _revoluteJointRectangleBrush.Draw(spriteBatch, fixedRevoluteJoint.Anchor);
                }

                if (_physicsSimulator.JointList[i] is RevoluteJoint)
                {
                    RevoluteJoint revoluteJoint = (RevoluteJoint)_physicsSimulator.JointList[i];
                    _revoluteJointRectangleBrush.Draw(spriteBatch, revoluteJoint.CurrentAnchor);
                    _revoluteJointLineBrush.Draw(spriteBatch, revoluteJoint.CurrentAnchor, revoluteJoint.Body1.Position);
                    _revoluteJointLineBrush.Draw(spriteBatch, revoluteJoint.CurrentAnchor, revoluteJoint.Body2.Position);
                }
            }
        }
コード例 #13
0
        internal override void UpdateJoint()
        {
            base.UpdateJoint();
            if (this.joint == null)
            {
                return;
            }

            FixedRevoluteJoint j = this.joint as FixedRevoluteJoint;

            j.WorldAnchorB   = PhysicsConvert.ToPhysicalUnit(this.worldAnchor);
            j.LocalAnchorA   = GetFarseerPoint(this.BodyA, this.localAnchor);
            j.MotorEnabled   = this.motorEnabled;
            j.MotorSpeed     = -this.motorSpeed / Time.SPFMult;
            j.MaxMotorTorque = PhysicsConvert.ToPhysicalUnit(this.maxMotorTorque) / Time.SPFMult;
            j.LimitEnabled   = this.limitEnabled;
            j.LowerLimit     = -this.upperLimit;
            j.UpperLimit     = -this.lowerLimit;
            j.ReferenceAngle = -this.refAngle;
        }
コード例 #14
0
ファイル: NailTool.cs プロジェクト: guozanhua/KinectRagdoll
        public override void HandleInput()
        {
            InputHelper input = game.inputManager.inputHelper;

            if (input.IsNewButtonPress(MouseButtons.LeftButton))
            {
                Vector2 position = ProjectionHelper.PixelToFarseer(input.MousePosition);



                List <Fixture> list = game.farseerManager.world.TestPointAll(position);

                if (list.Count > 0)
                {
                    FixedRevoluteJoint j = new FixedRevoluteJoint(list[0].Body, list[0].Body.GetLocalPoint(position), position);

                    game.farseerManager.world.AddJoint(j);

                    FormManager.Property.setSelectedObject(j);
                }
            }
        }
コード例 #15
0
        private Vector3 getGroupPosition(IEnumerable <Object> objects)
        {
            Vector2 total = new Vector2();
            int     count = 0;

            foreach (object o in objects)
            {
                if (o is Body)
                {
                    Body b = (Body)o;
                    total += b.Position;
                    count++;
                }
                else if (o is FixedRevoluteJoint)
                {
                    FixedRevoluteJoint j = (FixedRevoluteJoint)o;
                    total += j.WorldAnchorB;
                    count++;
                }
            }

            total /= count;
            return(new Vector3(total.X, total.Y, 0));
        }
コード例 #16
0
        private RevoluteTest()
        {
            //Ground
            BodyFactory.CreateEdge(World, new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));

            {
                //The big fixed wheel
                CircleShape shape = new CircleShape(5.0f, 5);

                Body body = BodyFactory.CreateBody(World);
                body.Position = new Vector2(0.0f, 15.0f);
                body.BodyType = BodyType.Dynamic;

                body.CreateFixture(shape);

                _fixedJoint                = new FixedRevoluteJoint(body, Vector2.Zero, body.Position);
                _fixedJoint.MotorSpeed     = 0.25f * Settings.Pi;
                _fixedJoint.MaxMotorTorque = 5000.0f;
                _fixedJoint.MotorEnabled   = true;
                World.AddJoint(_fixedJoint);

                // The small gear attached to the big one
                Body body1 = BodyFactory.CreateGear(World, 1.5f, 10, 0.1f, 1, 1);
                body1.Position = new Vector2(0.0f, 12.0f);
                body1.BodyType = BodyType.Dynamic;

                _joint = new RevoluteJoint(body, body1, body.GetLocalPoint(body1.Position),
                                           Vector2.Zero);
                _joint.MotorSpeed       = 1.0f * Settings.Pi;
                _joint.MaxMotorTorque   = 5000.0f;
                _joint.MotorEnabled     = true;
                _joint.CollideConnected = false;

                World.AddJoint(_joint);
            }
        }
コード例 #17
0
ファイル: level3Screen.cs プロジェクト: kyung01/Squareosity
        /// <summary>
        /// Load graphics content for the game.
        /// </summary>
        public override void Activate(bool instancePreserved)
        {
            if (!instancePreserved)
            {
                if (content == null)
                {
                    content = new ContentManager(ScreenManager.Game.Services, "Content");
                }

                gameFont = content.Load <SpriteFont>("gamefont");

                bloom = new BloomComponent(ScreenManager.Game);
                ScreenManager.Game.Components.Add(bloom);



                cam2D   = new Cam2d(ScreenManager.GraphicsDevice);
                reticle = content.Load <Texture2D>("redReticle");
                /// player
                playerBody            = new PlayerBody(content.Load <Texture2D>("redPlayer"), world, content);
                playerBody.setPostion = new Vector2(900, 100);


                Squares.Add(new Square(content.Load <Texture2D>("Squares/greenSquare"), new Vector2(-28, 200), world));
                //spinning wheel.
                {
                    Walls.Add(new Wall(content.Load <Texture2D>("Walls/blueWallMedium"), new Vector2(-5, 250), false, world));

                    Walls.Add(new Wall(content.Load <Texture2D>("Walls/blueWallMedium"), new Vector2(-5, 350), false, world));

                    // filler
                    Walls.Add(new Wall(content.Load <Texture2D>("Walls/blueWallMedium"), new Vector2(205, 360), false, world));

                    Walls.Add(new Wall(content.Load <Texture2D>("Walls/blueWallMedium"), new Vector2(205, 250), false, world));
                    // filler
                    Walls.Add(new Wall(content.Load <Texture2D>("Walls/blueWallMedium"), new Vector2(205, 240), false, world));

                    Walls.Add(new Wall(content.Load <Texture2D>("Walls/blueWallMedium"), new Vector2(205, 350), false, world));

                    Walls.Add(new Wall(content.Load <Texture2D>("Walls/blueWallMedium"), new Vector2(160, 190), true, world));

                    Walls.Add(new Wall(content.Load <Texture2D>("Walls/blueWallMedium"), new Vector2(160, 410), true, world));

                    spinningWheel = new Shape(content.Load <Texture2D>("Shapes/orangeSpinningWheel"), new Vector2(100, 300), true, false, world);

                    FixedRevoluteJoint fixedJoint = new FixedRevoluteJoint(spinningWheel.shapeBody, Vector2.Zero, spinningWheel.shapeBody.Position);
                    world.AddJoint(fixedJoint);
                }


                int space = 0;

                int k = 260;
                for (; k < 1000; k += 100)
                {
                    Walls.Add(new Wall(content.Load <Texture2D>("Walls/blueWallMedium"), new Vector2(k, 190), true, world));
                }

                Walls.Add(new Wall(content.Load <Texture2D>("Walls/blueWallMedium"), new Vector2(k - 65, 190), true, world));


                for (int i = 0; i < 11; i++)
                {
                    Walls.Add(new Wall(content.Load <Texture2D>("Walls/blueWallMedium"), new Vector2(space - 5, 0), true, world));
                    Walls.Add(new Wall(content.Load <Texture2D>("Walls/blueWallMedium"), new Vector2(space - 5, 600), true, world));

                    if (i <= 5)
                    {
                        //vertical walls
                        Walls.Add(new Wall(content.Load <Texture2D>("Walls/blueWallMedium"), new Vector2(-50, space + 50), false, world));
                        Walls.Add(new Wall(content.Load <Texture2D>("Walls/blueWallMedium"), new Vector2(1050, space + 50), false, world));
                    }

                    space += 100;
                }

                Walls.Add(new Wall(content.Load <Texture2D>("Walls/blueWallMedium"), new Vector2(1050, 45), false, world));
                Walls.Add(new Wall(content.Load <Texture2D>("Walls/blueWallMedium"), new Vector2(1050, 555), false, world));



                // set cam track

                cam2D.TrackingBody   = playerBody.playerBody;
                cam2D.EnableTracking = true;



                // once the load has finished, we use ResetElapsedTime to tell the game's
                // timing mechanism that we have just finished a very long frame, and that
                // it should not try to catch up.
                ScreenManager.Game.ResetElapsedTime();
            }

            base.Activate(instancePreserved);
        }
コード例 #18
0
        private DominosTest()
        {
            //Ground
            BodyFactory.CreateEdge(World, new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));

            {
                Vertices     box   = PolygonTools.CreateRectangle(6.0f, 0.25f);
                PolygonShape shape = new PolygonShape(box, 0);

                Body ground = BodyFactory.CreateBody(World);
                ground.Position = new Vector2(-1.5f, 10.0f);

                ground.CreateFixture(shape);
            }

            {
                Vertices     box   = PolygonTools.CreateRectangle(0.1f, 1.0f);
                PolygonShape shape = new PolygonShape(box, 20);

                for (int i = 0; i < 10; ++i)
                {
                    Body body = BodyFactory.CreateBody(World);
                    body.BodyType = BodyType.Dynamic;
                    body.Position = new Vector2(-6.0f + 1.0f * i, 11.25f);

                    Fixture fixture = body.CreateFixture(shape);
                    fixture.Friction = 0.1f;
                }
            }

            {
                Vertices     box   = PolygonTools.CreateRectangle(7.0f, 0.25f, Vector2.Zero, 0.3f);
                PolygonShape shape = new PolygonShape(box, 0);

                Body ground = BodyFactory.CreateBody(World);
                ground.Position = new Vector2(1.0f, 6.0f);

                ground.CreateFixture(shape);
            }

            Body b2;
            {
                Vertices     box   = PolygonTools.CreateRectangle(0.25f, 1.5f);
                PolygonShape shape = new PolygonShape(box, 0);

                b2          = BodyFactory.CreateBody(World);
                b2.Position = new Vector2(-7.0f, 4.0f);

                b2.CreateFixture(shape);
            }

            Body b3;
            {
                Vertices     box   = PolygonTools.CreateRectangle(6.0f, 0.125f);
                PolygonShape shape = new PolygonShape(box, 10);

                b3          = BodyFactory.CreateBody(World);
                b3.BodyType = BodyType.Dynamic;
                b3.Position = new Vector2(-0.9f, 1.0f);
                b3.Rotation = -0.15f;

                b3.CreateFixture(shape);
            }

            Vector2            anchor = new Vector2(-2.0f, 1.0f);
            FixedRevoluteJoint jd     = new FixedRevoluteJoint(b3, b3.GetLocalPoint(anchor), anchor);

            jd.CollideConnected = true;
            World.AddJoint(jd);

            Body b4;

            {
                Vertices     box   = PolygonTools.CreateRectangle(0.25f, 0.25f);
                PolygonShape shape = new PolygonShape(box, 10);

                b4          = BodyFactory.CreateBody(World);
                b4.BodyType = BodyType.Dynamic;
                b4.Position = new Vector2(-10.0f, 15.0f);

                b4.CreateFixture(shape);
            }

            anchor = new Vector2(-7.0f, 15.0f);
            FixedRevoluteJoint jd2 = new FixedRevoluteJoint(b4, b4.GetLocalPoint(anchor), anchor);

            World.AddJoint(jd2);

            Body b5;

            {
                b5          = BodyFactory.CreateBody(World);
                b5.BodyType = BodyType.Dynamic;
                b5.Position = new Vector2(6.5f, 3.0f);

                Vertices     vertices = PolygonTools.CreateRectangle(1.0f, 0.1f, new Vector2(0.0f, -0.9f), 0.0f);
                PolygonShape shape    = new PolygonShape(vertices, 10);

                Fixture fix = b5.CreateFixture(shape);
                fix.Friction = 0.1f;

                vertices = PolygonTools.CreateRectangle(0.1f, 1.0f, new Vector2(-0.9f, 0.0f), 0.0f);

                shape.Set(vertices);
                fix          = b5.CreateFixture(shape);
                fix.Friction = 0.1f;

                vertices = PolygonTools.CreateRectangle(0.1f, 1.0f, new Vector2(0.9f, 0.0f), 0.0f);

                shape.Set(vertices);
                fix          = b5.CreateFixture(shape);
                fix.Friction = 0.1f;
            }

            anchor = new Vector2(6.0f, 2.0f);
            FixedRevoluteJoint jd3 = new FixedRevoluteJoint(b5, b5.GetLocalPoint(anchor), anchor);

            World.AddJoint(jd3);

            Body b6;

            {
                Vertices     box   = PolygonTools.CreateRectangle(1.0f, 0.1f);
                PolygonShape shape = new PolygonShape(box, 30);

                b6          = BodyFactory.CreateBody(World);
                b6.BodyType = BodyType.Dynamic;
                b6.Position = new Vector2(6.5f, 4.1f);

                b6.CreateFixture(shape);
            }

            anchor = new Vector2(1.0f, -0.1f);
            RevoluteJoint jd4 = new RevoluteJoint(b5, b6, b5.GetLocalPoint(b6.GetWorldPoint(anchor)), anchor);

            jd4.CollideConnected = true;
            World.AddJoint(jd4);

            Body b7;
            {
                Vertices     box   = PolygonTools.CreateRectangle(0.1f, 1.0f);
                PolygonShape shape = new PolygonShape(box, 10);

                b7          = BodyFactory.CreateBody(World);
                b7.BodyType = BodyType.Dynamic;
                b7.Position = new Vector2(7.4f, 1.0f);

                b7.CreateFixture(shape);
            }

            DistanceJoint djd = new DistanceJoint(b3, b7, new Vector2(6.0f, 0.0f), new Vector2(0.0f, -1.0f));
            Vector2       d   = djd.BodyB.GetWorldPoint(djd.LocalAnchorB) - djd.BodyA.GetWorldPoint(djd.LocalAnchorA);

            djd.Length = d.Length();
            World.AddJoint(djd);

            {
                const float radius = 0.2f;

                CircleShape shape = new CircleShape(radius, 10);

                for (int i = 0; i < 4; ++i)
                {
                    Body body = BodyFactory.CreateBody(World);
                    body.BodyType = BodyType.Dynamic;
                    body.Position = new Vector2(5.9f + 2.0f * radius * i, 2.4f);

                    Fixture fix = body.CreateFixture(shape);
                    fix.OnCollision += BallCollision;
                }
            }
        }
コード例 #19
0
        public void Restart(int mode)
        {
            World   world        = game.world;
            float   Pixel2Meter  = game.Pixel2Meter;
            Vector2 screenCenter = game.screenCenter;

            gameMode = mode;

            freeFlight = false;
            if (connectRectJoint != null)
            {
                world.RemoveJoint(connectRectJoint);
            }

            if (clampedJoint != null)
            {
                world.RemoveJoint(clampedJoint);
            }

            if (rectBody1 != null)
            {
                world.RemoveBody(rectBody1);
            }

            if (rectBody2 != null)
            {
                world.RemoveBody(rectBody2);
            }

            Vector2 rectPosition1 = (screenCenter * Pixel2Meter) + new Vector2(-2f, -2f);

            //create rectange fixture
            rectBody1             = BodyFactory.CreateRectangle(world, 128f * Pixel2Meter, 16f * Pixel2Meter, 1f, rectPosition1);
            rectBody1.BodyType    = BodyType.Dynamic;
            rectBody1.Restitution = 1f;
            rectBody1.Friction    = 0.0f;

            Vector2 rectPosition2 = rectPosition1;

            //create rectange fixture
            rectBody2             = BodyFactory.CreateRectangle(world, 128f * Pixel2Meter, 16f * Pixel2Meter, 1f, rectPosition2);
            rectBody2.BodyType    = BodyType.Dynamic;
            rectBody2.Restitution = 1f;
            rectBody2.Friction    = 0.0f;

            rectBody1.IgnoreCollisionWith(rectBody2);
            rectBody2.IgnoreCollisionWith(rectBody1);
            rectBody1.LinearDamping = 0;
            rectBody2.LinearDamping = 0;

            clampedJoint   = JointFactory.CreateFixedRevoluteJoint(world, rectBody1, new Vector2(-64f * Pixel2Meter, 0), rectPosition1 + new Vector2(-64f * Pixel2Meter, 0));
            currentClamped = rectBody1;
            notClamped     = rectBody2;

            setupClampedJoint();


            connectRectJoint = JointFactory.CreateRevoluteJoint(world, rectBody1, rectBody2, new Vector2(+64f * Pixel2Meter, 0));
            stopSpinning();


            currentMaxPosition = connectRectJoint.WorldAnchorA.X;
        }
コード例 #20
0
        protected override void SetupPhysics(World world)
        {
#if !EDITOR
            Vector2 simPosition = ConvertUnits.ToSimUnits(this._position);

            if (_useShape)
            {
                float simHeight = ConvertUnits.ToSimUnits(this._height);
                float simWidth  = ConvertUnits.ToSimUnits(this._width);
                this.Body          = new Body(world);
                this.Body.Position = simPosition;

                this._origin = new Vector2(this._texture.Width, this._texture.Height) * 0.5f;

                switch (_shapeType)
                {
                case ObjectShape.Quadrilateral:
                {
                    Fixture fixture = FixtureFactory.AttachRectangle(simWidth, simHeight, _mass, Vector2.Zero, Body);
                    break;
                }

                case ObjectShape.Circle:
                {
                    Fixture fixture = FixtureFactory.AttachCircle(simWidth * 0.5f, _mass, this.Body);
                    break;
                }
                }

                this._revoluteJoint = JointFactory.CreateFixedRevoluteJoint(world, Body, Vector2.Zero, simPosition);
            }
            else
            {
                bool useCentroid = false;

                if (_rotatesWithLevel)
                {
                    useCentroid = true;
                }

                TexVertOutput input = SpinAssist.TexToVert(world, _texture, _mass, false);

                this._origin = Vector2.Zero;
                this.Body    = input.Body;


                this.Body.Position = simPosition;
                if (useCentroid)
                {
                    this._revoluteJoint = JointFactory.CreateFixedRevoluteJoint(world, Body, this.Body.LocalCenter, simPosition);
                }
                else
                {
                    this._revoluteJoint = JointFactory.CreateFixedRevoluteJoint(world, Body, this.Body.LocalCenter, simPosition);
                }

                this._revoluteJoint.MaxMotorTorque = float.MaxValue;
                this._revoluteJoint.MotorEnabled   = true;

                if (!_rotatesWithLevel)
                {
                    this.Body.BodyType = BodyType.Dynamic;
                }
                else
                {
                    this.Body.BodyType = BodyType.Dynamic;
                    this.Body.Rotation = this._rotation;
                    float newSpeed = 1 / _motorSpeed;
                    this._motorSpeed = newSpeed;
                }

                if (this._motorEnabled)
                {
                    this._revoluteJoint.MotorSpeed = _motorSpeed;
                }
                else
                {
                    this._revoluteJoint.MotorSpeed = 0.0f;
                }
            }
            this.Body.CollidesWith        = Category.All & ~Category.Cat20;
            this.Body.CollisionCategories = Category.Cat20;

            this.Body.Friction = 3.0f;

            this.Body.UserData = _materialType;
#endif
        }
コード例 #21
0
        public override void LoadContent()
        {
            bodyA = BodyFactory.Instance.CreateRectangleBody(100, 25, 5);
            bodyB = BodyFactory.Instance.CreateRectangleBody(100, 25, 5);

            bodyA.Position = new Vector2(250, 300);
            bodyB.Position = new Vector2(350, 300);

            geomA = GeomFactory.Instance.CreateRectangleGeom(bodyA, 100, 25);
            geomB = GeomFactory.Instance.CreateRectangleGeom(bodyB, 100, 25);

            weldJoint            = new WeldJoint(bodyA, bodyB, new Vector2(300, 300));
            weldJoint.Broke     += weldJoint_Broke;
            weldJoint.Breakpoint = 5.0f;

            PhysicsSimulator.Add(bodyA);
            PhysicsSimulator.Add(bodyB);
            PhysicsSimulator.Add(geomA);
            PhysicsSimulator.Add(geomB);
            PhysicsSimulator.Add(weldJoint);

            brush = new PolygonBrush(Vertices.CreateRectangle(100, 25), Color.White, Color.Black, 2.0f, 0.5f);

            brush.Load(ScreenManager.GraphicsDevice);

            bodyC          = BodyFactory.Instance.CreatePolygonBody(Vertices.CreateGear(50, 20, .50f, 10), 10);
            bodyC.Position = new Vector2(500, 200);

            geomC = GeomFactory.Instance.CreatePolygonGeom(bodyC, Vertices.CreateGear(50, 20, .50f, 10), 1.5f);

            bodyD          = BodyFactory.Instance.CreatePolygonBody(Vertices.CreateGear(50, 20, .50f, 10), 10);
            bodyD.Position = new Vector2(613, 200);

            geomD = GeomFactory.Instance.CreatePolygonGeom(bodyD, Vertices.CreateGear(50, 20, .50f, 10), 1.5f);

            geomC.CollisionGroup      = 2;
            geomD.CollisionGroup      = 3;
            geomC.FrictionCoefficient = 0f;
            geomD.FrictionCoefficient = 0f;

            PhysicsSimulator.Add(bodyC);
            PhysicsSimulator.Add(geomC);
            PhysicsSimulator.Add(bodyD);
            PhysicsSimulator.Add(geomD);

            gearBrushA = new PolygonBrush(Vertices.CreateGear(50, 20, .50f, 10), Color.White, Color.Black, 0.5f, 0.5f);

            gearBrushA.Load(ScreenManager.GraphicsDevice);

            gearBrushB = new PolygonBrush(Vertices.CreateGear(50, 20, .50f, 10), Color.White, Color.Black, 0.5f, 0.5f);

            gearBrushB.Load(ScreenManager.GraphicsDevice);

            revJointA = JointFactory.Instance.CreateFixedRevoluteJoint(bodyC, bodyC.Position);
            revJointB = JointFactory.Instance.CreateFixedRevoluteJoint(bodyD, bodyD.Position);

            PhysicsSimulator.Add(revJointA);
            PhysicsSimulator.Add(revJointB);

            table = new List <Table>();

            table.Add(new Table(new Vector2(200, 450), 200, 50));

            table[0].Load(PhysicsSimulator, ScreenManager.GraphicsDevice);

            base.LoadContent();
        }
コード例 #22
0
        public void Load(GraphicsDevice graphicsDevice, PhysicsSimulator physicsSimulator)
        {
            _rectangleTexture = DrawingHelper.CreateRectangleTexture(graphicsDevice, _rectangleWidth, _rectangleHeight,
                                                                     Color.White, Color.Black);
            int radius;

            if (_attachPoint == 0 | _attachPoint == 2)
            {
                radius = _rectangleHeight;
            }
            else
            {
                radius = _rectangleWidth;
            }
            _circleTexture = DrawingHelper.CreateCircleTexture(graphicsDevice, radius, Color.White, Color.Black);

            //body is created as rectangle so that it has the moment of inertia closer to the final shape of the object.
            _angleSpringleverBody = BodyFactory.Instance.CreateBody(physicsSimulator, 1,
                                                                    BodyFactory.MOIForRectangle(_rectangleWidth,
                                                                                                _rectangleHeight, 1f));

            _rectangleGeom = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, _angleSpringleverBody,
                                                                      _rectangleWidth, _rectangleHeight);
            _rectangleGeom.FrictionCoefficient = .5f;
            _rectangleGeom.CollisionGroup      = _collisionGroup;

            Vector2 offset = Vector2.Zero;

            switch (_attachPoint)
            {
            case 0:
            {
                offset = new Vector2(-_rectangleWidth / 2f, 0);       //offset to rectangle to left
                break;
            }

            case 1:
            {
                offset = new Vector2(0, -_rectangleHeight / 2f);       //offset to rectangle to top
                break;
            }

            case 2:
            {
                offset = new Vector2(_rectangleWidth / 2f, 0);       //offset to rectangle to right
                break;
            }

            case 3:
            {
                offset = new Vector2(0, _rectangleHeight / 2f);       //offset to rectangle to bottom
                break;
            }
            }

            _angleSpringleverBody.Position = _position - offset;

            _circleGeom = GeomFactory.Instance.CreateCircleGeom(physicsSimulator, _angleSpringleverBody, radius, 20,
                                                                offset, 0);
            _circleGeom.FrictionCoefficient = .5f;
            _circleGeom.CollisionGroup      = _collisionGroup;

            _revoluteJoint = JointFactory.Instance.CreateFixedRevoluteJoint(physicsSimulator, _angleSpringleverBody,
                                                                            _position);
            physicsSimulator.Add(_revoluteJoint);
            ControllerFactory.Instance.CreateFixedAngleSpring(physicsSimulator, _angleSpringleverBody,
                                                              _springConstant, _dampningConstant);
        }
コード例 #23
0
        /// <summary>
        /// Load graphics content for the game.
        /// </summary>
        public override void Activate(bool instancePreserved)
        {
            if (!instancePreserved)
            {
                if (content == null)
                {
                    content = new ContentManager(ScreenManager.Game.Services, "Content");
                }

                gameFont = content.Load <SpriteFont>("gamefont");

                bloom = new BloomComponent(ScreenManager.Game);
                ScreenManager.Game.Components.Add(bloom);

                playerBody = new PlayerBody(content.Load <Texture2D>("redPlayer"), world);
                dead       = content.Load <Texture2D>("DeadScreen/redDeathScreen");

                cam2D = new Cam2d(ScreenManager.GraphicsDevice);

                // neon whip wheel
                {
                    // instantiate the cirlce body
                    circleBody                     = BodyFactory.CreateCircle(world, 20.0f / 64, 1f, new Vector2(1000f / 64f, 1000f / 64f));
                    circleBody.BodyType            = BodyType.Static;
                    circleBody.CollisionCategories = Category.Cat2;



                    // fix the circle to the world
                    circleTex = content.Load <Texture2D>("orangeCircle");
                    FixedRevoluteJoint fixedJoint = new FixedRevoluteJoint(circleBody, Vector2.Zero, circleBody.Position);
                    world.AddJoint(fixedJoint);

                    // create the whips
                    Whips.Add(new Whip(content.Load <Texture2D>("orangeChianLinkSmall"), new Vector2(1000, 1000), new Vector2(1350, 1000f), world));
                    Whips.Add(new Whip(content.Load <Texture2D>("orangeChianLinkSmall"), new Vector2(1000, 1000), new Vector2(650, 1000), world));
                    //    Whips.Add(new Whip(content.Load<Texture2D>("orangeChianLinkSmall"), new Vector2(1000, 1000), new Vector2(1000, 1350), world));

                    for (int k = 0; k < Whips.Count; k++)
                    {
                        world.AddJoint(new FixedRevoluteJoint(Whips[k].chainLinks[k], Vector2.Zero, circleBody.Position));
                    }
                }

                // add badies
                {
                    Badies.Add(new Bady(content.Load <Texture2D>("Badies/orangeBady"), new Vector2(-1, -2), 15, true, 180f, content, world));
                    Badies.Add(new Bady(content.Load <Texture2D>("Badies/pinkBady"), new Vector2(-3, 0.5f), 8, false, 90f, content, world));
                }


                // add some squares and some walls
                {
                    int space = 0;

                    for (int i = 0; i < 10; i++)
                    {
                        Walls.Add(new Wall(content.Load <Texture2D>("Walls/blueWallMedium"), new Vector2(space - 5, 0), true, world));
                        Walls.Add(new Wall(content.Load <Texture2D>("Walls/blueWallMedium"), new Vector2(space - 5, 600), true, world));

                        Squares.Add(new Square(content.Load <Texture2D>("Squares/greenSquare"), new Vector2(space, 100), world));
                        Squares.Add(new Square(content.Load <Texture2D>("Squares/pinkSquare"), new Vector2(space + 50, 200), world));

                        Squares.Add(new Square(content.Load <Texture2D>("Squares/yellowSquare"), new Vector2(space, 300), world));
                        Squares.Add(new Square(content.Load <Texture2D>("Squares/orangeSquare"), new Vector2(space + 50, 400), world));

                        Squares.Add(new Square(content.Load <Texture2D>("Squares/greenSquare"), new Vector2(space, 500), world));


                        if (i <= 5)
                        {
                            //vertical walls
                            Walls.Add(new Wall(content.Load <Texture2D>("Walls/blueWallMedium"), new Vector2(-50, space + 50), false, world));
                            Walls.Add(new Wall(content.Load <Texture2D>("Walls/blueWallMedium"), new Vector2(1050, space + 50), false, world));
                        }

                        space += 100;
                    }

                    // extra wall
                    Walls.Add(new Wall(content.Load <Texture2D>("Walls/blueWallMedium"), new Vector2(space + 5, 0), true, world));
                    Walls.Add(new Wall(content.Load <Texture2D>("Walls/blueWallMedium"), new Vector2(space - 10, 0), true, world));

                    // Ninja gate need to change the colour of the ninja gate
                    {
                        ninjaGate = new Shape(content.Load <Texture2D>("NinjaWeapons/blueNinjaWheel"), new Vector2((space - 5), 600f), false, world);
                        Shapes.Add(ninjaGate);
                        Shapes[0].shapeBody.AngularVelocity = 10f;
                        //  Shapes.Add(new Shape(content.Load<Texture2D>("NinjaWeapons/blueNinjaWheel"),new Vector2((space - 5), 600f),false,world));
                        world.AddJoint(new FixedRevoluteJoint(Shapes[0].shapeBody, Vector2.Zero, Shapes[0].shapeBody.Position));
                    }
                }


                //add collectables
                {
                    Collectables.Add(new Collectable(content.Load <Texture2D>("redStar"), new Vector2(0, 25), world));
                    Collectables.Add(new Collectable(content.Load <Texture2D>("redStar"), new Vector2(900, 25), world));
                    Collectables.Add(new Collectable(content.Load <Texture2D>("redStar"), new Vector2(900, 625), world));
                }

                // set cam track

                cam2D.TrackingBody   = playerBody.playerBody;
                cam2D.EnableTracking = true;



                // once the load has finished, we use ResetElapsedTime to tell the game's
                // timing mechanism that we have just finished a very long frame, and that
                // it should not try to catch up.
                ScreenManager.Game.ResetElapsedTime();
            }
        }