Root/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | using System; using System.Collections.Generic; using FarseerPhysics.Collision.Shapes; using FarseerPhysics.Common; using FarseerPhysics.Common.Decomposition; using FarseerPhysics.Dynamics; using FarseerPhysics.Dynamics.Joints; using Microsoft.Xna.Framework; namespace FarseerPhysics.Factories { /// <summary> /// An easy to use manager for creating paths. /// </summary> public static class PathManager { #region LinkType enum public enum LinkType { Revolute, Slider } #endregion //Contributed by Matthew Bettcher /// <summary> /// Convert a path into a set of edges and attaches them to the specified body. /// Note: use only for static edges. /// </summary> /// <param name="path">The path.</param> /// <param name="body">The body.</param> /// <param name="subdivisions">The subdivisions.</param> public static void ConvertPathToEdges(Path path, Body body, int subdivisions) { Vertices verts = path.GetVertices(subdivisions); if (path.Closed) { LoopShape loop = new LoopShape(verts); body.CreateFixture(loop); } else { for ( int i = 1; i < verts.Count; i++) { body.CreateFixture( new EdgeShape(verts[i], verts[i - 1])); } } } /// <summary> /// Convert a closed path into a polygon. /// Convex decomposition is automatically performed. /// </summary> /// <param name="path">The path.</param> /// <param name="body">The body.</param> /// <param name="density">The density.</param> /// <param name="subdivisions">The subdivisions.</param> public static void ConvertPathToPolygon(Path path, Body body, float density, int subdivisions) { if (!path.Closed) throw new Exception( "The path must be closed to convert to a polygon." ); List<Vector2> verts = path.GetVertices(subdivisions); List<Vertices> decomposedVerts = EarclipDecomposer.ConvexPartition( new Vertices(verts)); //List<Vertices> decomposedVerts = BayazitDecomposer.ConvexPartition(new Vertices(verts)); foreach (Vertices item in decomposedVerts) { body.CreateFixture( new PolygonShape(item, density)); } } /// <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<Body> EvenlyDistributeShapesAlongPath(World world, Path path, IEnumerable<Shape> shapes, BodyType type, int copies, object userData) { List<Vector3> centers = path.SubdivideEvenly(copies); List<Body> bodyList = new List<Body>(); for ( int i = 0; i < centers.Count; i++) { Body b = new Body(world); // copy the type from original body b.BodyType = type; b.Position = new Vector2(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; } public static List<Body> EvenlyDistributeShapesAlongPath(World world, Path path, IEnumerable<Shape> shapes, BodyType type, int copies) { return EvenlyDistributeShapesAlongPath(world, path, shapes, type, copies, null ); } /// <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<Body> EvenlyDistributeShapesAlongPath(World 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); } public static List<Body> EvenlyDistributeShapesAlongPath(World world, Path path, Shape shape, BodyType type, int copies) { return EvenlyDistributeShapesAlongPath(world, path, shape, type, copies, null ); } //TODO: Comment better /// <summary> /// Moves the body on the path. /// </summary> /// <param name="path">The path.</param> /// <param name="body">The body.</param> /// <param name="time">The time.</param> /// <param name="strength">The strength.</param> /// <param name="timeStep">The time step.</param> public static void MoveBodyOnPath(Path path, Body body, float time, float strength, float timeStep) { Vector2 destination = path.GetPosition(time); Vector2 positionDelta = body.Position - destination; Vector2 velocity = (positionDelta / timeStep) * strength; body.LinearVelocity = -velocity; } /// <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> public static List<RevoluteJoint> AttachBodiesWithRevoluteJoint(World world, List<Body> bodies, Vector2 localAnchorA, Vector2 localAnchorB, bool connectFirstAndLast, bool collideConnected) { List<RevoluteJoint> joints = new List<RevoluteJoint>(bodies.Count + 1); for ( int i = 1; i < bodies.Count; i++) { RevoluteJoint joint = new RevoluteJoint(bodies[i], bodies[i - 1], localAnchorA, localAnchorB); joint.CollideConnected = collideConnected; world.AddJoint(joint); joints.Add(joint); } if (connectFirstAndLast) { RevoluteJoint lastjoint = new RevoluteJoint(bodies[0], bodies[bodies.Count - 1], localAnchorA, localAnchorB); lastjoint.CollideConnected = collideConnected; world.AddJoint(lastjoint); joints.Add(lastjoint); } return joints; } /// <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<SliderJoint> AttachBodiesWithSliderJoint(World world, List<Body> bodies, Vector2 localAnchorA, Vector2 localAnchorB, bool connectFirstAndLast, bool collideConnected, float minLength, float maxLength) { List<SliderJoint> joints = new List<SliderJoint>(bodies.Count + 1); for ( int i = 1; i < bodies.Count; i++) { SliderJoint joint = new SliderJoint(bodies[i], bodies[i - 1], localAnchorA, localAnchorB, minLength, maxLength); joint.CollideConnected = collideConnected; world.AddJoint(joint); joints.Add(joint); } if (connectFirstAndLast) { SliderJoint lastjoint = new SliderJoint(bodies[0], bodies[bodies.Count - 1], localAnchorA, localAnchorB, minLength, maxLength); lastjoint.CollideConnected = collideConnected; world.AddJoint(lastjoint); joints.Add(lastjoint); } return joints; } } } |
Source at commit e14c3e870754 created 12 years 5 months ago. By Nathan Adams, Changing version |
---|