A distance joint rains two points on two bodies to remain at a fixed distance from each other. You can view this as a massless, rigid rod.
Inheritance: Joint
Beispiel #1
0
 public static DistanceJoint CreateDistanceJoint(World world, Body bodyA, Body bodyB, Vector2 anchorA,
                                                 Vector2 anchorB)
 {
     DistanceJoint distanceJoint = new DistanceJoint(bodyA, bodyB, anchorA, anchorB);
     world.AddJoint(distanceJoint);
     return distanceJoint;
 }
Beispiel #2
0
        public static DistanceJoint CreateDistanceJoint(World world, Body bodyA, Body bodyB, Vector2 anchorA, Vector2 anchorB, bool useWorldCoordinates = false)
        {
            DistanceJoint distanceJoint = new DistanceJoint(bodyA, bodyB, anchorA, anchorB, useWorldCoordinates);

            world.Add(distanceJoint);
            return(distanceJoint);
        }
Beispiel #3
0
 public void Release()
 {
     if (_joint != null)
     {
         Physic.World.RemoveJoint(_joint);
         _joint = null;
     }
 }
Beispiel #4
0
		public override Joint createJoint()
		{
			var joint = new DistanceJoint( bodyA, bodyB, ownerBodyAnchor * FSConvert.displayToSim, otherBodyAnchor * FSConvert.displayToSim );
			joint.collideConnected = collideConnected;
			joint.frequency = frequency;
			joint.dampingRatio = dampingRatio;
			return joint;
		}
	public override void InitJoint ()
	{
		base.InitJoint ();
		joint = FarseerPhysics.Factories.JointFactory.CreateDistanceJoint(FSWorldComponent.PhysicsWorld, 
			BodyA.PhysicsBody, 
			BodyB.PhysicsBody,
			Microsoft.Xna.Framework.FVector2.Zero,
			Microsoft.Xna.Framework.FVector2.Zero);
		joint.CollideConnected = CollideConnected;
		joint.Frequency = Frequency;
		//joint.DampingRatio = 0.5f;
	}
Beispiel #6
0
        private float HandleRaycast(Fixture fixture, Vector2 point, Vector2 normal, float fraction)
        {
            if (fixture.CollisionCategories == _type.Category || Body.FixtureList.Any(f => f.CollisionCategories == fixture.CollisionCategories))
                return fraction;

            var other = fixture.Body;

            _joint = Physic.World.CreateDistanceJoint(other, Body, other.GetLocalPoint(point), new Vector2(0, 0));
            _joint.Frequency = 1f;
            _joint.CollideConnected = true;

            return 0;
        }
        public PendulumPhysicsComponent(Engine engine, Point gameWorldPivotPosition, Point gameWorldWeightPosition)
            : base(engine)
        {
            pivotFixture = FixtureFactory.CreateRectangle(Engine.Physics.World, 0.8f, 0.8f, 1.0f);
              MainFixture = FixtureFactory.CreateRectangle(Engine.Physics.World, 0.8f, 0.8f, 1.0f);
              MainFixture.Body.Mass = 100.0f;

              pivotFixture.Body.Position = Engine.Physics.PositionToPhysicsWorld(new Vector2(gameWorldPivotPosition.X, gameWorldPivotPosition.Y));
              MainFixture.Body.Position = Engine.Physics.PositionToPhysicsWorld(new Vector2(gameWorldWeightPosition.X, gameWorldWeightPosition.Y));

              pivotFixture.CollisionFilter.CollidesWith = Category.Cat31;
              MainFixture.Body.BodyType = BodyType.Dynamic;

              distanceJoint = JointFactory.CreateDistanceJoint(Engine.Physics.World, pivotFixture.Body, MainFixture.Body, Vector2.Zero, MainFixture.Body.LocalCenter - new Vector2(0.0f, 0.4f));
        }
Beispiel #8
0
        private float SilkSlingLeft(Fixture f, Vector2 p, Vector2 n, float fr)
        {

            if (f.Body == ragdoll._lowerLeftArm) return -1;

            Vector2 handAnchor = getHandAnchor(false);

           
            UndoSilk(false);
           

            leftSilk = new DistanceJoint(ragdoll._lowerLeftArm, f.Body, ragdoll._lowerLeftArm.GetLocalPoint(handAnchor), f.Body.GetLocalPoint(p));
            leftSilk.Frequency = silkForce;
            leftSilk.DampingRatio = .5f;
            leftSilk.Length = 0;
            leftSilk.CollideConnected = true;
            world.AddJoint(leftSilk);

            return fr; 
        }
Beispiel #9
0
        private bool LoadJoint()
        {
            var otherObject = GameObject.Scene.GameObjects.FirstOrDefault(g => g.Id == OtherObjectId) as GameObject;
            if (otherObject == null) return false;

            var otherRigidBody = otherObject.Components.FirstOrDefault(c => c is RigidBodyFarseerComponent) as RigidBodyFarseerComponent;
            if (otherRigidBody == null) return false;

            if (!otherObject.IsLoaded) otherObject.Load();
            if (!otherRigidBody.IsLoaded) return false;

            var myRigidBody = GameObject.Components.FirstOrDefault(c => c is RigidBodyFarseerComponent) as RigidBodyFarseerComponent;
            if (!myRigidBody.IsLoaded) myRigidBody.Load();
            if (!myRigidBody.IsLoaded) return false;

            var world = GameObject.Scene.Physics.GetWorld() as World;
            if (world == null) throw new InvalidOperationException("Not a farseer physics world.");

            _joint = JointFactory.CreateDistanceJoint(world, myRigidBody.RigidBody, otherRigidBody.RigidBody, Vector2.Zero, Vector2.Zero);

            //world.AddJoint(_joint);

            return true;
        }
 public static DistanceJoint CreateDistanceJoint(World world, Body bodyA, Body bodyB, Vector2 anchorA, Vector2 anchorB, bool useWorldCoordinates = false)
 {
     DistanceJoint distanceJoint = new DistanceJoint(bodyA, bodyB, anchorA, anchorB, useWorldCoordinates);
     world.AddJoint(distanceJoint);
     return distanceJoint;
 }
Beispiel #11
0
 private void UndoSilk(bool rightHand)
 {
     if (rightHand)
     {
         if (rightSilk != null)
         {
             world.RemoveJoint(rightSilk);
             rightSilk = null;
         }
     }
     else
     {
         if (leftSilk != null)
         {
             world.RemoveJoint(leftSilk);
             leftSilk = null;
         }
     }
 }
        public override void LoadContent()
        {
            base.LoadContent();

            World.Gravity = new Vector2(0f, 20f);

            _border = new Border(World, Lines, Framework.GraphicsDevice);

            _obstacles = new Body(World);
            FixtureFactory.AttachEdge(new Vector2(-16f, -1f), new Vector2(-14f, 1f), _obstacles);
            FixtureFactory.AttachEdge(new Vector2(-14f, 1f), new Vector2(-12f, -1f), _obstacles);

            FixtureFactory.AttachEdge(new Vector2(14f, -1f), new Vector2(12f, 5f), _obstacles);
            FixtureFactory.AttachEdge(new Vector2(14f, -1f), new Vector2(16f, 5f), _obstacles);

            _angleBody[0] = BodyFactory.CreateRectangle(World, 1.5f, 1.5f, 1f);
            _angleBody[0].BodyType = BodyType.Dynamic;
            _angleBody[0].Friction = 0.7f;
            _angleBody[0].Position = new Vector2(-15f, -5f);
            _angleBody[1] = BodyFactory.CreateRectangle(World, 1.5f, 1.5f, 1f);
            _angleBody[1].BodyType = BodyType.Dynamic;
            _angleBody[1].Friction = 0.7f;
            _angleBody[1].Position = new Vector2(-18f, 5f);
            _angleBody[2] = BodyFactory.CreateRectangle(World, 1.5f, 1.5f, 1f);
            _angleBody[2].BodyType = BodyType.Dynamic;
            _angleBody[2].Friction = 0.7f;
            _angleBody[2].Position = new Vector2(-10f, 5f);

            World.AddJoint(new AngleJoint(_angleBody[0], _angleBody[1]));
            World.AddJoint(new AngleJoint(_angleBody[0], _angleBody[2]));

            _distanceBody[0] = BodyFactory.CreateRectangle(World, 1.5f, 1.5f, 1f);
            _distanceBody[0].BodyType = BodyType.Dynamic;
            _distanceBody[0].Friction = 0.7f;
            _distanceBody[0].Position = new Vector2(11.5f, -4f);
            _distanceBody[1] = BodyFactory.CreateRectangle(World, 1.5f, 1.5f, 1f);
            _distanceBody[1].BodyType = BodyType.Dynamic;
            _distanceBody[1].Friction = 0.7f;
            _distanceBody[1].Position = new Vector2(16.5f, -4f);
            _distanceBody[2] = BodyFactory.CreateRectangle(World, 1.5f, 1.5f, 1f);
            _distanceBody[2].BodyType = BodyType.Dynamic;
            _distanceBody[2].Friction = 0.7f;
            _distanceBody[2].Position = new Vector2(11.5f, -6f);
            _distanceBody[3] = BodyFactory.CreateRectangle(World, 1.5f, 1.5f, 1f);
            _distanceBody[3].BodyType = BodyType.Dynamic;
            _distanceBody[3].Friction = 0.7f;
            _distanceBody[3].Position = new Vector2(16.5f, -6f);

            DistanceJoint softDistance = new DistanceJoint(_distanceBody[0], _distanceBody[1], Vector2.Zero, Vector2.Zero, false);
            softDistance.DampingRatio = 0.3f;
            softDistance.Frequency = 5f;
            World.AddJoint(softDistance);
            World.AddJoint(new DistanceJoint(_distanceBody[2], _distanceBody[3], Vector2.Zero, Vector2.Zero, false));

            // create sprites based on bodies
            _angleCube = new Sprite(ContentWrapper.TextureFromShape(_angleBody[0].FixtureList[0].Shape, "Square", ContentWrapper.Gold, ContentWrapper.Orange, ContentWrapper.Grey, 1f));
            _distanceCube = new Sprite(ContentWrapper.TextureFromShape(_distanceBody[0].FixtureList[0].Shape, "Stripe", ContentWrapper.Red, ContentWrapper.Blue, ContentWrapper.Grey, 4f));
        }
