cloth2

diff src/cloth.cc @ 1:dc15b741486c

exploding cloth
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 11 Jan 2016 20:24:13 +0200
parents ef0c22554406
children b4b8f736332b
line diff
     1.1 --- a/src/cloth.cc	Mon Jan 11 16:51:16 2016 +0200
     1.2 +++ b/src/cloth.cc	Mon Jan 11 20:24:13 2016 +0200
     1.3 @@ -9,6 +9,7 @@
     1.4  	mass = 1.0;
     1.5  	elast = 0.8;
     1.6  	spring_k = 1.0;
     1.7 +	damping = 0.01;
     1.8  	grav = Vector3(0, -9, 0);
     1.9  }
    1.10  
    1.11 @@ -31,7 +32,7 @@
    1.12  			masses[idx].pos.x = (float)j * dx - width / 2.0;
    1.13  			masses[idx].pos.y = 0;
    1.14  			masses[idx].pos.z = (float)i * dy - height / 2.0;
    1.15 -			masses[idx].fixed = i == 0 && (j == 0 || j == x - 1);	// XXX debug hack
    1.16 +			masses[idx].fixed = i == 0;	// XXX debug hack
    1.17  			++idx;
    1.18  		}
    1.19  	}
    1.20 @@ -43,10 +44,10 @@
    1.21  			conn[idx][idx] = -1;
    1.22  
    1.23  			// connect node to its neighbors
    1.24 -			for(int ni=0; ni<5; ni++) {
    1.25 -				for(int nj=0; nj<5; nj++) {
    1.26 -					int nx = j + nj - 2;
    1.27 -					int ny = i + ni - 2;
    1.28 +			for(int ni=0; ni<3; ni++) {
    1.29 +				for(int nj=0; nj<3; nj++) {
    1.30 +					int nx = j + nj - 1;
    1.31 +					int ny = i + ni - 1;
    1.32  
    1.33  					if(!nx && !ny) continue;
    1.34  					if(nx < 0 || nx >= x || ny < 0 || ny >= y) continue;
    1.35 @@ -63,6 +64,36 @@
    1.36  	}
    1.37  }
    1.38  
    1.39 +void Cloth::set_mass(float m)
    1.40 +{
    1.41 +	mass = m;
    1.42 +}
    1.43 +
    1.44 +void Cloth::set_elasticity(float e)
    1.45 +{
    1.46 +	elast = e;
    1.47 +}
    1.48 +
    1.49 +void Cloth::set_spring_constant(float k)
    1.50 +{
    1.51 +	spring_k = k;
    1.52 +}
    1.53 +
    1.54 +void Cloth::set_damping(float d)
    1.55 +{
    1.56 +	damping = d;
    1.57 +}
    1.58 +
    1.59 +void Cloth::set_gravity(const Vector3 &g)
    1.60 +{
    1.61 +	grav = g;
    1.62 +}
    1.63 +
    1.64 +void Cloth::set_force(const Vector3 &f)
    1.65 +{
    1.66 +	forces = f;
    1.67 +}
    1.68 +
    1.69  void Cloth::transform(const Matrix4x4 &xform)
    1.70  {
    1.71  	for(size_t i=0; i<masses.size(); i++) {
    1.72 @@ -79,25 +110,35 @@
    1.73  {
    1.74  	int num_masses = (int)masses.size();
    1.75  
    1.76 +	Vector3 *newp = new Vector3[num_masses];
    1.77 +
    1.78  	for(int i=0; i<num_masses; i++) {
    1.79 -		if(masses[i].fixed) continue;
    1.80 -
    1.81 -		Vector3 newpos = masses[i].pos + masses[i].vel * dt;
    1.82 +		if(masses[i].fixed) {
    1.83 +			newp[i] = masses[i].pos;
    1.84 +			continue;
    1.85 +		}
    1.86  
    1.87  		Vector3 force = grav + forces;
    1.88  
    1.89  		for(int j=0; j<num_masses; j++) {
    1.90 -			if(conn[i][j] <= 0.0) continue;	// skip unconnected
    1.91 +			if(i == j || conn[i][j] <= 0.0) {
    1.92 +				continue;	// skip unconnected
    1.93 +			}
    1.94  
    1.95  			Vector3 dir = masses[j].pos - masses[i].pos;
    1.96  			float dist = length(dir);
    1.97 +			float disp = dist - conn[i][j];
    1.98 +			if(fabs(disp) < 1e-5) disp = 0.0f;
    1.99  
   1.100  			// apply an impulse based on the displacement from the original state
   1.101 -			force += dir * (dist - conn[i][j]) * spring_k;
   1.102 +			force += dir * ((disp * spring_k) / dist * 0.5);
   1.103  		}
   1.104  
   1.105  		// update the velocity by integrating the accumulation of all forces
   1.106 -		masses[i].vel += force * dt;
   1.107 +		Vector3 accel = force / mass;
   1.108 +		masses[i].vel = masses[i].vel * (1.0 - damping) + accel * dt;
   1.109 +
   1.110 +		Vector3 newpos = masses[i].pos + masses[i].vel * dt;
   1.111  
   1.112  		Ray ray;
   1.113  		ray.origin = masses[i].pos;
   1.114 @@ -108,8 +149,15 @@
   1.115  			newpos = pt.pos;
   1.116  			masses[i].vel = reflect(masses[i].vel, pt.normal) * elast;	// TODO collider elasticity
   1.117  		}
   1.118 -		masses[i].pos = newpos;
   1.119 +		//masses[i].pos = newpos;
   1.120 +		newp[i] = newpos;
   1.121  	}
   1.122 +
   1.123 +	for(int i=0; i<num_masses; i++) {
   1.124 +		masses[i].pos = newp[i];
   1.125 +	}
   1.126 +
   1.127 +	delete [] newp;
   1.128  }
   1.129  
   1.130  bool Cloth::find_collision(const Ray &ray, HitPoint *hit) const