+void Gear::attach(Gear *g)
+{
+ if(g->supergear) {
+ if(g->supergear == this) {
+ return;
+ }
+ g->supergear->detach(g);
+ }
+ g->supergear = this;
+ subgears.push_back(g);
+
+ // make co-axial
+ g->axis = axis;
+ g->pos.x = pos.x;
+ g->pos.y = pos.y;
+}
+
+bool Gear::detach(Gear *g)
+{
+ int nsubgears = (int)subgears.size();
+ for(int i=0; i<nsubgears; i++) {
+ if(subgears[i] == g) {
+ subgears.erase(subgears.begin() + i);
+ g->supergear = 0;
+ return true;
+ }
+ }
+ return false;
+}
+
+Gear *Gear::get_super() const
+{
+ return supergear;
+}
+
+void Gear::set_angular_offset(float offs)
+{
+ init_angle = offs;
+ xform_valid = false;
+}
+
+float Gear::get_angular_offset() const
+{
+ return init_angle;
+}
+
+void Gear::set_teeth(int nt, float tooth_pitch)
+{
+ if(tooth_pitch <= 0) {
+ tooth_pitch = this->tooth_pitch;
+ } else {
+ this->tooth_pitch = tooth_pitch;
+ }
+ float circ = tooth_pitch * nt;
+ radius = circ / (2.0 * M_PI);
+ nteeth = nt;
+}
+
+void Gear::set_axis(const Vec3 &axis)
+{
+ this->axis = normalize(axis);
+ xform_valid = false;
+}
+
+const Vec3 &Gear::get_axis() const
+{
+ return axis;
+}
+
+void Gear::set_position(const Vec3 &pos)
+{
+ if(!supergear) {
+ this->pos = pos;
+ } else {
+ this->pos = supergear->pos;
+ this->pos.z = pos.z;
+ }
+ xform_valid = false;
+
+ for(int i=0; i<(int)subgears.size(); i++) {
+ Vec3 subpos = this->pos;
+ subpos.z = subgears[i]->pos.z;
+ subgears[i]->set_position(subpos);
+ }
+}
+
+const Vec3 &Gear::get_position() const
+{
+ return pos;
+}
+
+Vec3 Gear::get_global_position() const
+{
+ const Mat4 &m = get_matrix();
+ return m * Vec3(0, 0, 0);
+}
+
+void Gear::set_angle(float angle)
+{
+ this->angle = angle;
+ xform_valid = false;
+}
+
+float Gear::get_angle() const
+{
+ return angle;
+}
+
+float Gear::get_vis_rotation() const
+{
+ return fmod(init_angle + angle, M_PI * 2.0);
+}
+
+const Mat4 &Gear::get_matrix() const
+{
+ if(!xform_valid) {
+ calc_matrix();
+ xform_valid = true;
+ }
+ return xform;
+}
+
+const Mat4 &Gear::get_dir_matrix() const
+{
+ if(!xform_valid) {
+ calc_matrix();
+ xform_valid = true;
+ }
+ return dir_xform;
+}
+
+float Gear::get_angular_pitch() const
+{
+ return 2.0 * M_PI / (float)nteeth;
+}
+