public override void LoadContent() { base.LoadContent(); World.Gravity = new Vector2(0, 9.82f); _border = new Border(World, this, ScreenManager.GraphicsDevice.Viewport); /* Bridge */ //We make a path using 2 points. Path bridgePath = new Path(); bridgePath.Add(new Vector2(-15, 5)); bridgePath.Add(new Vector2(15, 5)); bridgePath.Closed = false; Vertices box = PolygonTools.CreateRectangle(0.125f, 0.5f); PolygonShape shape = new PolygonShape(box, 20); _bridgeBodies = PathManager.EvenlyDistributeShapesAlongPath(World, bridgePath, shape, BodyType.Dynamic, 29); _bridgeBox = new Sprite(ScreenManager.Assets.TextureFromShape(shape, MaterialType.Dots, Color.SandyBrown, 1f)); //Attach the first and last fixtures to the world JointFactory.CreateFixedRevoluteJoint(World, _bridgeBodies[0], new Vector2(0f, -0.5f), _bridgeBodies[0].Position); JointFactory.CreateFixedRevoluteJoint(World, _bridgeBodies[_bridgeBodies.Count - 1], new Vector2(0, 0.5f), _bridgeBodies[_bridgeBodies.Count - 1].Position); PathManager.AttachBodiesWithRevoluteJoint(World, _bridgeBodies, new Vector2(0f, -0.5f), new Vector2(0f, 0.5f), false, true); /* Soft body */ //We make a rectangular path. Path rectanglePath = new Path(); rectanglePath.Add(new Vector2(-6, -11)); rectanglePath.Add(new Vector2(-6, 1)); rectanglePath.Add(new Vector2(6, 1)); rectanglePath.Add(new Vector2(6, -11)); rectanglePath.Closed = true; //Creating two shapes. A circle to form the circle and a rectangle to stabilize the soft body. List <Shape> shapes = new List <Shape>(2); shapes.Add(new PolygonShape(PolygonTools.CreateRectangle(0.5f, 0.5f, new Vector2(-0.1f, 0f), 0f), 1f)); shapes.Add(new CircleShape(0.5f, 1f)); //We distribute the shapes in the rectangular path. _softBodies = PathManager.EvenlyDistributeShapesAlongPath(World, rectanglePath, shapes, BodyType.Dynamic, 30); _softBodyBox = new Sprite(ScreenManager.Assets.TextureFromShape(shapes[0], MaterialType.Blank, Color.Silver * 0.8f, 1f)); _softBodyBox.Origin += new Vector2(ConvertUnits.ToDisplayUnits(0.1f), 0f); _softBodyCircle = new Sprite(ScreenManager.Assets.TextureFromShape(shapes[1], MaterialType.Waves, Color.Silver, 1f)); //Attach the bodies together with revolute joints. The rectangular form will converge to a circular form. PathManager.AttachBodiesWithRevoluteJoint(World, _softBodies, new Vector2(0f, -0.5f), new Vector2(0f, 0.5f), true, true); }
public Texture2D TextureFromVertices(Vertices vertices, MaterialType type, Color color, float materialScale) { // copy vertices Vertices verts = new Vertices(vertices); // scale to display units (i.e. pixels) for rendering to texture Vector2 scale = ConvertUnits.ToDisplayUnits(Vector2.One); verts.Scale(ref scale); // translate the boundingbox center to the texture center // because we use an orthographic projection for rendering later AABB vertsBounds = verts.GetCollisionBox(); verts.Translate(-vertsBounds.Center); List <Vertices> decomposedVerts; if (!verts.IsConvex()) { decomposedVerts = EarclipDecomposer.ConvexPartition(verts); } else { decomposedVerts = new List <Vertices>(); decomposedVerts.Add(verts); } List <VertexPositionColorTexture[]> verticesFill = new List <VertexPositionColorTexture[]>(decomposedVerts.Count); materialScale /= _materials[type].Width; for (int i = 0; i < decomposedVerts.Count; ++i) { verticesFill.Add(new VertexPositionColorTexture[3 * (decomposedVerts[i].Count - 2)]); for (int j = 0; j < decomposedVerts[i].Count - 2; ++j) { // fill vertices verticesFill[i][3 * j].Position = new Vector3(decomposedVerts[i][0], 0f); verticesFill[i][3 * j + 1].Position = new Vector3(decomposedVerts[i].NextVertex(j), 0f); verticesFill[i][3 * j + 2].Position = new Vector3(decomposedVerts[i].NextVertex(j + 1), 0f); verticesFill[i][3 * j].TextureCoordinate = decomposedVerts[i][0] * materialScale; verticesFill[i][3 * j + 1].TextureCoordinate = decomposedVerts[i].NextVertex(j) * materialScale; verticesFill[i][3 * j + 2].TextureCoordinate = decomposedVerts[i].NextVertex(j + 1) * materialScale; verticesFill[i][3 * j].Color = verticesFill[i][3 * j + 1].Color = verticesFill[i][3 * j + 2].Color = color; } } // calculate outline VertexPositionColor[] verticesOutline = new VertexPositionColor[2 * verts.Count]; for (int i = 0; i < verts.Count; ++i) { verticesOutline[2 * i].Position = new Vector3(verts[i], 0f); verticesOutline[2 * i + 1].Position = new Vector3(verts.NextVertex(i), 0f); verticesOutline[2 * i].Color = verticesOutline[2 * i + 1].Color = Color.Black; } Vector2 vertsSize = new Vector2(vertsBounds.UpperBound.X - vertsBounds.LowerBound.X, vertsBounds.UpperBound.Y - vertsBounds.LowerBound.Y); return(RenderTexture((int)vertsSize.X, (int)vertsSize.Y, _materials[type], verticesFill, verticesOutline)); }