# HG changeset patch # User John Tsiombikas # Date 1452536653 -7200 # Node ID dc15b741486c40a6179dd4710464bb8facc4427a # Parent ef0c22554406d8b8fadd1ba5305aaf782cdf2486 exploding cloth diff -r ef0c22554406 -r dc15b741486c src/cloth.cc --- a/src/cloth.cc Mon Jan 11 16:51:16 2016 +0200 +++ b/src/cloth.cc Mon Jan 11 20:24:13 2016 +0200 @@ -9,6 +9,7 @@ mass = 1.0; elast = 0.8; spring_k = 1.0; + damping = 0.01; grav = Vector3(0, -9, 0); } @@ -31,7 +32,7 @@ masses[idx].pos.x = (float)j * dx - width / 2.0; masses[idx].pos.y = 0; masses[idx].pos.z = (float)i * dy - height / 2.0; - masses[idx].fixed = i == 0 && (j == 0 || j == x - 1); // XXX debug hack + masses[idx].fixed = i == 0; // XXX debug hack ++idx; } } @@ -43,10 +44,10 @@ conn[idx][idx] = -1; // connect node to its neighbors - for(int ni=0; ni<5; ni++) { - for(int nj=0; nj<5; nj++) { - int nx = j + nj - 2; - int ny = i + ni - 2; + for(int ni=0; ni<3; ni++) { + for(int nj=0; nj<3; nj++) { + int nx = j + nj - 1; + int ny = i + ni - 1; if(!nx && !ny) continue; if(nx < 0 || nx >= x || ny < 0 || ny >= y) continue; @@ -63,6 +64,36 @@ } } +void Cloth::set_mass(float m) +{ + mass = m; +} + +void Cloth::set_elasticity(float e) +{ + elast = e; +} + +void Cloth::set_spring_constant(float k) +{ + spring_k = k; +} + +void Cloth::set_damping(float d) +{ + damping = d; +} + +void Cloth::set_gravity(const Vector3 &g) +{ + grav = g; +} + +void Cloth::set_force(const Vector3 &f) +{ + forces = f; +} + void Cloth::transform(const Matrix4x4 &xform) { for(size_t i=0; i masses; @@ -30,6 +31,13 @@ void create_rect(int x, int y, float width, float height); + void set_mass(float m); + void set_elasticity(float e); + void set_spring_constant(float k); + void set_damping(float d); + void set_gravity(const Vector3 &g); + void set_force(const Vector3 &f); + void transform(const Matrix4x4 &xform); void add_collider(Object *o); diff -r ef0c22554406 -r dc15b741486c src/main.cc --- a/src/main.cc Mon Jan 11 16:51:16 2016 +0200 +++ b/src/main.cc Mon Jan 11 20:24:13 2016 +0200 @@ -19,7 +19,7 @@ void mouse(int bn, int st, int x, int y); void motion(int x, int y); -float cam_theta, cam_phi, cam_dist = 8; +float cam_theta, cam_phi = 25, cam_dist = 10; Cloth cloth; @@ -55,13 +55,17 @@ //glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); - cloth.create_rect(32, 32, 10, 10); + cloth.create_rect(4, 4, 10, 10); Matrix4x4 xform = Matrix4x4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, - 0, 1, 0, 1); + 0, 1, 1, 1); cloth.transform(xform); + cloth.set_mass(0.1); + cloth.set_spring_constant(0.1); + cloth.set_gravity(Vector3(0, -0.1, 0)); + cloth.set_damping(0.1); return true; @@ -73,6 +77,14 @@ void display() { + static unsigned int prev_upd; + unsigned int msec = glutGet(GLUT_ELAPSED_TIME); + + if(!prev_upd) prev_upd = msec; + float dt = (msec - prev_upd) / 1000.0f; + + cloth.step(dt); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW);