public static Body AddRectangle(DemoOpenInfo info, Scalar height, Scalar width, Scalar mass, ALVector2D position) { Vector2D[] vertexes = VertexHelper.CreateRectangle(width, height); vertexes = VertexHelper.Subdivide(vertexes, Math.Min(height, width) / 5); IShape boxShape = ShapeFactory.CreateColoredPolygon(vertexes, Math.Min(height, width) / 5); Body body = new Body(new PhysicsState(position), boxShape, mass, Coefficients.Duplicate(), new Lifespan()); info.Scene.AddGraphic(CreateGraphic(body)); return(body); }
public static Body AddFloor(DemoOpenInfo info, ALVector2D position) { Scalar height = 60; Scalar width = 2000; Vector2D[] vertexes = VertexHelper.CreateRectangle(width, height); IShape boxShape = ShapeFactory.CreateColoredPolygon(vertexes, Math.Min(height, width) / 5); Body body = new Body(new PhysicsState(position), boxShape, Scalar.PositiveInfinity, Coefficients.Duplicate(), new Lifespan()); body.IgnoresGravity = true; info.Scene.AddGraphic(CreateGraphic(body)); return(body); }
protected override void Open() { dispose += DemoHelper.BasicDemoSetup(DemoInfo); Vector2D gravityCenter = new Vector2D(500, 500); Scalar gravityPower = 200; Scene.Engine.AddLogic(new GravityPointField(gravityCenter, gravityPower, new Lifespan())); DemoHelper.AddRagDoll(DemoInfo, gravityCenter + new Vector2D(0, -20)); Scalar length = 41; Scalar size = 8 ; bool reverse = false; for (Scalar distance = 250; distance < 650; length += 10, size *= 2, distance += 60 + length) { Scalar da = MathHelper.TwoPi / size;// ((MathHelper.TWO_PI * distance) / size); Scalar l2 = length / 2; // da /= 2; Vector2D[] vertexes = new Vector2D[] { Vector2D.FromLengthAndAngle(distance - l2, da / 2), Vector2D.FromLengthAndAngle(distance - l2, -da / 2), Vector2D.FromLengthAndAngle(distance + l2, -da / 2), Vector2D.FromLengthAndAngle(distance + l2, da / 2), }; //da *= 2; Vector2D[] vertexes2 = VertexHelper.CenterVertexes(vertexes); vertexes = VertexHelper.Subdivide(vertexes2, 5); PolygonShape shape = ShapeFactory.CreateColoredPolygon(vertexes, 1.5f); for (Scalar angle = 0; angle < MathHelper.TwoPi; angle += da) { Vector2D position = Vector2D.FromLengthAndAngle(distance, angle) + gravityCenter; Body body = DemoHelper.AddShape(DemoInfo, shape, (size * length) / 10, new ALVector2D(angle, position)); body.State.Velocity.Linear = DemoHelper.GetOrbitVelocity(gravityCenter, Vector2D.FromLengthAndAngle(distance - length, angle) + gravityCenter, gravityPower); body.State.Velocity.Linear *= .5f; body.State.Velocity.Angular = -(body.State.Velocity.Linear.Magnitude) / (distance);// *(1 / MathHelper.TWO_PI); if (reverse) { body.State.Velocity.Linear = -body.State.Velocity.Linear; body.State.Velocity.Angular = -body.State.Velocity.Angular; } } reverse = !reverse; } }
protected override void Open() { dispose += DemoHelper.BasicDemoSetup(DemoInfo); Scene.Engine.AddLogic(new GravityField(new Vector2D(0, 1000), new Lifespan())); SurfacePolygons surfacePolygons = Cache <SurfacePolygons> .GetItem("physicsplayGround.png"); foreach (Vector2D[] vertexes in surfacePolygons.Polygons) { Vector2D[] processed = vertexes; for (int index = 1; index < 4; index++) { processed = VertexHelper.Reduce(processed, index); } processed = VertexHelper.Subdivide(processed, 16); IShape shape = ShapeFactory.CreateColoredPolygon(processed, 10); DemoHelper.AddShape(DemoInfo, shape, Scalar.PositiveInfinity, new ALVector2D(0, surfacePolygons.Offset)).IgnoresGravity = true; } for (int x = 440; x < 480; x += 10) { for (int y = -2000; y < 0; y += 12) { Body body = DemoHelper.AddCircle(DemoInfo, 5, 7, 3, new ALVector2D(0, x + DemoHelper.NextScalar(-400, 400), y)); body.Updated += delegate(object sender, UpdatedEventArgs e) { if (body.State.Position.Linear.Y > 900) { body.State.Position.Linear.Y = -100; } }; } } for (int x = 490; x < 510; x += 10) { for (int y = -550; y < -500; y += 12) { Body body = DemoHelper.AddRectangle(DemoInfo, 10, 20, 10, new ALVector2D(0, x + DemoHelper.NextScalar(-400, 400), y)); body.Updated += delegate(object sender, UpdatedEventArgs e) { if (body.State.Position.Linear.Y > 900) { body.State.Position.Linear.Y = -100; } }; } } }
public static Body AddLine(DemoOpenInfo info, Vector2D point1, Vector2D point2, Scalar thickness, Scalar mass) { Vector2D line = point1 - point2; Vector2D avg = (point1 + point2) * .5f; Scalar length = line.Magnitude; Scalar angle = line.Angle; Scalar Hd2 = thickness * .5f; Scalar Wd2 = length * .5f; int curveEdgeCount = 5; Scalar da = MathHelper.Pi / curveEdgeCount; List <Vector2D> vertexes = new List <Vector2D>(); vertexes.Add(new Vector2D(Wd2, Hd2)); vertexes.Add(new Vector2D(-Wd2, Hd2)); for (Scalar angle2 = MathHelper.PiOver2 + da; angle2 < MathHelper.ThreePiOver2; angle2 += da) { vertexes.Add(new Vector2D(-Wd2, 0) + Vector2D.FromLengthAndAngle(Hd2, angle2)); } vertexes.Add(new Vector2D(-Wd2, -Hd2)); vertexes.Add(new Vector2D(Wd2, -Hd2)); for (Scalar angle2 = -MathHelper.PiOver2 + da; angle2 < MathHelper.PiOver2; angle2 += da) { vertexes.Add(new Vector2D(Wd2, 0) + Vector2D.FromLengthAndAngle(Hd2, angle2)); } IShape shape = ShapeFactory.CreateColoredPolygon(vertexes.ToArray(), thickness / 4); Body body = new Body( new PhysicsState(new ALVector2D(0, avg)), shape, mass, Coefficients.Duplicate(), new Lifespan()); body.Transformation = Matrix2x3.FromRotationZ(angle); body.ApplyPosition(); info.Scene.AddGraphic(CreateGraphic(body)); return(body); }
/* public static List<Body> AddSpringChain(DemoOpenInfo info, Vector2D position, Scalar boxLenght, Scalar boxWidth, Scalar boxMass, Scalar spacing, Scalar length) * { * List<Body> bodies = new List<Body>(); * Body last = null; * for (Scalar x = 0; x < length; x += boxLenght + spacing, position.X += boxLenght + spacing) * { * Body current = AddRectangle(info, boxWidth, boxLenght, boxMass, new ALVector2D(0, position)); * bodies.Add(current); * if (last != null) * { * * SpringJoint joint = new SpringJoint(last, Vector2D.Zero, current, Vector2D.Zero, 100, 100, new Lifespan()); * info.Scene.Engine.AddJoint(joint); * } * last = current; * } * return bodies; * }*/ public static List <Body> AddRagDoll(DemoOpenInfo info, Vector2D location) { List <Body> result = new List <Body>(); Scalar mass = 10; Body head = AddCircle(info, 12, 9, mass, new ALVector2D(0, location + new Vector2D(0, 0))); Scalar Ld2 = 50 / 2; Scalar Wd2 = 25 / 2; Vector2D[] vertexes = new Vector2D[] { new Vector2D(Wd2, Ld2), new Vector2D(5, Ld2 + 7), new Vector2D(-5, Ld2 + 7), new Vector2D(-Wd2, Ld2), new Vector2D(-Wd2, 0), new Vector2D(-(Wd2 + 4), -Ld2 / 2 + 6), new Vector2D(-Wd2 + 2, -Ld2), new Vector2D(0, -Ld2), new Vector2D(Wd2 - 2, -Ld2), new Vector2D(Wd2 + 4, -Ld2 / 2 + 6), new Vector2D(Wd2, 0), }; IShape shape = ShapeFactory.CreateColoredPolygon(vertexes, 5); Body torso = AddShape(info, shape, mass * 4, new ALVector2D(0, location + new Vector2D(0, 40))); Body ltarm = AddRectangle(info, 10, 30, mass, new ALVector2D(0, location + new Vector2D(-30, 20))); Body lbarm = AddRectangle(info, 10, 30, mass, new ALVector2D(0, location + new Vector2D(-65, 20))); Body rtarm = AddRectangle(info, 10, 30, mass, new ALVector2D(0, location + new Vector2D(30, 20))); Body rbarm = AddRectangle(info, 10, 30, mass, new ALVector2D(0, location + new Vector2D(65, 20))); Body ltleg = AddRectangle(info, 40, 15, mass * 2, new ALVector2D(.06f, location + new Vector2D(-10, 95))); Body lbleg = AddRectangle(info, 40, 15, mass * 2, new ALVector2D(0, location + new Vector2D(-11, 140))); Body rtleg = AddRectangle(info, 40, 15, mass * 1.5f, new ALVector2D(-.06f, location + new Vector2D(10, 95))); Body rbleg = AddRectangle(info, 40, 15, mass * 1.5f, new ALVector2D(0, location + new Vector2D(11, 140))); result.Add(head); result.Add(torso); result.Add(ltarm); result.Add(lbarm); result.Add(rtarm); result.Add(rbarm); result.Add(ltleg); result.Add(lbleg); result.Add(rtleg); result.Add(rbleg); HingeJoint neck = new HingeJoint(head, torso, location + new Vector2D(0, 15), new Lifespan()); HingeJoint lshoulder = new HingeJoint(ltarm, torso, location + new Vector2D(-18, 20), new Lifespan()); HingeJoint lelbow = new HingeJoint(ltarm, lbarm, location + new Vector2D(-47, 20), new Lifespan()); HingeJoint rshoulder = new HingeJoint(rtarm, torso, location + new Vector2D(18, 20), new Lifespan()); HingeJoint relbow = new HingeJoint(rtarm, rbarm, location + new Vector2D(47, 20), new Lifespan()); HingeJoint lhip = new HingeJoint(ltleg, torso, location + new Vector2D(-8, 72), new Lifespan()); HingeJoint lknee = new HingeJoint(ltleg, lbleg, location + new Vector2D(-11, 115), new Lifespan()); HingeJoint rhip = new HingeJoint(rtleg, torso, location + new Vector2D(8, 72), new Lifespan()); HingeJoint rknee = new HingeJoint(rtleg, rbleg, location + new Vector2D(11, 115), new Lifespan()); List <Joint> joints = new List <Joint>(); joints.Add(neck); joints.Add(lelbow); joints.Add(rshoulder); joints.Add(relbow); joints.Add(lshoulder); joints.Add(lhip); joints.Add(lknee); joints.Add(rhip); joints.Add(rknee); foreach (HingeJoint joint in joints) { joint.DistanceTolerance = 10; } info.Scene.Engine.AddJointRange(joints); return(result); }