Adding initial files

This commit is contained in:
nathan@daedalus
2012-03-19 18:57:59 -05:00
commit 5bdc5db408
162 changed files with 43840 additions and 0 deletions

View File

@@ -0,0 +1,399 @@
using System;
using System.Collections.Generic;
using FarseerPhysics.Collision.Shapes;
using FarseerPhysics.Common;
using FarseerPhysics.Common.Decomposition;
using FarseerPhysics.Dynamics;
using Microsoft.Xna.Framework;
namespace FarseerPhysics.Factories
{
public static class BodyFactory
{
public static Body CreateBody(World world)
{
return CreateBody(world, null);
}
public static Body CreateBody(World world, object userData)
{
Body body = new Body(world, userData);
return body;
}
public static Body CreateBody(World world, Vector2 position)
{
return CreateBody(world, position, null);
}
public static Body CreateBody(World world, Vector2 position, object userData)
{
Body body = CreateBody(world, userData);
body.Position = position;
return body;
}
public static Body CreateEdge(World world, Vector2 start, Vector2 end)
{
return CreateEdge(world, start, end, null);
}
public static Body CreateEdge(World world, Vector2 start, Vector2 end, object userData)
{
Body body = CreateBody(world);
FixtureFactory.AttachEdge(start, end, body, userData);
return body;
}
public static Body CreateLoopShape(World world, Vertices vertices)
{
return CreateLoopShape(world, vertices, null);
}
public static Body CreateLoopShape(World world, Vertices vertices, object userData)
{
return CreateLoopShape(world, vertices, Vector2.Zero, userData);
}
public static Body CreateLoopShape(World world, Vertices vertices, Vector2 position)
{
return CreateLoopShape(world, vertices, position, null);
}
public static Body CreateLoopShape(World world, Vertices vertices, Vector2 position,
object userData)
{
Body body = CreateBody(world, position);
FixtureFactory.AttachLoopShape(vertices, body, userData);
return body;
}
public static Body CreateRectangle(World world, float width, float height, float density)
{
return CreateRectangle(world, width, height, density, null);
}
public static Body CreateRectangle(World world, float width, float height, float density, object userData)
{
return CreateRectangle(world, width, height, density, Vector2.Zero, userData);
}
public static Body CreateRectangle(World world, float width, float height, float density, Vector2 position)
{
return CreateRectangle(world, width, height, density, position, null);
}
public static Body CreateRectangle(World world, float width, float height, float density, Vector2 position,
object userData)
{
if (width <= 0)
throw new ArgumentOutOfRangeException("width", "Width must be more than 0 meters");
if (height <= 0)
throw new ArgumentOutOfRangeException("height", "Height must be more than 0 meters");
Body newBody = CreateBody(world, position, userData);
Vertices rectangleVertices = PolygonTools.CreateRectangle(width / 2, height / 2);
PolygonShape rectangleShape = new PolygonShape(rectangleVertices, density);
newBody.CreateFixture(rectangleShape, userData);
return newBody;
}
public static Body CreateCircle(World world, float radius, float density)
{
return CreateCircle(world, radius, density, null);
}
public static Body CreateCircle(World world, float radius, float density, object userData)
{
return CreateCircle(world, radius, density, Vector2.Zero, userData);
}
public static Body CreateCircle(World world, float radius, float density, Vector2 position)
{
return CreateCircle(world, radius, density, position, null);
}
public static Body CreateCircle(World world, float radius, float density, Vector2 position, object userData)
{
Body body = CreateBody(world, position);
FixtureFactory.AttachCircle(radius, density, body, userData);
return body;
}
public static Body CreateEllipse(World world, float xRadius, float yRadius, int edges, float density)
{
return CreateEllipse(world, xRadius, yRadius, edges, density, null);
}
public static Body CreateEllipse(World world, float xRadius, float yRadius, int edges, float density,
object userData)
{
return CreateEllipse(world, xRadius, yRadius, edges, density, Vector2.Zero, userData);
}
public static Body CreateEllipse(World world, float xRadius, float yRadius, int edges, float density,
Vector2 position)
{
return CreateEllipse(world, xRadius, yRadius, edges, density, position, null);
}
public static Body CreateEllipse(World world, float xRadius, float yRadius, int edges, float density,
Vector2 position, object userData)
{
Body body = CreateBody(world, position);
FixtureFactory.AttachEllipse(xRadius, yRadius, edges, density, body, userData);
return body;
}
public static Body CreatePolygon(World world, Vertices vertices, float density)
{
return CreatePolygon(world, vertices, density, null);
}
public static Body CreatePolygon(World world, Vertices vertices, float density, object userData)
{
return CreatePolygon(world, vertices, density, Vector2.Zero, userData);
}
public static Body CreatePolygon(World world, Vertices vertices, float density, Vector2 position)
{
return CreatePolygon(world, vertices, density, position, null);
}
public static Body CreatePolygon(World world, Vertices vertices, float density, Vector2 position,
object userData)
{
Body body = CreateBody(world, position);
FixtureFactory.AttachPolygon(vertices, density, body, userData);
return body;
}
public static Body CreateCompoundPolygon(World world, List<Vertices> list, float density)
{
return CreateCompoundPolygon(world, list, density, BodyType.Static);
}
public static Body CreateCompoundPolygon(World world, List<Vertices> list, float density,
object userData)
{
return CreateCompoundPolygon(world, list, density, Vector2.Zero, userData);
}
public static Body CreateCompoundPolygon(World world, List<Vertices> list, float density,
Vector2 position)
{
return CreateCompoundPolygon(world, list, density, position, null);
}
public static Body CreateCompoundPolygon(World world, List<Vertices> list, float density,
Vector2 position, object userData)
{
//We create a single body
Body polygonBody = CreateBody(world, position);
FixtureFactory.AttachCompoundPolygon(list, density, polygonBody, userData);
return polygonBody;
}
public static Body CreateGear(World world, float radius, int numberOfTeeth, float tipPercentage,
float toothHeight, float density)
{
return CreateGear(world, radius, numberOfTeeth, tipPercentage, toothHeight, density, null);
}
public static Body CreateGear(World world, float radius, int numberOfTeeth, float tipPercentage,
float toothHeight, float density, object userData)
{
Vertices gearPolygon = PolygonTools.CreateGear(radius, numberOfTeeth, tipPercentage, toothHeight);
//Gears can in some cases be convex
if (!gearPolygon.IsConvex())
{
//Decompose the gear:
List<Vertices> list = EarclipDecomposer.ConvexPartition(gearPolygon);
return CreateCompoundPolygon(world, list, density, userData);
}
return CreatePolygon(world, gearPolygon, density, userData);
}
/// <summary>
/// Creates a capsule.
/// Note: Automatically decomposes the capsule if it contains too many vertices (controlled by Settings.MaxPolygonVertices)
/// </summary>
/// <param name="world">The world.</param>
/// <param name="height">The height.</param>
/// <param name="topRadius">The top radius.</param>
/// <param name="topEdges">The top edges.</param>
/// <param name="bottomRadius">The bottom radius.</param>
/// <param name="bottomEdges">The bottom edges.</param>
/// <param name="density">The density.</param>
/// <param name="position">The position.</param>
/// <returns></returns>
public static Body CreateCapsule(World world, float height, float topRadius, int topEdges,
float bottomRadius,
int bottomEdges, float density, Vector2 position, object userData)
{
Vertices verts = PolygonTools.CreateCapsule(height, topRadius, topEdges, bottomRadius, bottomEdges);
Body body;
//There are too many vertices in the capsule. We decompose it.
if (verts.Count >= Settings.MaxPolygonVertices)
{
List<Vertices> vertList = EarclipDecomposer.ConvexPartition(verts);
body = CreateCompoundPolygon(world, vertList, density, userData);
body.Position = position;
return body;
}
body = CreatePolygon(world, verts, density, userData);
body.Position = position;
return body;
}
public static Body CreateCapsule(World world, float height, float topRadius, int topEdges,
float bottomRadius,
int bottomEdges, float density, Vector2 position)
{
return CreateCapsule(world, height, topRadius, topEdges, bottomRadius, bottomEdges, density, position, null);
}
public static Body CreateCapsule(World world, float height, float endRadius, float density)
{
return CreateCapsule(world, height, endRadius, density, null);
}
public static Body CreateCapsule(World world, float height, float endRadius, float density,
object userData)
{
//Create the middle rectangle
Vertices rectangle = PolygonTools.CreateRectangle(endRadius, height / 2);
List<Vertices> list = new List<Vertices>();
list.Add(rectangle);
Body body = CreateCompoundPolygon(world, list, density, userData);
//Create the two circles
CircleShape topCircle = new CircleShape(endRadius, density);
topCircle.Position = new Vector2(0, height / 2);
body.CreateFixture(topCircle, userData);
CircleShape bottomCircle = new CircleShape(endRadius, density);
bottomCircle.Position = new Vector2(0, -(height / 2));
body.CreateFixture(bottomCircle, userData);
return body;
}
/// <summary>
/// Creates a rounded rectangle.
/// Note: Automatically decomposes the capsule if it contains too many vertices (controlled by Settings.MaxPolygonVertices)
/// </summary>
/// <param name="world">The world.</param>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <param name="xRadius">The x radius.</param>
/// <param name="yRadius">The y radius.</param>
/// <param name="segments">The segments.</param>
/// <param name="density">The density.</param>
/// <param name="position">The position.</param>
/// <returns></returns>
public static Body CreateRoundedRectangle(World world, float width, float height, float xRadius,
float yRadius,
int segments, float density, Vector2 position,
object userData)
{
Vertices verts = PolygonTools.CreateRoundedRectangle(width, height, xRadius, yRadius, segments);
//There are too many vertices in the capsule. We decompose it.
if (verts.Count >= Settings.MaxPolygonVertices)
{
List<Vertices> vertList = EarclipDecomposer.ConvexPartition(verts);
Body body = CreateCompoundPolygon(world, vertList, density, userData);
body.Position = position;
return body;
}
return CreatePolygon(world, verts, density);
}
public static Body CreateRoundedRectangle(World world, float width, float height, float xRadius,
float yRadius,
int segments, float density, Vector2 position)
{
return CreateRoundedRectangle(world, width, height, xRadius, yRadius, segments, density, position, null);
}
public static Body CreateRoundedRectangle(World world, float width, float height, float xRadius,
float yRadius,
int segments, float density)
{
return CreateRoundedRectangle(world, width, height, xRadius, yRadius, segments, density, null);
}
public static Body CreateRoundedRectangle(World world, float width, float height, float xRadius,
float yRadius,
int segments, float density, object userData)
{
return CreateRoundedRectangle(world, width, height, xRadius, yRadius, segments, density, Vector2.Zero,
userData);
}
public static BreakableBody CreateBreakableBody(World world, Vertices vertices, float density)
{
return CreateBreakableBody(world, vertices, density, null);
}
public static BreakableBody CreateBreakableBody(World world, Vertices vertices, float density, object userData)
{
return CreateBreakableBody(world, vertices, density, Vector2.Zero, userData);
}
/// <summary>
/// Creates a breakable body. You would want to remove collinear points before using this.
/// </summary>
/// <param name="world">The world.</param>
/// <param name="vertices">The vertices.</param>
/// <param name="density">The density.</param>
/// <param name="position">The position.</param>
/// <returns></returns>
public static BreakableBody CreateBreakableBody(World world, Vertices vertices, float density, Vector2 position,
object userData)
{
List<Vertices> triangles = EarclipDecomposer.ConvexPartition(vertices);
BreakableBody breakableBody = new BreakableBody(triangles, world, density, userData);
breakableBody.MainBody.Position = position;
world.AddBreakableBody(breakableBody);
return breakableBody;
}
public static BreakableBody CreateBreakableBody(World world, Vertices vertices, float density, Vector2 position)
{
return CreateBreakableBody(world, vertices, density, position, null);
}
public static Body CreateLineArc(World world, float radians, int sides, float radius, Vector2 position,
float angle, bool closed)
{
Body body = CreateBody(world);
FixtureFactory.AttachLineArc(radians, sides, radius, position, angle, closed, body);
return body;
}
public static Body CreateSolidArc(World world, float density, float radians, int sides, float radius,
Vector2 position, float angle)
{
Body body = CreateBody(world);
FixtureFactory.AttachSolidArc(density, radians, sides, radius, position, angle, body);
return body;
}
}
}

