Exemple #1
0
        /// <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 FSAngleJoint CreateAngleJoint(FSWorld world, FSBody bodyA, FSBody bodyB)
        {
            FSAngleJoint angleJoint = new FSAngleJoint(bodyA, bodyB);
            world.AddJoint(angleJoint);

            return angleJoint;
        }
Exemple #2
0
 public static FSDistanceJoint CreateDistanceJoint(FSWorld world, FSBody bodyA, FSBody bodyB, FVector2 anchorA,
                                                 FVector2 anchorB)
 {
     FSDistanceJoint distanceJoint = new FSDistanceJoint(bodyA, bodyB, anchorA, anchorB);
     world.AddJoint(distanceJoint);
     return distanceJoint;
 }
Exemple #3
0
        /// <summary>
        /// Creates a fixed angle joint.
        /// </summary>
        /// <param name="world">The world.</param>
        /// <param name="body">The body.</param>
        /// <returns></returns>
        public static FSFixedAngleJoint CreateFixedAngleJoint(FSWorld world, FSBody body)
        {
            FSFixedAngleJoint angleJoint = new FSFixedAngleJoint(body);
            world.AddJoint(angleJoint);

            return angleJoint;
        }
Exemple #4
0
        /// <summary>
        /// Attaches the bodies with revolute joints.
        /// </summary>
        /// <param name="world">The world.</param>
        /// <param name="bodies">The bodies.</param>
        /// <param name="localAnchorA">The local anchor A.</param>
        /// <param name="localAnchorB">The local anchor B.</param>
        /// <param name="connectFirstAndLast">if set to <c>true</c> [connect first and last].</param>
        /// <param name="collideConnected">if set to <c>true</c> [collide connected].</param>
        /// <param name="minLength">Minimum length of the slider joint.</param>
        /// <param name="maxLength">Maximum length of the slider joint.</param>
        /// <returns></returns>
        public static List<FSSliderJoint> AttachBodiesWithSliderJoint(FSWorld world, List<FSBody> bodies, FVector2 localAnchorA,
                                                                    FVector2 localAnchorB, bool connectFirstAndLast,
                                                                    bool collideConnected, float minLength,
                                                                    float maxLength)
        {
            List<FSSliderJoint> joints = new List<FSSliderJoint>(bodies.Count + 1);

            for (int i = 1; i < bodies.Count; i++)
            {
                FSSliderJoint joint = new FSSliderJoint(bodies[i], bodies[i - 1], localAnchorA, localAnchorB, minLength,
                                                    maxLength);
                joint.CollideConnected = collideConnected;
                world.AddJoint(joint);
                joints.Add(joint);
            }

            if (connectFirstAndLast)
            {
                FSSliderJoint lastjoint = new FSSliderJoint(bodies[0], bodies[bodies.Count - 1], localAnchorA, localAnchorB,
                                                        minLength, maxLength);
                lastjoint.CollideConnected = collideConnected;
                world.AddJoint(lastjoint);
                joints.Add(lastjoint);
            }

            return joints;
        }
Exemple #5
0
 public static FSFrictionJoint CreateFrictionJoint(FSWorld world, FSBody bodyA, FSBody bodyB, FVector2 anchorA,
                                                 FVector2 anchorB)
 {
     FSFrictionJoint frictionJoint = new FSFrictionJoint(bodyA, bodyB, anchorA, anchorB);
     world.AddJoint(frictionJoint);
     return frictionJoint;
 }
Exemple #6
0
        /// <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 FSBreakableBody CreateBreakableBody(FSWorld world, Vertices vertices, float density, FVector2 position,
                                                        object userData)
        {
            List<Vertices> triangles = EarclipDecomposer.ConvexPartition(vertices);

            FSBreakableBody breakableBody = new FSBreakableBody(triangles, world, density, userData);
            breakableBody.MainBody.Position = position;
            world.AddBreakableBody(breakableBody);

            return breakableBody;
        }
