[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [12101] branches/cloth/blender: Fixed some collision response issues.

Daniel Genrich daniel.genrich at gmx.net
Fri Sep 21 15:34:20 CEST 2007


Revision: 12101
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=12101
Author:   genscher
Date:     2007-09-21 15:34:19 +0200 (Fri, 21 Sep 2007)

Log Message:
-----------
Fixed some collision response issues. (weekend commit, some half done work in)

Modified Paths:
--------------
    branches/cloth/blender/extern/bullet2/src/BulletDynamics/Dynamics/Bullet-C-Api.cpp
    branches/cloth/blender/source/blender/blenkernel/BKE_cloth.h
    branches/cloth/blender/source/blender/blenkernel/intern/cloth.c
    branches/cloth/blender/source/blender/blenkernel/intern/collision.c
    branches/cloth/blender/source/blender/blenkernel/intern/implicit.c
    branches/cloth/blender/source/blender/blenkernel/intern/modifier.c
    branches/cloth/blender/source/blender/makesdna/DNA_cloth_types.h
    branches/cloth/blender/source/blender/src/buttons_object.c

Modified: branches/cloth/blender/extern/bullet2/src/BulletDynamics/Dynamics/Bullet-C-Api.cpp
===================================================================
--- branches/cloth/blender/extern/bullet2/src/BulletDynamics/Dynamics/Bullet-C-Api.cpp	2007-09-21 13:30:38 UTC (rev 12100)
+++ branches/cloth/blender/extern/bullet2/src/BulletDynamics/Dynamics/Bullet-C-Api.cpp	2007-09-21 13:34:19 UTC (rev 12101)
@@ -41,11 +41,10 @@
 	
 	static btGjkEpaPenetrationDepthSolver Solver0;
 	static btMinkowskiPenetrationDepthSolver Solver1;
-	
+		
 	btConvexPenetrationDepthSolver* Solver = NULL;
 	
-	Solver = &Solver1;
-	
+	Solver = &Solver0;	
 		
 	btGjkPairDetector convexConvex(&trishapeA ,&trishapeB,&sGjkSimplexSolver,Solver);
 	

Modified: branches/cloth/blender/source/blender/blenkernel/BKE_cloth.h
===================================================================
--- branches/cloth/blender/source/blender/blenkernel/BKE_cloth.h	2007-09-21 13:30:38 UTC (rev 12100)
+++ branches/cloth/blender/source/blender/blenkernel/BKE_cloth.h	2007-09-21 13:34:19 UTC (rev 12101)
@@ -60,7 +60,7 @@
 
 /* This is approximately the smallest number that can be
 * represented by a float, given its precision. */
-#define ALMOST_ZERO		0.0000001
+#define ALMOST_ZERO		0.00001
 
 /* Bits to or into the ClothVertex.flags. */
 #define CVERT_FLAG_PINNED	1
@@ -91,6 +91,7 @@
 	CSIMSETT_FLAG_CCACHE_FREE_ALL = (1 << 4),  // delete all from cache
 	CSIMSETT_FLAG_CCACHE_FREE_PART = (1 << 5), // delete some part of cache
 	CSIMSETT_FLAG_TEARING_ENABLED = (1 << 6), // true if tearing is enabled
+	CSIMSETT_FLAG_CCACHE_PROTECT = (1 << 7), // true if tearing is enabled
 } CSIMSETT_FLAGS;
 
 /* Spring types as defined in the paper.*/

Modified: branches/cloth/blender/source/blender/blenkernel/intern/cloth.c
===================================================================
--- branches/cloth/blender/source/blender/blenkernel/intern/cloth.c	2007-09-21 13:30:38 UTC (rev 12100)
+++ branches/cloth/blender/source/blender/blenkernel/intern/cloth.c	2007-09-21 13:34:19 UTC (rev 12101)
@@ -153,7 +153,7 @@
 	clmd->sim_parms.mass = 1.0f;
 	clmd->sim_parms.stepsPerFrame = 5;
 	clmd->sim_parms.sim_time = 1.0;