View File

@@ -0,0 +1,179 @@
using System;
using System.Collections.Generic;
using FarseerPhysics.Collision.Shapes;
using FarseerPhysics.Common;
using FarseerPhysics.Common.Decomposition;
using FarseerPhysics.Dynamics;
using Microsoft.Xna.Framework;
namespace FarseerPhysics.Factories
{
/// <summary>
/// An easy to use factory for creating bodies
/// </summary>
public static class FixtureFactory
{
public static Fixture AttachEdge(Vector2 start, Vector2 end, Body body)
{
return AttachEdge(start, end, body, null);
}
public static Fixture AttachEdge(Vector2 start, Vector2 end, Body body, object userData)
{
EdgeShape edgeShape = new EdgeShape(start, end);
return body.CreateFixture(edgeShape, userData);
}
public static Fixture AttachLoopShape(Vertices vertices, Body body)
{
return AttachLoopShape(vertices, body, null);
}
public static Fixture AttachLoopShape(Vertices vertices, Body body, object userData)
{
LoopShape shape = new LoopShape(vertices);
return body.CreateFixture(shape, userData);
}
public static Fixture AttachRectangle(float width, float height, float density, Vector2 offset, Body body,
object userData)
{
Vertices rectangleVertices = PolygonTools.CreateRectangle(width / 2, height / 2);
rectangleVertices.Translate(ref offset);
PolygonShape rectangleShape = new PolygonShape(rectangleVertices, density);
return body.CreateFixture(rectangleShape, userData);
}
public static Fixture AttachRectangle(float width, float height, float density, Vector2 offset, Body body)
{
return AttachRectangle(width, height, density, offset, body, null);
}
public static Fixture AttachCircle(float radius, float density, Body body)
{
return AttachCircle(radius, density, body, null);
}
public static Fixture AttachCircle(float radius, float density, Body body, object userData)
{
if (radius <= 0)
throw new ArgumentOutOfRangeException("radius", "Radius must be more than 0 meters");
CircleShape circleShape = new CircleShape(radius, density);
return body.CreateFixture(circleShape, userData);
}
public static Fixture AttachCircle(float radius, float density, Body body, Vector2 offset)
{
return AttachCircle(radius, density, body, offset, null);
}
public static Fixture AttachCircle(float radius, float density, Body body, Vector2 offset, object userData)
{
if (radius <= 0)
throw new ArgumentOutOfRangeException("radius", "Radius must be more than 0 meters");
CircleShape circleShape = new CircleShape(radius, density);
circleShape.Position = offset;
return body.CreateFixture(circleShape, userData);
}
public static Fixture AttachPolygon(Vertices vertices, float density, Body body)
{
return AttachPolygon(vertices, density, body, null);
}
public static Fixture AttachPolygon(Vertices vertices, float density, Body body, object userData)
{
if (vertices.Count <= 1)
throw new ArgumentOutOfRangeException("vertices", "Too few points to be a polygon");
PolygonShape polygon = new PolygonShape(vertices, density);
return body.CreateFixture(polygon, userData);
}
public static Fixture AttachEllipse(float xRadius, float yRadius, int edges, float density, Body body)
{
return AttachEllipse(xRadius, yRadius, edges, density, body, null);
}
public static Fixture AttachEllipse(float xRadius, float yRadius, int edges, float density, Body body,
object userData)
{
if (xRadius <= 0)
throw new ArgumentOutOfRangeException("xRadius", "X-radius must be more than 0");
if (yRadius <= 0)
throw new ArgumentOutOfRangeException("yRadius", "Y-radius must be more than 0");
Vertices ellipseVertices = PolygonTools.CreateEllipse(xRadius, yRadius, edges);
PolygonShape polygonShape = new PolygonShape(ellipseVertices, density);
return body.CreateFixture(polygonShape, userData);
}
public static List<Fixture> AttachCompoundPolygon(List<Vertices> list, float density, Body body)
{
return AttachCompoundPolygon(list, density, body, null);
}
public static List<Fixture> AttachCompoundPolygon(List<Vertices> list, float density, Body body, object userData)
{
List<Fixture> res = new List<Fixture>(list.Count);
//Then we create several fixtures using the body
foreach (Vertices vertices in list)
{
if (vertices.Count == 2)
{
EdgeShape shape = new EdgeShape(vertices[0], vertices[1]);
res.Add(body.CreateFixture(shape, userData));
}
else
{
PolygonShape shape = new PolygonShape(vertices, density);
res.Add(body.CreateFixture(shape, userData));
}
}
return res;
}
public static List<Fixture> AttachLineArc(float radians, int sides, float radius, Vector2 position, float angle,
bool closed, Body body)
{
Vertices arc = PolygonTools.CreateArc(radians, sides, radius);
arc.Rotate((MathHelper.Pi - radians) / 2 + angle);
arc.Translate(ref position);
List<Fixture> fixtures = new List<Fixture>(arc.Count);
if (closed)
{
fixtures.Add(AttachLoopShape(arc, body));
}
for (int i = 1; i < arc.Count; i++)
{
fixtures.Add(AttachEdge(arc[i], arc[i - 1], body));
}
return fixtures;
}
public static List<Fixture> AttachSolidArc(float density, float radians, int sides, float radius,
Vector2 position, float angle, Body body)
{
Vertices arc = PolygonTools.CreateArc(radians, sides, radius);
arc.Rotate((MathHelper.Pi - radians) / 2 + angle);
arc.Translate(ref position);
//Close the arc
arc.Add(arc[0]);
List<Vertices> triangles = EarclipDecomposer.ConvexPartition(arc);
return AttachCompoundPolygon(triangles, density, body);
}
}
}

