GEAR JOINT

21
Jun
2012

If you want to create a sophisticated mechanical contraption you might want to use gears. In principle you can create gears in Box2D by using compound shapes to model gear teeth. This is not very efficient and might be tedious to author. You also have to be careful to line up the gears so the teeth mesh smoothly. Box2D has a simpler method of creating gears: the gear joint.

The gear joint can only connect revolute and/or prismatic joints.

Like the pulley ratio, you can specify a gear ratio. However, in this case the gear ratio can be negative. Also keep in mind that when one joint is a revolute joint (angular) and the other joint is prismatic (translation), and then the gear ratio will have units of length or one over length.

coordinate1 + ratio * coordinate2 == constant

Make sure the following codes are at the beginning.
```var    b2Vec2 = Box2D.Common.Math.b2Vec2
,      b2BodyDef = Box2D.Dynamics.b2BodyDef
,      b2Body = Box2D.Dynamics.b2Body
,      b2FixtureDef = Box2D.Dynamics.b2FixtureDef
,      b2World = Box2D.Dynamics.b2World
,      b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape
,      b2CircleShape = Box2D.Collision.Shapes.b2CircleShape
,      b2MouseJointDef =  Box2D.Dynamics.Joints.b2MouseJointDef
,	   b2GearJointDef = Box2D.Dynamics.Joints.b2GearJointDef
,	   b2PrismaticJointDef = Box2D.Dynamics.Joints.b2PrismaticJointDef
,      b2RevoluteJointDef = Box2D.Dynamics.Joints.b2RevoluteJointDef
,	   b2WeldJointDef = Box2D.Dynamics.Joints.b2WeldJointDeff
,      b2DebugDraw = Box2D.Dynamics.b2DebugDraw
,      b2Fixture = Box2D.Dynamics.b2Fixture
,      b2AABB = Box2D.Collision.b2AABB;
```
Line 8, 9 and 10 should be present.

### Gear Joint Definition

```// Revolute Joint
var revoluteJointDef = new b2RevoluteJointDef();
revoluteJointDef.Initialize(world.GetGroundBody(), wheel, wheel.GetWorldCenter());  //ground must be the first body
revoluteJointDef.maxMotorTorque = 1100.0;
revoluteJointDef.motorSpeed = 3.0;
revoluteJointDef.enableMotor = true;
revoluteJoint = world.CreateJoint(revoluteJointDef);

// Prismatic Joint
var worldAxis = new b2Vec2(0, 1);
var prismaticJointDef = new b2PrismaticJointDef();
prismaticJointDef.Initialize(world.GetGroundBody(), box, box.GetWorldCenter(), worldAxis);  //ground must be the first body
prismaticJointDef.lowerTranslation = -5.0;
prismaticJointDef.upperTranslation = 2.5;
prismaticJointDef.enableLimit = true;
prismaticJoint = world.CreateJoint(prismaticJointDef);

// Finally Gear Joint
var gearJointDef = new b2GearJointDef();
gearJointDef.joint1 = revoluteJoint;
gearJointDef.joint2 = prismaticJoint;
gearJointDef.bodyA = box;
gearJointDef.bodyB = wheel;
gearJointDef.ratio = 1;
gearJoint = world.CreateJoint(gearJointDef);
```
I have created a "Revolute Joint" between a Ground Body and wheel. Then a "Prismatic Joint" between a Ground Body and box. And finally "Gear Joint" between box and wheel using the two joints previously created. The definition for gear joint is quite simple so no need to explain.

The world provides a single static ground body with no collision shapes. You can use this to simplify the creation of joints and static shapes.

```world.GetGroundBody()
```

(Try playing with mouse)

By administrator at 10:55:44 PM 13 Comment(s)

 By 9TbWeL71ed on 12 Oct, 2016 at 03:23:46 PM
 By HfAhEZcI on 13 Oct, 2016 at 09:34:01 AM
 By GEmAunDo6xTm on 26 Oct, 2016 at 12:41:56 PM
 By N2tpkFqWAe on 27 Oct, 2016 at 07:00:53 AM
 takes points car insurance quotes been advances something NJ car insurance receive sex car insurance free quotes however two premiums http://www.autoinsurancequotespl.us/ encourage than purchasing discount car insurance benz By u6bVPUjnW on 31 Oct, 2016 at 12:40:29 PM
 By pWNVpNzpexk on 5 Nov, 2016 at 02:27:26 AM
 By PS5tHIZ1W on 12 Jan, 2017 at 07:18:52 PM
 By bmQGuMvXz on 15 Jan, 2017 at 09:26:32 AM
 By lXLE1BlYV on 16 Feb, 2017 at 05:35:40 PM