Beispiel #13
0
        private void CreateLeg(float s, Vector2 wheelAnchor)
        {
            Vector2 p1 = new Vector2(5.4f * s, -6.1f);
            Vector2 p2 = new Vector2(7.2f * s, -1.2f);
            Vector2 p3 = new Vector2(4.3f * s, -1.9f);
            Vector2 p4 = new Vector2(3.1f * s, 0.8f);
            Vector2 p5 = new Vector2(6.0f * s, 1.5f);
            Vector2 p6 = new Vector2(2.5f * s, 3.7f);

            PolygonShape poly1 = new PolygonShape(1);
            PolygonShape poly2 = new PolygonShape(1);

            Vertices vertices = new Vertices(3);

            if (s > 0.0f)
            {
                vertices.Add(p1);
                vertices.Add(p2);
                vertices.Add(p3);
                poly1.Vertices = vertices;

                vertices[0] = Vector2.Zero;
                vertices[1] = p5 - p4;
                vertices[2] = p6 - p4;
                poly2.Vertices = vertices;
            }
            else
            {
                vertices.Add(p1);
                vertices.Add(p3);
                vertices.Add(p2);
                poly1.Vertices = vertices;

                vertices[0] = Vector2.Zero;
                vertices[1] = p6 - p4;
                vertices[2] = p5 - p4;
                poly2.Vertices = vertices;
            }

            Body body1 = BodyFactory.CreateBody(World);
            body1.BodyType = BodyType.Dynamic;
            body1.Position = _offset;
            body1.AngularDamping = 10.0f;

            Body body2 = BodyFactory.CreateBody(World);
            body2.BodyType = BodyType.Dynamic;
            body2.Position = p4 + _offset;
            body2.AngularDamping = 10.0f;

            Fixture f1 = body1.CreateFixture(poly1);
            f1.CollisionGroup = -1;

            Fixture f2 = body2.CreateFixture(poly2);
            f2.CollisionGroup = -1;

            // Using a soft distanceraint can reduce some jitter.
            // It also makes the structure seem a bit more fluid by
            // acting like a suspension system.
            DistanceJoint djd = new DistanceJoint(body1, body2, p2 + _offset, p5 + _offset, true);
            djd.DampingRatio = 0.5f;
            djd.Frequency = 10.0f;

            World.AddJoint(djd);

            DistanceJoint djd2 = new DistanceJoint(body1, body2, p3 + _offset, p4 + _offset, true);
            djd2.DampingRatio = 0.5f;
            djd2.Frequency = 10.0f;

            World.AddJoint(djd2);

            DistanceJoint djd3 = new DistanceJoint(body1, _wheel, p3 + _offset, wheelAnchor + _offset, true);
            djd3.DampingRatio = 0.5f;
            djd3.Frequency = 10.0f;

            World.AddJoint(djd3);

            DistanceJoint djd4 = new DistanceJoint(body2, _wheel, p6 + _offset, wheelAnchor + _offset, true);
            djd4.DampingRatio = 0.5f;
            djd4.Frequency = 10.0f;

            World.AddJoint(djd4);

            Vector2 anchor = p4 - new Vector2(0.0f, 0.8f) /*+ _offset*/;
            RevoluteJoint rjd = new RevoluteJoint(body2, _chassis, p4 + _offset, true);
            World.AddJoint(rjd);
        }
        private void CreateLeg(World world, float s, Vector2 wheelAnchor, int index)
        {
            Vector2 p1 = new Vector2(5.4f * s, 6.1f);
            Vector2 p2 = new Vector2(7.2f * s, 1.2f);
            Vector2 p3 = new Vector2(4.3f * s, 1.9f);
            Vector2 p4 = new Vector2(3.1f * s, -0.8f);
            Vector2 p5 = new Vector2(6.0f * s, -1.5f);
            Vector2 p6 = new Vector2(2.5f * s, -3.7f);

            PolygonShape poly1 = new PolygonShape(1f);
            PolygonShape poly2 = new PolygonShape(2f);

            Vertices vertices = new Vertices(3);

            if (s < 0f)
            {
                vertices.Add(p1);
                vertices.Add(p2);
                vertices.Add(p3);
                poly1.Set(vertices);

                vertices[0] = Vector2.Zero;
                vertices[1] = p5 - p4;
                vertices[2] = p6 - p4;
                poly2.Set(vertices);
            }
            else
            {
                vertices.Add(p1);
                vertices.Add(p3);
                vertices.Add(p2);
                poly1.Set(vertices);

                vertices[0] = Vector2.Zero;
                vertices[1] = p6 - p4;
                vertices[2] = p5 - p4;
                poly2.Set(vertices);
            }

            Body body1 = BodyFactory.CreateBody(world);
            body1.BodyType = BodyType.Dynamic;
            body1.Position = _position;
            body1.AngularDamping = 10f;
            if (s < 0f)
            {
                _leftLegs[index] = body1;
            }
            else
            {
                _rightLegs[index] = body1;
            }

            Body body2 = BodyFactory.CreateBody(world);
            body2.BodyType = BodyType.Dynamic;
            body2.Position = p4 + _position;
            body2.AngularDamping = 10f;
            if (s < 0f)
            {
                _leftShoulders[index] = body2;
            }
            else
            {
                _rightShoulders[index] = body2;
            }

            Fixture f1 = body1.CreateFixture(poly1);
            f1.CollisionGroup = -1;

            Fixture f2 = body2.CreateFixture(poly2);
            f2.CollisionGroup = -1;

            // Using a soft distanceraint can reduce some jitter.
            // It also makes the structure seem a bit more fluid by
            // acting like a suspension system.
            DistanceJoint djd = new DistanceJoint(body1, body2, body1.GetLocalPoint(p2 + _position),
                                                  body2.GetLocalPoint(p5 + _position));
            djd.DampingRatio = 0.5f;
            djd.Frequency = 10f;

            world.AddJoint(djd);
            _walkerJoints.Add(djd);

            DistanceJoint djd2 = new DistanceJoint(body1, body2, body1.GetLocalPoint(p3 + _position),
                                                   body2.GetLocalPoint(p4 + _position));
            djd2.DampingRatio = 0.5f;
            djd2.Frequency = 10f;

            world.AddJoint(djd2);
            _walkerJoints.Add(djd2);

            DistanceJoint djd3 = new DistanceJoint(body1, _wheel, body1.GetLocalPoint(p3 + _position),
                                                   _wheel.GetLocalPoint(wheelAnchor + _position));
            djd3.DampingRatio = 0.5f;
            djd3.Frequency = 10f;

            world.AddJoint(djd3);
            _walkerJoints.Add(djd3);

            DistanceJoint djd4 = new DistanceJoint(body2, _wheel, body2.GetLocalPoint(p6 + _position),
                                                   _wheel.GetLocalPoint(wheelAnchor + _position));
            djd4.DampingRatio = 0.5f;
            djd4.Frequency = 10f;

            world.AddJoint(djd4);
            _walkerJoints.Add(djd4);

            Vector2 anchor = p4 - new Vector2(0f, -0.8f);
            RevoluteJoint rjd = new RevoluteJoint(body2, _chassis, body2.GetLocalPoint(_chassis.GetWorldPoint(anchor)),
                                                  anchor);
            world.AddJoint(rjd);
        }
        private void CreateLeg(World world, float direction, Vector2 wheelAnchor, int index)
        {
            Vector2[] points = {
                                new Vector2(5.4f * direction, 6.1f),
                                new Vector2(7.2f * direction, 1.2f),
                                new Vector2(4.3f * direction, 1.9f),
                                new Vector2(3.1f * direction, -0.8f),
                                new Vector2(6.0f * direction, -1.5f),
                                new Vector2(2.5f * direction, -3.7f)
                               };

            PolygonShape legPolygon = new PolygonShape(1f);
            PolygonShape shoulderPolygon = new PolygonShape(1f);

            if (direction < 0f)
            {
                legPolygon.Vertices = new Vertices(new[] { points[0], points[1], points[2] });
                shoulderPolygon.Vertices = new Vertices(new[] { Vector2.Zero, points[4] - points[3], points[5] - points[3] });
            }

            if (direction > 0f)
            {
                legPolygon.Vertices = new Vertices(new[] { points[0], points[2], points[1] });
                shoulderPolygon.Vertices = new Vertices(new[] { Vector2.Zero, points[5] - points[3], points[4] - points[3] });
            }

            Body leg = BodyFactory.CreateBody(world);
            leg.BodyType = BodyType.Dynamic;
            leg.Position = _position;
            leg.AngularDamping = 10f;

            if (direction < 0f)
                _leftLegs[index] = leg;

            if (direction > 0f)
                _rightLegs[index] = leg;

            Body shoulder = BodyFactory.CreateBody(world);
            shoulder.BodyType = BodyType.Dynamic;
            shoulder.Position = points[3] + _position;
            shoulder.AngularDamping = 10f;

            if (direction < 0f)
                _leftShoulders[index] = shoulder;

            if (direction > 0f)
                _rightShoulders[index] = shoulder;

            Fixture legFixture = leg.CreateFixture(legPolygon);
            legFixture.CollisionGroup = -1;

            Fixture shoulderFixture = shoulder.CreateFixture(shoulderPolygon);
            shoulderFixture.CollisionGroup = -1;

            // Using a soft distancejoint can reduce some jitter.
            // It also makes the structure seem a bit more fluid by
            // acting like a suspension system.
            DistanceJoint djd = new DistanceJoint(leg, shoulder, points[1] + _position, points[4] + _position, true);
            djd.DampingRatio = 0.5f;
            djd.Frequency = 10f;

            world.AddJoint(djd);
            _walkerJoints.Add(djd);

            DistanceJoint djd2 = new DistanceJoint(leg, shoulder, points[2] + _position, points[3] + _position, true);
            djd2.DampingRatio = 0.5f;
            djd2.Frequency = 10f;

            world.AddJoint(djd2);
            _walkerJoints.Add(djd2);

            DistanceJoint djd3 = new DistanceJoint(leg, _wheel, points[2] + _position, wheelAnchor + _position, true);
            djd3.DampingRatio = 0.5f;
            djd3.Frequency = 10f;

            world.AddJoint(djd3);
            _walkerJoints.Add(djd3);

            DistanceJoint djd4 = new DistanceJoint(shoulder, _wheel, points[5] + _position, wheelAnchor + _position, true);
            djd4.DampingRatio = 0.5f;
            djd4.Frequency = 10f;

            world.AddJoint(djd4);
            _walkerJoints.Add(djd4);

            RevoluteJoint rjd = new RevoluteJoint(shoulder, _chassis, points[3] + _position, true);
            world.AddJoint(rjd);
        }
        /// <summary>
        /// Adds a Physics Joint to the simulation
        /// </summary>
        /// <param name="joint"></param>
        public void AddPhysicsJoint(PhysicsJoint joint)
        {
            string bodyOne = joint.BodyOne;
            string bodyTwo = joint.BodyTwo;
            short collisionGroup = Convert.ToInt16(joint.CollisionGroup);
            bool isAngleSpringEnabled = joint.AngleSpringEnabled;
            float springConstant = (float)joint.AngleSpringConstant;
            float angleLowerLimit = (float)joint.AngleLowerLimit;
            float angleUpperLimit = (float)joint.AngleUpperLimit;


            Point center = joint.GetCenter();
            xna.Vector2 ptCollisionCenter = new xna.Vector2((float)center.X, (float)center.Y);


            if (!PhysicsObjects.ContainsKey(bodyOne))
                throw new Exception("Cannot add joint for an invalid BodyOne value of '" + bodyOne + "'. If using Behaviors, did you forgot to add a PhysicsObjectBehavior?");
            if (!PhysicsObjects.ContainsKey(bodyTwo))
                throw new Exception("Cannot add joint for an invalid BodyTwo value of '" + bodyTwo + "'. If using Behaviors, did you forgot to add a PhysicsObjectBehavior?");

            Body body1 = PhysicsObjects[bodyOne].BodyObject;
            Body body2 = PhysicsObjects[bodyTwo].BodyObject;

            xna.Vector2 ptCollisionCenterA = _boundaryHelper.ScreenToWorld(ptCollisionCenter);
            xna.Vector2 ptCollisionCenterB = _boundaryHelper.ScreenToWorld(ptCollisionCenter);

            // account for offset within body
            ptCollisionCenterA -= body1.Position;
            ptCollisionCenterB -= body2.Position;

            // DEMO: (5) Create Joints
            if (joint.IsWeldJoint)
            {
                WeldJoint weldJoint = new WeldJoint(body1, body2, ptCollisionCenterA, ptCollisionCenterB);
                Simulator.AddJoint(weldJoint);
            }
            else
                if (joint.IsDistanceJoint)
                {
                    DistanceJoint distanceJoint = new DistanceJoint(body1, body2, ptCollisionCenterA, ptCollisionCenterB);

                    Simulator.AddJoint(distanceJoint);

                }
                else
                {
                    RevoluteJoint revoluteJoint = new RevoluteJoint(body1, body2, ptCollisionCenterA, ptCollisionCenterB);

                    Simulator.AddJoint(revoluteJoint);

                    if (isAngleSpringEnabled)
                    {
                        AngleJoint aj = new AngleJoint(body1, body2);
                        aj.TargetAngle = 0;
                        aj.Softness = springConstant;


                        Simulator.AddJoint(aj);

                    }

                    if (angleUpperLimit != -1 && angleLowerLimit != -1)
                    {
                        float upperAngle = (float)PhysicsUtilities.DegreesToRadians(angleUpperLimit);
                        float lowerAngle = (float)PhysicsUtilities.DegreesToRadians(angleLowerLimit);

                        revoluteJoint.LimitEnabled = true;
                        revoluteJoint.LowerLimit = lowerAngle;
                        revoluteJoint.UpperLimit = upperAngle;
                    }
                }

            if (collisionGroup > 0)
            {

                foreach (Fixture f in PhysicsObjects[bodyOne].BodyObject.FixtureList)
                {
                    f.CollisionGroup = collisionGroup;
                }
                foreach (Fixture f in PhysicsObjects[bodyTwo].BodyObject.FixtureList)
                {
                    f.CollisionGroup = collisionGroup;
                }
            }

            // get rid of the UI representation of the joint
            joint.Visibility = Visibility.Collapsed;

        }
