//--------------- Internals Below ------------------- /** @private */ public b2GearJoint(b2GearJointDef def) : base(def) { int type1 = def.joint1.m_type; int type2 = def.joint2.m_type; //b2Settings.b2Assert(type1 == b2Joint.e_revoluteJoint || type1 == b2Joint.e_prismaticJoint); //b2Settings.b2Assert(type2 == b2Joint.e_revoluteJoint || type2 == b2Joint.e_prismaticJoint); //b2Settings.b2Assert(def.joint1.GetBodyA().GetType() == b2Body.b2_staticBody); //b2Settings.b2Assert(def.joint2.GetBodyA().GetType() == b2Body.b2_staticBody); m_revolute1 = null; m_prismatic1 = null; m_revolute2 = null; m_prismatic2 = null; float coordinate1; float coordinate2; m_ground1 = def.joint1.GetBodyA(); m_bodyA = def.joint1.GetBodyB(); if (type1 == b2Joint.e_revoluteJoint) { m_revolute1 = def.joint1 as b2RevoluteJoint; m_groundAnchor1.SetV(m_revolute1.m_localAnchor1); m_localAnchor1.SetV(m_revolute1.m_localAnchor2); coordinate1 = m_revolute1.GetJointAngle(); } else { m_prismatic1 = def.joint1 as b2PrismaticJoint; m_groundAnchor1.SetV(m_prismatic1.m_localAnchor1); m_localAnchor1.SetV(m_prismatic1.m_localAnchor2); coordinate1 = m_prismatic1.GetJointTranslation(); } m_ground2 = def.joint2.GetBodyA(); m_bodyB = def.joint2.GetBodyB(); if (type2 == b2Joint.e_revoluteJoint) { m_revolute2 = def.joint2 as b2RevoluteJoint; m_groundAnchor2.SetV(m_revolute2.m_localAnchor1); m_localAnchor2.SetV(m_revolute2.m_localAnchor2); coordinate2 = m_revolute2.GetJointAngle(); } else { m_prismatic2 = def.joint2 as b2PrismaticJoint; m_groundAnchor2.SetV(m_prismatic2.m_localAnchor1); m_localAnchor2.SetV(m_prismatic2.m_localAnchor2); coordinate2 = m_prismatic2.GetJointTranslation(); } m_ratio = def.ratio; m_constant = coordinate1 + m_ratio * coordinate2; m_impulse = 0.0f; }
public b2GearJoint(b2GearJointDef def) : base(def) { m_joint1 = def.joint1; m_joint2 = def.joint2; m_typeA = m_joint1.GetJointType(); m_typeB = m_joint2.GetJointType(); Debug.Assert(m_typeA == b2JointType.e_revoluteJoint || m_typeA == b2JointType.e_prismaticJoint); Debug.Assert(m_typeB == b2JointType.e_revoluteJoint || m_typeB == b2JointType.e_prismaticJoint); float coordinateA, coordinateB; // TODO_ERIN there might be some problem with the joint edges in b2Joint. m_bodyC = m_joint1.GetBodyA(); m_bodyA = m_joint1.GetBodyB(); // Get geometry of joint1 b2Transform xfA = m_bodyA.XF; float aA = m_bodyA.Sweep.a; b2Transform xfC = m_bodyC.XF; float aC = m_bodyC.Sweep.a; if (m_typeA == b2JointType.e_revoluteJoint) { b2RevoluteJoint revolute = (b2RevoluteJoint)def.joint1; m_localAnchorC = revolute.GetLocalAnchorA(); m_localAnchorA = revolute.GetLocalAnchorB(); m_referenceAngleA = revolute.GetReferenceAngle(); m_localAxisC.SetZero(); coordinateA = aA - aC - m_referenceAngleA; } else { b2PrismaticJoint prismatic = (b2PrismaticJoint)def.joint1; m_localAnchorC = prismatic.GetLocalAnchorA(); m_localAnchorA = prismatic.GetLocalAnchorB(); m_referenceAngleA = prismatic.GetReferenceAngle(); m_localAxisC = prismatic.GetLocalXAxisA(); b2Vec2 pC = m_localAnchorC; b2Vec2 pA = b2Math.b2MulT(xfC.q, b2Math.b2Mul(xfA.q, m_localAnchorA) + (xfA.p - xfC.p)); coordinateA = b2Math.b2Dot(pA - pC, m_localAxisC); } m_bodyD = m_joint2.GetBodyA(); m_bodyB = m_joint2.GetBodyB(); // Get geometry of joint2 b2Transform xfB = m_bodyB.XF; float aB = m_bodyB.Sweep.a; b2Transform xfD = m_bodyD.XF; float aD = m_bodyD.Sweep.a; if (m_typeB == b2JointType.e_revoluteJoint) { b2RevoluteJoint revolute = (b2RevoluteJoint)def.joint2; m_localAnchorD = revolute.GetLocalAnchorA(); m_localAnchorB = revolute.GetLocalAnchorB(); m_referenceAngleB = revolute.GetReferenceAngle(); m_localAxisD.SetZero(); coordinateB = aB - aD - m_referenceAngleB; } else { b2PrismaticJoint prismatic = (b2PrismaticJoint)def.joint2; m_localAnchorD = prismatic.GetLocalAnchorA(); m_localAnchorB = prismatic.GetLocalAnchorB(); m_referenceAngleB = prismatic.GetReferenceAngle(); m_localAxisD = prismatic.GetLocalXAxisA(); b2Vec2 pD = m_localAnchorD; b2Vec2 pB = b2Math.b2MulT(xfD.q, b2Math.b2Mul(xfB.q, m_localAnchorB) + (xfB.p - xfD.p)); coordinateB = b2Math.b2Dot(pB - pD, m_localAxisD); } m_ratio = def.ratio; m_constant = coordinateA + m_ratio * coordinateB; m_impulse = 0.0f; }
public Gears() { b2Body ground = null; { b2BodyDef bd = new b2BodyDef(); ground = m_world.CreateBody(bd); b2EdgeShape shape = new b2EdgeShape(); shape.Set(new b2Vec2(50.0f, 0.0f), new b2Vec2(-50.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } // Gears co { b2CircleShape circle1 = new b2CircleShape(); circle1.Radius = 1.0f; b2PolygonShape box = new b2PolygonShape(); box.SetAsBox(0.5f, 5.0f); b2CircleShape circle2 = new b2CircleShape(); circle2.Radius = 2.0f; b2BodyDef bd1 = new b2BodyDef(); bd1.type = b2BodyType.b2_staticBody; bd1.position.Set(10.0f, 9.0f); b2Body body1 = m_world.CreateBody(bd1); body1.CreateFixture(circle1, 0.0f); b2BodyDef bd2 = new b2BodyDef(); bd2.type = b2BodyType.b2_dynamicBody; bd2.position.Set(10.0f, 8.0f); b2Body body2 = m_world.CreateBody(bd2); body2.CreateFixture(box, 5.0f); b2BodyDef bd3 = new b2BodyDef(); bd3.type = b2BodyType.b2_dynamicBody; bd3.position.Set(10.0f, 6.0f); b2Body body3 = m_world.CreateBody(bd3); body3.CreateFixture(circle2, 5.0f); b2RevoluteJointDef jd1 = new b2RevoluteJointDef(); jd1.Initialize(body2, body1, bd1.position); b2Joint joint1 = m_world.CreateJoint(jd1); b2RevoluteJointDef jd2 = new b2RevoluteJointDef(); jd2.Initialize(body2, body3, bd3.position); b2Joint joint2 = m_world.CreateJoint(jd2); b2GearJointDef jd4 = new b2GearJointDef(); jd4.BodyA = body1; jd4.BodyB = body3; jd4.joint1 = joint1; jd4.joint2 = joint2; jd4.ratio = circle2.Radius / circle1.Radius; m_world.CreateJoint(jd4); } { b2CircleShape circle1 = new b2CircleShape(); circle1.Radius = 1.0f; b2CircleShape circle2 = new b2CircleShape(); circle2.Radius = 2.0f; b2PolygonShape box = new b2PolygonShape(); box.SetAsBox(0.5f, 5.0f); b2BodyDef bd1 = new b2BodyDef(); bd1.type = b2BodyType.b2_dynamicBody; bd1.position.Set(-3.0f, 12.0f); b2Body body1 = m_world.CreateBody(bd1); body1.CreateFixture(circle1, 5.0f); b2RevoluteJointDef jd1 = new b2RevoluteJointDef(); jd1.BodyA = ground; jd1.BodyB = body1; jd1.localAnchorA = ground.GetLocalPoint(bd1.position); jd1.localAnchorB = body1.GetLocalPoint(bd1.position); jd1.referenceAngle = body1.Angle - ground.Angle; m_joint1 = (b2RevoluteJoint) m_world.CreateJoint(jd1); b2BodyDef bd2 = new b2BodyDef(); bd2.type = b2BodyType.b2_dynamicBody; bd2.position.Set(0.0f, 12.0f); b2Body body2 = m_world.CreateBody(bd2); body2.CreateFixture(circle2, 5.0f); b2RevoluteJointDef jd2 = new b2RevoluteJointDef(); jd2.Initialize(ground, body2, bd2.position); m_joint2 = (b2RevoluteJoint) m_world.CreateJoint(jd2); b2BodyDef bd3 = new b2BodyDef(); bd3.type = b2BodyType.b2_dynamicBody; bd3.position.Set(2.5f, 12.0f); b2Body body3 = m_world.CreateBody(bd3); body3.CreateFixture(box, 5.0f); b2PrismaticJointDef jd3 = new b2PrismaticJointDef(); jd3.Initialize(ground, body3, bd3.position, new b2Vec2(0.0f, 1.0f)); jd3.lowerTranslation = -5.0f; jd3.upperTranslation = 5.0f; jd3.enableLimit = true; m_joint3 = (b2PrismaticJoint) m_world.CreateJoint(jd3); b2GearJointDef jd4 = new b2GearJointDef(); jd4.BodyA = body1; jd4.BodyB = body2; jd4.joint1 = m_joint1; jd4.joint2 = m_joint2; jd4.ratio = circle2.Radius / circle1.Radius; m_joint4 = (b2GearJoint) m_world.CreateJoint(jd4); b2GearJointDef jd5 = new b2GearJointDef(); jd5.BodyA = body2; jd5.BodyB = body3; jd5.joint1 = m_joint2; jd5.joint2 = m_joint3; jd5.ratio = -1.0f / circle2.Radius; m_joint5 = (b2GearJoint) m_world.CreateJoint(jd5); } }
// 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; }