Exemple #7
0
        public FSBreakableBody(IEnumerable<Vertices> vertices, FSWorld world, float density, object userData)
        {
            _world = world;
            _world.ContactManager.PostSolve += PostSolve;
            MainBody = new FSBody(_world);
            MainBody.BodyType = BodyType.Dynamic;

            foreach (Vertices part in vertices)
            {
                PolygonShape polygonShape = new PolygonShape(part, density);
                FSFixture fixture = MainBody.CreateFixture(polygonShape, userData);
                Parts.Add(fixture);
            }
        }
Exemple #8
0
        /// <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(FSWorld world, FVector2 start, FVector2 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<FSBody> 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 FVector2(0, -linkHeight),
                                                      new FVector2(0, linkHeight),
                                                      false, false);

            return (path);
        }
Exemple #9
0
 public static FSBreakableBody CreateBreakableBody(FSWorld world, Vertices vertices, float density, FVector2 position)
 {
     return CreateBreakableBody(world, vertices, density, position, null);
 }
Exemple #10
0
 public static FSWeldJoint CreateWeldJoint(FSWorld world, FSBody bodyA, FSBody bodyB, FVector2 localAnchorA,
                                         FVector2 localAnchorB)
 {
     FSWeldJoint weldJoint = new FSWeldJoint(bodyA, bodyB, localAnchorA, localAnchorB);
     world.AddJoint(weldJoint);
     return weldJoint;
 }
Exemple #11
0
 /// <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="worldAnchor">World space coordinates of weld joint</param>
 /// <returns></returns>
 public static FSWeldJoint CreateWeldJoint(FSWorld world, FSBody bodyA, FSBody bodyB, FVector2 worldAnchor)
 {
     FSWeldJoint joint = CreateWeldJoint(bodyA, bodyB, worldAnchor);
     world.AddJoint(joint);
     return joint;
 }
Exemple #12
0
 public static FSSliderJoint CreateSliderJoint(FSWorld world, FSBody bodyA, FSBody bodyB, FVector2 anchorA,
                                             FVector2 anchorB, float minLength, float maxLength)
 {
     FSSliderJoint sliderJoint = new FSSliderJoint(bodyA, bodyB, anchorA, anchorB, minLength, maxLength);
     world.AddJoint(sliderJoint);
     return sliderJoint;
 }
Exemple #13
0
 /// <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 FSRevoluteJoint CreateRevoluteJoint(FSWorld world, FSBody bodyA, FSBody bodyB, FVector2 anchor)
 {
     FSRevoluteJoint joint = CreateRevoluteJoint(bodyA, bodyB, anchor);
     world.AddJoint(joint);
     return joint;
 }
Exemple #14
0
        /// <summary>
        /// Duplicates the given Body along the given path for approximatly the given copies.
        /// </summary>
        /// <param name="world">The world.</param>
        /// <param name="path">The path.</param>
        /// <param name="shapes">The shapes.</param>
        /// <param name="type">The type.</param>
        /// <param name="copies">The copies.</param>
        /// <param name="userData"></param>
        /// <returns></returns>
        public static List<FSBody> EvenlyDistributeShapesAlongPath(FSWorld world, Path path, IEnumerable<Shape> shapes,
                                                                 BodyType type, int copies, object userData)
        {
            List<FVector3> centers = path.SubdivideEvenly(copies);
            List<FSBody> bodyList = new List<FSBody>();

            for (int i = 0; i < centers.Count; i++)
            {
                FSBody b = new FSBody(world);

                // copy the type from original body
                b.BodyType = type;
                b.Position = new FVector2(centers[i].X, centers[i].Y);
                b.Rotation = centers[i].Z;

                foreach (Shape shape in shapes)
                {
                    b.CreateFixture(shape, userData);
                }

                bodyList.Add(b);
            }

            return bodyList;
        }
Exemple #15
0
 public static FSFixedMouseJoint CreateFixedMouseJoint(FSWorld world, FSBody body, FVector2 target)
 {
     FSFixedMouseJoint joint = new FSFixedMouseJoint(body, target);
     world.AddJoint(joint);
     return joint;
 }
