public b2DistanceJoint(b2DistanceJointDef def) : base(def) { m_localAnchorA = def.localAnchorA; m_localAnchorB = def.localAnchorB; m_length = def.length; m_frequencyHz = def.frequencyHz; m_dampingRatio = def.dampingRatio; m_impulse = 0.0f; m_gamma = 0.0f; m_bias = 0.0f; }
//--------------- Internals Below ------------------- /** @private */ public b2DistanceJoint(b2DistanceJointDef def) : base(def) { b2Mat22 tMat; float tX; float tY; m_localAnchor1.SetV(def.localAnchorA); m_localAnchor2.SetV(def.localAnchorB); m_length = def.length; m_frequencyHz = def.frequencyHz; m_dampingRatio = def.dampingRatio; m_impulse = 0.0f; m_gamma = 0.0f; m_bias = 0.0f; }
public Dominos() { b2Body b1; { b2EdgeShape shape = new b2EdgeShape(); shape.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f)); b2BodyDef bd = new b2BodyDef(); b1 = m_world.CreateBody(bd); b1.CreateFixture(shape, 0.0f); } { b2PolygonShape shape = new b2PolygonShape(); shape.SetAsBox(6.0f, 0.25f); b2BodyDef bd = new b2BodyDef(); bd.position.Set(-1.5f, 10.0f); b2Body ground = m_world.CreateBody(bd); ground.CreateFixture(shape, 0.0f); } { b2PolygonShape shape = new b2PolygonShape(); shape.SetAsBox(0.1f, 1.0f); b2FixtureDef fd = new b2FixtureDef(); fd.shape = shape; fd.density = 20.0f; fd.friction = 0.1f; for (int i = 0; i < 10; ++i) { b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(-6.0f + 1.0f * i, 11.25f); b2Body body = m_world.CreateBody(bd); body.CreateFixture(fd); } } { b2PolygonShape shape = new b2PolygonShape(); shape.SetAsBox(7.0f, 0.25f, b2Vec2.Zero, 0.3f); b2BodyDef bd = new b2BodyDef(); bd.position.Set(1.0f, 6.0f); b2Body ground = m_world.CreateBody(bd); ground.CreateFixture(shape, 0.0f); } b2Body b2; { b2PolygonShape shape = new b2PolygonShape(); shape.SetAsBox(0.25f, 1.5f); b2BodyDef bd = new b2BodyDef(); bd.position.Set(-7.0f, 4.0f); b2 = m_world.CreateBody(bd); b2.CreateFixture(shape, 0.0f); } b2Body b3; { b2PolygonShape shape = new b2PolygonShape(); shape.SetAsBox(6.0f, 0.125f); b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(-0.9f, 1.0f); bd.angle = -0.15f; b3 = m_world.CreateBody(bd); b3.CreateFixture(shape, 10.0f); } b2RevoluteJointDef jd = new b2RevoluteJointDef(); b2Vec2 anchor = new b2Vec2(); anchor.Set(-2.0f, 1.0f); jd.Initialize(b1, b3, anchor); jd.CollideConnected = true; m_world.CreateJoint(jd); b2Body b4; { b2PolygonShape shape = new b2PolygonShape(); shape.SetAsBox(0.25f, 0.25f); b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(-10.0f, 15.0f); b4 = m_world.CreateBody(bd); b4.CreateFixture(shape, 10.0f); } anchor.Set(-7.0f, 15.0f); jd.Initialize(b2, b4, anchor); m_world.CreateJoint(jd); b2Body b5; { b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(6.5f, 3.0f); b5 = m_world.CreateBody(bd); b2PolygonShape shape = new b2PolygonShape(); b2FixtureDef fd = new b2FixtureDef(); fd.shape = shape; fd.density = 10.0f; fd.friction = 0.1f; shape.SetAsBox(1.0f, 0.1f, new b2Vec2(0.0f, -0.9f), 0.0f); b5.CreateFixture(fd); shape.SetAsBox(0.1f, 1.0f, new b2Vec2(-0.9f, 0.0f), 0.0f); b5.CreateFixture(fd); shape.SetAsBox(0.1f, 1.0f, new b2Vec2(0.9f, 0.0f), 0.0f); b5.CreateFixture(fd); } anchor.Set(6.0f, 2.0f); jd.Initialize(b1, b5, anchor); m_world.CreateJoint(jd); b2Body b6; { b2PolygonShape shape = new b2PolygonShape(); shape.SetAsBox(1.0f, 0.1f); b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(6.5f, 4.1f); b6 = m_world.CreateBody(bd); b6.CreateFixture(shape, 30.0f); } anchor.Set(7.5f, 4.0f); jd.Initialize(b5, b6, anchor); m_world.CreateJoint(jd); b2Body b7; { b2PolygonShape shape = new b2PolygonShape(); shape.SetAsBox(0.1f, 1.0f); b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(7.4f, 1.0f); b7 = m_world.CreateBody(bd); b7.CreateFixture(shape, 10.0f); } b2DistanceJointDef djd = new b2DistanceJointDef(); djd.BodyA = b3; djd.BodyB = b7; djd.localAnchorA.Set(6.0f, 0.0f); djd.localAnchorB.Set(0.0f, -1.0f); b2Vec2 d = djd.BodyB.GetWorldPoint(djd.localAnchorB) - djd.BodyA.GetWorldPoint(djd.localAnchorA); djd.length = d.Length; m_world.CreateJoint(djd); { float radius = 0.2f; b2CircleShape shape = new b2CircleShape(); shape.Radius = radius; for (int i = 0; i < 4; ++i) { b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(5.9f + 2.0f * radius * i, 2.4f); b2Body body = m_world.CreateBody(bd); body.CreateFixture(shape, 10.0f); } } }
public Web() { b2Body ground = null; { b2BodyDef bd = new b2BodyDef(); ground = m_world.CreateBody(bd); b2EdgeShape shape = new b2EdgeShape(); shape.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } { b2PolygonShape shape = new b2PolygonShape(); shape.SetAsBox(0.5f, 0.5f); b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(-5.0f, 5.0f); m_bodies[0] = m_world.CreateBody(bd); m_bodies[0].CreateFixture(shape, 5.0f); bd.position.Set(5.0f, 5.0f); m_bodies[1] = m_world.CreateBody(bd); m_bodies[1].CreateFixture(shape, 5.0f); bd.position.Set(5.0f, 15.0f); m_bodies[2] = m_world.CreateBody(bd); m_bodies[2].CreateFixture(shape, 5.0f); bd.position.Set(-5.0f, 15.0f); m_bodies[3] = m_world.CreateBody(bd); m_bodies[3].CreateFixture(shape, 5.0f); b2DistanceJointDef jd = new b2DistanceJointDef(); b2Vec2 p1 = new b2Vec2(); b2Vec2 p2 = new b2Vec2(); b2Vec2 d = new b2Vec2(); jd.frequencyHz = 2.0f; jd.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; m_joints[0] = m_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; m_joints[1] = m_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; m_joints[2] = m_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; m_joints[3] = m_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; m_joints[4] = m_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; m_joints[5] = m_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; m_joints[6] = m_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; m_joints[7] = m_world.CreateJoint(jd); } }
public void CreateLeg(float s, b2Vec2 wheelAnchor) { b2Vec2 p1 = new b2Vec2(5.4f * s, -6.1f); b2Vec2 p2 = new b2Vec2(7.2f * s, -1.2f); b2Vec2 p3 = new b2Vec2(4.3f * s, -1.9f); b2Vec2 p4 = new b2Vec2(3.1f * s, 0.8f); b2Vec2 p5 = new b2Vec2(6.0f * s, 1.5f); b2Vec2 p6 = new b2Vec2(2.5f * s, 3.7f); b2FixtureDef fd1 = new b2FixtureDef(); b2FixtureDef fd2 = new b2FixtureDef(); fd1.filter.groupIndex = -1; fd2.filter.groupIndex = -1; fd1.density = 1.0f; fd2.density = 1.0f; b2PolygonShape poly1 = new b2PolygonShape(); b2PolygonShape poly2 = new b2PolygonShape(); if (s > 0.0f) { b2Vec2[] vertices = new b2Vec2[3]; vertices[0] = p1; vertices[1] = p2; vertices[2] = p3; poly1.Set(vertices, 3); vertices[0] = b2Vec2.Zero; vertices[1] = p5 - p4; vertices[2] = p6 - p4; poly2.Set(vertices, 3); } else { b2Vec2[] vertices = new b2Vec2[3]; vertices[0] = p1; vertices[1] = p3; vertices[2] = p2; poly1.Set(vertices, 3); vertices[0] = b2Vec2.Zero; vertices[1] = p6 - p4; vertices[2] = p5 - p4; poly2.Set(vertices, 3); } fd1.shape = poly1; fd2.shape = poly2; b2BodyDef bd1 = new b2BodyDef(); b2BodyDef bd2 = new b2BodyDef(); bd1.type = b2BodyType.b2_dynamicBody; bd2.type = b2BodyType.b2_dynamicBody; bd1.position = m_offset; bd2.position = p4 + m_offset; bd1.angularDamping = 10.0f; bd2.angularDamping = 10.0f; b2Body body1 = m_world.CreateBody(bd1); b2Body body2 = m_world.CreateBody(bd2); body1.CreateFixture(fd1); body2.CreateFixture(fd2); b2DistanceJointDef djd = new b2DistanceJointDef(); // 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. djd.dampingRatio = 0.5f; djd.frequencyHz = 10.0f; djd.Initialize(body1, body2, p2 + m_offset, p5 + m_offset); m_world.CreateJoint(djd); djd.Initialize(body1, body2, p3 + m_offset, p4 + m_offset); m_world.CreateJoint(djd); djd.Initialize(body1, m_wheel, p3 + m_offset, wheelAnchor + m_offset); m_world.CreateJoint(djd); djd.Initialize(body2, m_wheel, p6 + m_offset, wheelAnchor + m_offset); m_world.CreateJoint(djd); b2RevoluteJointDef rjd = new b2RevoluteJointDef(); rjd.Initialize(body2, m_chassis, p4 + m_offset); m_world.CreateJoint(rjd); }
// SIN REVISAR b2Joint j2b2Joint(b2World world, JObject jointValue) { b2Joint joint = null; int bodyIndexA = (int)jointValue["bodyA"]; int bodyIndexB = (int)jointValue["bodyB"]; if (bodyIndexA >= m_bodies.Count || bodyIndexB >= m_bodies.Count) return null; // set features common to all joints //var bodyA = m_bodies[bodyIndexA]; //var bodyB = m_bodies[bodyIndexB]; //var collideConnected = jointValue["collideConnected"] == null ? false : (bool)jointValue["collideConnected"]; // keep these in scope after the if/else below b2RevoluteJointDef revoluteDef; b2PrismaticJointDef prismaticDef; b2DistanceJointDef distanceDef; b2PulleyJointDef pulleyDef; b2MouseJointDef mouseDef; b2GearJointDef gearDef; //b2WheelJoint wheelDef; b2WeldJointDef weldDef; b2FrictionJointDef frictionDef; b2RopeJointDef ropeDef; //MotorJoint motorDef; b2JointDef jointDef = null; b2Vec2 mouseJointTarget = new b2Vec2(0, 0); string type = jointValue["type"].ToString() == null ? "" : jointValue["type"].ToString(); if (type == "revolute") { jointDef = revoluteDef = new b2RevoluteJointDef(); // JointFactory.CreateRevoluteJoint(world, bodyA, bodyB, jsonToVec("anchorB", jointValue)); revoluteDef.localAnchorA = jsonToVec("anchorA", jointValue); revoluteDef.localAnchorB = jsonToVec("anchorB", jointValue); revoluteDef.referenceAngle = jsonToFloat("refAngle", jointValue); revoluteDef.enableLimit = jointValue["enableLimit"] == null ? false : (bool)jointValue["enableLimit"]; revoluteDef.lowerAngle = jsonToFloat("lowerLimit", jointValue); revoluteDef.upperAngle = jsonToFloat("upperLimit", jointValue); revoluteDef.enableMotor = jointValue["enableMotor"] == null ? false : (bool)jointValue["enableMotor"]; revoluteDef.motorSpeed = jsonToFloat("motorSpeed", jointValue); revoluteDef.maxMotorTorque = jsonToFloat("maxMotorTorque", jointValue); } else if (type == "prismatic") { jointDef = prismaticDef = new b2PrismaticJointDef(); //JointFactory.CreatePrismaticJoint(world, bodyA, bodyB, localAnchorB, localAxis); prismaticDef.localAnchorA = jsonToVec("anchorA", jointValue); prismaticDef.localAnchorB = jsonToVec("anchorB", jointValue); if (jointValue["localAxisA"] != null) prismaticDef.localAxisA = jsonToVec("localAxisA", jointValue); else prismaticDef.localAxisA = jsonToVec("localAxis1", jointValue); prismaticDef.referenceAngle = jsonToFloat("refAngle", jointValue); prismaticDef.enableLimit = jointValue["enableLimit"] == null ? false : (bool)jointValue["enableLimit"]; prismaticDef.lowerTranslation = jsonToFloat("lowerLimit", jointValue); prismaticDef.upperTranslation = jsonToFloat("upperLimit", jointValue); prismaticDef.enableMotor = jointValue["enableMotor"] == null ? false : (bool)jointValue["enableMotor"]; prismaticDef.motorSpeed = jsonToFloat("motorSpeed", jointValue); prismaticDef.maxMotorForce = jsonToFloat("maxMotorForce", jointValue); } else if (type == "distance") { jointDef = distanceDef = new b2DistanceJointDef(); distanceDef.localAnchorA = jsonToVec("anchorA", jointValue); distanceDef.localAnchorB = jsonToVec("anchorB", jointValue); distanceDef.length = jsonToFloat("length", jointValue); distanceDef.frequencyHz = jsonToFloat("frequency", jointValue); distanceDef.dampingRatio = jsonToFloat("dampingRatio", jointValue); } else if (type == "pulley") { jointDef = pulleyDef = new b2PulleyJointDef(); pulleyDef.groundAnchorA = jsonToVec("groundAnchorA", jointValue); pulleyDef.groundAnchorB = jsonToVec("groundAnchorB", jointValue); pulleyDef.localAnchorA = jsonToVec("anchorA", jointValue); pulleyDef.localAnchorB = jsonToVec("anchorB", jointValue); pulleyDef.lengthA = jsonToFloat("lengthA", jointValue); pulleyDef.lengthB = jsonToFloat("lengthB", jointValue); pulleyDef.ratio = jsonToFloat("ratio", jointValue); } else if (type == "mouse") { jointDef = mouseDef = new b2MouseJointDef(); mouseJointTarget = jsonToVec("target", jointValue); mouseDef.target = jsonToVec("anchorB", jointValue);// alter after creating joint mouseDef.maxForce = jsonToFloat("maxForce", jointValue); mouseDef.frequencyHz = jsonToFloat("frequency", jointValue); mouseDef.dampingRatio = jsonToFloat("dampingRatio", jointValue); } // Gear joints are apparently not implemented in JBox2D yet, but // when they are, commenting out the following section should work. else if (type == "gear") { jointDef = gearDef = new b2GearJointDef(); //JointFactory.CreateGearJoint(world, joint1, joint2, ratio); int jointIndex1 = (int)jointValue["joint1"]; int jointIndex2 = (int)jointValue["joint2"]; var joint1 = m_joints[jointIndex1]; var joint2 = m_joints[jointIndex2]; var ratio = jsonToFloat("ratio", jointValue); //joint = gearDef = JointFactory.CreateGearJoint(world, joint1, joint2, ratio); } // Wheel joints are apparently not implemented in JBox2D yet, but // when they are, commenting out the following section should work. else if (type == "wheel") { jointDef = revoluteDef = new b2RevoluteJointDef(); revoluteDef.localAnchorA = jsonToVec("anchorA", jointValue); revoluteDef.localAnchorB = jsonToVec("anchorB", jointValue); revoluteDef.enableMotor = jointValue["enableMotor"] == null ? false : (bool)jointValue["enableMotor"]; revoluteDef.motorSpeed = jsonToFloat("motorSpeed", jointValue); revoluteDef.maxMotorTorque = jsonToFloat("maxMotorTorque", jointValue); //jointDef = wheelDef = new b2WheelJointDef(); //JointFactory.CreateWheelJoint(world, bodyA, bodyB, localAnchorB, localAxisA); //var localAnchorA = jsonToVec("anchorA", jointValue); //var localAnchorB = (jsonToVec("anchorB", jointValue)); //var localAxisA = (jsonToVec("localAxisA", jointValue)); //var enableMotor = jointValue["enableMotor"] == null ? false : (bool)jointValue["enableMotor"]; //var motorSpeed = jsonToFloat("motorSpeed", jointValue); //var maxMotorTorque = jsonToFloat("maxMotorTorque", jointValue); //var frequencyHz = jsonToFloat("springFrequency", jointValue); //var dampingRatio = jsonToFloat("springDampingRatio", jointValue); //wheelDef.LocalAnchorA = localAnchorA; //wheelDef.LocalAnchorB = localAnchorB; //wheelDef.MotorEnabled = enableMotor; //wheelDef.MotorSpeed = motorSpeed; //wheelDef.SpringFrequencyHz = frequencyHz; //wheelDef.MaxMotorTorque = maxMotorTorque; //wheelDef.SpringDampingRatio = dampingRatio; } else if (type == "weld") { jointDef = weldDef = new b2WeldJointDef(); weldDef.localAnchorA = jsonToVec("anchorA", jointValue); weldDef.localAnchorB = jsonToVec("anchorB", jointValue); weldDef.referenceAngle = 0; } else if (type == "friction") { jointDef = frictionDef = new b2FrictionJointDef(); frictionDef.localAnchorA = jsonToVec("anchorA", jointValue); frictionDef.localAnchorB = jsonToVec("anchorB", jointValue); frictionDef.maxForce = jsonToFloat("maxForce", jointValue); frictionDef.maxTorque = jsonToFloat("maxTorque", jointValue); } else if (type == "rope") { jointDef = ropeDef = new b2RopeJointDef(); ropeDef.localAnchorA = jsonToVec("anchorA", jointValue); ropeDef.localAnchorB = jsonToVec("anchorB", jointValue); ropeDef.maxLength = jsonToFloat("maxLength", jointValue); } //else if (type == "motor") //{ // var maxForce = jsonToFloat("maxForce", jointValue); // var maxMotorTorque = jsonToFloat("maxTorque", jointValue); // var angularOffset = jsonToFloat("refAngle", jointValue); // joint = motorDef = new MotorJoint(bodyA, bodyB); // world.AddJoint(joint); // motorDef.LinearOffset = jsonToVec("anchorA", jointValue); // motorDef.MaxForce = maxForce; // motorDef.MaxTorque = maxMotorTorque; // motorDef.AngularOffset = angularOffset; //} if (null != jointDef) { // set features common to all joints jointDef.BodyA = m_bodies[bodyIndexA]; jointDef.BodyB = m_bodies[bodyIndexB]; jointDef.CollideConnected = jointValue["collideConnected"] == null ? false : (bool)jointValue["collideConnected"]; joint = world.CreateJoint(jointDef); if (type.Equals("mouse")) ((b2MouseJoint)joint).SetTarget(mouseJointTarget); String jointName = jointValue["name"] == null ? "" : (string)jointValue["name"]; if (!jointName.Equals("")) { SetJointName(joint, jointName); } } return joint; }