GearJoint constructor
GearJoint(
- GearJointDef<
Body, Body> def
Implementation
GearJoint(GearJointDef def)
: _joint1 = def.joint1,
_joint2 = def.joint2,
_bodyC = def.joint1.bodyA,
_bodyD = def.joint2.bodyA,
super(def) {
assert(
(_joint1 is RevoluteJoint || _joint1 is PrismaticJoint) &&
(_joint2 is RevoluteJoint || _joint2 is PrismaticJoint),
'A GearJoint can only be joint with RevoluteJoint and/or PrismaticJoint.',
);
double coordinateA;
double coordinateB;
// TODO_ERIN there might be some problem with the joint edges in Joint.
bodyA = _joint1.bodyB;
// Get geometry of joint1
final xfA = bodyA.transform;
final aA = bodyA.sweep.a;
final xfC = _bodyC.transform;
final aC = _bodyC.sweep.a;
if (_joint1 is RevoluteJoint) {
final revolute = def.joint1 as RevoluteJoint;
_localAnchorC.setFrom(revolute.localAnchorA);
localAnchorA.setFrom(revolute.localAnchorB);
_referenceAngleA = revolute.referenceAngle;
_localAxisC.setZero();
coordinateA = aA - aC - _referenceAngleA;
} else {
final pA = Vector2.zero();
final temp = Vector2.zero();
final prismatic = def.joint1 as PrismaticJoint;
_localAnchorC.setFrom(prismatic.localAnchorA);
localAnchorA.setFrom(prismatic.localAnchorB);
_referenceAngleA = prismatic.referenceAngle;
_localAxisC.setFrom(prismatic.localXAxisA);
final pC = _localAnchorC;
temp
..setFrom(Rot.mulVec2(xfA.q, localAnchorA))
..add(xfA.p)
..sub(xfC.p);
pA.setFrom(Rot.mulTransVec2(xfC.q, temp));
coordinateA = (pA..sub(pC)).dot(_localAxisC);
}
bodyB = _joint2.bodyB;
// Get geometry of joint2
final xfB = bodyB.transform;
final aB = bodyB.sweep.a;
final xfD = _bodyD.transform;
final aD = _bodyD.sweep.a;
if (_joint2 is RevoluteJoint) {
final revolute = def.joint2 as RevoluteJoint;
_localAnchorD.setFrom(revolute.localAnchorA);
localAnchorB.setFrom(revolute.localAnchorB);
_referenceAngleB = revolute.referenceAngle;
_localAxisD.setZero();
coordinateB = aB - aD - _referenceAngleB;
} else {
final pB = Vector2.zero();
final temp = Vector2.zero();
final prismatic = def.joint2 as PrismaticJoint;
_localAnchorD.setFrom(prismatic.localAnchorA);
localAnchorB.setFrom(prismatic.localAnchorB);
_referenceAngleB = prismatic.referenceAngle;
_localAxisD.setFrom(prismatic.localXAxisA);
final pD = _localAnchorD;
temp
..setFrom(Rot.mulVec2(xfB.q, localAnchorB))
..add(xfB.p)
..sub(xfD.p);
pB.setFrom(Rot.mulTransVec2(xfD.q, temp));
coordinateB = (pB..sub(pD)).dot(_localAxisD);
}
ratio = def.ratio;
_constant = coordinateA + ratio * coordinateB;
_impulse = 0.0;
}