View File

@@ -0,0 +1,288 @@
using FarseerPhysics.Dynamics;
using FarseerPhysics.Dynamics.Joints;
using Microsoft.Xna.Framework;
namespace FarseerPhysics.Factories
{
/// <summary>
/// An easy to use factory for using joints.
/// </summary>
public static class JointFactory
{
#region Revolute Joint
/// <summary>
/// Creates a revolute joint.
/// </summary>
/// <param name="bodyA"></param>
/// <param name="bodyB"></param>
/// <param name="localAnchorB">The anchor of bodyB in local coordinates</param>
/// <returns></returns>
public static RevoluteJoint CreateRevoluteJoint(Body bodyA, Body bodyB, Vector2 localAnchorB)
{
Vector2 localanchorA = bodyA.GetLocalPoint(bodyB.GetWorldPoint(localAnchorB));
RevoluteJoint joint = new RevoluteJoint(bodyA, bodyB, localanchorA, localAnchorB);
return joint;
}
/// <summary>
/// Creates a revolute joint and adds it to the world
/// </summary>
/// <param name="world"></param>
/// <param name="bodyA"></param>
/// <param name="bodyB"></param>
/// <param name="anchor"></param>
/// <returns></returns>
public static RevoluteJoint CreateRevoluteJoint(World world, Body bodyA, Body bodyB, Vector2 anchor)
{
RevoluteJoint joint = CreateRevoluteJoint(bodyA, bodyB, anchor);
world.AddJoint(joint);
return joint;
}
/// <summary>
/// Creates the fixed revolute joint.
/// </summary>
/// <param name="world">The world.</param>
/// <param name="body">The body.</param>
/// <param name="bodyAnchor">The body anchor.</param>
/// <param name="worldAnchor">The world anchor.</param>
/// <returns></returns>
public static FixedRevoluteJoint CreateFixedRevoluteJoint(World world, Body body, Vector2 bodyAnchor,
Vector2 worldAnchor)
{
FixedRevoluteJoint fixedRevoluteJoint = new FixedRevoluteJoint(body, bodyAnchor, worldAnchor);
world.AddJoint(fixedRevoluteJoint);
return fixedRevoluteJoint;
}
#endregion
#region Weld Joint
/// <summary>
/// Creates a weld joint
/// </summary>
/// <param name="bodyA"></param>
/// <param name="bodyB"></param>
/// <param name="localAnchor"></param>
/// <returns></returns>
public static WeldJoint CreateWeldJoint(Body bodyA, Body bodyB, Vector2 localAnchor)
{
WeldJoint joint = new WeldJoint(bodyA, bodyB, bodyA.GetLocalPoint(localAnchor),
bodyB.GetLocalPoint(localAnchor));
return joint;
}
/// <summary>
/// Creates a weld joint and adds it to the world
/// </summary>
/// <param name="world"></param>
/// <param name="bodyA"></param>
/// <param name="bodyB"></param>
/// <param name="localanchorB"></param>
/// <returns></returns>
public static WeldJoint CreateWeldJoint(World world, Body bodyA, Body bodyB, Vector2 localanchorB)
{
WeldJoint joint = CreateWeldJoint(bodyA, bodyB, localanchorB);
world.AddJoint(joint);
return joint;
}
public static WeldJoint CreateWeldJoint(World world, Body bodyA, Body bodyB, Vector2 localAnchorA,
Vector2 localAnchorB)
{
WeldJoint weldJoint = new WeldJoint(bodyA, bodyB, localAnchorA, localAnchorB);
world.AddJoint(weldJoint);
return weldJoint;
}
#endregion
#region Prismatic Joint
/// <summary>
/// Creates a prsimatic joint
/// </summary>
/// <param name="bodyA"></param>
/// <param name="bodyB"></param>
/// <param name="localanchorB"></param>
/// <param name="axis"></param>
/// <returns></returns>
public static PrismaticJoint CreatePrismaticJoint(Body bodyA, Body bodyB, Vector2 localanchorB, Vector2 axis)
{
Vector2 localanchorA = bodyA.GetLocalPoint(bodyB.GetWorldPoint(localanchorB));
PrismaticJoint joint = new PrismaticJoint(bodyA, bodyB, localanchorA, localanchorB, axis);
return joint;
}
/// <summary>
/// Creates a prismatic joint and adds it to the world
/// </summary>
/// <param name="world"></param>
/// <param name="bodyA"></param>
/// <param name="bodyB"></param>
/// <param name="localanchorB"></param>
/// <param name="axis"></param>
/// <returns></returns>
public static PrismaticJoint CreatePrismaticJoint(World world, Body bodyA, Body bodyB, Vector2 localanchorB,
Vector2 axis)
{
PrismaticJoint joint = CreatePrismaticJoint(bodyA, bodyB, localanchorB, axis);
world.AddJoint(joint);
return joint;
}
public static FixedPrismaticJoint CreateFixedPrismaticJoint(World world, Body body, Vector2 worldAnchor,
Vector2 axis)
{
FixedPrismaticJoint joint = new FixedPrismaticJoint(body, worldAnchor, axis);
world.AddJoint(joint);
return joint;
}
#endregion
#region Line Joint
/// <summary>
/// Creates a line joint
/// </summary>
/// <param name="bodyA"></param>
/// <param name="bodyB"></param>
/// <param name="anchor"></param>
/// <param name="axis"></param>
/// <returns></returns>
public static LineJoint CreateLineJoint(Body bodyA, Body bodyB, Vector2 anchor, Vector2 axis)
{
LineJoint joint = new LineJoint(bodyA, bodyB, anchor, axis);
return joint;
}
/// <summary>
/// Creates a line joint and adds it to the world
/// </summary>
/// <param name="world"></param>
/// <param name="bodyA"></param>
/// <param name="bodyB"></param>
/// <param name="localanchorB"></param>
/// <param name="axis"></param>
/// <returns></returns>
public static LineJoint CreateLineJoint(World world, Body bodyA, Body bodyB, Vector2 localanchorB, Vector2 axis)
{
LineJoint joint = CreateLineJoint(bodyA, bodyB, localanchorB, axis);
world.AddJoint(joint);
return joint;
}
#endregion
#region Angle Joint
/// <summary>
/// Creates an angle joint.
/// </summary>
/// <param name="world">The world.</param>
/// <param name="bodyA">The first body.</param>
/// <param name="bodyB">The second body.</param>
/// <returns></returns>
public static AngleJoint CreateAngleJoint(World world, Body bodyA, Body bodyB)
{
AngleJoint angleJoint = new AngleJoint(bodyA, bodyB);
world.AddJoint(angleJoint);
return angleJoint;
}
/// <summary>
/// Creates a fixed angle joint.
/// </summary>
/// <param name="world">The world.</param>
/// <param name="body">The body.</param>
/// <returns></returns>
public static FixedAngleJoint CreateFixedAngleJoint(World world, Body body)
{
FixedAngleJoint angleJoint = new FixedAngleJoint(body);
world.AddJoint(angleJoint);
return angleJoint;
}
#endregion
#region Distance Joint
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;
}
public static FixedDistanceJoint CreateFixedDistanceJoint(World world, Body body, Vector2 localAnchor,
Vector2 worldAnchor)
{
FixedDistanceJoint distanceJoint = new FixedDistanceJoint(body, localAnchor, worldAnchor);
world.AddJoint(distanceJoint);
return distanceJoint;
}
#endregion
#region Friction Joint
public static FrictionJoint CreateFrictionJoint(World world, Body bodyA, Body bodyB, Vector2 anchorA,
Vector2 anchorB)
{
FrictionJoint frictionJoint = new FrictionJoint(bodyA, bodyB, anchorA, anchorB);
world.AddJoint(frictionJoint);
return frictionJoint;
}
public static FixedFrictionJoint CreateFixedFrictionJoint(World world, Body body, Vector2 bodyAnchor)
{
FixedFrictionJoint frictionJoint = new FixedFrictionJoint(body, bodyAnchor);
world.AddJoint(frictionJoint);
return frictionJoint;
}
#endregion
#region Gear Joint
public static GearJoint CreateGearJoint(World world, Joint jointA, Joint jointB, float ratio)
{
GearJoint gearJoint = new GearJoint(jointA, jointB, ratio);
world.AddJoint(gearJoint);
return gearJoint;
}
#endregion
#region Pulley Joint
public static PulleyJoint CreatePulleyJoint(World world, Body bodyA, Body bodyB, Vector2 groundAnchorA,
Vector2 groundAnchorB, Vector2 anchorA, Vector2 anchorB, float ratio)
{
PulleyJoint pulleyJoint = new PulleyJoint(bodyA, bodyB, groundAnchorA, groundAnchorB, anchorA, anchorB,
ratio);
world.AddJoint(pulleyJoint);
return pulleyJoint;
}
#endregion
#region Slider Joint
public static SliderJoint CreateSliderJoint(World world, Body bodyA, Body bodyB, Vector2 anchorA,
Vector2 anchorB, float minLength, float maxLength)
{
SliderJoint sliderJoint = new SliderJoint(bodyA, bodyB, anchorA, anchorB, minLength, maxLength);
world.AddJoint(sliderJoint);
return sliderJoint;
}
#endregion
}
}

