Fixed some collision response issues. (weekend commit, some half done work in)
[blender.git] / source / blender / blenkernel / intern / collision.c
1 /*  collision.c      
2
3 *
4 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version. The Blender
10 * Foundation also sells licenses for use in proprietary software under
11 * the Blender License.  See http://www.blender.org/BL/ for information
12 * about this.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22 *
23 * The Original Code is Copyright (C) Blender Foundation
24 * All rights reserved.
25 *
26 * The Original Code is: all of this file.
27 *
28 * Contributor(s): none yet.
29 *
30 * ***** END GPL/BL DUAL LICENSE BLOCK *****
31 */
32
33 #include <math.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include "MEM_guardedalloc.h"
37 /* types */
38 #include "DNA_curve_types.h"
39 #include "DNA_object_types.h"
40 #include "DNA_object_force.h"
41 #include "DNA_cloth_types.h"    
42 #include "DNA_key_types.h"
43 #include "DNA_mesh_types.h"
44 #include "DNA_meshdata_types.h"
45 #include "DNA_lattice_types.h"
46 #include "DNA_scene_types.h"
47 #include "DNA_modifier_types.h"
48 #include "BLI_blenlib.h"
49 #include "BLI_arithb.h"
50 #include "BLI_edgehash.h"
51 #include "BLI_linklist.h"
52 #include "BKE_curve.h"
53 #include "BKE_deform.h"
54 #include "BKE_DerivedMesh.h"
55 #include "BKE_cdderivedmesh.h"
56 #include "BKE_displist.h"
57 #include "BKE_effect.h"
58 #include "BKE_global.h"
59 #include "BKE_mesh.h"
60 #include "BKE_object.h"
61 #include "BKE_cloth.h"
62 #include "BKE_modifier.h"
63 #include "BKE_utildefines.h"
64 #include "BKE_DerivedMesh.h"
65 #include "DNA_screen_types.h"
66 #include "BSE_headerbuttons.h"
67 #include "BIF_screen.h"
68 #include "BIF_space.h"
69 #include "mydevice.h"
70
71 #include "Bullet-C-Api.h"
72
73
74 #define DERANDOMIZE 1
75
76
77 enum TRIANGLE_MARK 
78
79         TM_MV = 1,
80         TM_ME = 2,
81         TM_V1 = 4,
82         TM_V2 = 8,
83         TM_V3 = 16,
84         TM_E1 = 32,
85         TM_E2 = 64,
86         TM_E3 = 128 
87 };
88
89 DO_INLINE int hasTriangleMark(unsigned char mark, unsigned char bit) { return mark & bit; }
90 DO_INLINE void setTriangleMark(unsigned char *mark, unsigned char bit) { mark[0] |= bit; }
91 DO_INLINE void clearTriangleMark(unsigned char *mark, unsigned char bit) { mark[0] &= ~bit; }
92
93
94 void generateTriangleMarks() 
95 {
96         /*
97         unsigned int firstEdge = 0;
98         
99         // 1. Initialization
100         memset(m_triangleMarks, 0, sizeof(unsigned char) * m_triangleCount);
101
102         // 2. The Marking Process
103         
104         // 2.1 Randomly mark triangles for covering vertices.
105         for (unsigned int v = 0; v < m_vertexCount; ++v) 
106         {
107                 if (vertexCover(v) == 0) 
108                 {
109
110                         // Randomly select an edge whose first triangle we're going to flag. 
111
112 #ifndef DERANDOMIZE
113                         firstEdge = (unsigned int)((float)(random() & 0x7FFFFFFF) /
114                                         (float)(0x80000000) *
115                                         (float)(m_vertices[v].getEdgeCount()));
116 #endif
117                         for (unsigned int ofs = 0; ofs < m_vertices[v].getEdgeCount(); ++ofs) 
118                         {
119                                 unsigned int edgeIdx = (firstEdge + ofs) % m_vertices[v].getEdgeCount();
120                                 if (m_edges[m_vertices[v].getEdge(edgeIdx)].getTriangleCount())
121                                         setTriangleMark(m_triangleMarks[m_edges[m_vertices[v].getEdge(edgeIdx)].getTriangle(0)], TM_MV);
122                         }
123                 }
124         }
125         */
126         /* If the Cloth is malformed (vertices without adjacent triangles) there might still be uncovered vertices. (Bad luck.) */
127         /*
128         // 2.2 Randomly mark triangles for covering edges.
129         for (unsigned int e = 0; e < m_edgeCount; ++e) 
130         {
131                 if (m_edges[e].getTriangleCount() && (edgeCover(e) == 0)) 
132                 {
133 #ifndef DERANDOMIZE
134                         setTriangleMark(m_triangleMarks[m_edges[e].getTriangle(static_cast<UINT32>((float)(random() & 0x7FFFFFFF) /
135                                         (float)(0x80000000) *
136                                         (float)(m_edges[e].getTriangleCount())))], TM_ME);
137 #else
138                         setTriangleMark(m_triangleMarks[m_edges[e].getTriangle(0)], TM_ME);
139 #endif
140                 }
141         }
142
143         
144         // 3. The Unmarking Process
145         for (unsigned int t = 0; (t < m_triangleCount); ++t) 
146         {
147                 bool overCoveredVertices = true;
148                 bool overCoveredEdges = true;
149                 for (unsigned char i = 0; (i < 3) && (overCoveredVertices || overCoveredEdges); ++i) 
150                 {
151
152                         if (vertexCover(m_triangles[t].getVertex(i)) == 1)
153                                 overCoveredVertices = false;
154                         if (edgeCover(m_triangles[t].getEdge(i)) == 1)
155                                 overCoveredEdges = false;
156
157                         assert(vertexCover(m_triangles[t].getVertex(i)) > 0);
158                         assert(edgeCover(m_triangles[t].getEdge(i)) > 0);
159                 }
160                 if (overCoveredVertices)
161                         clearTriangleMark(m_triangleMarks[t], TM_MV);
162                 if (overCoveredEdges)
163                         clearTriangleMark(m_triangleMarks[t], TM_ME);
164         }
165
166
167         // 4. The Bit Masking Process
168         vector<bool> vertexAssigned(m_vertexCount, false);
169         vector<bool> edgeAssigned(m_edgeCount, false);
170         for (unsigned int t = 0; (t < m_triangleCount); ++t) 
171         {
172                 for (unsigned char i = 0; i < 3; ++i) 
173                 {
174                         if (!vertexAssigned[m_triangles[t].getVertex(i)]) 
175                         {
176                                 vertexAssigned[m_triangles[t].getVertex(i)] = true;
177                                 setTriangleMark(m_triangleMarks[t], 1 << (2 + i));
178                         }
179                         if (!edgeAssigned[m_triangles[t].getEdge(i)]) 
180                         {
181                                 edgeAssigned[m_triangles[t].getEdge(i)] = true;
182                                 setTriangleMark(m_triangleMarks[t], 1 << (5 + i));
183                         }
184                 }
185         }
186         */
187 }
188
189 // w3 is not perfect
190 void bvh_compute_barycentric (float pv[3], float p1[3], float p2[3], float p3[3], double *w1, double *w2, double *w3)
191 {
192         double  tempV1[3], tempV2[3], tempV4[3];
193         double  a,b,c,d,e,f;
194
195         VECSUB (tempV1, p1, p3);        
196         VECSUB (tempV2, p2, p3);        
197         VECSUB (tempV4, pv, p3);        
198         
199         a = INPR (tempV1, tempV1);      
200         b = INPR (tempV1, tempV2);      
201         c = INPR (tempV2, tempV2);      
202         e = INPR (tempV1, tempV4);      
203         f = INPR (tempV2, tempV4);      
204         
205         d = (a * c - b * b);
206         
207         if (ABS(d) < ALMOST_ZERO) {
208                 *w1 = *w2 = *w3 = 1.0f / 3.0f;
209                 return;
210         }
211         
212         w1[0] = (e * c - b * f) / d;
213         
214         if(w1[0] < 0)
215                 w1[0] = 0.0;
216         
217         w2[0] = (f - b * w1[0]) / c;
218         
219         if(w2[0] < 0)
220                 w2[0] = 0.0;
221         
222         w3[0] = 1.0f - w1[0] - w2[0];
223 }
224
225 DO_INLINE void interpolateOnTriangle(float to[3], float v1[3], float v2[3], float v3[3], double w1, double w2, double w3) 
226 {
227         to[0] = to[1] = to[2] = 0;
228         VECADDMUL(to, v1, w1);
229         VECADDMUL(to, v2, w2);
230         VECADDMUL(to, v3, w3);
231 }
232
233
234 DO_INLINE void calculateFrictionImpulse(float to[3], float vrel[3], float normal[3], double normalVelocity,
235         double frictionConstant, double delta_V_n) 
236 {
237         float vrel_t_pre[3];
238         float vrel_t[3];
239         VECSUBS(vrel_t_pre, vrel, normal, normalVelocity);
240         VECCOPY(to, vrel_t_pre);
241         VecMulf(to, MAX2(1.0f - frictionConstant * delta_V_n / INPR(vrel_t_pre,vrel_t_pre), 0.0f));
242 }
243
244                 
245 int collision_static(ClothModifierData *clmd, ClothModifierData *coll_clmd, LinkNode **collision_list)
246 {
247         unsigned int i = 0, numfaces = 0;
248         int result = 0;
249         LinkNode *search = NULL;
250         CollPair *collpair = NULL;
251         Cloth *cloth1, *cloth2;
252         MFace *face1, *face2;
253         double w1, w2, w3, u1, u2, u3, a1, a2, a3;
254         float v1[3], v2[3], relativeVelocity[3];
255         float magrelVel;
256         
257         cloth1 = clmd->clothObject;
258         cloth2 = coll_clmd->clothObject;
259         
260         numfaces = clmd->clothObject->numfaces;
261                 
262         for(i = 0; i < numfaces; i++)
263         {
264                 search = collision_list[i];
265                 
266                 while(search)
267                 {
268                         collpair = search->link;
269                         
270                         face1 = &(cloth1->mfaces[collpair->face1]);
271                         face2 = &(cloth2->mfaces[collpair->face2]);
272                         
273                         // compute barycentric coordinates for both collision points
274                         
275                         if(!collpair->quadA)
276                         {       
277                                 bvh_compute_barycentric(collpair->p1,
278                                                         cloth1->verts[face1->v1].txold,
279                                                         cloth1->verts[face1->v2].txold,
280                                                         cloth1->verts[face1->v3].txold, 
281                                                         &w1, &w2, &w3);
282                         }
283                         else
284                                 bvh_compute_barycentric(collpair->p1,
285                                                         cloth1->verts[face1->v4].txold,
286                                                         cloth1->verts[face1->v1].txold,
287                                                         cloth1->verts[face1->v3].txold, 
288                                                         &w1, &w2, &w3);
289                         
290                         if(!collpair->quadB)
291                                 bvh_compute_barycentric(collpair->p2,
292                                                         cloth2->verts[face2->v1].txold,
293                                                         cloth2->verts[face2->v2].txold,
294                                                         cloth2->verts[face2->v3].txold, 
295                                                         &u1, &u2, &u3);
296                         else
297                                 bvh_compute_barycentric(collpair->p2,
298                                                         cloth2->verts[face2->v4].txold,
299                                                         cloth2->verts[face2->v1].txold,
300                                                         cloth2->verts[face2->v3].txold, 
301                                                         &u1, &u2, &u3);
302                         
303                         // Calculate relative "velocity".
304                         
305                         if(!collpair->quadA)
306                                 interpolateOnTriangle(v1, cloth1->verts[face1->v1].tv, cloth1->verts[face1->v2].tv, cloth1->verts[face1->v3].tv, w1, w2, w3);
307                         else
308                                 interpolateOnTriangle(v1, cloth1->verts[face1->v4].tv, cloth1->verts[face1->v1].tv, cloth1->verts[face1->v3].tv, w1, w2, w3);
309                         
310                         if(!collpair->quadB)
311                                 interpolateOnTriangle(v2, cloth2->verts[face2->v1].tv, cloth2->verts[face2->v2].tv, cloth2->verts[face2->v3].tv, u1, u2, u3);
312                         else
313                                 interpolateOnTriangle(v2, cloth2->verts[face2->v4].tv, cloth2->verts[face2->v1].tv, cloth2->verts[face2->v3].tv, u1, u2, u3);
314                         
315                         VECSUB(relativeVelocity, v1, v2);
316                                 
317                         // Calculate the normal component of the relative velocity (actually only the magnitude - the direction is stored in 'normal').
318                         magrelVel = INPR(relativeVelocity, collpair->normal);
319                                         
320                         // Calculate masses of points.
321                         
322                         // If v_n_mag > 0 the edges are approaching each other.
323                         
324                         if(magrelVel < -ALMOST_ZERO)
325                         {
326                                 // Calculate Impulse magnitude to stop all motion in normal direction.
327                                 // const double I_mag = v_n_mag / (1/m1 + 1/m2);
328                                 float magnitude_i = magrelVel / 2.0f; // TODO implement masses
329                                 float tangential[3], magtangent, magnormal, collvel[3];
330                                 float vrel_t_pre[3];
331                                 float vrel_t[3];
332                                 double impulse;
333                                 float epsilon = clmd->coll_parms.epsilon;
334                                 float overlap = (epsilon + ALMOST_ZERO-collpair->distance);
335                                 
336                                 // calculateFrictionImpulse(tangential, relativeVelocity, collpair->normal, magrelVel, clmd->coll_parms.friction*0.01, magrelVel);
337                                 
338                                 // magtangent = INPR(tangential, tangential);
339                                 
340                                 // Apply friction impulse.
341                                 if (magtangent < ALMOST_ZERO) 
342                                 {
343                                         
344                                         // printf("friction applied: %f\n", magtangent);
345                                         // TODO check original code 
346                                         /*
347                                         VECSUB(cloth1->verts[face1->v1].tv, cloth1->verts[face1->v1].tv,tangential);
348                                         VECSUB(cloth1->verts[face1->v1].tv, cloth1->verts[face1->v2].tv,tangential);
349                                         VECSUB(cloth1->verts[face1->v1].tv, cloth1->verts[face1->v3].tv,tangential);
350                                         VECSUB(cloth1->verts[face1->v1].tv, cloth1->verts[face1->v4].tv,tangential);
351                                         */
352                                 }
353                                 
354                                 impulse = -magrelVel / ( 1.0 + w1*w1 + w2*w2 + w3*w3);
355                                 VECADDMUL(cloth1->verts[face1->v1].impulse, collpair->normal, impulse); 
356                                 cloth1->verts[face1->v1].impulse_count++;
357                                 
358                                 VECADDMUL(cloth1->verts[face1->v2].impulse, collpair->normal, impulse); 
359                                 cloth1->verts[face1->v2].impulse_count++;
360                                 
361                                 VECADDMUL(cloth1->verts[face1->v3].impulse, collpair->normal, impulse); 
362                                 cloth1->verts[face1->v3].impulse_count++;
363                                 
364                                 if(face1->v4)
365                                 {
366                                         VECADDMUL(cloth1->verts[face1->v4].impulse, collpair->normal, impulse); 
367                                         cloth1->verts[face1->v4].impulse_count++;
368                                 }
369                                 
370                                 
371                                 if (overlap > ALMOST_ZERO) {
372                                         double I_mag  = overlap * 0.1;
373                                         
374                                         impulse = I_mag / ( 1.0 + w1*w1 + w2*w2 + w3*w3);
375                                         
376                                         VECADDMUL(cloth1->verts[face1->v1].impulse, collpair->normal, impulse); 
377                                         cloth1->verts[face1->v1].impulse_count++;
378                                                                 
379                                         VECADDMUL(cloth1->verts[face1->v2].impulse, collpair->normal, impulse); 
380                                         cloth1->verts[face1->v2].impulse_count++;
381                                 
382                                         VECADDMUL(cloth1->verts[face1->v3].impulse, collpair->normal, impulse); 
383                                         cloth1->verts[face1->v3].impulse_count++;
384                                 
385                                         if(face1->v4)
386                                         {
387                                                 VECADDMUL(cloth1->verts[face1->v4].impulse, collpair->normal, impulse); 
388                                                 cloth1->verts[face1->v4].impulse_count++;
389                                         }
390                                 
391                                 }
392                                 
393                                 result = 1;
394                                 
395                         
396                                 // printf("magnitude_i: %f\n", magnitude_i); // negative before collision in my case
397                                 
398                                 // Apply the impulse and increase impulse counters.
399
400                                 /*                      
401                                 // calculateFrictionImpulse(tangential, collvel, collpair->normal, magtangent, clmd->coll_parms.friction*0.01, magtangent);
402                                 VECSUBS(vrel_t_pre, collvel, collpair->normal, magnormal);
403                                 // VecMulf(vrel_t_pre, clmd->coll_parms.friction*0.01f/INPR(vrel_t_pre,vrel_t_pre));
404                                 magtangent = Normalize(vrel_t_pre);
405                                 VecMulf(vrel_t_pre, MIN2(clmd->coll_parms.friction*0.01f*magnormal,magtangent));
406                                 
407                                 VECSUB(cloth1->verts[face1->v1].tv, cloth1->verts[face1->v1].tv,vrel_t_pre);
408                                 */
409                                 
410                                 
411                                 
412                         }
413                         
414                         search = search->next;
415                 }
416         }
417                 
418         return result;
419 }
420
421 // return distance between two triangles using bullet engine
422 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)
423 {
424         MFace *face1=NULL, *face2=NULL;
425         float  a[3][3];
426         float  b[3][3];
427         double distance=0, tempdistance=0;
428         Cloth *cloth1=NULL, *cloth2=NULL;
429         float tpa[3], tpb[3], tnormal[3];
430         unsigned int indexA=0, indexB=0, indexC=0, indexD=0, indexE=0, indexF=0;
431         int i = 0;
432         
433         cloth1 = clmd->clothObject;
434         cloth2 = coll_clmd->clothObject;
435         
436         face1 = &(cloth1->mfaces[tri_index1]);
437         face2 = &(cloth2->mfaces[tri_index2]);
438         
439         // face a1 + face b1
440         VECCOPY(a[0], cloth1->verts[face1->v1].txold);
441         VECCOPY(a[1], cloth1->verts[face1->v2].txold);
442         VECCOPY(a[2], cloth1->verts[face1->v3].txold);
443         
444         
445         VECCOPY(b[0], cloth2->verts[face2->v1].txold);
446         VECCOPY(b[1], cloth2->verts[face2->v2].txold);
447         VECCOPY(b[2], cloth2->verts[face2->v3].txold);
448
449         distance = plNearestPoints(a,b,pa,pb,normal);
450         
451         quadA = quadB = 0;
452         
453         for(i = 0; i < 3; i++)
454         {
455                 if(i == 0)
456                 {
457                         if(face1->v4)
458                         {
459                                 indexA = face1->v4;
460                                 indexB = face1->v1;
461                                 indexC = face1->v3;
462                                 
463                                 indexD = face2->v1;
464                                 indexE = face2->v2;
465                                 indexF = face2->v3;
466                         }
467                         else 
468                                 i+=2;
469                 }
470                 
471                 if(i == 1)
472                 {
473                         if((face1->v4)&&(face2->v4))
474                         {
475                                 indexA = face1->v4;
476                                 indexB = face1->v1;
477                                 indexC = face1->v3;
478                         
479                                 indexD = face2->v4;
480                                 indexE = face2->v1;
481                                 indexF = face2->v3;
482                         }
483                         else
484                                 i++;
485                 }
486                 
487                 if(i == 2)
488                 {
489                         if(face2->v4)
490                         {
491                                 indexA = face1->v1;
492                                 indexB = face1->v2;
493                                 indexC = face1->v3;
494                         
495                                 indexD = face2->v4;
496                                 indexE = face2->v1;
497                                 indexF = face2->v3;
498                         }
499                         else
500                                 i++;
501                         
502                 }
503                 
504                 if(i<3)
505                 {
506                         // face a2 + face b1
507                         VECCOPY(a[0], cloth1->verts[indexA].txold);
508                         VECCOPY(a[1], cloth1->verts[indexB].txold);
509                         VECCOPY(a[2], cloth1->verts[indexC].txold);
510                         
511                         
512                         VECCOPY(b[0], cloth2->verts[indexD].txold);
513                         VECCOPY(b[1], cloth2->verts[indexE].txold);
514                         VECCOPY(b[2], cloth2->verts[indexF].txold);
515         
516                         tempdistance = plNearestPoints(a,b,tpa,tpb,tnormal);
517                         
518                         if(tempdistance < distance)
519                         {
520                                 VECCOPY(pa, tpa);
521                                 VECCOPY(pb, tpb);
522                                 VECCOPY(normal, tnormal);
523                                 distance = tempdistance;
524                                 
525                                 if(i == 0)
526                                 {
527                                         quadA = 1; quadB = 0;
528                                 }
529                                 else if(i == 1)
530                                 {
531                                         quadA = quadB = 1;
532                                 }
533                                 else if(i == 2)
534                                 {
535                                         quadA = 0; quadB = 1;
536                                 }
537                         }
538                 }
539         }
540         return distance;
541 }
542
543 void bvh_collision_response(ClothModifierData *clmd, ClothModifierData *coll_clmd, Tree * tree1, Tree * tree2)
544 {
545         CollPair *collpair = NULL;
546         LinkNode **linknode;
547         double distance = 0;
548         float epsilon = clmd->coll_parms.epsilon;
549
550         collpair = (CollPair *)MEM_callocN(sizeof(CollPair), "cloth coll pair");
551         linknode = clmd->coll_parms.temp;
552         
553         // calc SIPcode (?)
554         
555         // calc distance + normal       
556         distance = implicit_tri_check_coherence(clmd, coll_clmd, tree1->tri_index, tree2->tri_index, collpair->p1, collpair->p2, collpair->vector, collpair->quadA, collpair->quadB);
557         
558         if ((distance <= (epsilon + ALMOST_ZERO)) && (distance > -1.0f)) // max overlap = 1.0 
559         {
560                 // printf("dist: %f\n", (float)distance);
561                 
562                 collpair->face1 = tree1->tri_index;
563                 collpair->face2 = tree2->tri_index;
564                 
565                 VECCOPY(collpair->normal, collpair->vector);
566                 Normalize(collpair->normal);
567                 
568                 collpair->distance = distance;
569                 BLI_linklist_append(&linknode[tree1->tri_index], collpair);
570         }
571         else
572         {
573                 MEM_freeN(collpair);
574         }
575 }
576
577 // move collision objects forward in time and update static bounding boxes
578 void cloth_update_collision_objects(float step)
579 {
580         Base *base=NULL;
581         ClothModifierData *coll_clmd=NULL;
582         Object *coll_ob=NULL;
583         unsigned int i=0;
584         
585         // search all objects for collision object
586         for (base = G.scene->base.first; base; base = base->next)
587         {
588
589                 coll_ob = base->object;
590                 coll_clmd = (ClothModifierData *) modifiers_findByType (coll_ob, eModifierType_Cloth);
591                 if (!coll_clmd)
592                         continue;
593
594                 // if collision object go on
595                 if (coll_clmd->sim_parms.flags & CSIMSETT_FLAG_COLLOBJ)
596                 {
597                         if (coll_clmd->clothObject && coll_clmd->clothObject->tree) 
598                         {
599                                 Cloth *coll_cloth = coll_clmd->clothObject;
600                                 BVH *coll_bvh = coll_clmd->clothObject->tree;
601                                 unsigned int coll_numverts = coll_cloth->numverts;
602
603                                 // update position of collision object
604                                 for(i = 0; i < coll_numverts; i++)
605                                 {
606                                         VECCOPY(coll_cloth->verts[i].txold, coll_cloth->verts[i].tx);
607
608                                         VECADDS(coll_cloth->verts[i].tx, coll_cloth->verts[i].xold, coll_cloth->verts[i].v, step);
609                                         
610                                         // no dt here because of float rounding errors
611                                         VECSUB(coll_cloth->verts[i].tv, coll_cloth->verts[i].tx, coll_cloth->verts[i].txold);
612                                 }
613                                                 
614                                 // update BVH of collision object
615                                 bvh_update_static(coll_clmd, coll_bvh);
616                         }
617                         else
618                                 printf ("cloth_bvh_objcollision: found a collision object with clothObject or collData NULL.\n");
619                 }
620         }
621 }
622
623 #define CLOTH_MAX_THRESHOLD 5
624
625 // cloth - object collisions
626 int cloth_bvh_objcollision(ClothModifierData * clmd, float step, CM_COLLISION_RESPONSE collision_response, float dt)
627 {
628         Base *base=NULL;
629         ClothModifierData *coll_clmd=NULL;
630         Cloth *cloth=NULL;
631         Object *coll_ob=NULL;
632         BVH *cloth_bvh=NULL;
633         unsigned int i=0, numfaces = 0, numverts = 0;
634         unsigned int result = 0, ic = 0, rounds = 0;
635         ClothVertex *verts = NULL;
636         float tnull[3] = {0,0,0};
637
638         if ((clmd->sim_parms.flags & CSIMSETT_FLAG_COLLOBJ) || !(((Cloth *)clmd->clothObject)->tree))
639         {
640                 return 0;
641         }
642         cloth = clmd->clothObject;
643         verts = cloth->verts;
644         cloth_bvh = (BVH *) cloth->tree;
645         numfaces = clmd->clothObject->numfaces;
646         numverts = clmd->clothObject->numverts;
647         
648         ////////////////////////////////////////////////////////////
649         // static collisions
650         ////////////////////////////////////////////////////////////
651
652         // update cloth bvh
653         bvh_update_static(clmd, cloth_bvh);
654         
655         // update collision objects
656         cloth_update_collision_objects(step);
657
658         do
659         {
660                 result = 0;
661                 ic = 0;
662                         
663                 // handle all collision objects
664                 for (base = G.scene->base.first; base; base = base->next)
665                 {
666         
667                         coll_ob = base->object;
668                         coll_clmd = (ClothModifierData *) modifiers_findByType (coll_ob, eModifierType_Cloth);
669                         if (!coll_clmd)
670                                 continue;
671         
672                         // if collision object go on
673                         if (coll_clmd->sim_parms.flags & CSIMSETT_FLAG_COLLOBJ)
674                         {
675                                 if (coll_clmd->clothObject && coll_clmd->clothObject->tree) 
676                                 {
677                                         LinkNode **collision_list = MEM_callocN (sizeof(LinkNode *)*(numfaces), "collision_list");
678                                         BVH *coll_bvh = coll_clmd->clothObject->tree;
679         
680                                         if(collision_list)
681                                         {                                       
682                                                 memset(collision_list, 0, sizeof(LinkNode *)*numfaces);
683                                                 clmd->coll_parms.temp = collision_list;
684         
685                                                 bvh_traverse(clmd, coll_clmd, cloth_bvh->root, coll_bvh->root, step, collision_response);
686                                                 
687                                                 result += collision_static(clmd, coll_clmd, collision_list);
688                                                 
689                                                 // calculate velocities
690                                                 
691                                                 // free temporary list 
692                                                 for(i = 0; i < numfaces; i++)
693                                                 {
694                                                         LinkNode *search = collision_list[i];
695                                                         while(search)
696                                                         {
697                                                                 LinkNode *next= search->next;
698                                                                 CollPair *collpair = search->link;
699                                                                 
700                                                                 if(collpair)
701                                                                         MEM_freeN(collpair);    
702         
703                                                                 search = next;
704                                                         }
705         
706                                                         BLI_linklist_free(collision_list[i],NULL); 
707                                                 }
708                                                 if(collision_list)
709                                                         MEM_freeN(collision_list);
710         
711                                                 clmd->coll_parms.temp = NULL;
712                                         }
713                                         
714         
715                                 }
716                                 else
717                                         printf ("cloth_bvh_objcollision: found a collision object with clothObject or collData NULL.\n");
718                         }
719                 }
720                 
721                 // now apply impulses parallel
722                 
723                 for(i = 0; i < numverts; i++)
724                 {
725                         if(verts[i].impulse_count)
726                         {
727                                 VECADDMUL(verts[i].tv, verts[i].impulse, 1.0f / verts[i].impulse_count);
728                                 VECCOPY(verts[i].impulse, tnull);
729                                 verts[i].impulse_count = 0;
730                                 
731                                 ic++;
732                         }
733                 }
734                 
735                 printf("ic: %d\n", ic);
736                 rounds++;
737         }
738         while(result && (CLOTH_MAX_THRESHOLD>rounds));
739         
740         printf("\n");
741                         
742         ////////////////////////////////////////////////////////////
743         // update positions + velocities
744         ////////////////////////////////////////////////////////////
745
746         // TODO 
747
748
749         ////////////////////////////////////////////////////////////
750         // moving collisions
751         ////////////////////////////////////////////////////////////
752
753         // TODO 
754         // bvh_update_moving(clmd, clmd->clothObject->tree);
755
756         return MIN2(result, 1);
757 }