[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [12122] branches/cloth/blender/source/ blender/blenkernel: In the middle of switching to own collision detection, WIP commit (don't expect anything to work, but compile)

Daniel Genrich daniel.genrich at gmx.net
Sun Sep 23 19:40:44 CEST 2007


Revision: 12122
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=12122
Author:   genscher
Date:     2007-09-23 19:40:44 +0200 (Sun, 23 Sep 2007)

Log Message:
-----------
In the middle of switching to own collision detection, WIP commit (don't expect anything to work, but compile)

Modified Paths:
--------------
    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

Modified: branches/cloth/blender/source/blender/blenkernel/BKE_cloth.h
===================================================================
--- branches/cloth/blender/source/blender/blenkernel/BKE_cloth.h	2007-09-23 17:26:22 UTC (rev 12121)
+++ branches/cloth/blender/source/blender/blenkernel/BKE_cloth.h	2007-09-23 17:40:44 UTC (rev 12122)
@@ -60,7 +60,7 @@
 
 /* This is approximately the smallest number that can be
 * represented by a float, given its precision. */
-#define ALMOST_ZERO		0.00001
+#define ALMOST_ZERO		0.000001
 
 /* Bits to or into the ClothVertex.flags. */
 #define CVERT_FLAG_PINNED	1
@@ -240,7 +240,7 @@
 	float p1[3], p2[3]; // collision point p1 on face1, p2 on face2
 	int lastsign; // indicates if the distance sign has changed, unused itm
 	float time; // collision time, from 0 up to 1
-	int quadA, quadB; // indicates the used triangle of the quad: 0 means verts 1,2,3; 1 means verts 4,1,3
+	unsigned int Aindex1, Aindex2, Aindex3, Aindex4, Bindex1, Bindex2, Bindex3, Bindex4;
 } CollPair;
 
 

Modified: branches/cloth/blender/source/blender/blenkernel/intern/cloth.c
===================================================================
--- branches/cloth/blender/source/blender/blenkernel/intern/cloth.c	2007-09-23 17:26:22 UTC (rev 12121)
+++ branches/cloth/blender/source/blender/blenkernel/intern/cloth.c	2007-09-23 17:40:44 UTC (rev 12122)
@@ -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 | CSIMSETT_FLAG_CCACHE_PROTECT;
+	clmd->sim_parms.flags = CSIMSETT_FLAG_RESET;
 	clmd->sim_parms.solver_type = 0; 
 	clmd->sim_parms.preroll = 0;
 	clmd->sim_parms.maxspringlen = 10;

Modified: branches/cloth/blender/source/blender/blenkernel/intern/collision.c
===================================================================
--- branches/cloth/blender/source/blender/blenkernel/intern/collision.c	2007-09-23 17:26:22 UTC (rev 12121)
+++ branches/cloth/blender/source/blender/blenkernel/intern/collision.c	2007-09-23 17:40:44 UTC (rev 12122)
@@ -211,14 +211,8 @@
 	
 	w1[0] = (e * c - b * f) / d;
 	
-	if(w1[0] < 0)
-		w1[0] = 0.0;
-	
 	w2[0] = (f - b * w1[0]) / c;
 	
-	if(w2[0] < 0)
-		w2[0] = 0.0;
-	
 	w3[0] = 1.0f - w1[0] - w2[0];
 }
 
@@ -241,7 +235,7 @@
 	VecMulf(to, MAX2(1.0f - frictionConstant * delta_V_n / INPR(vrel_t_pre,vrel_t_pre), 0.0f));
 }
 
-		
+/*
 int collision_static(ClothModifierData *clmd, ClothModifierData *coll_clmd, LinkNode **collision_list)
 {
 	unsigned int i = 0, numfaces = 0;
@@ -343,12 +337,12 @@
 					
 					// printf("friction applied: %f\n", magtangent);
 					// TODO check original code 
-					/*
+					
 					VECSUB(cloth1->verts[face1->v1].tv, cloth1->verts[face1->v1].tv,tangential);
 					VECSUB(cloth1->verts[face1->v1].tv, cloth1->verts[face1->v2].tv,tangential);
 					VECSUB(cloth1->verts[face1->v1].tv, cloth1->verts[face1->v3].tv,tangential);
 					VECSUB(cloth1->verts[face1->v1].tv, cloth1->verts[face1->v4].tv,tangential);
-					*/
+					
 				}
 				
 				impulse = -magrelVel / ( 1.0 + w1*w1 + w2*w2 + w3*w3);
@@ -397,7 +391,7 @@
 				
 				// Apply the impulse and increase impulse counters.
 