Beispiel #17
0
        private void CreateJoints(World world)
        {
            const float dampingRatio = 1;
            const float frequency = 25;

            //head -> body
            DistanceJoint jHeadBody = new DistanceJoint(_head.Body, _body[0].Body,
                                                        new Vector2(0, -1), new Vector2(0, 2));
            jHeadBody.CollideConnected = true;
            jHeadBody.DampingRatio = dampingRatio;
            jHeadBody.Frequency = frequency;
            jHeadBody.Length = 0.025f;
            world.AddJoint(jHeadBody);

            //lowerLeftArm -> upperLeftArm
            DistanceJoint jLeftArm = new DistanceJoint(_lowerLeftArm[0].Body, _upperLeftArm[0].Body,
                                                       new Vector2(0, 1), new Vector2(0, -1));
            jLeftArm.CollideConnected = true;
            jLeftArm.DampingRatio = dampingRatio;
            jLeftArm.Frequency = frequency;
            jLeftArm.Length = 0.02f;
            world.AddJoint(jLeftArm);

            //upperLeftArm -> body
            DistanceJoint jLeftArmBody = new DistanceJoint(_upperLeftArm[0].Body, _body[0].Body,
                                                           new Vector2(0, 1), new Vector2(-1, 1.5f));
            jLeftArmBody.CollideConnected = true;
            jLeftArmBody.DampingRatio = dampingRatio;
            jLeftArmBody.Frequency = frequency;
            jLeftArmBody.Length = 0.02f;
            world.AddJoint(jLeftArmBody);

            //lowerRightArm -> upperRightArm
            DistanceJoint jRightArm = new DistanceJoint(_lowerRightArm[0].Body, _upperRightArm[0].Body,
                                                        new Vector2(0, 1), new Vector2(0, -1));
            jRightArm.CollideConnected = true;
            jRightArm.DampingRatio = dampingRatio;
            jRightArm.Frequency = frequency;
            jRightArm.Length = 0.02f;
            world.AddJoint(jRightArm);

            //upperRightArm -> body
            DistanceJoint jRightArmBody = new DistanceJoint(_upperRightArm[0].Body, _body[0].Body,
                                                            new Vector2(0, 1), new Vector2(1, 1.5f));

            jRightArmBody.CollideConnected = true;
            jRightArmBody.DampingRatio = dampingRatio;
            jRightArmBody.Frequency = 25;
            jRightArmBody.Length = 0.02f;
            world.AddJoint(jRightArmBody);

            //lowerLeftLeg -> upperLeftLeg
            DistanceJoint jLeftLeg = new DistanceJoint(_lowerLeftLeg[0].Body, _upperLeftLeg[0].Body,
                                                       new Vector2(0, 1.1f), new Vector2(0, -1));
            jLeftLeg.CollideConnected = true;
            jLeftLeg.DampingRatio = dampingRatio;
            jLeftLeg.Frequency = frequency;
            jLeftLeg.Length = 0.05f;
            world.AddJoint(jLeftLeg);

            //upperLeftLeg -> body
            DistanceJoint jLeftLegBody = new DistanceJoint(_upperLeftLeg[0].Body, _body[0].Body,
                                                           new Vector2(0, 1.1f), new Vector2(-0.8f, -1.9f));
            jLeftLegBody.CollideConnected = true;
            jLeftLegBody.DampingRatio = dampingRatio;
            jLeftLegBody.Frequency = frequency;
            jLeftLegBody.Length = 0.02f;
            world.AddJoint(jLeftLegBody);

            //lowerRightleg -> upperRightleg
            DistanceJoint jRightLeg = new DistanceJoint(_lowerRightLeg[0].Body, _upperRightLeg[0].Body,
                                                        new Vector2(0, 1.1f), new Vector2(0, -1));
            jRightLeg.CollideConnected = true;
            jRightLeg.DampingRatio = dampingRatio;
            jRightLeg.Frequency = frequency;
            jRightLeg.Length = 0.05f;
            world.AddJoint(jRightLeg);

            //upperRightleg -> body
            DistanceJoint jRightLegBody = new DistanceJoint(_upperRightLeg[0].Body, _body[0].Body,
                                                            new Vector2(0, 1.1f), new Vector2(0.8f, -1.9f));
            jRightLegBody.CollideConnected = true;
            jRightLegBody.DampingRatio = dampingRatio;
            jRightLegBody.Frequency = frequency;
            jRightLegBody.Length = 0.02f;
            world.AddJoint(jRightLegBody);
        }
Beispiel #18
0
        public WebOfGoo(World world, Vector2 position, float radius, int rings, int sides)
        {
            _ringBodys = new List<List<Body>>(rings);
            _ringJoints = new List<DistanceJoint>();

            for (int i = 1; i < rings; i++)
            {
                Vertices vertices = PolygonTools.CreateCircle(i * 2.9f, sides);
                vertices.Translate(ref position);
                List<Body> bodies = new List<Body>(sides);

                //Create the first goo
                Body previous = BodyFactory.CreateCircle(world, radius, 0.2f, vertices[0]);
                previous.BodyType = BodyType.Dynamic;

                bodies.Add(previous);

                //Connect the first goo to the next
                for (int j = 1; j < vertices.Count; j++)
                {
                    Body current = BodyFactory.CreateCircle(world, radius, 0.2f, vertices[j]);
                    current.BodyType = BodyType.Dynamic;

                    DistanceJoint joint = new DistanceJoint(previous, current, Vector2.Zero, Vector2.Zero);
                    joint.Frequency = 4.0f;
                    joint.DampingRatio = 0.5f;
                    joint.Breakpoint = Breakpoint;
                    world.AddJoint(joint);
                    _ringJoints.Add(joint);

                    previous = current;
                    bodies.Add(current);
                }

                //Connect the first and the last goo
                DistanceJoint jointClose = new DistanceJoint(bodies[0], bodies[bodies.Count - 1], Vector2.Zero, Vector2.Zero);
                jointClose.Frequency = 4.0f;
                jointClose.DampingRatio = 0.5f;
                jointClose.Breakpoint = Breakpoint;
                world.AddJoint(jointClose);
                _ringJoints.Add(jointClose);

                _ringBodys.Add(bodies);
            }

            //Create an outer ring
            Vertices frame = PolygonTools.CreateCircle(rings * 2.9f - 0.9f, sides);
            frame.Translate(ref position);

            Body anchor = new Body(world, position);
            anchor.BodyType = BodyType.Static;

            //Attach the outer ring to the anchor
            for (int i = 0; i < _ringBodys[rings - 2].Count; i++)
            {
                DistanceJoint joint = new DistanceJoint(anchor, _ringBodys[rings - 2][i], frame[i], _ringBodys[rings - 2][i].Position, true);
                joint.Frequency = 8.0f;
                joint.DampingRatio = 0.5f;
                joint.Breakpoint = Breakpoint;
                world.AddJoint(joint);
                _ringJoints.Add(joint);
            }

            //Interconnect the rings
            for (int i = 1; i < _ringBodys.Count; i++)
            {
                for (int j = 0; j < sides; j++)
                {
                    DistanceJoint joint = new DistanceJoint(_ringBodys[i - 1][j], _ringBodys[i][j], Vector2.Zero, Vector2.Zero);
                    joint.Frequency = 4.0f;
                    joint.DampingRatio = 0.5f;
                    joint.Breakpoint = Breakpoint;
                    world.AddJoint(joint);
                    _ringJoints.Add(joint);
                }
            }

            _link = new Sprite(ContentWrapper.GetTexture("Link"));
            _goo = new Sprite(ContentWrapper.GetTexture("Goo"));
        }
Beispiel #19
0
        private WebTest()
        {
            World.JointRemoved += JointRemovedFired;
            World.BodyRemoved += BodyRemovedFired;

            Body ground = BodyFactory.CreateEdge(World, new Vector2(-40, 0), new Vector2(40, 0));

            {
                _bodies[0] = BodyFactory.CreateRectangle(World, 1f, 1f, 5, new Vector2(-5.0f, 5.0f));
                _bodies[0].BodyType = BodyType.Dynamic;

                _bodies[1] = BodyFactory.CreateRectangle(World, 1f, 1f, 5, new Vector2(5.0f, 5.0f));
                _bodies[1].BodyType = BodyType.Dynamic;

                _bodies[2] = BodyFactory.CreateRectangle(World, 1f, 1f, 5, new Vector2(5.0f, 15.0f));
                _bodies[2].BodyType = BodyType.Dynamic;

                _bodies[3] = BodyFactory.CreateRectangle(World, 1f, 1f, 5, new Vector2(-5.0f, 15.0f));
                _bodies[3].BodyType = BodyType.Dynamic;

                DistanceJoint dj = new DistanceJoint(ground, _bodies[0], new Vector2(-10.0f, 0.0f), new Vector2(-0.5f, -0.5f), false);
                _joints[0] = dj;
                //dj.Length = (dj.BodyB.GetWorldPoint(ref dj.LocalAnchorB) - dj.BodyA.GetWorldPoint(ref dj.LocalAnchorA)).Length();
                dj.Frequency = 2.0f;
                dj.DampingRatio = 0.0f;
                World.AddJoint(_joints[0]);

                DistanceJoint dj1 = new DistanceJoint(ground, _bodies[1], new Vector2(10.0f, 0.0f), new Vector2(0.5f, -0.5f), false);
                _joints[1] = dj1;
                //dj1.Length = (dj1.BodyB.GetWorldPoint(ref dj1.LocalAnchorB) - dj1.BodyA.GetWorldPoint(ref dj1.LocalAnchorA)).Length();
                dj1.Frequency = 2.0f;
                dj1.DampingRatio = 0.0f;
                World.AddJoint(_joints[1]);

                DistanceJoint dj2 = new DistanceJoint(ground, _bodies[2], new Vector2(10.0f, 20.0f), new Vector2(0.5f, 0.5f), false);
                _joints[2] = dj2;
                //dj2.Length = (dj2.BodyB.GetWorldPoint(ref dj2.LocalAnchorB) - dj2.BodyA.GetWorldPoint(ref dj2.LocalAnchorA)).Length();
                dj2.Frequency = 2.0f;
                dj2.DampingRatio = 0.0f;
                World.AddJoint(_joints[2]);

                DistanceJoint dj3 = new DistanceJoint(ground, _bodies[3], new Vector2(-10.0f, 20.0f), new Vector2(-0.5f, 0.5f), false);
                _joints[3] = dj3;
                //dj3.Length = (dj3.BodyB.GetWorldPoint(ref dj3.LocalAnchorB) - dj3.BodyA.GetWorldPoint(ref dj3.LocalAnchorA)).Length();
                dj3.Frequency = 2.0f;
                dj3.DampingRatio = 0.0f;
                World.AddJoint(_joints[3]);

                DistanceJoint dj4 = new DistanceJoint(_bodies[0], _bodies[1], new Vector2(0.5f, 0.0f), new Vector2(-0.5f, 0.0f), false);
                _joints[4] = dj4;
                // dj4.Length = (dj4.BodyB.GetWorldPoint(ref dj4.LocalAnchorB) - dj4.BodyA.GetWorldPoint(ref dj4.LocalAnchorA)).Length();
                dj4.Frequency = 2.0f;
                dj4.DampingRatio = 0.0f;
                World.AddJoint(_joints[4]);

                DistanceJoint dj5 = new DistanceJoint(_bodies[1], _bodies[2], new Vector2(0.0f, 0.5f), new Vector2(0.0f, -0.5f), false);
                _joints[5] = dj5;
                // dj5.Length = (dj5.BodyB.GetWorldPoint(ref dj5.LocalAnchorB) - dj5.BodyA.GetWorldPoint(ref dj5.LocalAnchorA)).Length();
                dj5.Frequency = 2.0f;
                dj5.DampingRatio = 0.0f;
                World.AddJoint(_joints[5]);

                DistanceJoint dj6 = new DistanceJoint(_bodies[2], _bodies[3], new Vector2(-0.5f, 0.0f), new Vector2(0.5f, 0.0f), false);
                _joints[6] = dj6;
                //dj6.Length = (dj6.BodyB.GetWorldPoint(ref dj6.LocalAnchorB) - dj6.BodyA.GetWorldPoint(ref dj6.LocalAnchorA)).Length();
                dj6.Frequency = 2.0f;
                dj6.DampingRatio = 0.0f;
                World.AddJoint(_joints[6]);

                DistanceJoint dj7 = new DistanceJoint(_bodies[3], _bodies[0], new Vector2(0.0f, -0.5f), new Vector2(0.0f, 0.5f), false);
                _joints[7] = dj7;
                // dj7.Length = (dj7.BodyB.GetWorldPoint(ref dj7.LocalAnchorB) - dj7.BodyA.GetWorldPoint(ref dj7.LocalAnchorA)).Length();
                dj7.Frequency = 2.0f;
                dj7.DampingRatio = 0.0f;
                World.AddJoint(_joints[7]);
            }
        }
