void CreateLeg(float s, Vector2 wheelAnchor) { Vector2 p1 = new Vector2(5.4f * s, -6.1f); Vector2 p2 = new Vector2(7.2f * s, -1.2f); Vector2 p3 = new Vector2(4.3f * s, -1.9f); Vector2 p4 = new Vector2(3.1f * s, 0.8f); Vector2 p5 = new Vector2(6.0f * s, 1.5f); Vector2 p6 = new Vector2(2.5f * s, 3.7f); FixtureDef fd1 = new FixtureDef(); FixtureDef fd2 = new FixtureDef(); fd1.filter.groupIndex = -1; fd2.filter.groupIndex = -1; fd1.density = 1.0f; fd2.density = 1.0f; PolygonShape poly1 = new PolygonShape(); PolygonShape poly2 = new PolygonShape(); Vector2[] vertices = new Vector2[3]; if (s > 0.0f) { vertices[0] = p1; vertices[1] = p2; vertices[2] = p3; poly1.Set(vertices, 3); vertices[0] = Vector2.Zero; vertices[1] = p5 - p4; vertices[2] = p6 - p4; poly2.Set(vertices, 3); } else { vertices[0] = p1; vertices[1] = p3; vertices[2] = p2; poly1.Set(vertices, 3); vertices[0] = Vector2.Zero; vertices[1] = p6 - p4; vertices[2] = p5 - p4; poly2.Set(vertices, 3); } fd1.shape = poly1; fd2.shape = poly2; BodyDef bd1 = new BodyDef(); BodyDef bd2 = new BodyDef(); bd1.type = BodyType.Dynamic; bd2.type = BodyType.Dynamic; bd1.position = _offset; bd2.position = p4 + _offset; bd1.angularDamping = 10.0f; bd2.angularDamping = 10.0f; Body body1 = _world.CreateBody(bd1); Body body2 = _world.CreateBody(bd2); body1.CreateFixture(fd1); body2.CreateFixture(fd2); DistanceJointDef djd = new DistanceJointDef(); // Using a soft distanceraint can reduce some jitter. // It also makes the structure seem a bit more fluid by // acting like a suspension system. djd.dampingRatio = 0.5f; djd.frequencyHz = 10.0f; djd.Initialize(body1, body2, p2 + _offset, p5 + _offset); _world.CreateJoint(djd); djd.Initialize(body1, body2, p3 + _offset, p4 + _offset); _world.CreateJoint(djd); djd.Initialize(body1, _wheel, p3 + _offset, wheelAnchor + _offset); _world.CreateJoint(djd); djd.Initialize(body2, _wheel, p6 + _offset, wheelAnchor + _offset); _world.CreateJoint(djd); RevoluteJointDef rjd = new RevoluteJointDef(); rjd.Initialize(body2, _chassis, p4 + _offset); _world.CreateJoint(rjd); }
public Dominos() { Body b1 = null; { PolygonShape shape = new PolygonShape(); shape.SetAsEdge(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); BodyDef bd = new BodyDef(); b1 = _world.CreateBody(bd); b1.CreateFixture(shape, 0.0f); } { PolygonShape shape = new PolygonShape(); shape.SetAsBox(6.0f, 0.25f); BodyDef bd = new BodyDef(); bd.position = new Vector2(-1.5f, 10.0f); Body ground = _world.CreateBody(bd); ground.CreateFixture(shape, 0.0f); } { PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.1f, 1.0f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f; fd.friction = 0.1f; for (int i = 0; i < 10; ++i) { BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(-6.0f + 1.0f * i, 11.25f); Body body = _world.CreateBody(bd); body.CreateFixture(fd); } } { PolygonShape shape = new PolygonShape(); shape.SetAsBox(7.0f, 0.25f, Vector2.Zero, 0.3f); BodyDef bd = new BodyDef(); bd.position = new Vector2(1.0f, 6.0f); Body ground = _world.CreateBody(bd); ground.CreateFixture(shape, 0.0f); } Body b2 = null; { PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.25f, 1.5f); BodyDef bd = new BodyDef(); bd.position = new Vector2(-7.0f, 4.0f); b2 = _world.CreateBody(bd); b2.CreateFixture(shape, 0.0f); } Body b3 = null; { PolygonShape shape = new PolygonShape(); shape.SetAsBox(6.0f, 0.125f); BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(-0.9f, 1.0f); bd.angle = -0.15f; b3 = _world.CreateBody(bd); b3.CreateFixture(shape, 10.0f); } RevoluteJointDef jd = new RevoluteJointDef(); Vector2 anchor; anchor = new Vector2(-2.0f, 1.0f); jd.Initialize(b1, b3, anchor); jd.collideConnected = true; _world.CreateJoint(jd); Body b4 = null; { PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.25f, 0.25f); BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(-10.0f, 15.0f); b4 = _world.CreateBody(bd); b4.CreateFixture(shape, 10.0f); } anchor = new Vector2(-7.0f, 15.0f); jd.Initialize(b2, b4, anchor); _world.CreateJoint(jd); Body b5 = null; { BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(6.5f, 3.0f); b5 = _world.CreateBody(bd); PolygonShape shape = new PolygonShape(); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 10.0f; fd.friction = 0.1f; shape.SetAsBox(1.0f, 0.1f, new Vector2(0.0f, -0.9f), 0.0f); b5.CreateFixture(fd); shape.SetAsBox(0.1f, 1.0f, new Vector2(-0.9f, 0.0f), 0.0f); b5.CreateFixture(fd); shape.SetAsBox(0.1f, 1.0f, new Vector2(0.9f, 0.0f), 0.0f); b5.CreateFixture(fd); } anchor = new Vector2(6.0f, 2.0f); jd.Initialize(b1, b5, anchor); _world.CreateJoint(jd); Body b6 = null; { PolygonShape shape = new PolygonShape(); shape.SetAsBox(1.0f, 0.1f); BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(6.5f, 4.1f); b6 = _world.CreateBody(bd); b6.CreateFixture(shape, 30.0f); } anchor = new Vector2(7.5f, 4.0f); jd.Initialize(b5, b6, anchor); _world.CreateJoint(jd); Body b7 = null; { PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.1f, 1.0f); BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(7.4f, 1.0f); b7 = _world.CreateBody(bd); b7.CreateFixture(shape, 10.0f); } DistanceJointDef djd = new DistanceJointDef(); djd.bodyA = b3; djd.bodyB = b7; djd.localAnchorA = new Vector2(6.0f, 0.0f); djd.localAnchorB = new Vector2(0.0f, -1.0f); Vector2 d = djd.bodyB.GetWorldPoint(djd.localAnchorB) - djd.bodyA.GetWorldPoint(djd.localAnchorA); djd.length = d.Length(); _world.CreateJoint(djd); { float radius = 0.2f; CircleShape shape = new CircleShape(); shape._radius = radius; for (int i = 0; i < 4; ++i) { BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(5.9f + 2.0f * radius * i, 2.4f); Body body = _world.CreateBody(bd); body.CreateFixture(shape, 10.0f); } } }
Web() { Body ground = null; { BodyDef bd = new BodyDef(); ground = _world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsEdge(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } { PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.5f, 0.5f); BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(-5.0f, 5.0f); _bodies[0] = _world.CreateBody(bd); _bodies[0].CreateFixture(shape, 5.0f); bd.position = new Vector2(5.0f, 5.0f); _bodies[1] = _world.CreateBody(bd); _bodies[1].CreateFixture(shape, 5.0f); bd.position = new Vector2(5.0f, 15.0f); _bodies[2] = _world.CreateBody(bd); _bodies[2].CreateFixture(shape, 5.0f); bd.position = new Vector2(-5.0f, 15.0f); _bodies[3] = _world.CreateBody(bd); _bodies[3].CreateFixture(shape, 5.0f); DistanceJointDef jd = new DistanceJointDef(); Vector2 p1, p2, d; jd.frequencyHz = 4.0f; jd.dampingRatio = 0.5f; jd.bodyA = ground; jd.bodyB = _bodies[0]; jd.localAnchorA = new Vector2(-10.0f, 0.0f); jd.localAnchorB = new Vector2(-0.5f, -0.5f); p1 = jd.bodyA.GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB.GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); _joints[0] = _world.CreateJoint(jd); jd.bodyA = ground; jd.bodyB = _bodies[1]; jd.localAnchorA = new Vector2(10.0f, 0.0f); jd.localAnchorB = new Vector2(0.5f, -0.5f); p1 = jd.bodyA.GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB.GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); _joints[1] = _world.CreateJoint(jd); jd.bodyA = ground; jd.bodyB = _bodies[2]; jd.localAnchorA = new Vector2(10.0f, 20.0f); jd.localAnchorB = new Vector2(0.5f, 0.5f); p1 = jd.bodyA.GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB.GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); _joints[2] = _world.CreateJoint(jd); jd.bodyA = ground; jd.bodyB = _bodies[3]; jd.localAnchorA = new Vector2(-10.0f, 20.0f); jd.localAnchorB = new Vector2(-0.5f, 0.5f); p1 = jd.bodyA.GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB.GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); _joints[3] = _world.CreateJoint(jd); jd.bodyA = _bodies[0]; jd.bodyB = _bodies[1]; jd.localAnchorA = new Vector2(0.5f, 0.0f); jd.localAnchorB = new Vector2(-0.5f, 0.0f);; p1 = jd.bodyA.GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB.GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); _joints[4] = _world.CreateJoint(jd); jd.bodyA = _bodies[1]; jd.bodyB = _bodies[2]; jd.localAnchorA = new Vector2(0.0f, 0.5f); jd.localAnchorB = new Vector2(0.0f, -0.5f); p1 = jd.bodyA.GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB.GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); _joints[5] = _world.CreateJoint(jd); jd.bodyA = _bodies[2]; jd.bodyB = _bodies[3]; jd.localAnchorA = new Vector2(-0.5f, 0.0f); jd.localAnchorB = new Vector2(0.5f, 0.0f); p1 = jd.bodyA.GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB.GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); _joints[6] = _world.CreateJoint(jd); jd.bodyA = _bodies[3]; jd.bodyB = _bodies[0]; jd.localAnchorA = new Vector2(0.0f, -0.5f); jd.localAnchorB = new Vector2(0.0f, 0.5f); p1 = jd.bodyA.GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB.GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); _joints[7] = _world.CreateJoint(jd); } }
public WorldData Deserialize(Stream stream) { XMLFragmentElement root = XMLFragmentParser.LoadFromStream(stream); if (root.Name.ToLower() != "world") throw new Exception(); WorldData data = new WorldData(); if (root.Attributes.Count == 0) throw new Exception("No version"); else if (int.Parse(root.Attributes[0].Value) != WorldXmlSerializer.XmlVersion) throw new Exception("Wrong version XML file"); data.Version = int.Parse(root.Attributes[0].Value); foreach (var main in root.Elements) { switch (main.Name.ToLower()) { case "gravity": { data.Gravity = ReadVector(main); } break; case "shapes": { foreach (var n in main.Elements) { if (n.Name.ToLower() != "shape") throw new Exception(); ShapeType type = (ShapeType)Enum.Parse(typeof(ShapeType), n.Attributes[0].Value, true); string name = ""; switch (type) { case ShapeType.Circle: { CircleShape shape = new CircleShape(); foreach (var sn in n.Elements) { switch (sn.Name.ToLower()) { case "name": name = sn.Value; break; case "radius": shape.Radius = float.Parse(sn.Value); break; case "position": shape.Position = ReadVector(sn); break; default: throw new Exception(); } } _shapes.Add(new ShapeSerialized(shape, name)); } break; case ShapeType.Polygon: { PolygonShape shape = new PolygonShape(); foreach (var sn in n.Elements) { switch (sn.Name.ToLower()) { case "name": name = sn.Value; break; case "vertices": { List<Vec2> verts = new List<Vec2>(); foreach (var vert in sn.Elements) verts.Add(ReadVector(vert)); shape.Vertices = verts.ToArray(); } break; case "centroid": shape.Centroid = ReadVector(sn); break; } } _shapes.Add(new ShapeSerialized(shape, name)); } break; } } } break; case "fixtures": { foreach (var n in main.Elements) { FixtureDef fixture = new FixtureDef(); if (n.Name.ToLower() != "fixture") throw new Exception(); string name = ""; int id = 0; foreach (var sn in n.Elements) { switch (sn.Name.ToLower()) { case "name": name = sn.Value; break; case "shape": id = int.Parse(sn.Value); break; case "density": fixture.Density = float.Parse(sn.Value); break; case "filterdata": fixture.Filter = (FilterData)ReadSimpleType(sn, typeof(FilterData), true); break; case "friction": fixture.Friction = float.Parse(sn.Value); break; case "issensor": fixture.IsSensor = bool.Parse(sn.Value); break; case "restitution": fixture.Restitution = float.Parse(sn.Value); break; case "userdata": fixture.UserData = ReadSimpleType(sn, null, false); break; } } fixture.Shape = _shapes[id].Shape; _fixtures.Add(new FixtureDefSerialized(fixture, id, name)); } } break; case "bodies": { foreach (var n in main.Elements) { BodyDef body = new BodyDef(); if (n.Name.ToLower() != "body") throw new Exception(); body.BodyType = (BodyType)Enum.Parse(typeof(BodyType), n.Attributes[0].Value, true); List<int> fixtures = new List<int>(); string name = ""; foreach (var sn in n.Elements) { switch (sn.Name.ToLower()) { case "name": name = sn.Value; break; case "active": body.Active = bool.Parse(sn.Value); break; case "allowsleep": body.AllowSleep = bool.Parse(sn.Value); break; case "angle": body.Angle = float.Parse(sn.Value); break; case "angulardamping": body.AngularDamping = float.Parse(sn.Value); break; case "angularvelocity": body.AngularVelocity = float.Parse(sn.Value); break; case "awake": body.Awake = bool.Parse(sn.Value); break; case "bullet": body.Bullet = bool.Parse(sn.Value); break; case "fixedrotation": body.FixedRotation = bool.Parse(sn.Value); break; case "inertiascale": body.InertiaScale = float.Parse(sn.Value); break; case "lineardamping": body.LinearDamping = float.Parse(sn.Value); break; case "linearvelocity": body.LinearVelocity = ReadVector(sn); break; case "position": body.Position = ReadVector(sn); break; case "userdata": body.UserData = ReadSimpleType(sn, null, false); break; case "fixtures": { foreach (var v in sn.Elements) fixtures.Add(int.Parse(v.Value)); break; } } } _bodies.Add(new BodyDefSerialized(null, body, fixtures, name)); } } break; case "joints": { foreach (var n in main.Elements) { JointDef mainDef = null; if (n.Name.ToLower() != "joint") throw new Exception(); JointType type = (JointType)Enum.Parse(typeof(JointType), n.Attributes[0].Value, true); int bodyA = -1, bodyB = -1; bool collideConnected = false; object userData = null; string name = ""; switch (type) { case JointType.Distance: mainDef = new DistanceJointDef(); break; case JointType.Friction: mainDef = new FrictionJointDef(); break; case JointType.Line: mainDef = new LineJointDef(); break; case JointType.Prismatic: mainDef = new PrismaticJointDef(); break; case JointType.Pulley: mainDef = new PulleyJointDef(); break; case JointType.Revolute: mainDef = new RevoluteJointDef(); break; case JointType.Weld: mainDef = new WeldJointDef(); break; default: throw new Exception("Invalid or unsupported joint"); } foreach (var sn in n.Elements) { // check for specific nodes switch (type) { case JointType.Distance: { switch (sn.Name.ToLower()) { case "dampingratio": ((DistanceJointDef)mainDef).DampingRatio = float.Parse(sn.Value); break; case "frequencyhz": ((DistanceJointDef)mainDef).FrequencyHz = float.Parse(sn.Value); break; case "length": ((DistanceJointDef)mainDef).Length = float.Parse(sn.Value); break; case "localanchora": ((DistanceJointDef)mainDef).LocalAnchorA = ReadVector(sn); break; case "localanchorb": ((DistanceJointDef)mainDef).LocalAnchorB = ReadVector(sn); break; } } break; case JointType.Friction: { switch (sn.Name.ToLower()) { case "localanchora": ((FrictionJointDef)mainDef).LocalAnchorA = ReadVector(sn); break; case "localanchorb": ((FrictionJointDef)mainDef).LocalAnchorB = ReadVector(sn); break; case "maxforce": ((FrictionJointDef)mainDef).MaxForce = float.Parse(sn.Value); break; case "maxtorque": ((FrictionJointDef)mainDef).MaxTorque = float.Parse(sn.Value); break; } } break; case JointType.Line: { switch (sn.Name.ToLower()) { case "enablelimit": ((LineJointDef)mainDef).EnableLimit = bool.Parse(sn.Value); break; case "enablemotor": ((LineJointDef)mainDef).EnableMotor = bool.Parse(sn.Value); break; case "localanchora": ((LineJointDef)mainDef).LocalAnchorA = ReadVector(sn); break; case "localanchorb": ((LineJointDef)mainDef).LocalAnchorB = ReadVector(sn); break; case "localaxisa": ((LineJointDef)mainDef).LocalAxisA = ReadVector(sn); break; case "maxmotorforce": ((LineJointDef)mainDef).MaxMotorForce = float.Parse(sn.Value); break; case "motorspeed": ((LineJointDef)mainDef).MotorSpeed = float.Parse(sn.Value); break; case "lowertranslation": ((LineJointDef)mainDef).LowerTranslation = float.Parse(sn.Value); break; case "uppertranslation": ((LineJointDef)mainDef).UpperTranslation = float.Parse(sn.Value); break; } } break; case JointType.Prismatic: { switch (sn.Name.ToLower()) { case "enablelimit": ((PrismaticJointDef)mainDef).EnableLimit = bool.Parse(sn.Value); break; case "enablemotor": ((PrismaticJointDef)mainDef).EnableMotor = bool.Parse(sn.Value); break; case "localanchora": ((PrismaticJointDef)mainDef).LocalAnchorA = ReadVector(sn); break; case "localanchorb": ((PrismaticJointDef)mainDef).LocalAnchorB = ReadVector(sn); break; case "localaxisa": ((PrismaticJointDef)mainDef).LocalAxis = ReadVector(sn); break; case "maxmotorforce": ((PrismaticJointDef)mainDef).MaxMotorForce = float.Parse(sn.Value); break; case "motorspeed": ((PrismaticJointDef)mainDef).MotorSpeed = float.Parse(sn.Value); break; case "lowertranslation": ((PrismaticJointDef)mainDef).LowerTranslation = float.Parse(sn.Value); break; case "uppertranslation": ((PrismaticJointDef)mainDef).UpperTranslation = float.Parse(sn.Value); break; case "referenceangle": ((PrismaticJointDef)mainDef).ReferenceAngle = float.Parse(sn.Value); break; } } break; case JointType.Pulley: { switch (sn.Name.ToLower()) { case "groundanchora": ((PulleyJointDef)mainDef).GroundAnchorA = ReadVector(sn); break; case "groundanchorb": ((PulleyJointDef)mainDef).GroundAnchorB = ReadVector(sn); break; case "lengtha": ((PulleyJointDef)mainDef).LengthA = float.Parse(sn.Value); break; case "lengthb": ((PulleyJointDef)mainDef).LengthB = float.Parse(sn.Value); break; case "localanchora": ((PulleyJointDef)mainDef).LocalAnchorA = ReadVector(sn); break; case "localanchorb": ((PulleyJointDef)mainDef).LocalAnchorB = ReadVector(sn); break; case "maxlengtha": ((PulleyJointDef)mainDef).MaxLengthA = float.Parse(sn.Value); break; case "maxlengthb": ((PulleyJointDef)mainDef).MaxLengthB = float.Parse(sn.Value); break; case "ratio": ((PulleyJointDef)mainDef).Ratio = float.Parse(sn.Value); break; } } break; case JointType.Revolute: { switch (sn.Name.ToLower()) { case "enablelimit": ((RevoluteJointDef)mainDef).EnableLimit = bool.Parse(sn.Value); break; case "enablemotor": ((RevoluteJointDef)mainDef).EnableMotor = bool.Parse(sn.Value); break; case "localanchora": ((RevoluteJointDef)mainDef).LocalAnchorA = ReadVector(sn); break; case "localanchorb": ((RevoluteJointDef)mainDef).LocalAnchorB = ReadVector(sn); break; case "maxmotortorque": ((RevoluteJointDef)mainDef).MaxMotorTorque = float.Parse(sn.Value); break; case "motorspeed": ((RevoluteJointDef)mainDef).MotorSpeed = float.Parse(sn.Value); break; case "lowerangle": ((RevoluteJointDef)mainDef).LowerAngle = float.Parse(sn.Value); break; case "upperangle": ((RevoluteJointDef)mainDef).UpperAngle = float.Parse(sn.Value); break; case "referenceangle": ((RevoluteJointDef)mainDef).ReferenceAngle = float.Parse(sn.Value); break; } } break; case JointType.Weld: { switch (sn.Name.ToLower()) { case "localanchora": ((WeldJointDef)mainDef).LocalAnchorA = ReadVector(sn); break; case "localanchorb": ((WeldJointDef)mainDef).LocalAnchorB = ReadVector(sn); break; } } break; case JointType.Gear: throw new Exception("Gear joint is unsupported"); } switch (sn.Name.ToLower()) { case "name": name = sn.Value; break; case "bodya": bodyA = int.Parse(sn.Value); break; case "bodyb": bodyB = int.Parse(sn.Value); break; case "collideconnected": collideConnected = bool.Parse(sn.Value); break; case "userdata": userData = ReadSimpleType(sn, null, false); break; } } mainDef.CollideConnected = collideConnected; mainDef.UserData = userData; _joints.Add(new JointDefSerialized(mainDef, bodyA, bodyB, name)); } } break; } } return data; }