-	clmd->sim_parms.flags = CSIMSETT_FLAG_RESET;
+	clmd->sim_parms.flags = CSIMSETT_FLAG_RESET | CSIMSETT_FLAG_CCACHE_PROTECT;
 	clmd->sim_parms.solver_type = 0; 
 	clmd->sim_parms.preroll = 0;
 	clmd->sim_parms.maxspringlen = 10;
@@ -576,6 +576,7 @@
 	}
 }
 
+// free cloth cache
 void cloth_cache_free(ClothModifierData *clmd, float time)
 {
 	Frame *frame = NULL;
@@ -659,7 +660,8 @@
 	float deltaTime = current_time - clmd->sim_parms.sim_time;	
 		
 	
-	// only be active during a specific period
+	// only be active during a specific period:
+	// that's "first frame" and "last frame" on GUI
 	if (!(clmd->sim_parms.flags & CSIMSETT_FLAG_COLLOBJ))
 	{
 		if(current_time < clmd->sim_parms.firstframe)
@@ -681,7 +683,7 @@
 		}
 	}
 	
-	// unused in the moment
+	// unused in the moment, calculated seperately in implicit.c
 	clmd->sim_parms.dt = 1.0f / clmd->sim_parms.stepsPerFrame;
 	
 	clmd->sim_parms.sim_time = current_time;
@@ -812,46 +814,49 @@
 		return;
 
 	cloth = clmd->clothObject;
-
-	// free our frame cache
-	clmd->sim_parms.flags |= CSIMSETT_FLAG_CCACHE_FREE_ALL;
-	cloth_cache_free(clmd, 0);
-
-	if (cloth) 
-	{	
-		// If our solver provides a free function, call it
-		if (cloth->old_solver_type < 255 && solvers [cloth->old_solver_type].free) 
+	
+	if(!(clmd->sim_parms.flags & CSIMSETT_FLAG_CCACHE_PROTECT))
+	{
+		// free our frame cache
+		clmd->sim_parms.flags |= CSIMSETT_FLAG_CCACHE_FREE_ALL;
+		cloth_cache_free(clmd, 0);
+	
+		if (cloth) 
 		{	
-			solvers [cloth->old_solver_type].free (clmd);
-		}
-		
-		// Free the verts.
-		if (cloth->verts != NULL)
-			MEM_freeN (cloth->verts);
-
-		cloth->verts = NULL;
-		cloth->numverts = -1;
-		
-		// Free the springs.
-		if (cloth->springs != NULL)
-			MEM_freeN (cloth->springs);
-
-		cloth->springs = NULL;
-		cloth->numsprings = -1;		
-		
-		// free BVH collision tree
-		if(cloth->tree)
-			bvh_free((BVH *)cloth->tree);
-		
-		// we save our faces for collision objects
-		if(cloth->mfaces)
-			MEM_freeN(cloth->mfaces);
+			// If our solver provides a free function, call it
+			if (cloth->old_solver_type < 255 && solvers [cloth->old_solver_type].free) 
+			{	
+				solvers [cloth->old_solver_type].free (clmd);
+			}
+			
+			// Free the verts.
+			if (cloth->verts != NULL)
+				MEM_freeN (cloth->verts);
 	
-		if(clmd->clothObject->facemarks)
-			MEM_freeN(clmd->clothObject->facemarks);
+			cloth->verts = NULL;
+			cloth->numverts = -1;
+			
+			// Free the springs.
+			if (cloth->springs != NULL)
+				MEM_freeN (cloth->springs);
+	
+			cloth->springs = NULL;
+			cloth->numsprings = -1;		
+			
+			// free BVH collision tree
+			if(cloth->tree)
+				bvh_free((BVH *)cloth->tree);
+			
+			// we save our faces for collision objects
+			if(cloth->mfaces)
+				MEM_freeN(cloth->mfaces);
 		
-		MEM_freeN (cloth);
-		clmd->clothObject = NULL;
+			if(clmd->clothObject->facemarks)
+				MEM_freeN(clmd->clothObject->facemarks);
+			
+			MEM_freeN (cloth);
+			clmd->clothObject = NULL;
+		}
 	}
 }
 