Beispiel #20
0
 private void RemovePlayerJoint()
 {
     if (_playerBodyJoint != null)
     {
         PlayWindow.World.RemoveJoint(_playerBodyJoint);
         _playerBodyJoint = null;
     }
 }
Beispiel #21
0
 private void CreatePlayerJoint(Body playerBody)
 {
     _playerBodyJoint = JointFactory.CreateDistanceJoint(PlayWindow.World, Body, playerBody, Vector2.Zero, Vector2.Zero);
     _playerBodyJoint.Frequency = 5.5f;
     _playerBodyJoint.DampingRatio = 1.5f;
     _playerBodyJoint.Length = 0.01f;
     _playerBodyJoint.CollideConnected = true;
 }
Beispiel #22
0
        public Player(Vector2 spawn)
        {
            // To locomote the player, we're going to create a model like this:
            //   +-----+
            //   |     |
            //   |torso|  <-- Rectangle
            //   |/---\|
            //   |legs |  <-- circle
            //    \---/
            // With the torso having a fixed angle joint, and the legs having a motorized joint.
            var torsoWidth = playerWidth + 0.25f;
            var torsoHeight = playerHeight - playerWidth;
            Torso.Body = BodyFactory.CreateRectangle(PhysicsSubsystem.Instance.World, torsoWidth, playerHeight - playerWidth / 4, 0.1f, spawn);
            Torso.Body.IgnoreGravity = true;
            Torso.Body.BodyType = BodyType.Dynamic;
            Torso.Body.FixedRotation = true;
            Torso.Body.Friction = 0;
            Torso.Body.UserData = this;

            Legs.Body = BodyFactory.CreateCircle(PhysicsSubsystem.Instance.World, playerWidth/2, 4f, new Vector2(spawn.X, spawn.Y + torsoHeight / 2 + playerWidth / 4));
            Legs.Body.BodyType = BodyType.Dynamic;
            Legs.Body.Friction = 1000;

            Piston.Body = BodyFactory.CreateRectangle(PhysicsSubsystem.Instance.World, playerWidth, playerHeight / 2,
                                                      0.5f, spawn);
            Piston.Body.BodyType = BodyType.Dynamic;

            axis = JointFactory.CreateRevoluteJoint(PhysicsSubsystem.Instance.World, Torso.Body, Legs.Body, Vector2.Zero);
            axis.CollideConnected = false;
            axis.MotorEnabled = true;
            axis.MaxMotorTorque = 20;

            //prismatic = JointFactory.CreatePrismaticJoint(PhysicsSubsystem.Instance.World, Piston.Body, Torso.Body,
            //                                              Vector2.Zero, -Vector2.UnitY);
            spring = JointFactory.CreateDistanceJoint(PhysicsSubsystem.Instance.World, Piston.Body, Torso.Body);
            spring.Frequency = 10f;
            spring.DampingRatio = 1f;
            spring.CollideConnected = false;

            prismatic = JointFactory.CreatePrismaticJoint(PhysicsSubsystem.Instance.World, Piston.Body, Torso.Body,
                                                          Vector2.Zero, Vector2.UnitY);
            prismatic.CollideConnected = false;
            Piston.Body.IgnoreCollisionWith(Legs.Body);

            Graphics.Sprite = new Sprite(Content.Player);
            Graphics.Animation = standingAnimation;
            Graphics.FrameSize = new Vector2i(64, 64);
            Graphics.Sprite.Origin = new Vector2f(32, 40);

            Slicing = 45;

            bool jumpInProgress = false, canJump = true, attacking = false, canAttack = true; // 1WEEK
            // Map the input to the legs
            Input.NoInput += () =>
                             {
                                 axis.MotorSpeed = 0;
                                 if (!jumpInProgress && !attacking)
                                    Graphics.Animation = standingAnimation;
                             };

            Input.KeyEvents[Keyboard.Key.A] = (key, mods, time) =>
                                           {
                                               if (attacking) return;

                                               if (!jumpInProgress)
                                               {
                                                   axis.MotorSpeed = -playerSpeed;
                                                   Graphics.Animation = walkingAnimation;
                                               }
                                               else
                                                   Legs.Body.ApplyLinearImpulse(new Vector2(-playerAirSpeed * (float)time, 0));

                                               Graphics.Sprite.Scale = new Vector2f(-1, 1); // just flip it
                                           };

            Input.KeyEvents[Keyboard.Key.D] = (key, mods, time) =>
                                           {
                                               if (attacking) return;

                                               if (!jumpInProgress)
                                               {
                                                   axis.MotorSpeed = playerSpeed;
                                                   Graphics.Animation = walkingAnimation;
                                               }
                                               else
                                                   Legs.Body.ApplyLinearImpulse(new Vector2(playerAirSpeed * (float)time, 0));

                                               Graphics.Sprite.Scale = new Vector2f(1, 1); // flip it good
                                           };

            Input.KeyEvents[Keyboard.Key.W] = (key, mods, time) =>
            {
                if (canJump)// && !jumpInProgress && !attacking)
                {
                    jumpInProgress = true;
                    canJump = false;
                    //Legs.Body.ApplyLinearImpulse(new Vector2(0, -playerJumpForce));
                    //Piston.Body.ApplyLinearImpulse(new Vector2(0, -playerJumpForce));
                    spring.Length = playerJumpForce;
                    axis.MotorSpeed = 0;
                    Legs.Body.Friction = 0;
                    jumpTimer.Start();
                    springResetTimer.Start();
                }
            };

            Input.MouseInput += (btn) =>
                                {
                Vector2 mousePos, position, direction, endPos;

                switch (btn)
                {
                    case Mouse.Button.Left:
                        if (!canAttack) break;

                        canAttack = false;
                        Graphics.Animation = jumpInProgress ? vSwingAerialAnimation : vSwingAnimation;
                        axis.MotorSpeed = 0;
                        attacking = true;
                        attackTimer.Start();

                        mousePos = new Vector2(ConvertUnits.ToSimUnits(Input.MousePosition.X),
                            ConvertUnits.ToSimUnits(Input.MousePosition.Y));
                        position = new Vector2(Torso.Position.X, Torso.Position.Y);
                        direction = mousePos - position;
                        direction.Normalize();
                        endPos = position + (direction * 2.5f);

                        PhysicsSubsystem.Instance.World.RayCast((f, p, n, fr) =>
                        {
                            var candy = f.Body.UserData as CandyObject;
                            if (candy != null && candy.HitPoints > 0)
                            {
                                candy.Hit(1, p, direction);
                                return 0;
                            }
                            if (f.Body.UserData is PlatformObject)
                                return 0;
                            return 1;
                        }, position, endPos);

                        break;
                    case Mouse.Button.Middle:
                        break;
                    case Mouse.Button.Right:
                        if (!canAttack || Slicing <= 0) break;

                        canAttack = false;
                        Graphics.Animation = jumpInProgress ? hSwingAerialAnimation : hSwingAnimation;
                        axis.MotorSpeed = 0;
                        attacking = true;
                        attackTimer.Start();

                        mousePos = new Vector2(ConvertUnits.ToSimUnits(Input.MousePosition.X),
                            ConvertUnits.ToSimUnits(Input.MousePosition.Y));
                        position = new Vector2(Torso.Position.X, Torso.Position.Y);
                        direction = mousePos - position;
                        direction.Normalize();
                        direction *= 2.5f;
                        endPos = position + direction;

                        // Find the first thing hit, and if it's a candy, keep track of it.
                        List<Fixture> fixtures = new List<Fixture>();
                        List<Vector2> entryPoints = new List<Vector2>();
                        List<Vector2> exitPoints = new List<Vector2>();

                        //Get the entry points
                        PhysicsSubsystem.Instance.World.RayCast((f, p, n, fr) =>
                                          {
                                              if (f.Body.UserData is PlatformObject)
                                              {
                                                  return 0;
                                              }
                                              if (f.Body.UserData is CandyObject)
                                              {
                                                  fixtures.Add(f);
                                                  entryPoints.Add(p);
                                              }
                                              return 1;
                                          }, position, endPos);
                        if (!entryPoints.Any())
                            return;
                        //Reverse the ray to get the exitpoints
                        PhysicsSubsystem.Instance.World.RayCast((f, p, n, fr) =>
                                          {
                                              if(f.Body.UserData is CandyObject)
                                                  exitPoints.Add(p);
                                              return 1;
                                          }, endPos, position);

                        while (fixtures.Count > exitPoints.Count)
                        {
                            fixtures.Remove(fixtures.Last());
                            entryPoints.Remove(entryPoints.Last());
                        }

                        GameplayState.Score += fixtures.Count * 500;
                        if (fixtures.Count > 0)
                            Content.SliceSound.Play();

                        for (int i = 0; i < fixtures.Count; i++)
                        {
                            if (fixtures[i].Body.Mass < 2.5) continue;
                            var originalCandy = fixtures[i].Body.UserData as CandyObject;
                            Debug.Assert(originalCandy != null);
                            Slicing = Math.Max(Slicing - originalCandy.Physics.Body.Mass, 0);
                            originalCandy.Slice(entryPoints[i], exitPoints[i]);
                        }

                        break;
                }
            };

            #if DEBUG
            Input.KeyEvents[Keyboard.Key.RShift] = (key, mod) => Framework.Graphics.TakeScreenshot();
            #endif

            Torso.OnFalling += (isFalling) =>
            {
                if (isFalling)
                {
                    if (!attacking)
                        Graphics.Animation = fallingAnimation;
                    jumpInProgress = true;
                }
            };

            Legs.Body.OnCollision += (a, b, c) =>
            {
                var userData = (a.Body.UserData ?? b.Body.UserData) as WallObject;
                if (userData == null)
                {
                    jumpInProgress = false;
                    Legs.Body.Friction = c.Friction = 1000;
                    if (!attacking)
                        Graphics.Animation = standingAnimation;
                }
                return true;
            };

            Piston.Body.OnCollision += (a, b, c) =>
                                       {
                                           Graphics.Animation = jumpingAnimation;
                                           Content.JumpSound.Play();
                                           return true;
                                       };

            var swingFinished = new EventHandler((sender, args) =>
                                      {
                                          attacking = false;
                                          Graphics.Animation = jumpInProgress ? aerialAnimation : standingAnimation;
                                      });

            vSwingAnimation.Finished += swingFinished;
            vSwingAerialAnimation.Finished += swingFinished;
            hSwingAnimation.Finished += swingFinished;
            hSwingAerialAnimation.Finished += swingFinished;

            attackTimer.DingDingDing += (sender, args) => canAttack = true;
            jumpTimer.DingDingDing += (sender, args) => canJump = true;
            springResetTimer.DingDingDing += (sender, args) => spring.Length = 0;
        }
