[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [12093] branches/cloth/blender/source/ blender: Fixed collision object init, New: Apply collision impulses so minimum distance is preserved ( friction still missing)

Daniel Genrich daniel.genrich at gmx.net
Wed Sep 19 14:13:16 CEST 2007


Revision: 12093
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=12093
Author:   genscher
Date:     2007-09-19 14:13:16 +0200 (Wed, 19 Sep 2007)

Log Message:
-----------
Fixed collision object init, New: Apply collision impulses so minimum distance is preserved (friction still missing)

Modified Paths:
--------------
    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/src/buttons_object.c

Modified: branches/cloth/blender/source/blender/blenkernel/intern/cloth.c
===================================================================
--- branches/cloth/blender/source/blender/blenkernel/intern/cloth.c	2007-09-19 10:54:49 UTC (rev 12092)
+++ branches/cloth/blender/source/blender/blenkernel/intern/cloth.c	2007-09-19 12:13:16 UTC (rev 12093)
@@ -657,25 +657,29 @@
 	Frame *frame = NULL;
 	LinkNode *search = NULL;
 	float deltaTime = current_time - clmd->sim_parms.sim_time;	
+		
 	
 	// only be active during a specific period
-	if(current_time < clmd->sim_parms.firstframe)
-		return;
-	else if(current_time > clmd->sim_parms.lastframe)
+	if (!(clmd->sim_parms.flags & CSIMSETT_FLAG_COLLOBJ))
 	{
-		int frametime = cloth_cache_last_frame(clmd);
-		if(cloth_cache_search_frame(clmd, frametime))
+		if(current_time < clmd->sim_parms.firstframe)
+			return;
+		else if(current_time > clmd->sim_parms.lastframe)
 		{
-			cloth_cache_get_frame(clmd, frametime);
-			cloth_to_object (ob, clmd, vertexCos, numverts);
+			int frametime = cloth_cache_last_frame(clmd);
+			if(cloth_cache_search_frame(clmd, frametime))
+			{
+				cloth_cache_get_frame(clmd, frametime);
+				cloth_to_object (ob, clmd, vertexCos, numverts);
+			}
+			return;
 		}
-		return;
+		else if(ABS(deltaTime) >= 2.0f ) // no timewarps allowed
+		{
+			if(!cloth_cache_search_frame(clmd, framenr))
+				return;
+		}
 	}
-	else if(ABS(deltaTime) >= 2.0f ) // no timewarps allowed
-	{
-		if(!cloth_cache_search_frame(clmd, framenr))
-			return;
-	}
 	
 	// unused in the moment
 	clmd->sim_parms.dt = 1.0f / clmd->sim_parms.stepsPerFrame;
@@ -693,8 +697,7 @@
 
 	// This is for collisions objects: check special case CSIMSETT_FLAG_COLLOBJ
 	if (clmd->sim_parms.flags & CSIMSETT_FLAG_COLLOBJ)
-	{				
-		
+	{
 		// save next position + time		
 		if ((clmd->clothObject == NULL) || (numverts != clmd->clothObject->numverts) )
 		{
@@ -723,7 +726,8 @@
 			VECCOPY (verts->x, vertexCos[i]);
 			Mat4MulVecfl(ob->obmat, verts->x);
 
-			// Compute the vertices velocity. 
+			// Compute the vertices "velocity".
+			// (no dt correction here because of float error)
 			VECSUB (verts->v, verts->x, verts->xold);
 		}
 		
@@ -762,9 +766,6 @@
 					/* Get the current position. */
 					VECCOPY (verts->xconst, vertexCos[i]);
 					Mat4MulVecfl(ob->obmat, verts->xconst);
-
-					/* Compute the vertices velocity. */
-					VECSUB (verts->v, verts->xconst, verts->xold);
 				}
 
 				tstart();

Modified: branches/cloth/blender/source/blender/blenkernel/intern/collision.c
===================================================================
--- branches/cloth/blender/source/blender/blenkernel/intern/collision.c	2007-09-19 10:54:49 UTC (rev 12092)
+++ branches/cloth/blender/source/blender/blenkernel/intern/collision.c	2007-09-19 12:13:16 UTC (rev 12093)
@@ -216,18 +216,18 @@
 	VECADDMUL(to, v3, w3);
 }
 
+
 DO_INLINE void calculateFrictionImpulse(float to[3], float vrel[3], float normal[3], double normalVelocity,
 	double frictionConstant, double delta_V_n) 
 {
 	float vrel_t_pre[3];
 	float vrel_t[3];
 	VECSUBS(vrel_t_pre, vrel, normal, normalVelocity);
-	VECCOPY(vrel_t, vrel_t_pre);
-	VecMulf(vrel_t, MAX2(1.0f - frictionConstant * delta_V_n / INPR(vrel_t_pre,vrel_t_pre), 0.0f));
-	VECSUB(to, vrel_t_pre, vrel_t);
-	VecMulf(to, 1.0f / 2.0f);
+	VECCOPY(to, vrel_t_pre);
+	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, numverts=0;
@@ -271,6 +271,7 @@
 			face2 = &(cloth2->mfaces[collpair->face2]);
 			
 			// compute barycentric coordinates for both collision points
+			
 			if(!collpair->quadA)
 				bvh_compute_barycentric(collpair->p1,
 							cloth1->verts[face1->v1].txold,
@@ -297,16 +298,17 @@
 							cloth2->verts[face2->v3].txold, 
 							&u1, &u2, &u3);
 			
-			// Calculate relative velocity.
+			// Calculate relative "velocity".
+			
 			if(!collpair->quadA)
-				interpolateOnTriangle(v1, cloth1->verts[face1->v1].v, cloth1->verts[face1->v2].v, cloth1->verts[face1->v3].v, w1, w2, w3);
+				interpolateOnTriangle(v1, cloth1->verts[face1->v1].tv, cloth1->verts[face1->v2].tv, cloth1->verts[face1->v3].tv, w1, w2, w3);
 			else
-				interpolateOnTriangle(v1, cloth1->verts[face1->v4].v, cloth1->verts[face1->v1].v, cloth1->verts[face1->v3].v, w1, w2, w3);
+				interpolateOnTriangle(v1, cloth1->verts[face1->v4].tv, cloth1->verts[face1->v1].tv, cloth1->verts[face1->v3].tv, w1, w2, w3);
 			
 			if(!collpair->quadB)
-				interpolateOnTriangle(v2, cloth2->verts[face2->v1].v, cloth2->verts[face2->v2].v, cloth2->verts[face2->v3].v, u1, u2, u3);
+				interpolateOnTriangle(v2, cloth2->verts[face2->v1].tv, cloth2->verts[face2->v2].tv, cloth2->verts[face2->v3].tv, u1, u2, u3);
 			else
-				interpolateOnTriangle(v2, cloth2->verts[face2->v4].v, cloth2->verts[face2->v1].v, cloth2->verts[face2->v3].v, u1, u2, u3);
+				interpolateOnTriangle(v2, cloth2->verts[face2->v4].tv, cloth2->verts[face2->v1].tv, cloth2->verts[face2->v3].tv, u1, u2, u3);
 			
 			VECSUB(relativeVelocity, v1, v2);
 				
@@ -323,18 +325,22 @@
 				// 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;
+				float tangential[3], magtangent, magnormal, collvel[3];
+				float vrel_t_pre[3];
+				float vrel_t[3], impulse;
+				float epsilon = clmd->coll_parms.epsilon;
 				
-				calculateFrictionImpulse(tangential, relativeVelocity, collpair->normal, magrelVel, clmd->coll_parms.friction*0.01, magrelVel);
+				// calculateFrictionImpulse(tangential, relativeVelocity, collpair->normal, magrelVel, clmd->coll_parms.friction*0.01, magrelVel);
 				
-				magtangent = INPR(tangential, tangential);
+				// magtangent = INPR(tangential, tangential);
 				
 				// Apply friction impulse.
 				if (magtangent > ALMOST_ZERO) 
 				{
+					
+					// printf("friction applied: %f\n", magtangent);
+					// TODO check original code 
 					/*
-					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);
@@ -345,26 +351,52 @@
 				// printf("magnitude_i: %f\n", magnitude_i); // negative before collision in my case
 				
 				// Apply the impulse and increase impulse counters.
-				/*
-				VECADDMUL(cloth1->verts[face1->v1].tv,collpair->normal, -magnitude_i);
-				VECADDMUL(cloth1->verts[face1->v2].tv,collpair->normal, -magnitude_i);
-				VECADDMUL(cloth1->verts[face1->v3].tv,collpair->normal, -magnitude_i);
-				VECADDMUL(cloth1->verts[face1->v4].tv,collpair->normal, -magnitude_i);
-				*/
+				// my try, works better than the papers ones (maybe i did just something wrong)
+				VECSUB(collvel, cloth1->verts[face1->v1].tv, v2);
+				magnormal = INPR(collvel, collpair->normal);
+				if(magnormal<ALMOST_ZERO)
+					magnormal = 0.0f;
 				
-				// my try
-				magtangent = INPR(cloth1->verts[face1->v1].tv, collpair->normal);
-				VECADDMUL(cloth1->verts[face1->v1].tv, collpair->normal, -magtangent); 
+				impulse = (epsilon + ALMOST_ZERO-collpair->distance) / 4.0f;
+				VECADDMUL(cloth1->verts[face1->v1].tv, collpair->normal, -magnormal + impulse); 
 				
-				magtangent = INPR(cloth1->verts[face1->v2].tv, collpair->normal);
-				VECADDMUL(cloth1->verts[face1->v2].tv, collpair->normal, -magtangent); 
+				// calculateFrictionImpulse(tangential, collvel, collpair->normal, magtangent, clmd->coll_parms.friction*0.01, magtangent);
 				
-				magtangent = INPR(cloth1->verts[face1->v3].tv, collpair->normal);
-				VECADDMUL(cloth1->verts[face1->v3].tv, collpair->normal, -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));
+				magtangent = Normalize(vrel_t_pre);
+				VecMulf(vrel_t_pre, MIN2(clmd->coll_parms.friction*0.01f*magnormal,magtangent));
 				
-				magtangent = INPR(cloth1->verts[face1->v4].tv, collpair->normal);
-				VECADDMUL(cloth1->verts[face1->v4].tv, collpair->normal, -magtangent); 
+				VECSUB(cloth1->verts[face1->v1].tv, cloth1->verts[face1->v1].tv,vrel_t_pre);
+*/				
 				
+				VECSUB(collvel, cloth1->verts[face1->v2].tv, v2);
+				magnormal = INPR(collvel, collpair->normal);
+				if(magnormal<ALMOST_ZERO)
+					magnormal = 0.0f;
+				
+				impulse = (epsilon + ALMOST_ZERO-collpair->distance) / 4.0f;
+				VECADDMUL(cloth1->verts[face1->v2].tv, collpair->normal, -magnormal+ impulse); 
+				
+				
+				VECSUB(collvel, cloth1->verts[face1->v3].tv, v2);
+				magnormal = INPR(collvel, collpair->normal);
+				if(magnormal<ALMOST_ZERO)
+					magnormal = 0.0f;
+				
+				impulse = (epsilon + ALMOST_ZERO-collpair->distance) / 4.0f;
+				VECADDMUL(cloth1->verts[face1->v3].tv, collpair->normal, -magnormal+ impulse); 
+				
+				
+				VECSUB(collvel, cloth1->verts[face1->v4].tv, v2);
+				magnormal = INPR(collvel, collpair->normal);
+				if(magnormal<ALMOST_ZERO)
+					magnormal = 0.0f;
+				
+				impulse = (epsilon + ALMOST_ZERO-collpair->distance) / 4.0f;
+				VECADDMUL(cloth1->verts[face1->v4].tv, collpair->normal, -magnormal+ impulse); 
+			
 				result = 1;
 				
 			}
@@ -586,7 +618,7 @@
 					bvh_traverse(clmd, coll_clmd, cloth_bvh->root, coll_bvh->root, step, collision_response);
 					
 					result += collision_static(clmd, coll_clmd, collision_list);
-					printf("result: %d\n", result);
+					
 					// calculate velocities
 					
 					// free temporary list 

Modified: branches/cloth/blender/source/blender/blenkernel/intern/implicit.c
===================================================================
--- branches/cloth/blender/source/blender/blenkernel/intern/implicit.c	2007-09-19 10:54:49 UTC (rev 12092)
+++ branches/cloth/blender/source/blender/blenkernel/intern/implicit.c	2007-09-19 12:13:16 UTC (rev 12093)
@@ -1484,6 +1484,7 @@
 			VECCOPY(verts[i].tx, id->Xnew[i]);
 			
 			VECSUB(verts[i].tv, verts[i].tx, verts[i].txold);
+			VECSUB(verts[i].v, verts[i].tx, verts[i].txold);
 		}
 
 		// call collision function

Modified: branches/cloth/blender/source/blender/src/buttons_object.c

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list