@@ -952,6 +957,7 @@
 	unsigned int i;
 	MVert *mvert = NULL; 
 	ClothVertex *verts = NULL;
+	float tnull[3] = {0,0,0}; 
 	
 	/* If we have a clothObject, free it. */
 	if (clmd->clothObject != NULL)
@@ -993,7 +999,9 @@
 				VECCOPY(verts->xold, verts->x);
 				VECCOPY(verts->txold, verts->x);
 				VECCOPY(verts->tx, verts->x);
-				VecMulf(verts->v, 0.0f);				
+				VecMulf(verts->v, 0.0f);
+				verts->impulse_count = 0;
+				VECCOPY(verts->impulse, tnull);
 			}
 			clmd->clothObject->tree =  bvh_build(clmd,clmd->coll_parms.epsilon);
 			
@@ -1159,6 +1167,7 @@
 	// dm->getNumVerts(dm);
 	MVert *mvert = NULL; // CDDM_get_verts(dm);
 	ClothVertex *verts = NULL;
+	float tnull[3] = {0,0,0};
 	
 	/* If we have a clothObject, free it. */
 	if (clmd->clothObject != NULL)
@@ -1220,6 +1229,9 @@
 				VECCOPY(verts->xconst, verts->x);
 				VECCOPY(verts->txold, verts->x);
 				VecMulf(verts->v, 0.0f);
+				
+				verts->impulse_count = 0;
+				VECCOPY(verts->impulse, tnull);
 			}
 
 			/* apply / set vertex groups */
@@ -1363,17 +1375,20 @@
 
 		shear_springs++;
 		temp_index++;
-
-		springs[temp_index].ij = mface[i].v2;
-		springs[temp_index].kl = mface[i].v4;
-		VECSUB(temp, mvert[springs[temp_index].kl].co, mvert[springs[temp_index].ij].co);
-		springs[temp_index].restlen =  sqrt(INPR(temp, temp));
-		springs[temp_index].type = SHEAR;
-
-		BLI_linklist_append(&edgelist[springs[temp_index].ij], &(springs[temp_index]));		
-		BLI_linklist_append(&edgelist[springs[temp_index].kl], &(springs[temp_index]));
-
-		shear_springs++;
+		
+		if(mface[i].v4)
+		{
+			springs[temp_index].ij = mface[i].v2;
+			springs[temp_index].kl = mface[i].v4;
+			VECSUB(temp, mvert[springs[temp_index].kl].co, mvert[springs[temp_index].ij].co);
+			springs[temp_index].restlen =  sqrt(INPR(temp, temp));
+			springs[temp_index].type = SHEAR;
+	
+			BLI_linklist_append(&edgelist[springs[temp_index].ij], &(springs[temp_index]));		
+			BLI_linklist_append(&edgelist[springs[temp_index].kl], &(springs[temp_index]));
+	
+			shear_springs++;
+		}
 	}
 
 	// bending springs

Modified: branches/cloth/blender/source/blender/blenkernel/intern/collision.c
===================================================================
--- branches/cloth/blender/source/blender/blenkernel/intern/collision.c	2007-09-21 13:30:38 UTC (rev 12100)
+++ branches/cloth/blender/source/blender/blenkernel/intern/collision.c	2007-09-21 13:34:19 UTC (rev 12101)
@@ -186,15 +186,15 @@
 	*/
 }
 