-				/*			
+				/		
 				// calculateFrictionImpulse(tangential, collvel, collpair->normal, magtangent, clmd->coll_parms.friction*0.01, magtangent);
 				VECSUBS(vrel_t_pre, collvel, collpair->normal, magnormal);
 				// VecMulf(vrel_t_pre, clmd->coll_parms.friction*0.01f/INPR(vrel_t_pre,vrel_t_pre));
@@ -405,10 +399,10 @@
 				VecMulf(vrel_t_pre, MIN2(clmd->coll_parms.friction*0.01f*magnormal,magtangent));
 				
 				VECSUB(cloth1->verts[face1->v1].tv, cloth1->verts[face1->v1].tv,vrel_t_pre);
-				*/
 				
 				
 				
+				
 			}
 			
 			search = search->next;
@@ -417,7 +411,8 @@
 		
 	return result;
 }
-
+*/
+		
 // return distance between two triangles using bullet engine
 double implicit_tri_check_coherence (ClothModifierData *clmd, ClothModifierData *coll_clmd, unsigned int tri_index1, unsigned int tri_index2, float pa[3], float pb[3], float normal[3], int quadA, int quadB)
 {
@@ -540,38 +535,332 @@
 	return distance;
 }
 
+// calculate plane normal
+void calcPlaneNormal(float normal[3], float p11[3], float p12[3], float p13[3])
+{
+	float temp1[3], temp2[3];
+	float tnormal[3];
+	
+	VECSUB(temp1, p12,p11);
+	VECSUB(temp2, p13,p11);
+	Crossf(normal, temp1, temp2);
+	Normalize(normal);
+	// VECCOPY(normal, tnormal);
+}
+
+float distance_triangle_point( float p11[3], float p12[3], float p13[3], float p21[3], float normal[3])
+{
+	float temp[3];
+	float magnitude = 0;
+	
+	VECSUB(temp, p21, p13);
+	magnitude = INPR(temp, normal);
+	
+	if(magnitude < 0)
+	{
+		magnitude *= -1.0f;
+		// VecMulf(normal, -1.0f);
+	}
+	
+	return magnitude;
+}
+
+float nearest_point_triangle_triangle(float p11[3], float p12[3], float p13[3], float p21[3], float p22[3], float p23[3], float normal[3])
+{
+	float distance = 0, tdistance = 0, tnormal[3];
+	
+	// first triangle 1-2-3 versus second triangle 1-2-3
+	calcPlaneNormal(normal, p11, p12, p13);
+	distance = distance_triangle_point(p11, p12, p13, p21, normal);
+	
+	tdistance = distance_triangle_point(p11, p12, p13, p22, normal);
+	
+	if(tdistance < distance)
+	{	
+		distance = tdistance;
+	}
+	
+	tdistance = distance_triangle_point(p11, p12, p13, p23, normal);
+	
+	if(tdistance < distance)
+	{	
+		distance = tdistance;
+	}
+	
+	// second triangle 1-2-3 versus first triangle 1-2-3
+	calcPlaneNormal(tnormal, p21, p22, p23);
+	
+	tdistance = distance_triangle_point(p21, p22, p23, p11, tnormal);
+	
+	if(tdistance < distance)
+	{	
+		distance = tdistance;
+		VECCOPY(normal, tnormal);
+	}
+	
+	tdistance = distance_triangle_point(p21, p22, p23, p12, tnormal);
+	
+	if(tdistance < distance)
+	{	
+		distance = tdistance;
+		VECCOPY(normal, tnormal);
+	}
+	
+	tdistance = distance_triangle_point(p21, p22, p23, p13, tnormal);
+	
+	if(tdistance < distance)
+	{	
+		distance = tdistance;
+		VECCOPY(normal, tnormal);
+	}
+	
+	
+	if (distance < 0) {
+		VecMulf(normal, -1.0f);
+		distance = -distance;
+	}
+	
+	return distance;	
+}
+
+		
+int collision_static2(ClothModifierData *clmd, ClothModifierData *coll_clmd, LinkNode **collision_list)
+{
+	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, a1, a2, a3;
+	float v1[3], v2[3], relativeVelocity[3];
+	float magrelVel;
+	
+	cloth1 = clmd->clothObject;
+	cloth2 = coll_clmd->clothObject;
+	
+	numfaces = clmd->clothObject->numfaces;
+		
+	for(i = 0; i < numfaces; i++)
+	{
+		search = collision_list[i];
+		
+		while(search)
+		{
+			collpair = search->link;
+			
+			face1 = &(cloth1->mfaces[collpair->face1]);
+			face2 = &(cloth2->mfaces[collpair->face2]);
+			
+			// compute barycentric coordinates for both collision points
+			
+		
+			bvh_compute_barycentric(collpair->p1,
+					cloth1->verts[collpair->Aindex1].txold,
+					cloth1->verts[collpair->Aindex2].txold,
+					cloth1->verts[collpair->Aindex3].txold, 
+					&w1, &w2, &w3);
+		
+			bvh_compute_barycentric(collpair->p2,
+					cloth2->verts[collpair->Bindex1].txold,
+					cloth2->verts[collpair->Bindex1].txold,
+					cloth2->verts[collpair->Bindex3].txold, 
+					&u1, &u2, &u3);
+			
+			// Calculate relative "velocity".
+			interpolateOnTriangle(v1, cloth1->verts[collpair->Aindex1].tv, cloth1->verts[collpair->Aindex2].tv, cloth1->verts[collpair->Aindex3].tv, w1, w2, w3);
+		
+			interpolateOnTriangle(v2, cloth2->verts[collpair->Bindex1].tv, cloth2->verts[collpair->Bindex2].tv, cloth2->verts[collpair->Bindex3].tv, u1, u2, u3);
+		
+			VECSUB(relativeVelocity, v1, v2);
+				
+			// Calculate the normal component of the relative velocity (actually only the magnitude - the direction is stored in 'normal').
+			magrelVel = INPR(relativeVelocity, collpair->normal);
+					
+			// Calculate masses of points.
+			
+			// 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.
+				// const double I_mag = v_n_mag / (1/m1 + 1/m2);
+				float magnitude_i = magrelVel / 2.0f; // TODO implement masses
+				float tangential[3], magtangent, magnormal, collvel[3];
+				float vrel_t_pre[3];
+				float vrel_t[3];
+				double impulse;
+				float epsilon = clmd->coll_parms.epsilon;
+				float overlap = (epsilon + ALMOST_ZERO-collpair->distance);
+				
+				/*
+				impulse = -magrelVel / ( 1.0 + w1*w1 + w2*w2 + w3*w3);
+				VECADDMUL(cloth1->verts[face1->v1].impulse, collpair->normal, impulse); 
+				cloth1->verts[face1->v1].impulse_count++;
+				
+				VECADDMUL(cloth1->verts[face1->v2].impulse, collpair->normal, impulse); 
+				cloth1->verts[face1->v2].impulse_count++;
+				
+				VECADDMUL(cloth1->verts[face1->v3].impulse, collpair->normal, impulse); 
+				cloth1->verts[face1->v3].impulse_count++;
+				*/
+			
+				
+				/*
+				if (overlap > ALMOST_ZERO) {
+					double I_mag  = overlap * 0.1;
+					
+					impulse = I_mag / ( 1.0 + w1*w1 + w2*w2 + w3*w3);
+					
+					VECADDMUL(cloth1->verts[face1->v1].impulse, collpair->normal, impulse); 
+					cloth1->verts[face1->v1].impulse_count++;
+								
+					VECADDMUL(cloth1->verts[face1->v2].impulse, collpair->normal, impulse); 
+					cloth1->verts[face1->v2].impulse_count++;
+				
+					VECADDMUL(cloth1->verts[face1->v3].impulse, collpair->normal, impulse); 
+					cloth1->verts[face1->v3].impulse_count++;
+				
+					if(face1->v4)
+					{
+						VECADDMUL(cloth1->verts[face1->v4].impulse, collpair->normal, impulse); 
+						cloth1->verts[face1->v4].impulse_count++;
+					}
+				
+				}
+				*/
+				
+				result = 1;
+			}
+			
+			search = search->next;
+		}
+	}
+		
+	return result;
+}
+
 void bvh_collision_response(ClothModifierData *clmd, ClothModifierData *coll_clmd, Tree * tree1, Tree * tree2)
 {
 	CollPair *collpair = NULL;
 	LinkNode **linknode;
 	double distance = 0;
-	float epsilon = clmd->coll_parms.epsilon;
-
-	collpair = (CollPair *)MEM_callocN(sizeof(CollPair), "cloth coll pair");
+	float epsilon = clmd->coll_parms.epsilon, tdistance=0;
+	MFace *face1, *face2;
+	ClothVertex *verts1, *verts2;
+	Cloth *cloth1=NULL, *cloth2=NULL;
+	int i = 0;
+	
 	linknode = clmd->coll_parms.temp;
 	
+	cloth1 = clmd->clothObject;
+	cloth2 = coll_clmd->clothObject;
+	
 	// calc SIPcode (?)
 	
-	// calc distance + normal 	

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list