Exemple #16
0
 public FSBreakableBody(IEnumerable<Vertices> vertices, FSWorld world, float density)
     : this(vertices, world, density, null)
 {
 }
Exemple #17
0
 public static FSGearJoint CreateGearJoint(FSWorld world, FarseerJoint jointA, FarseerJoint jointB, float ratio)
 {
     FSGearJoint gearJoint = new FSGearJoint(jointA, jointB, ratio);
     world.AddJoint(gearJoint);
     return gearJoint;
 }
Exemple #18
0
 public FSBody(FSWorld world)
     : this(world, null)
 {
 }
Exemple #19
0
        /// <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 FSBody CreateCapsule(FSWorld world, float height, float topRadius, int topEdges,
                                         float bottomRadius,
                                         int bottomEdges, float density, FVector2 position, object userData)
        {
            Vertices verts = PolygonTools.CreateCapsule(height, topRadius, topEdges, bottomRadius, bottomEdges);

            FSBody body;

            //There are too many vertices in the capsule. We decompose it.
            if (verts.Count >= FSSettings.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;
        }
Exemple #20
0
 public static List<FSBody> EvenlyDistributeShapesAlongPath(FSWorld world, Path path, Shape shape, BodyType type,
                                                          int copies)
 {
     return EvenlyDistributeShapesAlongPath(world, path, shape, type, copies, null);
 }
Exemple #21
0
        /// <summary>
        /// Duplicates the given Body along the given path for approximatly the given copies.
        /// </summary>
        /// <param name="world">The world.</param>
        /// <param name="path">The path.</param>
        /// <param name="shape">The shape.</param>
        /// <param name="type">The type.</param>
        /// <param name="copies">The copies.</param>
        /// <param name="userData">The user data.</param>
        /// <returns></returns>
        public static List<FSBody> EvenlyDistributeShapesAlongPath(FSWorld world, Path path, Shape shape, BodyType type,
                                                                 int copies, object userData)
        {
            List<Shape> shapes = new List<Shape>(1);
            shapes.Add(shape);

            return EvenlyDistributeShapesAlongPath(world, path, shapes, type, copies, userData);
        }
Exemple #22
0
 public static FSBreakableBody CreateBreakableBody(FSWorld world, Vertices vertices, float density, object userData)
 {
     return CreateBreakableBody(world, vertices, density, FVector2.Zero, userData);
 }
Exemple #23
0
 public static FSBody CreateSolidArc(FSWorld world, float density, float radians, int sides, float radius,
                                   FVector2 position, float angle)
 {
     FSBody body = CreateBody(world);
     FixtureFactory.AttachSolidArc(density, radians, sides, radius, position, angle, body);
     return body;
 }
Exemple #24
0
 public static FSBreakableBody CreateBreakableBody(FSWorld world, Vertices vertices, float density)
 {
     return CreateBreakableBody(world, vertices, density, null);
 }
Exemple #25
0
        public FSBody(FSWorld world, object userData)
        {
            FixtureList = new List<FSFixture>(32);
            BodyId = _bodyIdCounter++;

            World = world;
            UserData = userData;

            GravityScale = 1.0f;
            FixedRotation = false;
            IsBullet = false;
            SleepingAllowed = true;
            #if !USE_AWAKE_BODY_SET
            Awake = true;
            #endif
            BodyType = BodyType.Static;
            Enabled = true;

            Xf.q.Set(0);

            world.AddBody(this);
        }
Exemple #26
0
 public static FSPulleyJoint CreatePulleyJoint(FSWorld world, FSBody bodyA, FSBody bodyB, FVector2 groundAnchorA,
                                             FVector2 groundAnchorB, FVector2 anchorA, FVector2 anchorB, float ratio)
 {
     FSPulleyJoint pulleyJoint = new FSPulleyJoint(bodyA, bodyB, groundAnchorA, groundAnchorB, anchorA, anchorB,
                                               ratio);
     world.AddJoint(pulleyJoint);
     return pulleyJoint;
 }
Exemple #27
0
 /// <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 FSPrismaticJoint CreatePrismaticJoint(FSWorld world, FSBody bodyA, FSBody bodyB, FVector2 localanchorB,
                                                   FVector2 axis)
 {
     FSPrismaticJoint joint = CreatePrismaticJoint(bodyA, bodyB, localanchorB, axis);
     world.AddJoint(joint);
     return joint;
 }
