// Use this for initialization protected override IntPtr Init() { FrictionJointDef jd = new FrictionJointDef(other.body, body.body); jd.Initialize(other.body, body.body, anchor); jd.maxForce = maxForce; jd.maxTorque = maxTorque; return(API.CreateFrictionJoint(B2DWorld.instance.world, jd)); }
public FrictionJoint(FrictionJointDef def) : base(def) { _localAnchorA = def.LocalAnchorA; _localAnchorB = def.LocalAnchorB; _maxForce = def.MaxForce; _maxTorque = def.MaxTorque; }
public Mirrors() { m_world.Gravity = new Vec2(0.0f, 0.0f); { Body body = m_world.CreateBody(new BodyDef()); } { float startY = 0; FrictionJointDef jd = new FrictionJointDef(); BodyDef bd = new BodyDef(); bd.BodyType = BodyType.Dynamic; PolygonShape shape = new PolygonShape(1.0f, 0.25f); for (int i = 0; i < 20; ++i) { bd.Position = new Vec2(0, startY); startY += 1.5f; FixtureDef fix = new FixtureDef(shape, 1.0f); var body = m_world.CreateBody(bd); var ff = body.CreateFixture(fix); float I = body.Inertia; float mass = body.Mass; float radius = (float)Math.Sqrt(2.0f * I / mass); jd.LocalAnchorA = jd.LocalAnchorB = Vec2.Empty; jd.BodyA = m_groundBody; jd.BodyB = body; jd.CollideConnected = true; jd.MaxForce = body.Mass * 10.0f; jd.MaxTorque = body.Mass * radius * 10.0f; m_world.CreateJoint(jd); } } }
public ApplyForce() { World.Gravity = new Vector2(0.0f, 0.0f); const float restitution = 0.4f; Body ground; { var bd = new BodyDef(); bd.Position.Set(0.0f, 20.0f); ground = World.CreateBody(bd); var shape = new EdgeShape(); var sd = new FixtureDef(); sd.Shape = shape; sd.Density = 0.0f; sd.Restitution = restitution; // Left vertical shape.Set(new Vector2(-20.0f, -20.0f), new Vector2(-20.0f, 20.0f)); ground.CreateFixture(sd); // Right vertical shape.Set(new Vector2(20.0f, -20.0f), new Vector2(20.0f, 20.0f)); ground.CreateFixture(sd); // Top horizontal shape.Set(new Vector2(-20.0f, 20.0f), new Vector2(20.0f, 20.0f)); ground.CreateFixture(sd); // Bottom horizontal shape.Set(new Vector2(-20.0f, -20.0f), new Vector2(20.0f, -20.0f)); ground.CreateFixture(sd); } { var xf1 = new Transform(); xf1.Rotation.Set(0.3524f * Settings.Pi); xf1.Position = xf1.Rotation.GetXAxis(); var vertices = new Vector2[3]; vertices[0] = MathUtils.Mul(xf1, new Vector2(-1.0f, 0.0f)); vertices[1] = MathUtils.Mul(xf1, new Vector2(1.0f, 0.0f)); vertices[2] = MathUtils.Mul(xf1, new Vector2(0.0f, 0.5f)); var poly1 = new PolygonShape(); poly1.Set(vertices); var sd1 = new FixtureDef(); sd1.Shape = poly1; sd1.Density = 2.0f; var xf2 = new Transform(); xf2.Rotation.Set(-0.3524f * Settings.Pi); xf2.Position = -xf2.Rotation.GetXAxis(); vertices[0] = MathUtils.Mul(xf2, new Vector2(-1.0f, 0.0f)); vertices[1] = MathUtils.Mul(xf2, new Vector2(1.0f, 0.0f)); vertices[2] = MathUtils.Mul(xf2, new Vector2(0.0f, 0.5f)); var poly2 = new PolygonShape(); poly2.Set(vertices); var sd2 = new FixtureDef(); sd2.Shape = poly2; sd2.Density = 2.0f; var bd = new BodyDef(); bd.BodyType = BodyType.DynamicBody; bd.Position = new Vector2(0.0f, 3.0f); bd.Angle = Settings.Pi; bd.AllowSleep = false; _body = World.CreateBody(bd); _body.CreateFixture(sd1); _body.CreateFixture(sd2); var gravity = 10.0f; var I = _body.Inertia; var mass = _body.Mass; // Compute an effective radius that can be used to // set the max torque for a friction joint // For a circle: I = 0.5 * m * r * r ==> r = sqrt(2 * I / m) var radius = (float)Math.Sqrt(2.0f * I / mass); FrictionJointDef jd = new FrictionJointDef(); jd.BodyA = ground; jd.BodyB = _body; jd.LocalAnchorA.SetZero(); jd.LocalAnchorB = _body.GetLocalCenter(); jd.CollideConnected = true; jd.MaxForce = 0.5f * mass * gravity; jd.MaxTorque = 0.2f * mass * radius * gravity; World.CreateJoint(jd); } { var shape = new PolygonShape(); shape.SetAsBox(0.5f, 0.5f); var fd = new FixtureDef(); fd.Shape = shape; fd.Density = 1.0f; fd.Friction = 0.3f; for (var i = 0; i < 10; ++i) { var bd = new BodyDef(); bd.BodyType = BodyType.DynamicBody; bd.Position.Set(0.0f, 7.0f + 1.54f * i); var body = World.CreateBody(bd); body.CreateFixture(fd); var gravity = 10.0f; var I = body.Inertia; var mass = body.Mass; // For a circle: I = 0.5 * m * r * r ==> r = sqrt(2 * I / m) var radius = (float)Math.Sqrt(2.0f * I / mass); var jd = new FrictionJointDef(); jd.LocalAnchorA.SetZero(); jd.LocalAnchorB.SetZero(); jd.BodyA = ground; jd.BodyB = body; jd.CollideConnected = true; jd.MaxForce = mass * gravity; jd.MaxTorque = 0.1f * mass * radius * gravity; World.CreateJoint(jd); } } }
public ApplyForce() { _world.Gravity = new Vector2(0.0, 0.0); const double k_restitution = 0.4; Body ground; { BodyDef bd = new BodyDef(); bd.position = new Vector2(0.0f, 20.0f); ground = _world.CreateBody(bd); PolygonShape shape = new PolygonShape(); FixtureDef sd = new FixtureDef(); sd.shape = shape; sd.density = 0.0f; sd.restitution = k_restitution; // Left vertical shape.SetAsEdge(new Vector2(-20.0f, -20.0f), new Vector2(-20.0f, 20.0f)); ground.CreateFixture(sd); // Right vertical shape.SetAsEdge(new Vector2(20.0f, -20.0f), new Vector2(20.0f, 20.0f)); ground.CreateFixture(sd); // Top horizontal shape.SetAsEdge(new Vector2(-20.0f, 20.0f), new Vector2(20.0f, 20.0f)); ground.CreateFixture(sd); // Bottom horizontal shape.SetAsEdge(new Vector2(-20.0f, -20.0f), new Vector2(20.0f, -20.0f)); ground.CreateFixture(sd); } { Alt.Box2D.Transform xf1 = new Alt.Box2D.Transform(); xf1.R.Set(0.3524 * Alt.Box2D.Settings.b2_pi); xf1.Position = MathUtils.Multiply(ref xf1.R, new Vector2(1.0f, 0.0f)); Vector2[] vertices = new Vector2[3]; vertices[0] = MathUtils.Multiply(ref xf1, new Vector2(-1.0f, 0.0f)); vertices[1] = MathUtils.Multiply(ref xf1, new Vector2(1.0f, 0.0f)); vertices[2] = MathUtils.Multiply(ref xf1, new Vector2(0.0f, 0.5f)); PolygonShape poly1 = new PolygonShape(); poly1.Set(vertices, 3); FixtureDef sd1 = new FixtureDef(); sd1.shape = poly1; sd1.density = 4.0f; Alt.Box2D.Transform xf2 = new Alt.Box2D.Transform(); xf2.R.Set(-0.3524 * Alt.Box2D.Settings.b2_pi); xf2.Position = MathUtils.Multiply(ref xf2.R, new Vector2(-1.0f, 0.0f)); vertices[0] = MathUtils.Multiply(ref xf2, new Vector2(-1.0f, 0.0f)); vertices[1] = MathUtils.Multiply(ref xf2, new Vector2(1.0f, 0.0f)); vertices[2] = MathUtils.Multiply(ref xf2, new Vector2(0.0f, 0.5f)); PolygonShape poly2 = new PolygonShape(); poly2.Set(vertices, 3); FixtureDef sd2 = new FixtureDef(); sd2.shape = poly2; sd2.density = 2.0f; BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.angularDamping = 5.0f; bd.linearDamping = 0.1f; bd.position = new Vector2(0.0f, 2.0f); bd.angle = Alt.Box2D.Settings.b2_pi; bd.allowSleep = false; _body = _world.CreateBody(bd); _body.CreateFixture(sd1); _body.CreateFixture(sd2); } { PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.5f, 0.5f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 1.0f; fd.friction = 0.3f; for (int i = 0; i < 10; ++i) { BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(0.0f, 5.0f + 1.54f * i); Body body = _world.CreateBody(bd); body.CreateFixture(fd); double gravity = 10.0f; double I = body.GetInertia(); double mass = body.GetMass(); // For a circle: I = 0.5 * m * r * r ==> r = sqrt(2 * I / m) double radius = (double)Math.Sqrt(2.0 * (double)(I / mass)); FrictionJointDef jd = new FrictionJointDef(); jd.localAnchorA = Vector2.Zero; jd.localAnchorB = Vector2.Zero; jd.bodyA = ground; jd.bodyB = body; jd.collideConnected = true; jd.maxForce = mass * gravity; jd.maxTorque = mass * radius * gravity; _world.CreateJoint(jd); } } }
private ApplyForceTest() { World.Gravity = Vector2.Zero; const float restitution = 0.4f; Body ground; { BodyDef bd = new BodyDef(); bd.Position = new Vector2(0.0f, 20.0f); ground = BodyFactory.CreateFromDef(World, bd); FixtureDef sd = new FixtureDef(); sd.Restitution = restitution; // Left vertical sd.Shape = new EdgeShape(new Vector2(-20.0f, -20.0f), new Vector2(-20.0f, 20.0f)); ground.AddFixture(sd); // Right vertical sd.Shape = new EdgeShape(new Vector2(20.0f, -20.0f), new Vector2(20.0f, 20.0f)); ground.AddFixture(sd); // Top horizontal sd.Shape = new EdgeShape(new Vector2(-20.0f, 20.0f), new Vector2(20.0f, 20.0f)); ground.AddFixture(sd); // Bottom horizontal sd.Shape = new EdgeShape(new Vector2(-20.0f, -20.0f), new Vector2(20.0f, -20.0f)); ground.AddFixture(sd); } { Transform xf1 = new Transform(); xf1.q.Set(0.3524f * MathConstants.Pi); xf1.p = xf1.q.GetXAxis(); Vertices vertices = new Vertices(3); vertices.Add(MathUtils.Mul(ref xf1, new Vector2(-1.0f, 0.0f))); vertices.Add(MathUtils.Mul(ref xf1, new Vector2(1.0f, 0.0f))); vertices.Add(MathUtils.Mul(ref xf1, new Vector2(0.0f, 0.5f))); PolygonShape poly1 = new PolygonShape(vertices, 2.0f); FixtureDef sd1 = new FixtureDef(); sd1.Shape = poly1; Transform xf2 = new Transform(); xf2.q.Set(-0.3524f * MathConstants.Pi); xf2.p = -xf2.q.GetXAxis(); vertices[0] = MathUtils.Mul(ref xf2, new Vector2(-1.0f, 0.0f)); vertices[1] = MathUtils.Mul(ref xf2, new Vector2(1.0f, 0.0f)); vertices[2] = MathUtils.Mul(ref xf2, new Vector2(0.0f, 0.5f)); PolygonShape poly2 = new PolygonShape(vertices, 2.0f); FixtureDef sd2 = new FixtureDef(); sd2.Shape = poly2; BodyDef bd = new BodyDef(); bd.Type = BodyType.Dynamic; bd.Position = new Vector2(0.0f, 3.0f); bd.Angle = MathConstants.Pi; bd.AllowSleep = false; _body = BodyFactory.CreateFromDef(World, bd); _body.AddFixture(sd1); _body.AddFixture(sd2); float gravity = 10.0f; float I = _body.Inertia; float mass = _body.Mass; // Compute an effective radius that can be used to // set the max torque for a friction joint // For a circle: I = 0.5 * m * r * r ==> r = sqrt(2 * I / m) float radius = MathUtils.Sqrt(2.0f * I / mass); FrictionJointDef jd = new FrictionJointDef(); jd.BodyA = ground; jd.BodyB = _body; jd.LocalAnchorA = Vector2.Zero; jd.LocalAnchorB = _body.LocalCenter; jd.CollideConnected = true; jd.MaxForce = 0.5f * mass * gravity; jd.MaxTorque = 0.2f * mass * radius * gravity; JointFactory.CreateFromDef(World, jd); } { PolygonShape shape = new PolygonShape(1.0f); shape.SetAsBox(0.5f, 0.5f); FixtureDef fd = new FixtureDef(); fd.Shape = shape; fd.Friction = 0.3f; for (int i = 0; i < 10; ++i) { BodyDef bd = new BodyDef(); bd.Type = BodyType.Dynamic; bd.Position = new Vector2(0.0f, 7.0f + 1.54f * i); Body body = BodyFactory.CreateFromDef(World, bd); body.AddFixture(fd); float gravity = 10.0f; float I = body.Inertia; float mass = body.Mass; // For a circle: I = 0.5 * m * r * r ==> r = sqrt(2 * I / m) float radius = MathUtils.Sqrt(2.0f * I / mass); FrictionJointDef jd = new FrictionJointDef(); jd.LocalAnchorA = Vector2.Zero; jd.LocalAnchorB = Vector2.Zero; jd.BodyA = ground; jd.BodyB = body; jd.CollideConnected = true; jd.MaxForce = mass * gravity; jd.MaxTorque = 0.1f * mass * radius * gravity; JointFactory.CreateFromDef(World, jd); } } }
// Use this for initialization protected override void Init() { API.SetGravity(world, Vector2.zero); var ground = API.CreateBody(world, new Vector2(0, 20), 0, BodyType.STATIC_BODY); ShapeDef sp = new ShapeDef(0); sp.restitution = 0.4f; API.AddEdgeShape(ground, new Vector2(-20, -20), new Vector2(-20, 20), sp); API.AddEdgeShape(ground, new Vector2(20, -20), new Vector2(20, 20), sp); API.AddEdgeShape(ground, new Vector2(-20, 20), new Vector2(20, 20), sp); API.AddEdgeShape(ground, new Vector2(-20, -20), new Vector2(20, -20), sp); BodyDef bd = new BodyDef(BodyType.DYNAMIC_BODY); bd.allowSleep = false; bd.angle = 3.14159265359f; bd.angularDamping = 5.0f; bd.linearDamping = 0.1f; bd.position = new Vector2(0, 2); m_body = API.CreateBody(world, bd); sp.restitution = 0.0f; sp.density = 4.0f; b2Transform xf1 = new b2Transform(); xf1.q = new b2Rot(0.3524f * Mathf.PI); xf1.p = xf1.q.GetXAxis(); Vector2[] vertices = new Vector2[3]; vertices[0] = b2Math.b2Mul(xf1, new Vector2(-1.0f, 0.0f)); vertices[1] = b2Math.b2Mul(xf1, new Vector2(1.0f, 0.0f)); vertices[2] = b2Math.b2Mul(xf1, new Vector2(0.0f, 0.5f)); API.AddPolygonShape(m_body, vertices, vertices.Length, sp); sp.density = 2.0f; xf1.q = new b2Rot(-0.3524f * Mathf.PI); xf1.p = -xf1.q.GetXAxis(); vertices[0] = b2Math.b2Mul(xf1, new Vector2(-1.0f, 0.0f)); vertices[1] = b2Math.b2Mul(xf1, new Vector2(1.0f, 0.0f)); vertices[2] = b2Math.b2Mul(xf1, new Vector2(0.0f, 0.5f)); API.AddPolygonShape(m_body, vertices, vertices.Length, sp); sp.density = 1.0f; sp.friction = 0.3f; float gravity = 10; FrictionJointDef jd = new FrictionJointDef(); for (int i = 0; i < 10; ++i) { var b = API.CreateBody(world, new Vector2(0, 5.0f + 1.54f * i), 0, BodyType.DYNAMIC_BODY); API.AddBoxShape(b, 0.5f, 0.5f, Vector2.zero, 0, sp); float I = API.GetInertia(b); float mass = API.GetMass(b); float radius = Mathf.Sqrt(2.0f * I / mass); jd.bodyA = ground; jd.bodyB = b; jd.collideConnected = true; jd.maxForce = mass * gravity; jd.maxTorque = mass * radius * gravity; API.CreateFrictionJoint(world, jd); } }
public ApplyForce() { _world.Gravity = new Vector2(0.0f, 0.0f); const float k_restitution = 0.4f; Body ground; { BodyDef bd = new BodyDef(); bd.position = new Vector2(0.0f, 20.0f); ground = _world.CreateBody(bd); PolygonShape shape = new PolygonShape(); FixtureDef sd = new FixtureDef(); sd.shape = shape; sd.density = 0.0f; sd.restitution = k_restitution; // Left vertical shape.SetAsEdge(new Vector2(-20.0f, -20.0f), new Vector2(-20.0f, 20.0f)); ground.CreateFixture(sd); // Right vertical shape.SetAsEdge(new Vector2(20.0f, -20.0f), new Vector2(20.0f, 20.0f)); ground.CreateFixture(sd); // Top horizontal shape.SetAsEdge(new Vector2(-20.0f, 20.0f), new Vector2(20.0f, 20.0f)); ground.CreateFixture(sd); // Bottom horizontal shape.SetAsEdge(new Vector2(-20.0f, -20.0f), new Vector2(20.0f, -20.0f)); ground.CreateFixture(sd); } { Transform xf1 = new Transform(); xf1.R.Set(0.3524f * (float)Settings.b2_pi); xf1.Position = MathUtils.Multiply(ref xf1.R, new Vector2(1.0f, 0.0f)); Vector2[] vertices = new Vector2[3]; vertices[0] = MathUtils.Multiply(ref xf1, new Vector2(-1.0f, 0.0f)); vertices[1] = MathUtils.Multiply(ref xf1, new Vector2(1.0f, 0.0f)); vertices[2] = MathUtils.Multiply(ref xf1, new Vector2(0.0f, 0.5f)); PolygonShape poly1 = new PolygonShape(); poly1.Set(vertices, 3); FixtureDef sd1 = new FixtureDef(); sd1.shape = poly1; sd1.density = 4.0f; Transform xf2 = new Transform(); xf2.R.Set(-0.3524f * (float)Settings.b2_pi); xf2.Position = MathUtils.Multiply(ref xf2.R, new Vector2(-1.0f, 0.0f)); vertices[0] = MathUtils.Multiply(ref xf2, new Vector2(-1.0f, 0.0f)); vertices[1] = MathUtils.Multiply(ref xf2, new Vector2(1.0f, 0.0f)); vertices[2] = MathUtils.Multiply(ref xf2, new Vector2(0.0f, 0.5f)); PolygonShape poly2 = new PolygonShape(); poly2.Set(vertices, 3); FixtureDef sd2 = new FixtureDef(); sd2.shape = poly2; sd2.density = 2.0f; BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.angularDamping = 5.0f; bd.linearDamping = 0.1f; bd.position = new Vector2(0.0f, 2.0f); bd.angle = (float)Settings.b2_pi; bd.allowSleep = false; _body = _world.CreateBody(bd); _body.CreateFixture(sd1); _body.CreateFixture(sd2); } { PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.5f, 0.5f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 1.0f; fd.friction = 0.3f; for (int i = 0; i < 10; ++i) { BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(0.0f, 5.0f + 1.54f * i); Body body = _world.CreateBody(bd); body.CreateFixture(fd); float gravity = 10.0f; float I = body.GetInertia(); float mass = body.GetMass(); // For a circle: I = 0.5 * m * r * r ==> r = sqrt(2 * I / m) float radius = (float)Math.Sqrt(2.0 * (double)(I / mass)); FrictionJointDef jd = new FrictionJointDef(); jd.localAnchorA = Vector2.Zero; jd.localAnchorB = Vector2.Zero; jd.bodyA = ground; jd.bodyB = body; jd.collideConnected = true; jd.maxForce = mass * gravity; jd.maxTorque = mass * radius * gravity; _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; }
public ApplyForce() { m_world.Gravity = new Vec2(0.0f, 0.0f); const float k_restitution = 0.4f; Body ground; { BodyDef bd = new BodyDef(); bd.Position = new Vec2(0.0f, 20.0f); ground = m_world.CreateBody(bd); PolygonShape shape = new PolygonShape(); FixtureDef sd = new FixtureDef(); sd.Shape = shape; sd.Density = 0.0f; sd.Restitution = k_restitution; // Left vertical shape.SetAsEdge(new Vec2(-20.0f, -20.0f), new Vec2(-20.0f, 20.0f)); ground.CreateFixture(sd); // Right vertical shape.SetAsEdge(new Vec2(20.0f, -20.0f), new Vec2(20.0f, 20.0f)); ground.CreateFixture(sd); // Top horizontal shape.SetAsEdge(new Vec2(-20.0f, 20.0f), new Vec2(20.0f, 20.0f)); ground.CreateFixture(sd); // Bottom horizontal shape.SetAsEdge(new Vec2(-20.0f, -20.0f), new Vec2(20.0f, -20.0f)); ground.CreateFixture(sd); } { Transform xf1 = new Transform(); xf1.R = new Mat22(0.3524f * (float)Math.PI); xf1.Position = (xf1.R * new Vec2(1.0f, 0.0f)); Vec2[] vertices = new Vec2[3] { (xf1 * new Vec2(-1.0f, 0.0f)), (xf1 * new Vec2(1.0f, 0.0f)), (xf1 * new Vec2(0.0f, 0.5f)) }; PolygonShape poly1 = new PolygonShape(vertices); FixtureDef sd1 = new FixtureDef(); sd1.Shape = poly1; sd1.Density = 4.0f; Transform xf2 = new Transform(); xf2.R = new Mat22(-0.3524f * (float)Math.PI); xf2.Position = (xf2.R * new Vec2(-1.0f, 0.0f)); vertices = new Vec2[] { (xf2 * new Vec2(-1.0f, 0.0f)), (xf2 * new Vec2(1.0f, 0.0f)), (xf2 * new Vec2(0.0f, 0.5f)) }; PolygonShape poly2 = new PolygonShape(vertices); FixtureDef sd2 = new FixtureDef(); sd2.Shape = poly2; sd2.Density = 2.0f; BodyDef bd = new BodyDef(); bd.BodyType = BodyType.Dynamic; bd.AngularDamping = 5.0f; bd.LinearDamping = 0.1f; bd.Position = new Vec2(0.0f, 2.0f); bd.Angle = (float)Math.PI; bd.AllowSleep = false; m_body = m_world.CreateBody(bd); m_body.CreateFixture(sd1); m_body.CreateFixture(sd2); } { PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.5f, 0.5f); FixtureDef fd = new FixtureDef(); fd.Shape = shape; fd.Density = 1.0f; fd.Friction = 0.3f; for (int i = 0; i < 10; ++i) { BodyDef bd = new BodyDef(); bd.BodyType = BodyType.Dynamic; bd.Position = new Vec2(0.0f, 5.0f + 1.54f * i); Body body = m_world.CreateBody(bd); body.CreateFixture(fd); float gravity = 10.0f; float I = body.Inertia; float mass = body.Mass; // For a circle: I = 0.5 * m * r * r ==> r = sqrt(2 * I / m) float radius = (float)Math.Sqrt(2.0f * I / mass); FrictionJointDef jd = new FrictionJointDef(); jd.LocalAnchorA = jd.LocalAnchorB = Vec2.Empty; jd.BodyA = ground; jd.BodyB = body; jd.CollideConnected = true; jd.MaxForce = mass * gravity; jd.MaxTorque = mass * radius * gravity; m_world.CreateJoint(jd); } } }
public ApplyForce() { m_world.SetGravity(new Vec2(0.0f, 0.0f)); const float k_restitution = 0.4f; Body ground; { BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, 20.0f); ground = m_world.CreateBody(bd); EdgeShape shape = new EdgeShape(); FixtureDef sd = new FixtureDef(); sd.shape = shape; sd.Density = 0.0f; sd.restitution = k_restitution; // Left vertical shape.Set(new Vec2(-20.0f, -20.0f), new Vec2(-20.0f, 20.0f)); ground.CreateFixture(sd); // Right vertical shape.Set(new Vec2(20.0f, -20.0f), new Vec2(20.0f, 20.0f)); ground.CreateFixture(sd); // Top horizontal shape.Set(new Vec2(-20.0f, 20.0f), new Vec2(20.0f, 20.0f)); ground.CreateFixture(sd); // Bottom horizontal shape.Set(new Vec2(-20.0f, -20.0f), new Vec2(20.0f, -20.0f)); ground.CreateFixture(sd); } { Transform xf1 = new Transform(); xf1.q.Set(0.3524f * (float)Math.PI); xf1.p = xf1.q.GetXAxis(); Vec2[] vertices = new Vec2[3]; vertices[0] = Utilities.Mul(xf1, new Vec2(-1.0f, 0.0f)); vertices[1] = Utilities.Mul(xf1, new Vec2(1.0f, 0.0f)); vertices[2] = Utilities.Mul(xf1, new Vec2(0.0f, 0.5f)); PolygonShape poly1 = new PolygonShape(); poly1.Set(vertices, 3); FixtureDef sd1 = new FixtureDef(); sd1.shape = poly1; sd1.Density = 4.0f; Transform xf2 = new Transform(); xf2.q.Set(-0.3524f * (float)Math.PI); xf2.p = -xf2.q.GetXAxis(); vertices[0] = Utilities.Mul(xf2, new Vec2(-1.0f, 0.0f)); vertices[1] = Utilities.Mul(xf2, new Vec2(1.0f, 0.0f)); vertices[2] = Utilities.Mul(xf2, new Vec2(0.0f, 0.5f)); PolygonShape poly2 = new PolygonShape(); poly2.Set(vertices, 3); FixtureDef sd2 = new FixtureDef(); sd2.shape = poly2; sd2.Density = 2.0f; BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.angularDamping = 5.0f; bd.linearDamping = 0.1f; bd.Position.Set(0.0f, 2.0f); bd.angle = (float)Math.PI; bd.allowSleep = false; m_body = m_world.CreateBody(bd); m_body.CreateFixture(sd1); m_body.CreateFixture(sd2); } { PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.5f, 0.5f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.Density = 1.0f; fd.friction = 0.3f; for (int i = 0; i < 10; ++i) { BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position.Set(0.0f, 5.0f + 1.54f * i); Body body = m_world.CreateBody(bd); body.CreateFixture(fd); float gravity = 10.0f; float I = body.GetInertia(); float mass = body.GetMass(); // For a circle: I = 0.5 * m * r * r ==> r = sqrt(2 * I / m) float radius = (float)Math.Sqrt(2.0f * I / mass); FrictionJointDef jd = new FrictionJointDef(); jd.localAnchorA.SetZero(); jd.localAnchorB.SetZero(); jd.bodyA = ground; jd.bodyB = body; jd.collideConnected = true; jd.maxForce = mass * gravity; jd.maxTorque = mass * radius * gravity; m_world.CreateJoint(jd); } } }