X-Git-Url: https://eleni.mutantstargoat.com/git/?p=hair;a=blobdiff_plain;f=src%2Fhair.cc;h=b2eb4a058f2731d3ffa216ec7dbc1f3d7000a6cb;hp=2431f509cf7669331698b5327741f2035d4f6007;hb=8efb44efc8830ffc5ae8eb387926f8b4fd00fb6e;hpb=2c4aeaa83ca06d604c084f1fe146ee7f51e30f90 diff --git a/src/hair.cc b/src/hair.cc index 2431f50..b2eb4a0 100644 --- a/src/hair.cc +++ b/src/hair.cc @@ -8,6 +8,11 @@ #include "kdtree.h" #include "hair.h" +/* spring constant */ + +#define K_ANC 4.0 +#define DAMPING 1.5 + struct Triangle { Vec3 v[3]; Vec3 n[3]; @@ -122,56 +127,44 @@ bool Hair::init(const Mesh *m, int max_num_spawns, float thresh) for(size_t i=0; i 0.99)) { - vi = Vec3(0, -1, 0); - } - Vec3 vj = normalize(cross(vk, vi)); - vi = cross(vj, vk); - - /* identity when the hair points to the z axis */ - Mat4 basis = Mat4(vi, vj, vk); - - for(int j=0; j<3; j++) { - float angle = (float)j / 3.0 * 2 * M_PI; - /* dir of each anchor relative to hair end */ - Vec3 dir = Vec3(cos(angle), sin(angle), 0); - dir = basis * dir; - hair[i].anchor_dirs[j] = hair[i].pos + dir - hair[i].spawn_pt; - } } return true; } +static Vec3 dbg_force; void Hair::draw() const { glPushAttrib(GL_ENABLE_BIT); // glDisable(GL_DEPTH_TEST); glDisable(GL_LIGHTING); glPointSize(5); - glLineWidth(2); + glLineWidth(3); glBegin(GL_LINES); for(size_t i=0; ixform = xform; } + +void Hair::update(float dt) +{ + for(size_t i = 0; i < hair.size(); i++) { + /* in local space */ + Vec3 hair_end = hair[i].spawn_pt + hair[i].spawn_dir * hair_length; + Vec3 anchor = xform * hair_end; + + Vec3 force = (anchor - hair[i].pos) * K_ANC; + + Vec3 accel = force; /* mass 1 */ + hair[i].velocity += ((-hair[i].velocity * DAMPING) + accel) * dt; + Vec3 new_pos = hair[i].pos + hair[i].velocity * dt; + + /* collision detection with the head */ + Vec3 normal = xform.upper3x3() * hair[i].spawn_dir; + Vec3 root = xform * hair[i].spawn_pt; + Vec3 dir = new_pos - root; + + normal.normalize(); + + /* angle that will cause the hair to be rendered inside the head */ + float d = dot(dir, normal); + if(d < 0) { + new_pos += -d * normal; + } + + hair[i].pos = handle_collision(new_pos); + + dbg_force = force; + } +} + +void Hair::add_collider(CollSphere *cobj) { + colliders.push_back(cobj); +} + +Vec3 Hair::handle_collision(const Vec3 &v) const +{ + /* if we transform the center and the radius of the collider sphere + * we might end up with a spheroid, so better just multiply the + * position with the inverse transform before check for collisions :*/ + + Vec3 new_v = inverse(xform) * v; + + for(size_t i=0; icontains(new_v)) { + new_v = colliders[i]->project_surf(new_v); + } + } + return xform * new_v; +}