public DistanceJointTest() { Body ground = null; { BodyDef bd = new BodyDef(); ground = World.CreateBody(bd); EdgeShape shape = new EdgeShape(); shape.SetTwoSided(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } { BodyDef bd = new BodyDef(); bd.BodyType = BodyType.DynamicBody; bd.AngularDamping = 0.1f; bd.Position.Set(0.0f, 5.0f); Body body = World.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.5f, 0.5f); body.CreateFixture(shape, 5.0f); m_hertz = 1.0f; m_dampingRatio = 0.7f; DistanceJointDef jd = new DistanceJointDef(); jd.Initialize(ground, body, new Vector2(0.0f, 15.0f), bd.Position); jd.CollideConnected = true; m_length = jd.Length; m_minLength = m_length; m_maxLength = m_length; JointUtils.LinearStiffness(out jd.Stiffness, out jd.Damping, m_hertz, m_dampingRatio, jd.BodyA, jd.BodyB); m_joint = (DistanceJoint)World.CreateJoint(jd); } }
public WebTest() { Body ground; { BodyDef bd = new BodyDef(); ground = World.CreateBody(bd); EdgeShape shape = new EdgeShape(); shape.SetTwoSided(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.BodyType = BodyType.DynamicBody; bd.Position.Set(-5.0f, 5.0f); m_bodies[0] = World.CreateBody(bd); m_bodies[0].CreateFixture(shape, 5.0f); bd.Position.Set(5.0f, 5.0f); m_bodies[1] = World.CreateBody(bd); m_bodies[1].CreateFixture(shape, 5.0f); bd.Position.Set(5.0f, 15.0f); m_bodies[2] = World.CreateBody(bd); m_bodies[2].CreateFixture(shape, 5.0f); bd.Position.Set(-5.0f, 15.0f); m_bodies[3] = World.CreateBody(bd); m_bodies[3].CreateFixture(shape, 5.0f); DistanceJointDef jd = new DistanceJointDef(); Vector2 p1; Vector2 p2; Vector2 d; float frequencyHz = 2.0f; float dampingRatio = 0.0f; jd.BodyA = ground; jd.BodyB = m_bodies[0]; jd.LocalAnchorA.Set(-10.0f, 0.0f); jd.LocalAnchorB.Set(-0.5f, -0.5f); p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA); p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB); d = p2 - p1; jd.Length = d.Length(); JointUtils.LinearStiffness(out jd.Stiffness, out jd.Damping, frequencyHz, dampingRatio, jd.BodyA, jd.BodyB); m_joints[0] = World.CreateJoint(jd); jd.BodyA = ground; jd.BodyB = m_bodies[1]; jd.LocalAnchorA.Set(10.0f, 0.0f); jd.LocalAnchorB.Set(0.5f, -0.5f); p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA); p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB); d = p2 - p1; jd.Length = d.Length(); JointUtils.LinearStiffness(out jd.Stiffness, out jd.Damping, frequencyHz, dampingRatio, jd.BodyA, jd.BodyB); m_joints[1] = World.CreateJoint(jd); jd.BodyA = ground; jd.BodyB = m_bodies[2]; jd.LocalAnchorA.Set(10.0f, 20.0f); jd.LocalAnchorB.Set(0.5f, 0.5f); p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA); p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB); d = p2 - p1; jd.Length = d.Length(); JointUtils.LinearStiffness(out jd.Stiffness, out jd.Damping, frequencyHz, dampingRatio, jd.BodyA, jd.BodyB); m_joints[2] = World.CreateJoint(jd); jd.BodyA = ground; jd.BodyB = m_bodies[3]; jd.LocalAnchorA.Set(-10.0f, 20.0f); jd.LocalAnchorB.Set(-0.5f, 0.5f); p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA); p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB); d = p2 - p1; jd.Length = d.Length(); JointUtils.LinearStiffness(out jd.Stiffness, out jd.Damping, frequencyHz, dampingRatio, jd.BodyA, jd.BodyB); m_joints[3] = World.CreateJoint(jd); jd.BodyA = m_bodies[0]; jd.BodyB = m_bodies[1]; jd.LocalAnchorA.Set(0.5f, 0.0f); jd.LocalAnchorB.Set(-0.5f, 0.0f); ; p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA); p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB); d = p2 - p1; jd.Length = d.Length(); JointUtils.LinearStiffness(out jd.Stiffness, out jd.Damping, frequencyHz, dampingRatio, jd.BodyA, jd.BodyB); m_joints[4] = World.CreateJoint(jd); jd.BodyA = m_bodies[1]; jd.BodyB = m_bodies[2]; jd.LocalAnchorA.Set(0.0f, 0.5f); jd.LocalAnchorB.Set(0.0f, -0.5f); p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA); p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB); d = p2 - p1; jd.Length = d.Length(); JointUtils.LinearStiffness(out jd.Stiffness, out jd.Damping, frequencyHz, dampingRatio, jd.BodyA, jd.BodyB); m_joints[5] = World.CreateJoint(jd); jd.BodyA = m_bodies[2]; jd.BodyB = m_bodies[3]; jd.LocalAnchorA.Set(-0.5f, 0.0f); jd.LocalAnchorB.Set(0.5f, 0.0f); p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA); p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB); d = p2 - p1; jd.Length = d.Length(); JointUtils.LinearStiffness(out jd.Stiffness, out jd.Damping, frequencyHz, dampingRatio, jd.BodyA, jd.BodyB); m_joints[6] = World.CreateJoint(jd); jd.BodyA = m_bodies[3]; jd.BodyB = m_bodies[0]; jd.LocalAnchorA.Set(0.0f, -0.5f); jd.LocalAnchorB.Set(0.0f, 0.5f); p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA); p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB); d = p2 - p1; jd.Length = d.Length(); JointUtils.LinearStiffness(out jd.Stiffness, out jd.Damping, frequencyHz, dampingRatio, jd.BodyA, jd.BodyB); m_joints[7] = World.CreateJoint(jd); } }
private void CreateLeg(float s, Vector2 wheelAnchor) { var p1 = new Vector2(5.4f * s, -6.1f); var p2 = new Vector2(7.2f * s, -1.2f); var p3 = new Vector2(4.3f * s, -1.9f); var p4 = new Vector2(3.1f * s, 0.8f); var p5 = new Vector2(6.0f * s, 1.5f); var p6 = new Vector2(2.5f * s, 3.7f); var fd1 = new FixtureDef { Filter = { GroupIndex = -1 }, Density = 1.0f }; var fd2 = new FixtureDef { Filter = { GroupIndex = -1 }, Density = 1.0f }; var poly1 = new PolygonShape(); var poly2 = new PolygonShape(); if (s > 0.0f) { var vertices = new Vector2[3]; vertices[0] = p1; vertices[1] = p2; vertices[2] = p3; poly1.Set(vertices); vertices[0] = Vector2.Zero; vertices[1] = p5 - p4; vertices[2] = p6 - p4; poly2.Set(vertices); } else { var vertices = new Vector2[3]; vertices[0] = p1; vertices[1] = p3; vertices[2] = p2; poly1.Set(vertices); vertices[0] = Vector2.Zero; vertices[1] = p6 - p4; vertices[2] = p5 - p4; poly2.Set(vertices); } fd1.Shape = poly1; fd2.Shape = poly2; var bd1 = new BodyDef(); var bd2 = new BodyDef(); bd1.BodyType = BodyType.DynamicBody; bd2.BodyType = BodyType.DynamicBody; bd1.Position = _offset; bd2.Position = p4 + _offset; bd1.AngularDamping = 10.0f; bd2.AngularDamping = 10.0f; var body1 = World.CreateBody(bd1); var body2 = World.CreateBody(bd2); body1.CreateFixture(fd1); body2.CreateFixture(fd2); { var jd = new DistanceJointDef(); // Using a soft distance constraint can reduce some jitter. // It also makes the structure seem a bit more fluid by // acting like a suspension system. var dampingRatio = 0.5f; var frequencyHz = 10.0f; jd.Initialize(body1, body2, p2 + _offset, p5 + _offset); JointUtils.LinearStiffness(out jd.Stiffness, out jd.Damping, frequencyHz, dampingRatio, jd.BodyA, jd.BodyB); World.CreateJoint(jd); jd.Initialize(body1, body2, p3 + _offset, p4 + _offset); JointUtils.LinearStiffness(out jd.Stiffness, out jd.Damping, frequencyHz, dampingRatio, jd.BodyA, jd.BodyB); World.CreateJoint(jd); jd.Initialize(body1, _wheel, p3 + _offset, wheelAnchor + _offset); JointUtils.LinearStiffness(out jd.Stiffness, out jd.Damping, frequencyHz, dampingRatio, jd.BodyA, jd.BodyB); World.CreateJoint(jd); jd.Initialize(body2, _wheel, p6 + _offset, wheelAnchor + _offset); JointUtils.LinearStiffness(out jd.Stiffness, out jd.Damping, frequencyHz, dampingRatio, jd.BodyA, jd.BodyB); World.CreateJoint(jd); } { var jd = new RevoluteJointDef(); jd.Initialize(body2, _chassis, p4 + _offset); World.CreateJoint(jd); } }
public Dominos() { Body b1; { var shape = new EdgeShape(); shape.SetTwoSided(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); var bd = new BodyDef(); b1 = World.CreateBody(bd); b1.CreateFixture(shape, 0.0f); } { var shape = new PolygonShape(); shape.SetAsBox(6.0f, 0.25f); var bd = new BodyDef(); bd.Position.Set(-1.5f, 10.0f); var ground = World.CreateBody(bd); ground.CreateFixture(shape, 0.0f); } { var shape = new PolygonShape(); shape.SetAsBox(0.1f, 1.0f); var fd = new FixtureDef(); fd.Shape = shape; fd.Density = 20.0f; fd.Friction = 0.1f; for (var i = 0; i < 10; ++i) { var bd = new BodyDef(); bd.BodyType = BodyType.DynamicBody; bd.Position.Set(-6.0f + 1.0f * i, 11.25f); var body = World.CreateBody(bd); body.CreateFixture(fd); } } { var shape = new PolygonShape(); shape.SetAsBox(7.0f, 0.25f, Vector2.Zero, 0.3f); var bd = new BodyDef(); bd.Position.Set(1.0f, 6.0f); var ground = World.CreateBody(bd); ground.CreateFixture(shape, 0.0f); } Body b2; { var shape = new PolygonShape(); shape.SetAsBox(0.25f, 1.5f); var bd = new BodyDef(); bd.Position.Set(-7.0f, 4.0f); b2 = World.CreateBody(bd); b2.CreateFixture(shape, 0.0f); } Body b3; { var shape = new PolygonShape(); shape.SetAsBox(6.0f, 0.125f); var bd = new BodyDef(); bd.BodyType = BodyType.DynamicBody; bd.Position.Set(-0.9f, 1.0f); bd.Angle = -0.15f; b3 = World.CreateBody(bd); b3.CreateFixture(shape, 10.0f); } var jd = new RevoluteJointDef(); var anchor = new Vector2(); anchor.Set(-2.0f, 1.0f); jd.Initialize(b1, b3, anchor); jd.CollideConnected = true; World.CreateJoint(jd); Body b4; { var shape = new PolygonShape(); shape.SetAsBox(0.25f, 0.25f); var bd = new BodyDef(); bd.BodyType = BodyType.DynamicBody; bd.Position.Set(-10.0f, 15.0f); b4 = World.CreateBody(bd); b4.CreateFixture(shape, 10.0f); } anchor.Set(-7.0f, 15.0f); jd.Initialize(b2, b4, anchor); World.CreateJoint(jd); Body b5; { var bd = new BodyDef(); bd.BodyType = BodyType.DynamicBody; bd.Position.Set(6.5f, 3.0f); b5 = World.CreateBody(bd); var shape = new PolygonShape(); var 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.Set(6.0f, 2.0f); jd.Initialize(b1, b5, anchor); World.CreateJoint(jd); Body b6; { var shape = new PolygonShape(); shape.SetAsBox(1.0f, 0.1f); var bd = new BodyDef(); bd.BodyType = BodyType.DynamicBody; bd.Position.Set(6.5f, 4.1f); b6 = World.CreateBody(bd); b6.CreateFixture(shape, 30.0f); } anchor.Set(7.5f, 4.0f); jd.Initialize(b5, b6, anchor); World.CreateJoint(jd); Body b7; { var shape = new PolygonShape(); shape.SetAsBox(0.1f, 1.0f); var bd = new BodyDef(); bd.BodyType = BodyType.DynamicBody; bd.Position.Set(7.4f, 1.0f); b7 = World.CreateBody(bd); b7.CreateFixture(shape, 10.0f); } var djd = new DistanceJointDef(); djd.BodyA = b3; djd.BodyB = b7; djd.LocalAnchorA.Set(6.0f, 0.0f); djd.LocalAnchorB.Set(0.0f, -1.0f); var d = djd.BodyB.GetWorldPoint(djd.LocalAnchorB) - djd.BodyA.GetWorldPoint(djd.LocalAnchorA); djd.Length = d.Length(); JointUtils.LinearStiffness(out djd.Stiffness, out djd.Damping, 1.0f, 1.0f, djd.BodyA, djd.BodyB); World.CreateJoint(djd); { var radius = 0.2f; var shape = new CircleShape(); shape.Radius = radius; for (var i = 0; i < 4; ++i) { var bd = new BodyDef(); bd.BodyType = BodyType.DynamicBody; bd.Position.Set(5.9f + 2.0f * radius * i, 2.4f); var body = World.CreateBody(bd); body.CreateFixture(shape, 10.0f); } } }