Beispiel #23
0
        private void CreateLeg(World world, float direction, Vector2 wheelAnchor, int index)
        {
            Vector2 p1 = new Vector2(5.4f * direction, 6.1f);
            Vector2 p2 = new Vector2(7.2f * direction, 1.2f);
            Vector2 p3 = new Vector2(4.3f * direction, 1.9f);
            Vector2 p4 = new Vector2(3.1f * direction, -0.8f);
            Vector2 p5 = new Vector2(6.0f * direction, -1.5f);
            Vector2 p6 = new Vector2(2.5f * direction, -3.7f);

            PolygonShape shoulderPolygon;
            PolygonShape legPolygon;

            Vertices vertices = new Vertices(3);

            if (direction < 0f)
            {
                vertices.Add(p1);
                vertices.Add(p2);
                vertices.Add(p3);
                shoulderPolygon = new PolygonShape(vertices, 1);

                vertices[0] = Vector2.Zero;
                vertices[1] = p5 - p4;
                vertices[2] = p6 - p4;
                legPolygon = new PolygonShape(vertices, 2);
            }
            else
            {
                vertices.Add(p1);
                vertices.Add(p3);
                vertices.Add(p2);
                shoulderPolygon = new PolygonShape(vertices, 1);

                vertices[0] = Vector2.Zero;
                vertices[1] = p6 - p4;
                vertices[2] = p5 - p4;
                legPolygon = new PolygonShape(vertices, 2);
            }

            Body leg = BodyFactory.CreateBody(world);
            leg.BodyType = BodyType.Dynamic;
            leg.Position = _position;
            leg.AngularDamping = 10f;

            if (direction < 0f)
                _leftLegs[index] = leg;
            else
                _rightLegs[index] = leg;

            Body shoulder = BodyFactory.CreateBody(world);
            shoulder.BodyType = BodyType.Dynamic;
            shoulder.Position = p4 + _position;
            shoulder.AngularDamping = 10f;

            if (direction < 0f)
                _leftShoulders[index] = shoulder;
            else
                _rightShoulders[index] = shoulder;

            Fixture f1 = leg.CreateFixture(shoulderPolygon);
            f1.CollisionGroup = -1;

            Fixture f2 = shoulder.CreateFixture(legPolygon);
            f2.CollisionGroup = -1;

            // Using a soft distanceraint can reduce some jitter.
            // It also makes the structure seem a bit more fluid by
            // acting like a suspension system.
            DistanceJoint djd = new DistanceJoint(leg, shoulder, leg.GetLocalPoint(p2 + _position), shoulder.GetLocalPoint(p5 + _position));
            djd.DampingRatio = 0.5f;
            djd.Frequency = 10f;

            world.AddJoint(djd);
            _walkerJoints.Add(djd);

            DistanceJoint djd2 = new DistanceJoint(leg, shoulder, leg.GetLocalPoint(p3 + _position), shoulder.GetLocalPoint(p4 + _position));
            djd2.DampingRatio = 0.5f;
            djd2.Frequency = 10f;

            world.AddJoint(djd2);
            _walkerJoints.Add(djd2);

            DistanceJoint djd3 = new DistanceJoint(leg, _wheel, leg.GetLocalPoint(p3 + _position), _wheel.GetLocalPoint(wheelAnchor + _position));
            djd3.DampingRatio = 0.5f;
            djd3.Frequency = 10f;

            world.AddJoint(djd3);
            _walkerJoints.Add(djd3);

            DistanceJoint djd4 = new DistanceJoint(shoulder, _wheel, shoulder.GetLocalPoint(p6 + _position), _wheel.GetLocalPoint(wheelAnchor + _position));
            djd4.DampingRatio = 0.5f;
            djd4.Frequency = 10f;

            world.AddJoint(djd4);
            _walkerJoints.Add(djd4);

            Vector2 anchor = p4 - new Vector2(0f, -0.8f);
            RevoluteJoint rjd = new RevoluteJoint(shoulder, _chassis, shoulder.GetLocalPoint(_chassis.GetWorldPoint(anchor)), anchor);
            world.AddJoint(rjd);
        }
 public Player(Game1 Doc, float X, float Y, float Z)
     : base(Doc,X,Y,Z,Doc.LoadTex("Character"))
 {
     Global.Player = this;
     scale = 0.5f;
     //Point = new Obj(doc
     createRecBody(10, 0, 1f, true,false);
     obj = new Obj(doc, x + 200, y + 200, z, doc.LoadTex("Square"));
     obj.createRecBody(1,0.5f,0.3f, true, true);
     obj2 = new Obj(doc, x + 800, y + 400, z, doc.LoadTex("Square"));
     obj2.createRecBody(1, 0.5f, 0.3f, true, true);
     obj.type = obj2.type = Obj.PointType.SwingPoint;
     //body.JointList = new JointEdge();
     //JointFactory.CreateDistanceJoint(doc.getWorld(), body, obj.body, /*ConvertUnits.ToSimUnits(pos.ToVector2() + new Vector2(0,400))*/body.Position, obj.body.Position);
     //r = JointFactory.CreateRevoluteJoint(doc.World, body, obj.body, obj.body.Position);
     /*f = JointFactory.CreateDistanceJoint(doc.World, body, obj.body, obj.body.Position, obj.body.Position);
     f.Frequency = 100;*/
     //Console.WriteLine(r.MotorEnabled);
     //body.JointList.Other = obj.body;
     //body.JointList.Other
     whip = new Obj[7];
     for (int i = 0; i < whip.Length; i++)
     {
         Obj w = new Obj(doc, x + 50 +  (i * 50), y, z, doc.LoadTex("Square"));
         whip[i] = w;
         w.Scale.X = 0.5f;
         w.Scale.Y = 0.5f;
         w.createRecBody(1, 0, 0, true, false);
         w.body.IgnoreCollisionWith(body);
         if (i > 0)
         {
             for (int o = i - 1; o >= 0; o--)
             {
                 w.body.IgnoreCollisionWith(whip[i].body);
             }
         }
         if (i == 0)
         {
             f = JointFactory.CreateDistanceJoint(doc.World, body, w.body, w.body.Position, w.body.Position);
             f.DampingRatio = 1f;
             f.Length = ConvertUnits.ToSimUnits(150);
         }
         else
         {
             f = JointFactory.CreateDistanceJoint(doc.World, w.body, whip[i - 1].body, whip[i - 1].body.Position, whip[i - 1].body.Position);
             f.DampingRatio = 1f;
             f.Frequency = 20;
             f.Length = ConvertUnits.ToSimUnits(50);
         }
         if (i == whip.Length - 1)
         {
             w.body.Position = obj.body.Position;
             whipPoint = w;
             whipJoint = JointFactory.CreateDistanceJoint(doc.World, w.body, obj.body, obj.body.Position, obj.body.Position);
             Console.WriteLine(whipJoint.Frequency);
             whipJoint.Frequency = 10f;
             whipJoint.Length = ConvertUnits.ToSimUnits(10);
             whipJoint.DampingRatio = 1;
         }
     }
 }
