From 8efb44efc8830ffc5ae8eb387926f8b4fd00fb6e Mon Sep 17 00:00:00 2001 From: Eleni Maria Stea Date: Sat, 26 Jan 2019 20:51:42 +0200 Subject: [PATCH] Replaced the collision detection with a check of the angle Instead of performing collision detection with a sphere, I now calculate the dot product between the normal and the hair and if it's negative (=> the hair falls inside the head) we calc the projection of the hair on the head and find the new position from that. --- src/hair.cc | 15 ++++++++++++++- src/main.cc | 8 ++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/hair.cc b/src/hair.cc index ef93f2c..b2eb4a0 100644 --- a/src/hair.cc +++ b/src/hair.cc @@ -187,7 +187,20 @@ void Hair::update(float dt) hair[i].velocity += ((-hair[i].velocity * DAMPING) + accel) * dt; Vec3 new_pos = hair[i].pos + hair[i].velocity * dt; - hair[i].pos = new_pos; //= handle_collision(new_pos); + /* 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; } diff --git a/src/main.cc b/src/main.cc index 852b7cb..0fceafe 100644 --- a/src/main.cc +++ b/src/main.cc @@ -33,7 +33,7 @@ static int win_width, win_height; static float cam_theta, cam_phi = 25, cam_dist = 8; static float head_rz, head_rx; /* rot angles x, z axis */ static Mat4 head_xform; -static CollSphere coll_sphere; /* sphere used for collision detection */ +//static CollSphere coll_sphere; /* sphere used for collision detection */ int main(int argc, char **argv) { @@ -105,15 +105,15 @@ static bool init() return false; } - coll_sphere.radius = 1.0; - coll_sphere.center = Vec3(0, 0.6, 0.53); +// coll_sphere.radius = 1.0; +// coll_sphere.center = Vec3(0, 0.6, 0.53); if(!hair.init(mesh_head, MAX_NUM_SPAWNS, THRESH)) { fprintf(stderr, "Failed to initialize hair\n"); return false; } - hair.add_collider(&coll_sphere); +// hair.add_collider(&coll_sphere); return true; } -- 1.7.10.4