X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=antikythera;a=blobdiff_plain;f=src%2Fmachine.cc;h=913b0b0269405c64c7e3febbe78000f28600ea2b;hp=274362a3a60f12e6e66a5e47e918d3c2a282e6ab;hb=014fccdee30291407985658e8cefd9be7dc9e9fa;hpb=2c8e92970f198061a0cefdb59c2d0ec8c58409c9 diff --git a/src/machine.cc b/src/machine.cc index 274362a..913b0b0 100644 --- a/src/machine.cc +++ b/src/machine.cc @@ -1,6 +1,7 @@ #include #include #include +#include #include #include "machine.h" @@ -63,10 +64,10 @@ void Machine::calc_meshing() visited = new bool[ngears]; } - // we're going to need the inverse of each gear's matrix, so let's cache it here - Mat4 *inv_xform = (Mat4*)alloca(ngears * sizeof *inv_xform); + // we're going to need the planar position of each gear on its plane, so let's cache it + Vec3 *ppos = (Vec3*)alloca(ngears * sizeof *ppos); for(int i=0; iget_dir_matrix()); + ppos[i] = gears[i]->get_planar_position(); } for(int i=0; iaxis, gears[j]->axis)) < 1e-5) { // co-planar, just check Z range after inverse-transforming to the XY plane - Vec3 pos_i = inv_xform[i] * gears[i]->get_position(); - Vec3 pos_j = inv_xform[j] * gears[j]->get_position(); - - if(fabs(pos_i.z - pos_j.z) > (gears[i]->thickness + gears[j]->thickness) / 2.0) { + if(fabs(ppos[i].z - ppos[j].z) > (gears[i]->thickness + gears[j]->thickness) / 2.0) { continue; } // Z interval match, check distance - float dsq = length_sq(pos_i.xy() - pos_j.xy()); + float dsq = length_sq(ppos[i].xy() - ppos[j].xy()); float outer_rad_sum = gears[i]->radius + gears[j]->radius; float inner_rad_sum = outer_rad_sum - gears[i]->teeth_length - gears[j]->teeth_length; @@ -107,12 +105,19 @@ void Machine::calc_meshing() // fix the initial angles so that teeth mesh as best as possible // should work in one pass as long as the gear train is not impossible for(int i=0; iinit_angle = 0; + } + + for(int i=0; iinit_angle / gears[i]->get_angular_pitch() + 1.0, 1.0); - float frac_j = fmod(gears[j]->init_angle / gears[j]->get_angular_pitch() + 1.0, 1.0); + float frac_j = fmod((gears[j]->init_angle + rel_angle) / gears[j]->get_angular_pitch() + 1.0, 1.0); float delta = frac_j - frac_i; float correction = 0.5 - delta; @@ -120,6 +125,12 @@ void Machine::calc_meshing() } } } + + /* + for(int i=0; iinit_angle); + } + */ } void Machine::update_gear(int idx, float angle) @@ -128,6 +139,7 @@ void Machine::update_gear(int idx, float angle) if(delta_angle(angle, gears[idx]->angle) > 0.25 / gears[idx]->nteeth) { fprintf(stderr, "warning: trying to transmit different values to gear %s (%d)\n", gears[idx]->name.c_str(), idx); + gears[idx]->angle = 0; } return; } @@ -170,6 +182,29 @@ void Machine::draw() const } } +Gear *Machine::intersect_gear(const Ray &ray, HitPoint *hitp) const +{ + Gear *res = 0; + HitPoint nearest; + nearest.dist = FLT_MAX; + + for(size_t i=0; iget_global_position(); + float rad = gears[i]->radius; + + Plane plane = Plane(pos, gears[i]->axis); + + HitPoint hit; + if(plane.intersect(ray, &hit) && hit.dist < nearest.dist && + length_sq(hit.pos - pos) <= rad * rad) { + nearest = hit; + res = gears[i]; + } + } + + if(hitp) *hitp = nearest; + return res; +} static float delta_angle(float a, float b) {