Beispiel #25
0
        public Ragdoll(World world, Vector2 position)
        {
            // Physics
            // Head
            _head = BodyFactory.CreateCircle(world, 0.75f, 10f);
            _head.BodyType = BodyType.Dynamic;
            _head.AngularDamping = LimbAngularDamping;
            _head.Mass = 2f;
            _head.Position = position;

            // Torso
            _upperBody = BodyFactory.CreateCapsule(world, 0.5f, 0.75f, LegDensity);
            _upperBody.BodyType = BodyType.Dynamic;
            _upperBody.Mass = 1f;
            _upperBody.SetTransform(position + new Vector2(0f, 1.75f), MathHelper.Pi / 2f);
            _middleBody = BodyFactory.CreateCapsule(world, 0.5f, 0.75f, LegDensity);
            _middleBody.BodyType = BodyType.Dynamic;
            _middleBody.Mass = 1f;
            _middleBody.SetTransform(position + new Vector2(0f, 3f), MathHelper.Pi / 2f);
            _lowerBody = BodyFactory.CreateCapsule(world, 0.5f, 0.75f, LegDensity);
            _lowerBody.BodyType = BodyType.Dynamic;
            _lowerBody.Mass = 1f;
            _lowerBody.SetTransform(position + new Vector2(0f, 4.25f), MathHelper.Pi / 2f);

            // Left Arm
            _lowerLeftArm = BodyFactory.CreateCapsule(world, 1f, 0.45f, ArmDensity);
            _lowerLeftArm.BodyType = BodyType.Dynamic;
            _lowerLeftArm.AngularDamping = LimbAngularDamping;
            _lowerLeftArm.Mass = 2f;
            _lowerLeftArm.Rotation = 1.4f;
            _lowerLeftArm.Position = position + new Vector2(-4f, 2.2f);

            _upperLeftArm = BodyFactory.CreateCapsule(world, 1f, 0.45f, ArmDensity);
            _upperLeftArm.BodyType = BodyType.Dynamic;
            _upperLeftArm.AngularDamping = LimbAngularDamping;
            _upperLeftArm.Mass = 2f;
            _upperLeftArm.Rotation = 1.4f;
            _upperLeftArm.Position = position + new Vector2(-2f, 1.8f);

            // Right Arm
            _lowerRightArm = BodyFactory.CreateCapsule(world, 1f, 0.45f, ArmDensity);
            _lowerRightArm.BodyType = BodyType.Dynamic;
            _lowerRightArm.AngularDamping = LimbAngularDamping;
            _lowerRightArm.Mass = 2f;
            _lowerRightArm.Rotation = -1.4f;
            _lowerRightArm.Position = position + new Vector2(4f, 2.2f);

            _upperRightArm = BodyFactory.CreateCapsule(world, 1f, 0.45f, ArmDensity);
            _upperRightArm.BodyType = BodyType.Dynamic;
            _upperRightArm.AngularDamping = LimbAngularDamping;
            _upperRightArm.Mass = 2f;
            _upperRightArm.Rotation = -1.4f;
            _upperRightArm.Position = position + new Vector2(2f, 1.8f);

            // Left Leg
            _lowerLeftLeg = BodyFactory.CreateCapsule(world, 1f, 0.5f, LegDensity);
            _lowerLeftLeg.BodyType = BodyType.Dynamic;
            _lowerLeftLeg.AngularDamping = LimbAngularDamping;
            _lowerLeftLeg.Mass = 2f;
            _lowerLeftLeg.Position = position + new Vector2(-0.6f, 8f);

            _upperLeftLeg = BodyFactory.CreateCapsule(world, 1f, 0.5f, LegDensity);
            _upperLeftLeg.BodyType = BodyType.Dynamic;
            _upperLeftLeg.AngularDamping = LimbAngularDamping;
            _upperLeftLeg.Mass = 2f;
            _upperLeftLeg.Position = position + new Vector2(-0.6f, 6f);

            // Right Leg
            _lowerRightLeg = BodyFactory.CreateCapsule(world, 1f, 0.5f, LegDensity);
            _lowerRightLeg.BodyType = BodyType.Dynamic;
            _lowerRightLeg.AngularDamping = LimbAngularDamping;
            _lowerRightLeg.Mass = 2f;
            _lowerRightLeg.Position = position + new Vector2(0.6f, 8f);

            _upperRightLeg = BodyFactory.CreateCapsule(world, 1f, 0.5f, LegDensity);
            _upperRightLeg.BodyType = BodyType.Dynamic;
            _upperRightLeg.AngularDamping = LimbAngularDamping;
            _upperRightLeg.Mass = 2f;
            _upperRightLeg.Position = position + new Vector2(0.6f, 6f);

            // head -> upper body
            DistanceJoint jointHeadBody = new DistanceJoint(_head, _upperBody, new Vector2(0f, 1f), new Vector2(-0.75f, 0f));
            jointHeadBody.CollideConnected = true;
            jointHeadBody.DampingRatio = DampingRatio;
            jointHeadBody.Frequency = Frequency;
            jointHeadBody.Length = 0.025f;
            world.AddJoint(jointHeadBody);

            // lowerLeftArm -> upperLeftArm
            DistanceJoint jointLeftArm = new DistanceJoint(_lowerLeftArm, _upperLeftArm, new Vector2(0f, -1f), new Vector2(0f, 1f));
            jointLeftArm.CollideConnected = true;
            jointLeftArm.DampingRatio = DampingRatio;
            jointLeftArm.Frequency = Frequency;
            jointLeftArm.Length = 0.02f;
            world.AddJoint(jointLeftArm);

            // upperLeftArm -> upper body
            DistanceJoint jointLeftArmBody = new DistanceJoint(_upperLeftArm, _upperBody, new Vector2(0f, -1f), new Vector2(-0.15f, 1f));
            jointLeftArmBody.DampingRatio = DampingRatio;
            jointLeftArmBody.Frequency = Frequency;
            jointLeftArmBody.Length = 0.02f;
            world.AddJoint(jointLeftArmBody);

            // lowerRightArm -> upperRightArm
            DistanceJoint jointRightArm = new DistanceJoint(_lowerRightArm, _upperRightArm, new Vector2(0f, -1f), new Vector2(0f, 1f));
            jointRightArm.CollideConnected = true;
            jointRightArm.DampingRatio = DampingRatio;
            jointRightArm.Frequency = Frequency;
            jointRightArm.Length = 0.02f;
            world.AddJoint(jointRightArm);

            // upperRightArm -> upper body
            DistanceJoint jointRightArmBody = new DistanceJoint(_upperRightArm, _upperBody, new Vector2(0f, -1f), new Vector2(-0.15f, -1f));
            jointRightArmBody.DampingRatio = DampingRatio;
            jointRightArmBody.Frequency = 25;
            jointRightArmBody.Length = 0.02f;
            world.AddJoint(jointRightArmBody);

            // lowerLeftLeg -> upperLeftLeg
            DistanceJoint jointLeftLeg = new DistanceJoint(_lowerLeftLeg, _upperLeftLeg, new Vector2(0f, -1.1f), new Vector2(0f, 1f));
            jointLeftLeg.CollideConnected = true;
            jointLeftLeg.DampingRatio = DampingRatio;
            jointLeftLeg.Frequency = Frequency;
            jointLeftLeg.Length = 0.05f;
            world.AddJoint(jointLeftLeg);

            // upperLeftLeg -> lower body
            DistanceJoint jointLeftLegBody = new DistanceJoint(_upperLeftLeg, _lowerBody, new Vector2(0f, -1.1f), new Vector2(0.7f, 0.8f));
            jointLeftLegBody.CollideConnected = true;
            jointLeftLegBody.DampingRatio = DampingRatio;
            jointLeftLegBody.Frequency = Frequency;
            jointLeftLegBody.Length = 0.02f;
            world.AddJoint(jointLeftLegBody);

            // lowerRightleg -> upperRightleg
            DistanceJoint jointRightLeg = new DistanceJoint(_lowerRightLeg, _upperRightLeg, new Vector2(0f, -1.1f), new Vector2(0f, 1f));
            jointRightLeg.CollideConnected = true;
            jointRightLeg.DampingRatio = DampingRatio;
            jointRightLeg.Frequency = Frequency;
            jointRightLeg.Length = 0.05f;
            world.AddJoint(jointRightLeg);

            // upperRightleg -> lower body
            DistanceJoint jointRightLegBody = new DistanceJoint(_upperRightLeg, _lowerBody, new Vector2(0f, -1.1f), new Vector2(0.7f, -0.8f));
            jointRightLegBody.CollideConnected = true;
            jointRightLegBody.DampingRatio = DampingRatio;
            jointRightLegBody.Frequency = Frequency;
            jointRightLegBody.Length = 0.02f;
            world.AddJoint(jointRightLegBody);

            // upper body -> middle body
            RevoluteJoint jointUpperTorso = new RevoluteJoint(_upperBody, _middleBody, _upperBody.Position + new Vector2(0f, 0.625f), true);
            jointUpperTorso.LimitEnabled = true;
            jointUpperTorso.SetLimits(-MathHelper.Pi / 16f, MathHelper.Pi / 16f);
            world.AddJoint(jointUpperTorso);

            // middle body -> lower body
            RevoluteJoint jointLowerTorso = new RevoluteJoint(_middleBody, _lowerBody, _middleBody.Position + new Vector2(0f, 0.625f), true);
            jointLowerTorso.LimitEnabled = true;
            jointLowerTorso.SetLimits(-MathHelper.Pi / 8f, MathHelper.Pi / 8f);
            world.AddJoint(jointLowerTorso);

            // GFX
            _face = new Sprite(ContentWrapper.CircleTexture(0.75f, "Square", ContentWrapper.Gold, ContentWrapper.Orange, ContentWrapper.Grey, 1f));
            _torso = new Sprite(ContentWrapper.PolygonTexture(PolygonTools.CreateRoundedRectangle(1.5f, 2f, 0.75f, 0.75f, 2), "Stripe", ContentWrapper.Gold, ContentWrapper.Orange, ContentWrapper.Black, 2.0f));
            _upperLimb = new Sprite(ContentWrapper.PolygonTexture(PolygonTools.CreateCapsule(1.9f, 0.45f, 16), "Square", ContentWrapper.Gold, ContentWrapper.Orange, ContentWrapper.Black, 1f));
            _lowerLimb = new Sprite(ContentWrapper.PolygonTexture(PolygonTools.CreateCapsule(2f, 0.5f, 16), "Square", ContentWrapper.Gold, ContentWrapper.Orange, ContentWrapper.Black, 1f));
        }
Beispiel #26
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;
                }
            }
        }
Beispiel #27
0
        private WebTest()
        {
            World.JointRemoved += JointRemovedFired;
            World.BodyRemoved += BodyRemovedFired;

            Body ground = BodyFactory.CreateEdge(World, new Vector2(-40, 0), new Vector2(40, 0));

            {
                _bodies[0] = BodyFactory.CreateRectangle(World, 1f, 1f, 5, new Vector2(-5.0f, 5.0f));
                _bodies[0].BodyType = BodyType.Dynamic;

                _bodies[1] = BodyFactory.CreateRectangle(World, 1f, 1f, 5, new Vector2(5.0f, 5.0f));
                _bodies[1].BodyType = BodyType.Dynamic;

                _bodies[2] = BodyFactory.CreateRectangle(World, 1f, 1f, 5, new Vector2(5.0f, 15.0f));
                _bodies[2].BodyType = BodyType.Dynamic;

                _bodies[3] = BodyFactory.CreateRectangle(World, 1f, 1f, 5, new Vector2(-5.0f, 15.0f));
                _bodies[3].BodyType = BodyType.Dynamic;

                DistanceJoint dj = new DistanceJoint(ground, _bodies[0], new Vector2(-10.0f, 0.0f), new Vector2(-0.5f, -0.5f));
                _joints[0] = dj;
                dj.Frequency = 2.0f;
                dj.DampingRatio = 0.0f;
                World.AddJoint(_joints[0]);

                DistanceJoint dj1 = new DistanceJoint(ground, _bodies[1], new Vector2(10.0f, 0.0f), new Vector2(0.5f, -0.5f));
                _joints[1] = dj1;
                dj1.Frequency = 2.0f;
                dj1.DampingRatio = 0.0f;
                World.AddJoint(_joints[1]);

                DistanceJoint dj2 = new DistanceJoint(ground, _bodies[2], new Vector2(10.0f, 20.0f), new Vector2(0.5f, 0.5f));
                _joints[2] = dj2;
                dj2.Frequency = 2.0f;
                dj2.DampingRatio = 0.0f;
                World.AddJoint(_joints[2]);

                DistanceJoint dj3 = new DistanceJoint(ground, _bodies[3], new Vector2(-10.0f, 20.0f), new Vector2(-0.5f, 0.5f));
                _joints[3] = dj3;
                dj3.Frequency = 2.0f;
                dj3.DampingRatio = 0.0f;
                World.AddJoint(_joints[3]);

                DistanceJoint dj4 = new DistanceJoint(_bodies[0], _bodies[1], new Vector2(0.5f, 0.0f), new Vector2(-0.5f, 0.0f));
                _joints[4] = dj4;
                dj4.Frequency = 2.0f;
                dj4.DampingRatio = 0.0f;
                World.AddJoint(_joints[4]);

                DistanceJoint dj5 = new DistanceJoint(_bodies[1], _bodies[2], new Vector2(0.0f, 0.5f), new Vector2(0.0f, -0.5f));
                _joints[5] = dj5;
                dj5.Frequency = 2.0f;
                dj5.DampingRatio = 0.0f;
                World.AddJoint(_joints[5]);

                DistanceJoint dj6 = new DistanceJoint(_bodies[2], _bodies[3], new Vector2(-0.5f, 0.0f), new Vector2(0.5f, 0.0f));
                _joints[6] = dj6;
                dj6.Frequency = 2.0f;
                dj6.DampingRatio = 0.0f;
                World.AddJoint(_joints[6]);

                DistanceJoint dj7 = new DistanceJoint(_bodies[3], _bodies[0], new Vector2(0.0f, -0.5f), new Vector2(0.0f, 0.5f));
                _joints[7] = dj7;
                dj7.Frequency = 2.0f;
                dj7.DampingRatio = 0.0f;
                World.AddJoint(_joints[7]);
            }
        }
Beispiel #28
0
        private WebTest()
        {
            World.JointRemoved += JointRemovedFired;
            World.BodyRemoved += BodyRemovedFired;

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

                _bodies[0] = BodyFactory.CreateBody(World);
                _bodies[0].BodyType = BodyType.Dynamic;
                _bodies[0].Position = new Vector2(-5.0f, 5.0f);

                _bodies[0].CreateFixture(shape);

                _bodies[1] = BodyFactory.CreateBody(World);
                _bodies[1].BodyType = BodyType.Dynamic;
                _bodies[1].Position = new Vector2(5.0f, 5.0f);

                _bodies[1].CreateFixture(shape);

                _bodies[2] = BodyFactory.CreateBody(World);
                _bodies[2].BodyType = BodyType.Dynamic;
                _bodies[2].Position = new Vector2(5.0f, 15.0f);

                _bodies[2].CreateFixture(shape);

                _bodies[3] = BodyFactory.CreateBody(World);
                _bodies[3].BodyType = BodyType.Dynamic;
                _bodies[3].Position = new Vector2(-5.0f, 15.0f);

                _bodies[3].CreateFixture(shape);

                FixedDistanceJoint dj = new FixedDistanceJoint(_bodies[0], new Vector2(-0.5f, -0.5f),
                                                               new Vector2(-10.0f, 0.0f));
                _joints[0] = dj;
                dj.Frequency = 2.0f;
                dj.DampingRatio = 0.0f;
                World.AddJoint(_joints[0]);

                FixedDistanceJoint dj1 = new FixedDistanceJoint(_bodies[1], new Vector2(0.5f, -0.5f),
                                                                new Vector2(10.0f, 0.0f));
                _joints[1] = dj1;
                dj1.Frequency = 2.0f;
                dj1.DampingRatio = 0.0f;
                World.AddJoint(_joints[1]);

                FixedDistanceJoint dj2 = new FixedDistanceJoint(_bodies[2], new Vector2(0.5f, 0.5f),
                                                                new Vector2(10.0f, 20.0f));
                _joints[2] = dj2;
                dj2.Frequency = 2.0f;
                dj2.DampingRatio = 0.0f;
                World.AddJoint(_joints[2]);

                FixedDistanceJoint dj3 = new FixedDistanceJoint(_bodies[3], new Vector2(-0.5f, 0.5f),
                                                                new Vector2(-10.0f, 20.0f));
                _joints[3] = dj3;
                dj3.Frequency = 2.0f;
                dj3.DampingRatio = 0.0f;
                World.AddJoint(_joints[3]);

                DistanceJoint dj4 = new DistanceJoint(_bodies[0], _bodies[1], Vector2.Zero,
                                                      Vector2.Zero);
                _joints[4] = dj4;
                dj4.Frequency = 2.0f;
                dj4.DampingRatio = 0.0f;
                World.AddJoint(_joints[4]);

                DistanceJoint dj5 = new DistanceJoint(_bodies[1], _bodies[2], Vector2.Zero,
                                                      Vector2.Zero);
                _joints[5] = dj5;
                dj5.Frequency = 2.0f;
                dj5.DampingRatio = 0.0f;
                World.AddJoint(_joints[5]);

                DistanceJoint dj6 = new DistanceJoint(_bodies[2], _bodies[3], Vector2.Zero,
                                                      Vector2.Zero);
                _joints[6] = dj6;
                dj6.Frequency = 2.0f;
                dj6.DampingRatio = 0.0f;
                World.AddJoint(_joints[6]);

                DistanceJoint dj7 = new DistanceJoint(_bodies[3], _bodies[0], Vector2.Zero,
                                                      Vector2.Zero);
                _joints[7] = dj7;
                dj7.Frequency = 2.0f;
                dj7.DampingRatio = 0.0f;
                World.AddJoint(_joints[7]);
            }
        }