Exemple #28
0
 protected DebugView(FSWorld world)
 {
     World = world;
 }
Exemple #29
0
        /// <summary>
        /// This is a high-level function to cuts fixtures inside the given world, using the start and end points.
        /// Note: We don't support cutting when the start or end is inside a shape.
        /// </summary>
        /// <param name="world">The world.</param>
        /// <param name="start">The startpoint.</param>
        /// <param name="end">The endpoint.</param>
        /// <param name="thickness">The thickness of the cut</param>
        public static void Cut(FSWorld world, FVector2 start, FVector2 end, float thickness)
        {
            List<FSFixture> fixtures = new List<FSFixture>();
            List<FVector2> entryPoints = new List<FVector2>();
            List<FVector2> exitPoints = new List<FVector2>();

            //We don't support cutting when the start or end is inside a shape.
            if (world.TestPoint(start) != null || world.TestPoint(end) != null)
                return;

            //Get the entry points
            world.RayCast((f, p, n, fr) =>
                              {
                                  fixtures.Add(f);
                                  entryPoints.Add(p);
                                  return 1;
                              }, start, end);

            //Reverse the ray to get the exitpoints
            world.RayCast((f, p, n, fr) =>
                              {
                                  exitPoints.Add(p);
                                  return 1;
                              }, end, start);

            //We only have a single point. We need at least 2
            if (entryPoints.Count + exitPoints.Count < 2)
                return;

            for (int i = 0; i < fixtures.Count; i++)
            {
                // can't cut circles yet !
                if (fixtures[i].Shape.ShapeType != ShapeType.Polygon)
                    continue;

                if (fixtures[i].Body.BodyType != BodyType.Static)
                {
                    //Split the shape up into two shapes
                    Vertices first;
                    Vertices second;
                    SplitShape(fixtures[i], entryPoints[i], exitPoints[i], thickness, out first, out second);

                    //Delete the original shape and create two new. Retain the properties of the body.
                    if (SanityCheck(first))
                    {
                        FSBody firstFixture = BodyFactory.CreatePolygon(world, first, fixtures[i].Shape.Density,
                                                                            fixtures[i].Body.Position);
                        firstFixture.Rotation = fixtures[i].Body.Rotation;
                        firstFixture.LinearVelocity = fixtures[i].Body.LinearVelocity;
                        firstFixture.AngularVelocity = fixtures[i].Body.AngularVelocity;
                        firstFixture.BodyType = BodyType.Dynamic;
                    }

                    if (SanityCheck(second))
                    {
                        FSBody secondFixture = BodyFactory.CreatePolygon(world, second, fixtures[i].Shape.Density,
                                                                             fixtures[i].Body.Position);
                        secondFixture.Rotation = fixtures[i].Body.Rotation;
                        secondFixture.LinearVelocity = fixtures[i].Body.LinearVelocity;
                        secondFixture.AngularVelocity = fixtures[i].Body.AngularVelocity;
                        secondFixture.BodyType = BodyType.Dynamic;
                    }
                    world.RemoveBody(fixtures[i].Body);
                }
            }
        }
Exemple #30
0
 public static FSBody CreateRoundedRectangle(FSWorld world, float width, float height, float xRadius,
                                           float yRadius,
                                           int segments, float density, object userData)
 {
     return CreateRoundedRectangle(world, width, height, xRadius, yRadius, segments, density, FVector2.Zero,
                                   userData);
 }