FluidInfo ILineFluidAffectable.GetFluidInfo(GetTangentCallback callback, Line line) { if (polygons.Length == 1) { return(ShapeHelper.GetFluidInfo(Vertexes, callback, line)); } List <Vector2D[]> submerged = new List <Vector2D[]>(polygons.Length); for (int index = 0; index < polygons.Length; ++index) { Vector2D[] vertexes = ShapeHelper.GetIntersection(polygons[index], line); if (vertexes.Length >= 3) { submerged.Add(vertexes); } } if (submerged.Count == 0) { return(null); } Vector2D[][] newPolygons = submerged.ToArray(); Vector2D centroid = MultiPolygonShape.GetCentroid(newPolygons); Scalar area = MultiPolygonShape.GetArea(newPolygons); Vector2D tangent = callback(centroid); Vector2D dragCenter; Scalar dragArea; ShapeHelper.GetFluidInfo(newPolygons, tangent, out dragCenter, out dragArea); return(new FluidInfo(dragCenter, dragArea, centroid, area)); }
public static IShape CreateMultiPolygon(Vector2D[][] polygons, Scalar gridSpacing, ScalarColor4[][] colors) { if (polygons.Length == 1) { return CreatePolygon(polygons[0], gridSpacing, colors[0]); } else { MultiPolygonShape shape = new MultiPolygonShape(polygons, gridSpacing); shape.Tag = DrawableFactory.CreateMultiPolygon(polygons, colors); return shape; } }
void AddTowers() { Scalar xmin = 200; Scalar xmax = 800; Scalar ymin = 550; Scalar ymax = 700; Scalar size = 32; Scalar spacing = 1; Scalar Xspacing = 20; Scalar offset = 0; Scalar offsetchange = .9f; Sprite sprite = GetSprite("block.png"); Vector2D[][] vertexes = sprite.Polygons; MultiPolygonShape shape = new MultiPolygonShape(vertexes, 4); shape.Tag = sprite; for (Scalar x = xmin; x < xmax; x += spacing + Xspacing + size) { for (Scalar y = ymax; y > ymin; y -= spacing + size) { AddShape(shape, 20, new ALVector2D(0, new Vector2D(x + offset, y))); //AddRectangle(size, size, 20, new ALVector2D(0, new Vector2D(x + offset, y))); offset = MathHelper.WrapClamp(offset + offsetchange, -offsetchange, offsetchange); } } }
void AddPyramid() { Scalar size = 32; Scalar spacing = .01f; Scalar Xspacing = 2f; Scalar xmin = 300; Scalar xmax = 850; Scalar ymin = 50; Scalar ymax = 720 - size / 2; Scalar step = (size + spacing + Xspacing) / 2; Scalar currentStep = 0; Sprite sprite = GetSprite("block.png"); Vector2D[][] vertexes = sprite.Polygons; MultiPolygonShape shape = new MultiPolygonShape(vertexes, 4); shape.Tag = sprite; for (Scalar y = ymax; y >= ymin; y -= (spacing + size)) { for (Scalar x = xmin + currentStep; x < (xmax - currentStep); x += spacing + Xspacing + size) { AddShape(shape, 20, new ALVector2D(0, new Vector2D(x, y))); } currentStep += step; } }
void CreateAvatar() { Lifespan avatarLifespan = new Lifespan(); Sprite sprite = GetSprite("tank.png"); Vector2D[][] polygons = sprite.Polygons; MultiPolygonShape shape = new MultiPolygonShape(polygons, 4); shape.Tag = sprite; ObjectIgnorer ignorer = new ObjectIgnorer(); Body a = new Body(new PhysicsState(new ALVector2D(0, 0, 0)), shape, 300,//new MassInfo(40, Scalar.PositiveInfinity), coefficients.Duplicate(), avatarLifespan); a.Updated += new EventHandler<UpdatedEventArgs>(avatar_Updated); avatarBodies = new List<Body>(); avatarOffsets = new List<Vector2D>(); avatarJoints = new List<Joint>(); avatarBodies.Add(a); a.CollisionIgnorer = ignorer; Scalar wheelSize = 18; Scalar wheelSpacing = -9; Scalar lenghtPercent = .84f; Matrix2x3 ident = Matrix2x3.Identity; BoundingRectangle rect; shape.CalcBoundingRectangle(ref ident, out rect); Scalar y = (rect.Max.Y +4) ; Body lastWheel = null ; BoundingPolygon polygon = new BoundingPolygon(polygons[0]); Ray ray2 = new Ray(new Vector2D(rect.Max.X, y), -Vector2D.YAxis); Scalar y3 = y - polygon.Intersects(ray2); avatarBarrelOffset = new Vector2D(rect.Max.X, y3); for (Scalar x = rect.Min.X + wheelSize ; x < (rect.Max.X - wheelSize ) * lenghtPercent; x += (wheelSize*2 + wheelSpacing)) { Ray ray = new Ray(new Vector2D(x, y), -Vector2D.YAxis); Scalar y2 = y- polygon.Intersects(ray); Vector2D offset = new Vector2D(x, y2); Body wheel = new Body( new PhysicsState(new ALVector2D(0, offset)), new CircleShape(wheelSize, 30), 10, new Coefficients(0,3),// coefficients.Duplicate(), avatarLifespan); HingeJoint joint = new HingeJoint(a, wheel, offset, avatarLifespan); joint.Softness = .1f; wheel.CollisionIgnorer = ignorer; if (lastWheel != null) { AngleJoint joint2 = new AngleJoint(lastWheel, wheel, avatarLifespan); avatarJoints.Add(joint2); } avatarJoints.Add(joint); avatarOffsets.Add(offset); avatarBodies.Add(wheel); lastWheel = wheel; } }
List<Body> AddText(string text, Vector2D position) { /*if (font == null) { font = new Font(Path.Combine(dataDir, "FreeSans.ttf"), 40); font.Bold = true; }*/ List<Body> result = new List<Body>(); Scalar initialx = position.X; int maxy = 0; foreach (char c in text) { if (c == '\n') { position.Y += maxy; position.X = initialx; } else if (char.IsWhiteSpace(c)) { position.X += font.SizeText("" + c).Width; } else { Sprite sprite = GetLetter(c); Vector2D[][] vertexes = VertexHelper.SubdivideRange(sprite.Polygons, 3); BoundingRectangle rect = BoundingRectangle.FromVectors(sprite.Polygons[0]); MultiPolygonShape shape = new MultiPolygonShape( vertexes, Math.Min(Math.Min((rect.Max.X - rect.Min.X) / 8, (rect.Max.Y - rect.Min.Y) / 8), 3)); shape.Tag = sprite; Body b = AddShape(shape, 40, new ALVector2D(0, position + sprite.Offset)); maxy = Math.Max(maxy, sprite.Texture.Surface.Height); position.X += sprite.Texture.Surface.Width; result.Add(b); } } return result; }
void CreateBomb() { Sprite sprite = GetSprite("rocket.png"); Vector2D[][] vertexes = VertexHelper.SubdivideRange(sprite.Polygons, 10); MultiPolygonShape shape = new MultiPolygonShape(vertexes, 4); shape.Tag = sprite; bomb = new Body(new PhysicsState(new ALVector2D(0,500,-60)), shape,//new Physics2DDotNet.Circle(20, 20), 120, coefficients.Duplicate(), new Lifespan()); }
void AddTower() { Scalar size = 32; Scalar x = 500; Scalar spacing = size + 2; Scalar minY = 430; Scalar maxY = 720 - size / 2; Scalar offset = 0; Scalar offsetchange = .9f; Sprite sprite = GetSprite("block.png"); Vector2D[][] vertexes = sprite.Polygons; MultiPolygonShape shape = new MultiPolygonShape(vertexes, 4); shape.Tag = sprite; for (Scalar y = maxY; y > minY; y -= spacing) { AddShape(shape, 20, new ALVector2D(0, new Vector2D(x + offset, y))); //AddRectangle(size, size, 20, new ALVector2D(0, new Vector2D(x + offset, y))); offset = MathHelper.WrapClamp(offset + offsetchange, -offsetchange,offsetchange); } }
void DemoS() { BeginDemoChange(); Reset(); AddGravityField(); AddFloor(new ALVector2D(0, new Vector2D(700, 750))); Sprite blockSprite = GetSprite("fighter.png"); Vector2D[][] vertexes = blockSprite.Polygons; MultiPolygonShape shape = new MultiPolygonShape(vertexes, 4); shape.Tag = blockSprite; for (int i = 128 * 3; i > -128; i -= 128) { Body b = AddShape(shape, 40, new ALVector2D(0, new Vector2D(600, 272 + i))); } Body lever = AddRectangle(20, 100, 9, new ALVector2D(0, 275, 300)); FixedAngleJoint joint1 = new FixedAngleJoint(lever, new Lifespan()); FixedHingeJoint joint2 = new FixedHingeJoint(lever, new Vector2D(200, 300), new Lifespan()); joint1.Softness = .95f; engine.AddJointRange(new Joint[] { joint1, joint2 }); lever = AddRectangle(20, 100, 9, new ALVector2D(0, 385, 300)); joint1 = new FixedAngleJoint(lever, new Lifespan()); joint2 = new FixedHingeJoint(lever, new Vector2D(460, 300), new Lifespan()); joint1.Softness = .95f; engine.AddJointRange(new Joint[] { joint1, joint2 }); Body ball = AddShape(new CircleShape(80, 20), 4000, new ALVector2D(0, new Vector2D(1028, 272))); ball.Transformation *= Matrix2x3.FromScale(new Vector2D(1, .8f)); EndDemoChange(); /* BeginDemoChange(); Reset(false); AddGravityField(); engine.AddLogic(new GlobalFluidLogic(.45f, .02f, Vector2D.Zero, new Lifespan())); BoundingRectangle rect = this.clippersShape.Rectangle; rect.Min.X -= 75; rect.Min.Y -= 75; rect.Max.X += 75; rect.Max.Y += 75; AddShell(rect, 100, Scalar.PositiveInfinity).ForEach(delegate(Body b) { b.IgnoresGravity = true; }); Sprite blockSprite = GetSprite("fighter.png"); Vector2D[][] vertexes = blockSprite.Polygons; MultipartPolygon shape = new MultipartPolygon(vertexes, 4); shape.Tag = blockSprite; AddShape(shape, 40, new ALVector2D(0, new Vector2D(200, 300))); AddShape(shape, 40, new ALVector2D(0, new Vector2D(500, 300))); AddRectangle(50, 50, 50, new ALVector2D(0, 600, 600)); AddRectangle(50, 50, 500, new ALVector2D(0, 600, 450)); AddCircle(10, 9, 5, new ALVector2D(0, 600, 600)); AddCircle(10, 9, 50, new ALVector2D(0, 500, 600)); AddCircle(10, 9, 500, new ALVector2D(0, 400, 600)); AddCircle(10, 9, 5000, new ALVector2D(0, 300, 600)); EndDemoChange();*/ }
void DemoA() { BeginDemoChange(); Reset(false); AddGravityField(); Body body1IT; Body body2IT; body1IT = AddFloor(new ALVector2D(0, new Vector2D(700, 750))); Sprite blockSprite = GetSprite("fighter.png"); Vector2D[][] vertexes = blockSprite.Polygons; MultiPolygonShape shape = new MultiPolygonShape(vertexes, 4); shape.Tag = blockSprite; // body1IT = AddShape(shape, 40, new ALVector2D(0, new Vector2D(200, 300))); body2IT = AddShape(shape, 40, new ALVector2D(0, new Vector2D(300, 300))); AdvGroupIgnorer ignore1 = new AdvGroupIgnorer(); AdvGroupIgnorer ignore2 = new AdvGroupIgnorer(); ignore1.IsInverted = true; ignore2.IsInverted = true; //ignore1.Groups.Add(1); //ignore2.IgnoredGroups.Add(1); ignore2.Groups.Add(1); ignore1.IgnoredGroups.Add(1); body1IT.EventIgnorer = ignore1; body2IT.EventIgnorer = ignore2; body1IT.Collided += new EventHandler<CollisionEventArgs>(body2_Collided); //body2IT.Collided += new EventHandler<CollisionEventArgs>(body2_Collided); EndDemoChange(); }
void DemoI() { BeginDemoChange(); Reset(false); Sprite blockSprite = GetSprite("fighter.png"); Vector2D[][] vertexes = blockSprite.Polygons; MultiPolygonShape shape = new MultiPolygonShape(vertexes, 4); shape.Tag = blockSprite; for (Scalar x = 100; x < 900; x += 200) { for (Scalar y = 100; y < 900; y += 200) { Body c = AddShape(shape, 40, new ALVector2D(0, new Vector2D(x,y))); c.Updated += new EventHandler<UpdatedEventArgs>(DemoI_Body_Updated); } } EndDemoChange(); }
/* void Demo1() { BeginDemoChange(); Reset(); Body ball1 = AddShape(new CircleShape(80, 20), 4000, new ALVector2D(0, new Vector2D(400, 400))); Body ball2 = AddShape(new CircleShape(80, 20), 4000, new ALVector2D(0, new Vector2D(200, 400))); ball2.State.Velocity.Linear = new Vector2D(100, 0); ball1.State.Velocity.Linear = new Vector2D(-10, 0); Matrix2x3 mat = Matrix2x3.FromRotationZ(1) * Matrix2x3.FromScale(new Vector2D(.2f, .5f)) * Matrix2x3.FromRotationZ(-1); Matrix2x3 mat2 = Matrix2x3.FromScale(new Vector2D(.2f, .5f)); ball1.Transformation *= mat; ball2.Transformation *= mat; EndDemoChange(); }*/ void Demo1() { BeginDemoChange(); Reset(); AddGravityField(); AddFloor(new ALVector2D(0, new Vector2D(700, 750))); Sprite blockSprite = GetSprite("fighter.png"); Vector2D[][] vertexes = blockSprite.Polygons; MultiPolygonShape shape = new MultiPolygonShape(vertexes, 4); shape.Tag = blockSprite; for (int i = 128 * 3; i > -128; i -= 128) { Body b = AddShape(shape, 40, new ALVector2D(1, new Vector2D(600, 272 + i))); //b.Transformation *= Matrix2x3.FromScale(new Vector2D(1, .5f)); } Body b1 = AddShape(shape, 40, new ALVector2D(1, new Vector2D(400, 400))); Body b2 = AddShape(shape, 40, new ALVector2D(1, new Vector2D(400, 200))); //b1.LinearDamping = .9f; // b2.LinearDamping = .9f; //engine.AddProxy(b1, b2, Matrix2x2.FromRotation(MathHelper.PiOver2 )); b1.Transformation *= Matrix2x3.FromScale(new Vector2D(1, 1.8f)); b2.Transformation *= Matrix2x3.FromScale(new Vector2D(.9f, 1)); Body line = AddLine(new Vector2D(200, 400), new Vector2D(400, 400), 20); line.IgnoresGravity = true; line.CollisionIgnorer = new OneWayPlatformIgnorer(-Vector2D.YAxis, 10); Body ball = AddShape(new CircleShape(80, 20), 4000, new ALVector2D(0, new Vector2D(1028, 272))); ball.Transformation *= Matrix2x3.FromRotationZ(1) * Matrix2x3.FromScale(new Vector2D(.2f, .5f)) * Matrix2x3.FromRotationZ(-1); EndDemoChange(); }
void AddRandom_StressTest() { Scalar box = 900; Scalar xmin = -box; Scalar xmax = box; Scalar ymin = -box; Scalar ymax = box; Scalar size = 32; Scalar spacing = 2; Sprite sprite = GetSprite("block.png"); Vector2D[][] vertexes = sprite.Polygons; MultiPolygonShape shape = new MultiPolygonShape(vertexes, 4); shape.Tag = sprite; List<Body> bodies = new List<Body>(); for (Scalar x = xmin; x < xmax; x += spacing + size) { for (Scalar y = ymax; y > ymin; y -= spacing + size) { Body e = new Body( new PhysicsState(new ALVector2D(0, new Vector2D(x + (Scalar)rand.NextDouble() * 900000, y + (Scalar)rand.NextDouble() * 900000))), shape, 20, coefficients.Duplicate(), new Lifespan()); bodies.Add(e); } } AddGlObjectRange(bodies); engine.AddBodyRange(bodies); }
void AddDiaganol_StressTest() { Scalar size = 32; Scalar spacing = 3f; Scalar ymin = -90000; Scalar ymax = 90000; Sprite sprite = GetSprite("block.png"); Vector2D[][] vertexes = sprite.Polygons; MultiPolygonShape shape =new MultiPolygonShape(vertexes, 4); shape.Tag = sprite; for (Scalar y = ymax; y >= ymin; y -= (spacing + size)) { AddShape(shape, 20, new ALVector2D(0, new Vector2D(y, y))); } }
void AddTower2() { Scalar size = 32; Scalar x = 500; Scalar spacing = size + 2; Sprite sprite = GetSprite("block.png"); Vector2D[][] vertexes = sprite.Polygons; MultiPolygonShape shape = new MultiPolygonShape(vertexes, 4); shape.Tag = sprite; Scalar minY = 100; Scalar maxY = 400 - size / 2; for (Scalar y = maxY; y > minY; y -= spacing) { AddShape(shape, 20, new ALVector2D(rand.Next(-10, 0) * .1f, new Vector2D(x + rand.Next(-90, 90) * .1f, y))); } }
public static IShape CreateColoredMultiPolygon(Vector2D[][] polygons, Scalar gridSpacing) { if (polygons.Length == 1) { return CreateColoredPolygon(polygons[0], gridSpacing); } else { MultiPolygonShape shape = new MultiPolygonShape(polygons, gridSpacing); ScalarColor3[][] colors = new ScalarColor3[polygons.Length][]; Vector2D[][] reduced = new Vector2D[polygons.Length][]; for (int index = 0; index < polygons.Length; ++index) { reduced[index] = VertexHelper.Reduce(polygons[index]); colors[index] = CreateColor3Array(reduced[index].Length); } shape.Tag = DrawableFactory.CreateMultiPolygon(reduced, colors); return shape; } }
public static IShape CreateSprite(SurfacePolygons surfacePolygons, Surface bumpmap, bool xInverted, bool yInverted, int reduce, Scalar subdivide, Scalar gridSpacing, Light light) { Vector2D[][] polygons = surfacePolygons.Polygons; for (int index = 1; index < reduce; index++) { polygons = VertexHelper.ReduceRange(polygons, index); } polygons = VertexHelper.SubdivideRange(polygons, subdivide); Vector2D centroid = surfacePolygons.Offset; IShape shape; if (polygons.Length == 1) { shape = new PolygonShape(polygons[0], gridSpacing); } else { shape = new MultiPolygonShape(polygons, gridSpacing); } shape.Tag = DrawableFactory.CreateSprite(surfacePolygons.Surface, bumpmap,xInverted,yInverted, centroid,light); return shape; }
void DemoD() { BeginDemoChange(); Reset(false); hasWater = true; //engine.AddLogic(new GlobalFluidLogic(2.95f, .02f, new Vector2D(0, 0), new Lifespan())); engine.AddLogic(new LineFluidLogic(new Line(new Vector2D(0, 400),new Vector2D(6, 400)), 1.95f, .02f, new Vector2D(0, 0), new Lifespan())); // engine.AddLogic(new LineFluidLogic(new Line(0, -1, -400), 1.95f, .02f, new Vector2D(0, 0), new Lifespan())); AddGravityField(); BoundingRectangle rect = this.clipper.Rectangle; rect.Min.X -= 75; rect.Min.Y -= 75; rect.Max.X += 75; rect.Max.Y += 75; AddShell(rect, 100, Scalar.PositiveInfinity).ForEach(delegate(Body b) { b.IgnoresGravity = true; }); // AddPyramid(); Sprite blockSprite = GetSprite("fighter.png"); Vector2D[][] vertexes = blockSprite.Polygons; MultiPolygonShape shape = new MultiPolygonShape(vertexes, 4); shape.Tag = blockSprite; //AddShape(shape, 200, new ALVector2D(0, new Vector2D(200, 300))); AddShape(shape, 100, new ALVector2D(0, new Vector2D(500, 300))); AddRectangle(50, 200, 25, new ALVector2D(0, 600, 600)); AddRectangle(50, 100, 50, new ALVector2D(0, 200, 400)); // AddRectangle(50, 200, 50, new ALVector2D(0, 500, 600)); //AddRectangle(50, 50, 50, new ALVector2D(0, 600, 600)); //AddRectangle(50, 50, 100, new ALVector2D(0, 600, 450)); //AddCircle(30, 9, 5, new ALVector2D(0, 600, 200)); //AddCircle(25, 9, 25, new ALVector2D(0, 600, 200)); //AddCircle(20, 9, 50, new ALVector2D(0, 500, 200)); EndDemoChange(); }