public static Body CreateCapsule(World world, float height, float topRadius, int topEdges, float bottomRadius, int bottomEdges, float density, Vector2 position = new Vector2(), float rotation = 0, BodyType bodyType = BodyType.Static, object userData = null) { Vertices verts = PolygonUtils.CreateCapsule(height, topRadius, topEdges, bottomRadius, bottomEdges); //There are too many vertices in the capsule. We decompose it. if (verts.Count >= Settings.MaxPolygonVertices) { List <Vertices> vertList = Triangulate.ConvexPartition(verts, TriangulationAlgorithm.Earclip); return(CreateCompoundPolygon(world, vertList, density, position, rotation, bodyType, userData)); } return(CreatePolygon(world, verts, density, position, rotation, bodyType, userData)); }
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 Body = BodyFactory.CreateCapsule(world, 0.5f, 0.75f, LegDensity); Body.BodyType = BodyType.Dynamic; Body.Mass = 1f; Body.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, Body, 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, Body, 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, Body, 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(Body, _middleBody, Body.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(PolygonUtils.CreateRoundedRectangle(1.5f, 2f, 0.75f, 0.75f, 2), "Stripe", ContentWrapper.Gold, ContentWrapper.Orange, ContentWrapper.Black, 2.0f)); _upperLimb = new Sprite(ContentWrapper.PolygonTexture(PolygonUtils.CreateCapsule(1.9f, 0.45f, 16), "Square", ContentWrapper.Gold, ContentWrapper.Orange, ContentWrapper.Black, 1f)); _lowerLimb = new Sprite(ContentWrapper.PolygonTexture(PolygonUtils.CreateCapsule(2f, 0.5f, 16), "Square", ContentWrapper.Gold, ContentWrapper.Orange, ContentWrapper.Black, 1f)); }