[Bf-blender-cvs] [a0333b5] temp_merge_gooseberry_hair: Some initial collision code, without actual response forces still.

Lukas Tönne noreply at git.blender.org
Mon Jan 19 20:47:39 CET 2015


Commit: a0333b5afdbbacf26557827fcf0f7e51e2895853
Author: Lukas Tönne
Date:   Sat Aug 30 15:23:40 2014 +0200
Branches: temp_merge_gooseberry_hair
https://developer.blender.org/rBa0333b5afdbbacf26557827fcf0f7e51e2895853

Some initial collision code, without actual response forces still.

This is still using the old BVH tree collision methods to generate
contact points, similar to what cloth does. This should be replaced
by a Bullet collision check, but generating contacts in this way is
easier for now, and lets us test responses and stability (although in
more complex collision cases the BVH method fails utterly, beside being
terribly inefficient with many colliders).

===================================================================

M	source/blender/blenkernel/BKE_cloth.h
M	source/blender/blenkernel/BKE_collision.h
M	source/blender/blenkernel/intern/collision.c
M	source/blender/blenkernel/intern/implicit.c
M	source/blender/blenkernel/intern/particle_system.c
M	source/blender/makesrna/intern/rna_particle.c

===================================================================

diff --git a/source/blender/blenkernel/BKE_cloth.h b/source/blender/blenkernel/BKE_cloth.h
index a6e1058..e4fa0b06 100644
--- a/source/blender/blenkernel/BKE_cloth.h
+++ b/source/blender/blenkernel/BKE_cloth.h
@@ -159,6 +159,7 @@ typedef enum {
 typedef enum {
 	CLOTH_COLLSETTINGS_FLAG_ENABLED = ( 1 << 1 ), /* enables cloth - object collisions */
 	CLOTH_COLLSETTINGS_FLAG_SELF = ( 1 << 2 ), /* enables selfcollisions */
+	CLOTH_COLLSETTINGS_FLAG_POINTS = ( 1 << 3 ), /* enables point collisions (hair) */
 } CLOTH_COLLISIONSETTINGS_FLAGS;
 
 /* Spring types as defined in the paper.*/
@@ -183,6 +184,7 @@ typedef enum {
 
 // needed for implicit.c
 int cloth_bvh_objcollision (struct Object *ob, struct ClothModifierData *clmd, float step, float dt );
+int cloth_points_objcollision(struct Object *ob, struct ClothModifierData *clmd, float step, float dt);
 
 ////////////////////////////////////////////////
 
diff --git a/source/blender/blenkernel/BKE_collision.h b/source/blender/blenkernel/BKE_collision.h
index ec257a2..dab5631 100644
--- a/source/blender/blenkernel/BKE_collision.h
+++ b/source/blender/blenkernel/BKE_collision.h
@@ -81,6 +81,8 @@ typedef struct CollPair {
 	float pa[3], pb[3]; // collision point p1 on face1, p2 on face2
 	int flag;
 	float time; // collision time, from 0 up to 1
+
+	/* mesh-mesh collision */
 #ifdef WITH_ELTOPO /*either ap* or bp* can be set, but not both*/
 	float bary[3];
 	int ap1, ap2, ap3, collp, bp1, bp2, bp3;
@@ -89,6 +91,9 @@ typedef struct CollPair {
 	int ap1, ap2, ap3, bp1, bp2, bp3;
 #endif
 	int pointsb[4];
+
+	/* hair collision */
+	float va[3], vb[3]; /*  */
 }
 CollPair;
 
diff --git a/source/blender/blenkernel/intern/collision.c b/source/blender/blenkernel/intern/collision.c
index 3030ee4..b38609e 100644
--- a/source/blender/blenkernel/intern/collision.c
+++ b/source/blender/blenkernel/intern/collision.c
@@ -652,8 +652,8 @@ static void cloth_bvh_objcollisions_nearcheck ( ClothModifierData * clmd, Collis
 	*collisions_index = *collisions;
 
 	for ( i = 0; i < numresult; i++ ) {
-		*collisions_index = cloth_collision ( (ModifierData *)clmd, (ModifierData *)collmd,
-		                                      overlap+i, *collisions_index, dt );
+		*collisions_index = cloth_collision((ModifierData *)clmd, (ModifierData *)collmd,
+		                                    overlap+i, *collisions_index, dt);
 	}
 }
 
@@ -913,3 +913,433 @@ int cloth_bvh_objcollision(Object *ob, ClothModifierData *clmd, float step, floa
 
 	return 1|MIN2 ( ret, 1 );
 }
+
+
+static int cloth_points_collision_response_static(ClothModifierData *clmd, CollisionModifierData *collmd, CollPair *collpair, CollPair *collision_end)
+{
+	int result = 0;
+	Cloth *cloth1;
+	float w1, w2, u1, u2, u3;
+	float v1[3], v2[3], relativeVelocity[3];
+	float magrelVel;
+	float epsilon2 = BLI_bvhtree_getepsilon ( collmd->bvhtree );
+
+	cloth1 = clmd->clothObject;
+
+	for ( ; collpair != collision_end; collpair++ ) {
+//		float i1[3], i2[3], i3[3];
+
+//		zero_v3(i1);
+//		zero_v3(i2);
+//		zero_v3(i3);
+
+		/* only handle static collisions here */
+		if ( collpair->flag & COLLISION_IN_FUTURE )
+			continue;
+
+		/* compute barycentric coordinates for both collision points */
+		w1 = 1.0f - collpair->time;
+		w2 = collpair->time;
+
+		/* was: txold */
+		collision_compute_barycentric ( collpair->pb,
+			collmd->current_x[collpair->bp1].co,
+			collmd->current_x[collpair->bp2].co,
+			collmd->current_x[collpair->bp3].co,
+			&u1, &u2, &u3 );
+
+		/* Calculate relative velocity */
+		copy_v3_v3(v1, cloth1->verts[collpair->ap1].tv);
+
+		collision_interpolateOnTriangle ( v2, collmd->current_v[collpair->bp1].co, collmd->current_v[collpair->bp2].co, collmd->current_v[collpair->bp3].co, u1, u2, u3 );
+
+		sub_v3_v3v3(relativeVelocity, v2, v1);
+
+		/* Calculate the normal component of the relative velocity (actually only the magnitude - the direction is stored in 'normal'). */
+		magrelVel = dot_v3v3(relativeVelocity, collpair->normal);
+
+		/* printf("magrelVel: %f\n", magrelVel); */
+
+		/* Calculate masses of points.
+		 * TODO */
+
+#if 0
+		/* If v_n_mag < 0 the edges are approaching each other. */
+		if ( magrelVel > ALMOST_ZERO ) {
+			/* Calculate Impulse magnitude to stop all motion in normal direction. */
+			float magtangent = 0, repulse = 0, d = 0;
+			double impulse = 0.0;
+			float vrel_t_pre[3];
+			float temp[3], spf;
+
+			/* calculate tangential velocity */
+			copy_v3_v3 ( temp, collpair->normal );
+			mul_v3_fl(temp, magrelVel);
+			sub_v3_v3v3(vrel_t_pre, relativeVelocity, temp);
+
+			/* Decrease in magnitude of relative tangential velocity due to coulomb friction
+			 * in original formula "magrelVel" should be the "change of relative velocity in normal direction" */
+			magtangent = min_ff(clmd->coll_parms->friction * 0.01f * magrelVel, len_v3(vrel_t_pre));
+
+			/* Apply friction impulse. */
+			if ( magtangent > ALMOST_ZERO ) {
+				normalize_v3(vrel_t_pre);
+
+				impulse = magtangent / ( 1.0f + w1*w1 + w2*w2 + w3*w3 ); /* 2.0 * */
+				VECADDMUL ( i1, vrel_t_pre, w1 * impulse );
+				VECADDMUL ( i2, vrel_t_pre, w2 * impulse );
+				VECADDMUL ( i3, vrel_t_pre, w3 * impulse );
+			}
+
+			/* Apply velocity stopping impulse
+			 * I_c = m * v_N / 2.0
+			 * no 2.0 * magrelVel normally, but looks nicer DG */
+			impulse =  magrelVel / ( 1.0 + w1*w1 + w2*w2 + w3*w3 );
+
+			VECADDMUL ( i1, collpair->normal, w1 * impulse );
+			cloth1->verts[collpair->ap1].impulse_count++;
+
+			VECADDMUL ( i2, collpair->normal, w2 * impulse );
+			cloth1->verts[collpair->ap2].impulse_count++;
+
+			VECADDMUL ( i3, collpair->normal, w3 * impulse );
+			cloth1->verts[collpair->ap3].impulse_count++;
+
+			/* Apply repulse impulse if distance too short
+			 * I_r = -min(dt*kd, m(0, 1d/dt - v_n))
+			 * DG: this formula ineeds to be changed for this code since we apply impulses/repulses like this:
+			 * v += impulse; x_new = x + v;
+			 * We don't use dt!!
+			 * DG TODO: Fix usage of dt here! */
+			spf = (float)clmd->sim_parms->stepsPerFrame / clmd->sim_parms->timescale;
+
+			d = clmd->coll_parms->epsilon*8.0f/9.0f + epsilon2*8.0f/9.0f - collpair->distance;
+			if ( ( magrelVel < 0.1f*d*spf ) && ( d > ALMOST_ZERO ) ) {
+				repulse = MIN2 ( d*1.0f/spf, 0.1f*d*spf - magrelVel );
+
+				/* stay on the safe side and clamp repulse */
+				if ( impulse > ALMOST_ZERO )
+					repulse = min_ff( repulse, 5.0*impulse );
+				repulse = max_ff(impulse, repulse);
+
+				impulse = repulse / ( 1.0f + w1*w1 + w2*w2 + w3*w3 ); /* original 2.0 / 0.25 */
+				VECADDMUL ( i1, collpair->normal,  impulse );
+				VECADDMUL ( i2, collpair->normal,  impulse );
+				VECADDMUL ( i3, collpair->normal,  impulse );
+			}
+
+			result = 1;
+		}
+		else {
+			/* Apply repulse impulse if distance too short
+			 * I_r = -min(dt*kd, max(0, 1d/dt - v_n))
+			 * DG: this formula ineeds to be changed for this code since we apply impulses/repulses like this:
+			 * v += impulse; x_new = x + v;
+			 * We don't use dt!! */
+			float spf = (float)clmd->sim_parms->stepsPerFrame / clmd->sim_parms->timescale;
+
+			float d = clmd->coll_parms->epsilon*8.0f/9.0f + epsilon2*8.0f/9.0f - (float)collpair->distance;
+			if ( d > ALMOST_ZERO) {
+				/* stay on the safe side and clamp repulse */
+				float repulse = d*1.0f/spf;
+
+				float impulse = repulse / ( 3.0f * ( 1.0f + w1*w1 + w2*w2 + w3*w3 )); /* original 2.0 / 0.25 */
+
+				VECADDMUL ( i1, collpair->normal,  impulse );
+				VECADDMUL ( i2, collpair->normal,  impulse );
+				VECADDMUL ( i3, collpair->normal,  impulse );
+
+				cloth1->verts[collpair->ap1].impulse_count++;
+				cloth1->verts[collpair->ap2].impulse_count++;
+				cloth1->verts[collpair->ap3].impulse_count++;
+
+				result = 1;
+			}
+		}
+
+		if (result) {
+			int i = 0;
+
+			for (i = 0; i < 3; i++) {
+				if (cloth1->verts[collpair->ap1].impulse_count > 0 && ABS(cloth1->verts[collpair->ap1].impulse[i]) < ABS(i1[i]))
+					cloth1->verts[collpair->ap1].impulse[i] = i1[i];
+
+				if (cloth1->verts[collpair->ap2].impulse_count > 0 && ABS(cloth1->verts[collpair->ap2].impulse[i]) < ABS(i2[i]))
+					cloth1->verts[collpair->ap2].impulse[i] = i2[i];
+
+				if (cloth1->verts[collpair->ap3].impulse_count > 0 && ABS(cloth1->verts[collpair->ap3].impulse[i]) < ABS(i3[i]))
+					cloth1->verts[collpair->ap3].impulse[i] = i3[i];
+			}
+		}
+#endif
+	}
+	return result;
+}
+
+BLI_INLINE void face_normal(float co1[3], float co2[3], float co3[3], float nor[3])
+{
+	float p[3], q[3];
+	sub_v3_v3v3(p, co2, co1);
+	sub_v3_v3v3(q, co3, co1);
+	cross_v3_v3v3(nor, p, q);
+	normalize_v3(nor);
+}
+
+static CollPair *cloth_point_collpair(float p1[3], float p2[3], MVert *mverts, int bp1, int bp2, int bp3,
+                                      int index_cloth, int index_coll, CollPair *collpair)
+{
+	float *co1 = mverts[bp1].co, *co2 = mverts[bp2].co, *co3 = mverts[bp3].co;
+	float lambda, uv[2];
+	float fnor[3], vec[3];
+	float w[3];
+	
+	if (!isect_line_tri_v3(p1, p2, co1, co2, co3, &lambda, uv))
+		return collpair;
+	
+	face_normal(co1, co2, co3, fnor);
+	sub_v3_v3v3(vec, p2, p1);
+	
+	collpair->face1 = index_cloth; /* XXX actually not a face, but equivalent index for point */
+	collpair->face2 = index_coll;
+	collpair->ap1 = index_cloth;
+	collpair->ap2 = collpair->ap3 = -1; /* unused */
+	collpair->bp1 = bp1;
+	collpair->bp2 = bp2;
+	collpair->bp3 = bp3;
+	
+	copy_v3_v3(collpair->pa, p1);
+	w[0] = 1.0f - uv[0] - uv[1];
+	w[1] = uv[0];
+	w[2] = uv[1];
+	interp_v3_v3v3v3(collpair->pb, co1, co2, co3, w);
+	
+	/* note: face normal smoothing is ignored here,
+	 * it would probably not work with collision response
+	 */
+	copy_v3_v3(collpair->normal, fnor);
+	collpair->distance = -dot_v3v3(fnor, vec) * (1.0f - lambda);
+	copy_v3_v3(collpair->vector, vec);
+	collpair->time = lambda;
+	collpair->flag = 0;
+	
+	collpair++;
+	return collpair;
+}
+
+//Determines collisions on overlap, collisions are written to collpair[i] and collision+number_collision_found is returned
+static CollPair* cloth_point_collision(ModifierData *md1, ModifierData *md2,
+                                       BVHTreeOverlap *overlap, CollPair *collpair, float UNUSED(dt))
+{
+	ClothModifierData *clmd = (ClothModifierData *)md1;
+	CollisionModifie

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list