-
+// w3 is not perfect
 void bvh_compute_barycentric (float pv[3], float p1[3], float p2[3], float p3[3], double *w1, double *w2, double *w3)
 {
-	float	tempV1[3], tempV2[3], tempV4[3];
-	double	a,b,c,e,f;
+	double	tempV1[3], tempV2[3], tempV4[3];
+	double	a,b,c,d,e,f;
 
-	VECSUB (tempV1, p1, p3);	/* x1 - x3 */
-	VECSUB (tempV2, p2, p3);	/* x2 - x3 */
-	VECSUB (tempV4, pv, p3);	/* pv - x3 */
+	VECSUB (tempV1, p1, p3);	
+	VECSUB (tempV2, p2, p3);	
+	VECSUB (tempV4, pv, p3);	
 	
 	a = INPR (tempV1, tempV1);	
 	b = INPR (tempV1, tempV2);	
@@ -202,10 +202,24 @@
 	e = INPR (tempV1, tempV4);	
 	f = INPR (tempV2, tempV4);	
 	
+	d = (a * c - b * b);
 	
-	w1[0] = (e * c - b * f) / (a * c - b * b);
+	if (ABS(d) < ALMOST_ZERO) {
+		*w1 = *w2 = *w3 = 1.0f / 3.0f;
+		return;
+	}
+	
+	w1[0] = (e * c - b * f) / d;
+	
+	if(w1[0] < 0)
+		w1[0] = 0.0;
+	
 	w2[0] = (f - b * w1[0]) / c;
-	w3[0] = 1.0 - w1[0] - w2[0];
+	
+	if(w2[0] < 0)
+		w2[0] = 0.0;
+	
+	w3[0] = 1.0f - w1[0] - w2[0];
 }
 
 DO_INLINE void interpolateOnTriangle(float to[3], float v1[3], float v2[3], float v3[3], double w1, double w2, double w3) 
@@ -230,36 +244,22 @@
 		
 int collision_static(ClothModifierData *clmd, ClothModifierData *coll_clmd, LinkNode **collision_list)
 {
-	unsigned int i = 0, numverts=0;
+	unsigned int i = 0, numfaces = 0;
 	int result = 0;
 	LinkNode *search = NULL;
 	CollPair *collpair = NULL;
 	Cloth *cloth1, *cloth2;
 	MFace *face1, *face2;
-	double w1, w2, w3, u1, u2, u3;
+	double w1, w2, w3, u1, u2, u3, a1, a2, a3;
 	float v1[3], v2[3], relativeVelocity[3];
 	float magrelVel;
 	
 	cloth1 = clmd->clothObject;
 	cloth2 = coll_clmd->clothObject;
 	
-	numverts = clmd->clothObject->numverts;
-	
-	/*
-	for(i = 0; i < LIST_LENGTH; i++)
-	{
-		// calc SIP-code
-		//  TODO for later: calculateSipCode()
-	
-		// calc distance (?)
+	numfaces = clmd->clothObject->numfaces;
 		
-		// calc impulse
-		
-		// apply impulse
-	}
-	*/
-	
-	for(i = 0; i < numverts; i++)
+	for(i = 0; i < numfaces; i++)
 	{
 		search = collision_list[i];
 		
@@ -273,11 +273,13 @@
 			// compute barycentric coordinates for both collision points
 			
 			if(!collpair->quadA)
+			{	
 				bvh_compute_barycentric(collpair->p1,
 							cloth1->verts[face1->v1].txold,
 							cloth1->verts[face1->v2].txold,
         						cloth1->verts[face1->v3].txold, 
        							&w1, &w2, &w3);
+			}
 			else
 				bvh_compute_barycentric(collpair->p1,
 							cloth1->verts[face1->v4].txold,
@@ -317,25 +319,26 @@
 					
 			// Calculate masses of points.
 			
-			// printf("relativeVelocity -> x: %f, y: %f, z: %f\n", relativeVelocity[0], relativeVelocity[1],relativeVelocity[2]); 
+			// If v_n_mag > 0 the edges are approaching each other.
 			
-			// If v_n_mag > 0 the edges are approaching each other.
-			if(magrelVel > ALMOST_ZERO)
+			if(magrelVel < -ALMOST_ZERO)
 			{
 				// Calculate Impulse magnitude to stop all motion in normal direction.

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list