Gear joints are one of the real specialities of Box2D.
By creating gear joints between two bodies you can actually simulate a real world gear. For creating a gear joint you need two bodies connected by a revolute or prismatic joint.
The below code creates two bodies in the shape of a circle which is actually attached to the ground. however you can also create joints without joining it with the ground. Here you have to make a ground with possible four fixtures or no fixtures simply creating a body.
b2Body* body1 = circle1; b2Vec2 pos1 = circle1 -> GetPosition();//get position of first body..... b2Body* body2 = circle2; b2Vec2 pos2 = circle2 -> GetPosition(); // get position of second body.... revJointDef.Initialize(ground, circle1, pos1); b2RevoluteJointDef revJointDef2; revJointDef2.Initialize(ground, body2, pos2)//revolute joint is initialized joint1 = world->CreateJoint(&revJointDef;); //First joint..... revJointDef2.maxMotorTorque = 20000.000000f; //connect a motor to the joint2 revJointDef2.motorSpeed = 120.000000f / 60; //set the rotation speed..... revJointDef2.enableMotor = true; // enable the motor..... joint2 = world->CreateJoint(&revJointDef2;); //Second joint..... gearJointDef.bodyA = body1; gearJointDef.bodyA = body2; gearJointDef.joint1 = joint1; gearJointDef.joint2 = joint2; gearJointDef.collideConnected = true; gearJointDef.ratio = circleShape2.m_radius/circleShape1.m_radius; world->CreateJoint(&gearJointDef;); // this line creates the joint.
Please leave your valuable comments……………………..