View File

@@ -0,0 +1,62 @@
using System.Collections.Generic;
using FarseerPhysics.Collision.Shapes;
using FarseerPhysics.Common;
using FarseerPhysics.Dynamics;
using Microsoft.Xna.Framework;
namespace FarseerPhysics.Factories
{
public static class LinkFactory
{
/// <summary>
/// Creates a chain.
/// </summary>
/// <param name="world">The world.</param>
/// <param name="start">The start.</param>
/// <param name="end">The end.</param>
/// <param name="linkWidth">The width.</param>
/// <param name="linkHeight">The height.</param>
/// <param name="fixStart">if set to <c>true</c> [fix start].</param>
/// <param name="fixEnd">if set to <c>true</c> [fix end].</param>
/// <param name="numberOfLinks">The number of links.</param>
/// <param name="linkDensity">The link density.</param>
/// <returns></returns>
public static Path CreateChain(World world, Vector2 start, Vector2 end, float linkWidth, float linkHeight,
bool fixStart, bool fixEnd, int numberOfLinks, float linkDensity)
{
//Chain start / end
Path path = new Path();
path.Add(start);
path.Add(end);
//A single chainlink
PolygonShape shape = new PolygonShape(PolygonTools.CreateRectangle(linkWidth, linkHeight), linkDensity);
//Use PathManager to create all the chainlinks based on the chainlink created before.
List<Body> chainLinks = PathManager.EvenlyDistributeShapesAlongPath(world, path, shape, BodyType.Dynamic,
numberOfLinks);
if (fixStart)
{
//Fix the first chainlink to the world
JointFactory.CreateFixedRevoluteJoint(world, chainLinks[0], new Vector2(0, -(linkHeight / 2)),
chainLinks[0].Position);
}
if (fixEnd)
{
//Fix the last chainlink to the world
JointFactory.CreateFixedRevoluteJoint(world, chainLinks[chainLinks.Count - 1],
new Vector2(0, (linkHeight / 2)),
chainLinks[chainLinks.Count - 1].Position);
}
//Attach all the chainlinks together with a revolute joint
PathManager.AttachBodiesWithRevoluteJoint(world, chainLinks, new Vector2(0, -linkHeight),
new Vector2(0, linkHeight),
false, false);
return (path);
}
}
}