Beispiel #29
0
        public void Deserialize(World world, Stream stream)
        {
            world.Clear();

            XMLFragmentElement root = XMLFragmentParser.LoadFromStream(stream);

            if (root.Name.ToLower() != "world")
                throw new Exception();

            foreach (XMLFragmentElement main in root.Elements)
            {
                if (main.Name.ToLower() == "gravity")
                {
                    world.Gravity = ReadVector(main);
                }
            }

            foreach (XMLFragmentElement shapeElement in root.Elements)
            {
                if (shapeElement.Name.ToLower() == "shapes")
                {
                    foreach (XMLFragmentElement n in shapeElement.Elements)
                    {
                        if (n.Name.ToLower() != "shape")
                            throw new Exception();

                        ShapeType type = (ShapeType)Enum.Parse(typeof(ShapeType), n.Attributes[0].Value, true);

                        switch (type)
                        {
                            case ShapeType.Circle:
                                {
                                    CircleShape shape = new CircleShape();

                                    foreach (XMLFragmentElement sn in n.Elements)
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                            case "radius":
                                                shape.Radius = float.Parse(sn.Value);
                                                break;
                                            case "position":
                                                shape.Position = ReadVector(sn);
                                                break;
                                            default:
                                                throw new Exception();
                                        }
                                    }

                                    _shapes.Add(shape);
                                }
                                break;
                            case ShapeType.Polygon:
                                {
                                    PolygonShape shape = new PolygonShape();

                                    foreach (XMLFragmentElement sn in n.Elements)
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                            case "vertices":
                                                {
                                                    List<Vector2> verts = new List<Vector2>();

                                                    foreach (XMLFragmentElement vert in sn.Elements)
                                                        verts.Add(ReadVector(vert));

                                                    shape.Set(new Vertices(verts.ToArray()));
                                                }
                                                break;
                                            case "centroid":
                                                shape.MassData.Centroid = ReadVector(sn);
                                                break;
                                        }
                                    }

                                    _shapes.Add(shape);
                                }
                                break;
                            case ShapeType.Edge:
                                {
                                    EdgeShape shape = new EdgeShape();
                                    foreach (XMLFragmentElement sn in n.Elements)
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                            case "hasvertex0":
                                                shape.HasVertex0 = bool.Parse(sn.Value);
                                                break;
                                            case "hasvertex3":
                                                shape.HasVertex0 = bool.Parse(sn.Value);
                                                break;
                                            case "vertex0":
                                                shape.Vertex0 = ReadVector(sn);
                                                break;
                                            case "vertex1":
                                                shape.Vertex1 = ReadVector(sn);
                                                break;
                                            case "vertex2":
                                                shape.Vertex2 = ReadVector(sn);
                                                break;
                                            case "vertex3":
                                                shape.Vertex3 = ReadVector(sn);
                                                break;
                                            default:
                                                throw new Exception();
                                        }
                                    }
                                    _shapes.Add(shape);
                                }
                                break;
                        }
                    }
                }
            }

            foreach (XMLFragmentElement fixtureElement in root.Elements)
            {
                if (fixtureElement.Name.ToLower() == "fixtures")
                {
                    foreach (XMLFragmentElement n in fixtureElement.Elements)
                    {
                        Fixture fixture = new Fixture();

                        if (n.Name.ToLower() != "fixture")
                            throw new Exception();

                        foreach (XMLFragmentElement sn in n.Elements)
                        {
                            switch (sn.Name.ToLower())
                            {
                                case "shape":
                                    fixture.Shape = _shapes[int.Parse(sn.Value)];
                                    break;
                                case "density":
                                    fixture.Shape.Density = float.Parse(sn.Value);
                                    break;
                                case "filterdata":
                                    foreach (XMLFragmentElement ssn in sn.Elements)
                                    {
                                        switch (ssn.Name.ToLower())
                                        {
                                            case "categorybits":
                                                fixture._collisionCategories = (Category)int.Parse(ssn.Value);
                                                break;
                                            case "maskbits":
                                                fixture._collidesWith = (Category)int.Parse(ssn.Value);
                                                break;
                                            case "groupindex":
                                                fixture._collisionGroup = short.Parse(ssn.Value);
                                                break;
                                        }
                                    }

                                    break;
                                case "friction":
                                    fixture.Friction = float.Parse(sn.Value);
                                    break;
                                case "issensor":
                                    fixture.IsSensor = bool.Parse(sn.Value);
                                    break;
                                case "restitution":
                                    fixture.Restitution = float.Parse(sn.Value);
                                    break;
                                case "userdata":
                                    fixture.UserData = ReadSimpleType(sn, null, false);
                                    break;
                            }
                        }

                        _fixtures.Add(fixture);
                    }
                }
            }

            foreach (XMLFragmentElement bodyElement in root.Elements)
            {
                if (bodyElement.Name.ToLower() == "bodies")
                {
                    foreach (XMLFragmentElement n in bodyElement.Elements)
                    {
                        Body body = new Body(world);

                        if (n.Name.ToLower() != "body")
                            throw new Exception();

                        body.BodyType = (BodyType)Enum.Parse(typeof(BodyType), n.Attributes[0].Value, true);

                        foreach (XMLFragmentElement sn in n.Elements)
                        {
                            switch (sn.Name.ToLower())
                            {
                                case "active":
                                    if (bool.Parse(sn.Value))
                                        body.Flags |= BodyFlags.Enabled;
                                    else
                                        body.Flags &= ~BodyFlags.Enabled;
                                    break;
                                case "allowsleep":
                                    body.SleepingAllowed = bool.Parse(sn.Value);
                                    break;
                                case "angle":
                                    {
                                        Vector2 position = body.Position;
                                        body.SetTransformIgnoreContacts(ref position, float.Parse(sn.Value));
                                    }
                                    break;
                                case "angulardamping":
                                    body.AngularDamping = float.Parse(sn.Value);
                                    break;
                                case "angularvelocity":
                                    body.AngularVelocity = float.Parse(sn.Value);
                                    break;
                                case "awake":
                                    body.Awake = bool.Parse(sn.Value);
                                    break;
                                case "bullet":
                                    body.IsBullet = bool.Parse(sn.Value);
                                    break;
                                case "fixedrotation":
                                    body.FixedRotation = bool.Parse(sn.Value);
                                    break;
                                case "lineardamping":
                                    body.LinearDamping = float.Parse(sn.Value);
                                    break;
                                case "linearvelocity":
                                    body.LinearVelocity = ReadVector(sn);
                                    break;
                                case "position":
                                    {
                                        float rotation = body.Rotation;
                                        Vector2 position = ReadVector(sn);
                                        body.SetTransformIgnoreContacts(ref position, rotation);
                                    }
                                    break;
                                case "userdata":
                                    body.UserData = ReadSimpleType(sn, null, false);
                                    break;
                                case "fixtures":
                                    {
                                        foreach (XMLFragmentElement v in sn.Elements)
                                        {
                                            Fixture blueprint = _fixtures[int.Parse(v.Value)];
                                            Fixture f = new Fixture(body, blueprint.Shape, blueprint.CollisionCategories);
                                            f.Restitution = blueprint.Restitution;
                                            f.UserData = blueprint.UserData;
                                            f.Friction = blueprint.Friction;
                                            f.CollidesWith = blueprint.CollidesWith;
                                            f.CollisionGroup = blueprint.CollisionGroup;
                                        }
                                        break;
                                    }
                            }
                        }

                        _bodies.Add(body);
                    }
                }
            }

            foreach (XMLFragmentElement jointElement in root.Elements)
            {
                if (jointElement.Name.ToLower() == "joints")
                {
                    foreach (XMLFragmentElement n in jointElement.Elements)
                    {
                        Joint joint;

                        if (n.Name.ToLower() != "joint")
                            throw new Exception();

                        JointType type = (JointType)Enum.Parse(typeof(JointType), n.Attributes[0].Value, true);

                        int bodyAIndex = -1, bodyBIndex = -1;
                        bool collideConnected = false;
                        object userData = null;

                        foreach (XMLFragmentElement sn in n.Elements)
                        {
                            switch (sn.Name.ToLower())
                            {
                                case "bodya":
                                    bodyAIndex = int.Parse(sn.Value);
                                    break;
                                case "bodyb":
                                    bodyBIndex = int.Parse(sn.Value);
                                    break;
                                case "collideconnected":
                                    collideConnected = bool.Parse(sn.Value);
                                    break;
                                case "userdata":
                                    userData = ReadSimpleType(sn, null, false);
                                    break;
                            }
                        }

                        Body bodyA = _bodies[bodyAIndex];
                        Body bodyB = _bodies[bodyBIndex];

                        switch (type)
                        {
                            case JointType.Distance:
                                joint = new DistanceJoint();
                                break;
                            case JointType.Friction:
                                joint = new FrictionJoint();
                                break;
                            case JointType.Line:
                                joint = new LineJoint();
                                break;
                            case JointType.Prismatic:
                                joint = new PrismaticJoint();
                                break;
                            case JointType.Pulley:
                                joint = new PulleyJoint();
                                break;
                            case JointType.Revolute:
                                joint = new RevoluteJoint();
                                break;
                            case JointType.Weld:
                                joint = new WeldJoint();
                                break;
                            case JointType.Rope:
                                joint = new RopeJoint();
                                break;
                            case JointType.Angle:
                                joint = new AngleJoint();
                                break;
                            case JointType.Slider:
                                joint = new SliderJoint();
                                break;
                            case JointType.Gear:
                                throw new Exception("GearJoint is not supported.");
                            default:
                                throw new Exception("Invalid or unsupported joint.");
                        }

                        joint.CollideConnected = collideConnected;
                        joint.UserData = userData;
                        joint.BodyA = bodyA;
                        joint.BodyB = bodyB;
                        _joints.Add(joint);
                        world.AddJoint(joint);

                        foreach (XMLFragmentElement sn in n.Elements)
                        {
                            // check for specific nodes
                            switch (type)
                            {
                                case JointType.Distance:
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                            case "dampingratio":
                                                ((DistanceJoint)joint).DampingRatio = float.Parse(sn.Value);
                                                break;
                                            case "frequencyhz":
                                                ((DistanceJoint)joint).Frequency = float.Parse(sn.Value);
                                                break;
                                            case "length":
                                                ((DistanceJoint)joint).Length = float.Parse(sn.Value);
                                                break;
                                            case "localanchora":
                                                ((DistanceJoint)joint).LocalAnchorA = ReadVector(sn);
                                                break;
                                            case "localanchorb":
                                                ((DistanceJoint)joint).LocalAnchorB = ReadVector(sn);
                                                break;
                                        }
                                    }
                                    break;
                                case JointType.Friction:
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                            case "localanchora":
                                                ((FrictionJoint)joint).LocalAnchorA = ReadVector(sn);
                                                break;
                                            case "localanchorb":
                                                ((FrictionJoint)joint).LocalAnchorB = ReadVector(sn);
                                                break;
                                            case "maxforce":
                                                ((FrictionJoint)joint).MaxForce = float.Parse(sn.Value);
                                                break;
                                            case "maxtorque":
                                                ((FrictionJoint)joint).MaxTorque = float.Parse(sn.Value);
                                                break;
                                        }
                                    }
                                    break;
                                case JointType.Line:
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                            case "enablemotor":
                                                ((LineJoint)joint).MotorEnabled = bool.Parse(sn.Value);
                                                break;
                                            case "localanchora":
                                                ((LineJoint)joint).LocalAnchorA = ReadVector(sn);
                                                break;
                                            case "localanchorb":
                                                ((LineJoint)joint).LocalAnchorB = ReadVector(sn);
                                                break;
                                            case "motorspeed":
                                                ((LineJoint)joint).MotorSpeed = float.Parse(sn.Value);
                                                break;
                                            case "dampingratio":
                                                ((LineJoint)joint).DampingRatio = float.Parse(sn.Value);
                                                break;
                                            case "maxmotortorque":
                                                ((LineJoint)joint).MaxMotorTorque = float.Parse(sn.Value);
                                                break;
                                            case "frequencyhz":
                                                ((LineJoint)joint).Frequency = float.Parse(sn.Value);
                                                break;
                                            case "localxaxis":
                                                ((LineJoint)joint).LocalXAxis = ReadVector(sn);
                                                break;
                                        }
                                    }
                                    break;
                                case JointType.Prismatic:
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                            case "enablelimit":
                                                ((PrismaticJoint)joint).LimitEnabled = bool.Parse(sn.Value);
                                                break;
                                            case "enablemotor":
                                                ((PrismaticJoint)joint).MotorEnabled = bool.Parse(sn.Value);
                                                break;
                                            case "localanchora":
                                                ((PrismaticJoint)joint).LocalAnchorA = ReadVector(sn);
                                                break;
                                            case "localanchorb":
                                                ((PrismaticJoint)joint).LocalAnchorB = ReadVector(sn);
                                                break;
                                            case "local1axis1":
                                                ((PrismaticJoint)joint).LocalXAxis1 = ReadVector(sn);
                                                break;
                                            case "maxmotorforce":
                                                ((PrismaticJoint)joint).MaxMotorForce = float.Parse(sn.Value);
                                                break;
                                            case "motorspeed":
                                                ((PrismaticJoint)joint).MotorSpeed = float.Parse(sn.Value);
                                                break;
                                            case "lowertranslation":
                                                ((PrismaticJoint)joint).LowerLimit = float.Parse(sn.Value);
                                                break;
                                            case "uppertranslation":
                                                ((PrismaticJoint)joint).UpperLimit = float.Parse(sn.Value);
                                                break;
                                            case "referenceangle":
                                                ((PrismaticJoint)joint).ReferenceAngle = float.Parse(sn.Value);
                                                break;
                                        }
                                    }
                                    break;
                                case JointType.Pulley:
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                            case "groundanchora":
                                                ((PulleyJoint)joint).GroundAnchorA = ReadVector(sn);
                                                break;
                                            case "groundanchorb":
                                                ((PulleyJoint)joint).GroundAnchorB = ReadVector(sn);
                                                break;
                                            case "lengtha":
                                                ((PulleyJoint)joint).LengthA = float.Parse(sn.Value);
                                                break;
                                            case "lengthb":
                                                ((PulleyJoint)joint).LengthB = float.Parse(sn.Value);
                                                break;
                                            case "localanchora":
                                                ((PulleyJoint)joint).LocalAnchorA = ReadVector(sn);
                                                break;
                                            case "localanchorb":
                                                ((PulleyJoint)joint).LocalAnchorB = ReadVector(sn);
                                                break;
                                            case "maxlengtha":
                                                ((PulleyJoint)joint).MaxLengthA = float.Parse(sn.Value);
                                                break;
                                            case "maxlengthb":
                                                ((PulleyJoint)joint).MaxLengthB = float.Parse(sn.Value);
                                                break;
                                            case "ratio":
                                                ((PulleyJoint)joint).Ratio = float.Parse(sn.Value);
                                                break;
                                        }
                                    }
                                    break;
                                case JointType.Revolute:
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                            case "enablelimit":
                                                ((RevoluteJoint)joint).LimitEnabled = bool.Parse(sn.Value);
                                                break;
                                            case "enablemotor":
                                                ((RevoluteJoint)joint).MotorEnabled = bool.Parse(sn.Value);
                                                break;
                                            case "localanchora":
                                                ((RevoluteJoint)joint).LocalAnchorA = ReadVector(sn);
                                                break;
                                            case "localanchorb":
                                                ((RevoluteJoint)joint).LocalAnchorB = ReadVector(sn);
                                                break;
                                            case "maxmotortorque":
                                                ((RevoluteJoint)joint).MaxMotorTorque = float.Parse(sn.Value);
                                                break;
                                            case "motorspeed":
                                                ((RevoluteJoint)joint).MotorSpeed = float.Parse(sn.Value);
                                                break;
                                            case "lowerangle":
                                                ((RevoluteJoint)joint).LowerLimit = float.Parse(sn.Value);
                                                break;
                                            case "upperangle":
                                                ((RevoluteJoint)joint).UpperLimit = float.Parse(sn.Value);
                                                break;
                                            case "referenceangle":
                                                ((RevoluteJoint)joint).ReferenceAngle = float.Parse(sn.Value);
                                                break;
                                        }
                                    }
                                    break;
                                case JointType.Weld:
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                            case "localanchora":
                                                ((WeldJoint)joint).LocalAnchorA = ReadVector(sn);
                                                break;
                                            case "localanchorb":
                                                ((WeldJoint)joint).LocalAnchorB = ReadVector(sn);
                                                break;
                                        }
                                    }
                                    break;
                                case JointType.Rope:
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                            case "localanchora":
                                                ((RopeJoint)joint).LocalAnchorA = ReadVector(sn);
                                                break;
                                            case "localanchorb":
                                                ((RopeJoint)joint).LocalAnchorB = ReadVector(sn);
                                                break;
                                            case "maxlength":
                                                ((RopeJoint)joint).MaxLength = float.Parse(sn.Value);
                                                break;
                                        }
                                    }
                                    break;
                                case JointType.Gear:
                                    throw new Exception("Gear joint is unsupported");
                                case JointType.Angle:
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                            case "biasfactor":
                                                ((AngleJoint)joint).BiasFactor = float.Parse(sn.Value);
                                                break;
                                            case "maximpulse":
                                                ((AngleJoint)joint).MaxImpulse = float.Parse(sn.Value);
                                                break;
                                            case "softness":
                                                ((AngleJoint)joint).Softness = float.Parse(sn.Value);
                                                break;
                                            case "targetangle":
                                                ((AngleJoint)joint).TargetAngle = float.Parse(sn.Value);
                                                break;
                                        }
                                    }
                                    break;
                                case JointType.Slider:
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                            case "dampingratio":
                                                ((SliderJoint)joint).DampingRatio = float.Parse(sn.Value);
                                                break;
                                            case "frequencyhz":
                                                ((SliderJoint)joint).Frequency = float.Parse(sn.Value);
                                                break;
                                            case "maxlength":
                                                ((SliderJoint)joint).MaxLength = float.Parse(sn.Value);
                                                break;
                                            case "minlength":
                                                ((SliderJoint)joint).MinLength = float.Parse(sn.Value);
                                                break;
                                            case "localanchora":
                                                ((SliderJoint)joint).LocalAnchorA = ReadVector(sn);
                                                break;
                                            case "localanchorb":
                                                ((SliderJoint)joint).LocalAnchorB = ReadVector(sn);
                                                break;
                                        }
                                    }
                                    break;
                            }
                        }
                    }
                }
            }
        }
        public static Joint CopyJoint(Joint joint, Body bodyA, Body bodyB, World world)
        {
            Joint newJoint = null;

            switch (joint.JointType)
            {
                case JointType.Angle:
                    newJoint = JointFactory.CreateAngleJoint(world, bodyA, bodyB);
                    break;
                case JointType.Distance:
                    newJoint = new DistanceJoint(bodyA, bodyB, bodyA.WorldCenter, bodyB.WorldCenter);
                    break;
                case JointType.FixedAngle:
                    newJoint = new FixedAngleJoint(bodyA);
                    break;
                case JointType.FixedDistance:
                    newJoint = new FixedDistanceJoint(bodyA, bodyA.WorldCenter, Vector2.Zero);
                    break;
                case JointType.FixedFriction:
                    newJoint = new FixedFrictionJoint(bodyA, bodyA.WorldCenter);
                    break;
                case JointType.FixedPrismatic:
                    var fpJoint = joint as FixedPrismaticJoint;
                    var fpAxis = fpJoint.LocalXAxis1;
                    newJoint = new FixedPrismaticJoint(bodyA, bodyA.WorldCenter, fpAxis);
                    break;
                case JointType.FixedRevolute:
                    newJoint = new FixedRevoluteJoint(bodyA, bodyA.WorldCenter, Vector2.Zero);
                    break;
                case JointType.Friction:
                    newJoint = new FrictionJoint(bodyA, bodyB, bodyA.WorldCenter, bodyB.WorldCenter);
                    break;
                case JointType.Line:
                    var lineJoint = joint as LineJoint;
                    var axis = lineJoint.LocalXAxis;
                    newJoint = new LineJoint(bodyA, bodyB, bodyA.WorldCenter, axis);
                    break;
                case JointType.Prismatic:
                    var pJoint = joint as PrismaticJoint;
                    var pAxis = pJoint.LocalXAxis1;
                    newJoint = new PrismaticJoint(bodyA, bodyB, bodyA.WorldCenter, bodyB.WorldCenter, pAxis);
                    ((PrismaticJoint)newJoint).LimitEnabled = pJoint.LimitEnabled;
                    ((PrismaticJoint)newJoint).MotorEnabled = pJoint.MotorEnabled;
                    ((PrismaticJoint)newJoint).MaxMotorForce = pJoint.MaxMotorForce;
                    ((PrismaticJoint)newJoint).MotorSpeed = pJoint.MotorSpeed;
                    ((PrismaticJoint)newJoint).LowerLimit = pJoint.LowerLimit;
                    ((PrismaticJoint)newJoint).UpperLimit = pJoint.UpperLimit;
                    ((PrismaticJoint)newJoint).ReferenceAngle = pJoint.ReferenceAngle;
                    ((PrismaticJoint)newJoint).LocalXAxis1 = pJoint.LocalXAxis1;
                    break;
                case JointType.Pulley:
                    var pulleyJoint = joint as PulleyJoint;
                    var ratio = pulleyJoint.Ratio;
                    newJoint = new PulleyJoint(bodyA, bodyB, Vector2.Zero, Vector2.Zero, bodyA.WorldCenter, bodyB.WorldCenter, ratio);
                    break;
                case JointType.Revolute:
                    newJoint = new RevoluteJoint(bodyA, bodyB, bodyA.WorldCenter, bodyB.WorldCenter);
                    break;
                case JointType.Slider:
                    var sliderJoint = joint as SliderJoint;
                    var minLength = sliderJoint.MinLength;
                    var maxLength = sliderJoint.MaxLength;
                    newJoint = new SliderJoint(bodyA, bodyB, bodyA.WorldCenter, bodyB.WorldCenter, minLength, maxLength);
                    break;
                case JointType.Weld:
                    newJoint = new WeldJoint(bodyA, bodyB, bodyA.WorldCenter, bodyB.WorldCenter);
                    break;
            }

            var data = new FarseerJointUserData();
            data.BodyAName = ((FarseerJointUserData) joint.UserData).BodyAName;
            data.BodyBName = ((FarseerJointUserData) joint.UserData).BodyBName;

            joint.UserData = data;

            return newJoint;
        }