Change !BLI_ghashIterator_isDone to BLI_ghashIterator_notDone. It is
[blender.git] / source / blender / blenkernel / intern / softbody.c
index 5b02731050ef629ee681d5c2dfac85baf910ca38..01d4696de94dcbd4b81faec4decafe407b501534 100644 (file)
@@ -78,8 +78,8 @@ variables on the UI for now
 #include "BKE_DerivedMesh.h"
 #include "BKE_pointcache.h"
 #include "BKE_deform.h"
 #include "BKE_DerivedMesh.h"
 #include "BKE_pointcache.h"
 #include "BKE_deform.h"
-//XXX #include  "BIF_editdeform.h"
-//XXX #include  "BIF_graphics.h"
+#include "BKE_mesh.h"
+
 #include  "PIL_time.h"
 // #include  "ONL_opennl.h" remove linking to ONL for now
 
 #include  "PIL_time.h"
 // #include  "ONL_opennl.h" remove linking to ONL for now
 
@@ -89,18 +89,18 @@ static int (*SB_localInterruptCallBack)(void) = NULL;
 
 /* ********** soft body engine ******* */
 
 
 /* ********** soft body engine ******* */
 
-typedef        enum {SB_EDGE=1,SB_BEND=2,SB_STIFFQUAD=3,SB_HANDLE=4} type_spring;
+typedef        enum {SB_EDGE=1, SB_BEND=2, SB_STIFFQUAD=3, SB_HANDLE=4} type_spring;
 
 typedef struct BodySpring {
        int v1, v2;
 
 typedef struct BodySpring {
        int v1, v2;
-       float len,cf,load;
+       float len, cf, load;
        float ext_force[3]; /* edges colliding and sailing */
        type_spring springtype;
        short flag;
 } BodySpring;
 
 typedef struct BodyFace {
        float ext_force[3]; /* edges colliding and sailing */
        type_spring springtype;
        short flag;
 } BodySpring;
 
 typedef struct BodyFace {
-       int v1, v2, v3 ,v4;
+       int v1, v2, v3v4;
        float ext_force[3]; /* faces colliding */
        short flag;
 } BodyFace;
        float ext_force[3]; /* faces colliding */
        short flag;
 } BodyFace;
@@ -113,7 +113,7 @@ typedef struct ReferenceVert {
 typedef struct ReferenceState {
        float com[3]; /* center of mass*/
        ReferenceVert *ivert; /* list of intial values */
 typedef struct ReferenceState {
        float com[3]; /* center of mass*/
        ReferenceVert *ivert; /* list of intial values */
-}ReferenceState ;
+} ReferenceState;
 
 
 /*private scratch pad for caching and other data only needed when alive*/
 
 
 /*private scratch pad for caching and other data only needed when alive*/
@@ -123,9 +123,9 @@ typedef struct SBScratch {
        short flag;
        BodyFace *bodyface;
        int totface;
        short flag;
        BodyFace *bodyface;
        int totface;
-       float aabbmin[3],aabbmax[3];
+       float aabbmin[3], aabbmax[3];
        ReferenceState Ref;
        ReferenceState Ref;
-}SBScratch;
+} SBScratch;
 
 typedef struct  SB_thread_context {
                Scene *scene;
 
 typedef struct  SB_thread_context {
                Scene *scene;
@@ -140,17 +140,19 @@ typedef struct  SB_thread_context {
                float windfactor;
                int nr;
                int tot;
                float windfactor;
                int nr;
                int tot;
-}SB_thread_context;
+} SB_thread_context;
 
 #define NLF_BUILD  1
 
 #define NLF_BUILD  1
-#define NLF_SOLVE  2
+#if 0
+#  define NLF_SOLVE  2
+#endif
 
 #define MID_PRESERVE 1
 
 #define SOFTGOALSNAP  0.999f
 /* if bp-> goal is above make it a *forced follow original* and skip all ODE stuff for this bp
 
 #define MID_PRESERVE 1
 
 #define SOFTGOALSNAP  0.999f
 /* if bp-> goal is above make it a *forced follow original* and skip all ODE stuff for this bp
  removes *unnecessary* stiffnes from ODE system
-*/
* removes *unnecessary* stiffness from ODE system
+ */
 #define HEUNWARNLIMIT 1 /* 500 would be fine i think for detecting severe *stiff* stuff */
 
 
 #define HEUNWARNLIMIT 1 /* 500 would be fine i think for detecting severe *stiff* stuff */
 
 
@@ -178,16 +180,16 @@ static void Vec3PlusStVec(float *v, float s, float *v1);
 
 static float sb_grav_force_scale(Object *UNUSED(ob))
 /* since unit of g is [m/sec^2] and F = mass * g we rescale unit mass of node to 1 gramm
 
 static float sb_grav_force_scale(Object *UNUSED(ob))
 /* since unit of g is [m/sec^2] and F = mass * g we rescale unit mass of node to 1 gramm
-  put it to a function here, so we can add user options later without touching simulation code
-*/
* put it to a function here, so we can add user options later without touching simulation code
+ */
 {
        return (0.001f);
 }
 
 static float sb_fric_force_scale(Object *UNUSED(ob))
 /* rescaling unit of drag [1 / sec] to somehow reasonable
 {
        return (0.001f);
 }
 
 static float sb_fric_force_scale(Object *UNUSED(ob))
 /* rescaling unit of drag [1 / sec] to somehow reasonable
-  put it to a function here, so we can add user options later without touching simulation code
-*/
* put it to a function here, so we can add user options later without touching simulation code
+ */
 {
        return (0.01f);
 }
 {
        return (0.01f);
 }
@@ -196,7 +198,7 @@ static float sb_time_scale(Object *ob)
 /* defining the frames to *real* time relation */
 {
        SoftBody *sb= ob->soft; /* is supposed to be there */
 /* defining the frames to *real* time relation */
 {
        SoftBody *sb= ob->soft; /* is supposed to be there */
-       if (sb){
+       if (sb) {
                return(sb->physics_speed);
                /*hrms .. this could be IPO as well :)
                 estimated range [0.001 sluggish slug - 100.0 very fast (i hope ODE solver can handle that)]
                return(sb->physics_speed);
                /*hrms .. this could be IPO as well :)
                 estimated range [0.001 sluggish slug - 100.0 very fast (i hope ODE solver can handle that)]
@@ -215,40 +217,40 @@ static float sb_time_scale(Object *ob)
 
 /* helper functions for everything is animatable jow_go_for2_5 +++++++*/
 /* introducing them here, because i know: steps in properties  ( at frame timing )
 
 /* helper functions for everything is animatable jow_go_for2_5 +++++++*/
 /* introducing them here, because i know: steps in properties  ( at frame timing )
  will cause unwanted responses of the softbody system (which does inter frame calculations )
  so first 'cure' would be: interpolate linear in time ..
  Q: why do i write this?
  A: because it happend once, that some eger coder 'streamlined' code to fail.
  We DO linear interpolation for goals .. and i think we should do on animated properties as well
-*/
* will cause unwanted responses of the softbody system (which does inter frame calculations )
* so first 'cure' would be: interpolate linear in time ..
* Q: why do i write this?
* A: because it happend once, that some eger coder 'streamlined' code to fail.
* We DO linear interpolation for goals .. and i think we should do on animated properties as well
+ */
 
 
-/* animate sb->maxgoal,sb->mingoal */
-static float _final_goal(Object *ob,BodyPoint *bp)/*jow_go_for2_5 */
+/* animate sb->maxgoal, sb->mingoal */
+static float _final_goal(Object *ob, BodyPoint *bp)/*jow_go_for2_5 */
 {
        float f = -1999.99f;
 {
        float f = -1999.99f;
-       if (ob){
+       if (ob) {
                SoftBody *sb= ob->soft; /* is supposed to be there */
                SoftBody *sb= ob->soft; /* is supposed to be there */
-               if(!(ob->softflag & OB_SB_GOAL)) return (0.0f);
-               if (sb&&bp){
+               if (!(ob->softflag & OB_SB_GOAL)) return (0.0f);
+               if (sb&&bp) {
                        if (bp->goal < 0.0f) return (0.0f);
                        f = sb->mingoal + bp->goal*ABS(sb->maxgoal - sb->mingoal);
                        f = pow(f, 4.0f);
                        return (f);
                }
        }
                        if (bp->goal < 0.0f) return (0.0f);
                        f = sb->mingoal + bp->goal*ABS(sb->maxgoal - sb->mingoal);
                        f = pow(f, 4.0f);
                        return (f);
                }
        }
-       printf("_final_goal failed! sb or bp ==NULL \n" );
+       printf("_final_goal failed! sb or bp ==NULL\n");
        return f; /*using crude but spot able values some times helps debuggin */
 }
 
        return f; /*using crude but spot able values some times helps debuggin */
 }
 
-static float _final_mass(Object *ob,BodyPoint *bp)
+static float _final_mass(Object *ob, BodyPoint *bp)
 {
 {
-       if (ob){
+       if (ob) {
                SoftBody *sb= ob->soft; /* is supposed to be there */
                SoftBody *sb= ob->soft; /* is supposed to be there */
-               if (sb&&bp){
+               if (sb&&bp) {
                        return(bp->mass*sb->nodemass);
                }
        }
                        return(bp->mass*sb->nodemass);
                }
        }
-       printf("_final_mass failed! sb or bp ==NULL \n" );
+       printf("_final_mass failed! sb or bp ==NULL\n");
        return 1.0f;
 }
 /* helper functions for everything is animateble jow_go_for2_5 ------*/
        return 1.0f;
 }
 /* helper functions for everything is animateble jow_go_for2_5 ------*/
@@ -258,7 +260,7 @@ static float _final_mass(Object *ob,BodyPoint *bp)
 /********************
 for each target object/face the axis aligned bounding box (AABB) is stored
 faces parallel to global axes
 /********************
 for each target object/face the axis aligned bounding box (AABB) is stored
 faces parallel to global axes
-so only simple "value" in [min,max] ckecks are used
+so only simple "value" in [min, max] ckecks are used
 float operations still
 */
 
 float operations still
 */
 
@@ -267,9 +269,9 @@ float operations still
 */
 static const int CCD_SAVETY = 190561;
 
 */
 static const int CCD_SAVETY = 190561;
 
-typedef struct ccdf_minmax{
-float minx,miny,minz,maxx,maxy,maxz;
-}ccdf_minmax;
+typedef struct ccdf_minmax {
+       float minx, miny, minz, maxx, maxy, maxz;
+} ccdf_minmax;
 
 
 
 
 
 
@@ -283,7 +285,7 @@ typedef struct ccd_Mesh {
        /* Axis Aligned Bounding Box AABB */
        float bbmin[3];
        float bbmax[3];
        /* Axis Aligned Bounding Box AABB */
        float bbmin[3];
        float bbmax[3];
-}ccd_Mesh;
+} ccd_Mesh;
 
 
 
 
 
 
@@ -292,9 +294,9 @@ static ccd_Mesh *ccd_mesh_make(Object *ob)
 {
        CollisionModifierData *cmd;
        ccd_Mesh *pccd_M = NULL;
 {
        CollisionModifierData *cmd;
        ccd_Mesh *pccd_M = NULL;
-       ccdf_minmax *mima =NULL;
+       ccdf_minmax *mima = NULL;
        MFace *mface=NULL;
        MFace *mface=NULL;
-       float v[3],hull;
+       float v[3], hull;
        int i;
 
        cmd =(CollisionModifierData *)modifiers_findByType(ob, eModifierType_Collision);
        int i;
 
        cmd =(CollisionModifierData *)modifiers_findByType(ob, eModifierType_Collision);
@@ -303,7 +305,7 @@ static ccd_Mesh *ccd_mesh_make(Object *ob)
        if (!cmd) return NULL;
        if (!cmd->numverts || !cmd->numfaces) return NULL;
 
        if (!cmd) return NULL;
        if (!cmd->numverts || !cmd->numfaces) return NULL;
 
-       pccd_M = MEM_mallocN(sizeof(ccd_Mesh),"ccd_Mesh");
+       pccd_M = MEM_mallocN(sizeof(ccd_Mesh), "ccd_Mesh");
        pccd_M->totvert = cmd->numverts;
        pccd_M->totface = cmd->numfaces;
        pccd_M->savety  = CCD_SAVETY;
        pccd_M->totvert = cmd->numverts;
        pccd_M->totface = cmd->numfaces;
        pccd_M->savety  = CCD_SAVETY;
@@ -313,70 +315,70 @@ static ccd_Mesh *ccd_mesh_make(Object *ob)
 
 
        /* blow it up with forcefield ranges */
 
 
        /* blow it up with forcefield ranges */
-       hull = MAX2(ob->pd->pdef_sbift,ob->pd->pdef_sboft);
+       hull = MAX2(ob->pd->pdef_sbift, ob->pd->pdef_sboft);
 
        /* alloc and copy verts*/
        pccd_M->mvert = MEM_dupallocN(cmd->xnew);
        /* note that xnew coords are already in global space, */
        /* determine the ortho BB */
 
        /* alloc and copy verts*/
        pccd_M->mvert = MEM_dupallocN(cmd->xnew);
        /* note that xnew coords are already in global space, */
        /* determine the ortho BB */
-       for(i=0; i < pccd_M->totvert; i++){
+       for (i=0; i < pccd_M->totvert; i++) {
                /* evaluate limits */
                /* evaluate limits */
-               copy_v3_v3(v,pccd_M->mvert[i].co);
-               pccd_M->bbmin[0] = MIN2(pccd_M->bbmin[0],v[0]-hull);
-               pccd_M->bbmin[1] = MIN2(pccd_M->bbmin[1],v[1]-hull);
-               pccd_M->bbmin[2] = MIN2(pccd_M->bbmin[2],v[2]-hull);
+               copy_v3_v3(v, pccd_M->mvert[i].co);
+               pccd_M->bbmin[0] = MIN2(pccd_M->bbmin[0], v[0]-hull);
+               pccd_M->bbmin[1] = MIN2(pccd_M->bbmin[1], v[1]-hull);
+               pccd_M->bbmin[2] = MIN2(pccd_M->bbmin[2], v[2]-hull);
 
 
-               pccd_M->bbmax[0] = MAX2(pccd_M->bbmax[0],v[0]+hull);
-               pccd_M->bbmax[1] = MAX2(pccd_M->bbmax[1],v[1]+hull);
-               pccd_M->bbmax[2] = MAX2(pccd_M->bbmax[2],v[2]+hull);
+               pccd_M->bbmax[0] = MAX2(pccd_M->bbmax[0], v[0]+hull);
+               pccd_M->bbmax[1] = MAX2(pccd_M->bbmax[1], v[1]+hull);
+               pccd_M->bbmax[2] = MAX2(pccd_M->bbmax[2], v[2]+hull);
 
        }
        /* alloc and copy faces*/
        pccd_M->mface = MEM_dupallocN(cmd->mfaces);
 
        /* OBBs for idea1 */
 
        }
        /* alloc and copy faces*/
        pccd_M->mface = MEM_dupallocN(cmd->mfaces);
 
        /* OBBs for idea1 */
-       pccd_M->mima = MEM_mallocN(sizeof(ccdf_minmax)*pccd_M->totface,"ccd_Mesh_Faces_mima");
+       pccd_M->mima = MEM_mallocN(sizeof(ccdf_minmax)*pccd_M->totface, "ccd_Mesh_Faces_mima");
        mima  = pccd_M->mima;
        mface = pccd_M->mface;
 
 
        /* anyhoo we need to walk the list of faces and find OBB they live in */
        mima  = pccd_M->mima;
        mface = pccd_M->mface;
 
 
        /* anyhoo we need to walk the list of faces and find OBB they live in */
-       for(i=0; i < pccd_M->totface; i++){
+       for (i=0; i < pccd_M->totface; i++) {
                mima->minx=mima->miny=mima->minz=1e30f;
                mima->maxx=mima->maxy=mima->maxz=-1e30f;
 
                mima->minx=mima->miny=mima->minz=1e30f;
                mima->maxx=mima->maxy=mima->maxz=-1e30f;
 
-               copy_v3_v3(v,pccd_M->mvert[mface->v1].co);
-               mima->minx = MIN2(mima->minx,v[0]-hull);
-               mima->miny = MIN2(mima->miny,v[1]-hull);
-               mima->minz = MIN2(mima->minz,v[2]-hull);
-               mima->maxx = MAX2(mima->maxx,v[0]+hull);
-               mima->maxy = MAX2(mima->maxy,v[1]+hull);
-               mima->maxz = MAX2(mima->maxz,v[2]+hull);
-
-               copy_v3_v3(v,pccd_M->mvert[mface->v2].co);
-               mima->minx = MIN2(mima->minx,v[0]-hull);
-               mima->miny = MIN2(mima->miny,v[1]-hull);
-               mima->minz = MIN2(mima->minz,v[2]-hull);
-               mima->maxx = MAX2(mima->maxx,v[0]+hull);
-               mima->maxy = MAX2(mima->maxy,v[1]+hull);
-               mima->maxz = MAX2(mima->maxz,v[2]+hull);
-
-               copy_v3_v3(v,pccd_M->mvert[mface->v3].co);
-               mima->minx = MIN2(mima->minx,v[0]-hull);
-               mima->miny = MIN2(mima->miny,v[1]-hull);
-               mima->minz = MIN2(mima->minz,v[2]-hull);
-               mima->maxx = MAX2(mima->maxx,v[0]+hull);
-               mima->maxy = MAX2(mima->maxy,v[1]+hull);
-               mima->maxz = MAX2(mima->maxz,v[2]+hull);
-
-               if(mface->v4){
-                       copy_v3_v3(v,pccd_M->mvert[mface->v4].co);
-               mima->minx = MIN2(mima->minx,v[0]-hull);
-               mima->miny = MIN2(mima->miny,v[1]-hull);
-               mima->minz = MIN2(mima->minz,v[2]-hull);
-               mima->maxx = MAX2(mima->maxx,v[0]+hull);
-               mima->maxy = MAX2(mima->maxy,v[1]+hull);
-               mima->maxz = MAX2(mima->maxz,v[2]+hull);
+               copy_v3_v3(v, pccd_M->mvert[mface->v1].co);
+               mima->minx = MIN2(mima->minx, v[0]-hull);
+               mima->miny = MIN2(mima->miny, v[1]-hull);
+               mima->minz = MIN2(mima->minz, v[2]-hull);
+               mima->maxx = MAX2(mima->maxx, v[0]+hull);
+               mima->maxy = MAX2(mima->maxy, v[1]+hull);
+               mima->maxz = MAX2(mima->maxz, v[2]+hull);
+
+               copy_v3_v3(v, pccd_M->mvert[mface->v2].co);
+               mima->minx = MIN2(mima->minx, v[0]-hull);
+               mima->miny = MIN2(mima->miny, v[1]-hull);
+               mima->minz = MIN2(mima->minz, v[2]-hull);
+               mima->maxx = MAX2(mima->maxx, v[0]+hull);
+               mima->maxy = MAX2(mima->maxy, v[1]+hull);
+               mima->maxz = MAX2(mima->maxz, v[2]+hull);
+
+               copy_v3_v3(v, pccd_M->mvert[mface->v3].co);
+               mima->minx = MIN2(mima->minx, v[0]-hull);
+               mima->miny = MIN2(mima->miny, v[1]-hull);
+               mima->minz = MIN2(mima->minz, v[2]-hull);
+               mima->maxx = MAX2(mima->maxx, v[0]+hull);
+               mima->maxy = MAX2(mima->maxy, v[1]+hull);
+               mima->maxz = MAX2(mima->maxz, v[2]+hull);
+
+               if (mface->v4) {
+                       copy_v3_v3(v, pccd_M->mvert[mface->v4].co);
+               mima->minx = MIN2(mima->minx, v[0]-hull);
+               mima->miny = MIN2(mima->miny, v[1]-hull);
+               mima->minz = MIN2(mima->minz, v[2]-hull);
+               mima->maxx = MAX2(mima->maxx, v[0]+hull);
+               mima->maxy = MAX2(mima->maxy, v[1]+hull);
+               mima->maxz = MAX2(mima->maxz, v[2]+hull);
                }
 
 
                }
 
 
@@ -386,57 +388,60 @@ static ccd_Mesh *ccd_mesh_make(Object *ob)
        }
        return pccd_M;
 }
        }
        return pccd_M;
 }
-static void ccd_mesh_update(Object *ob,ccd_Mesh *pccd_M)
+static void ccd_mesh_update(Object *ob, ccd_Mesh *pccd_M)
 {
        CollisionModifierData *cmd;
 {
        CollisionModifierData *cmd;
-       ccdf_minmax *mima =NULL;
+       ccdf_minmax *mima = NULL;
        MFace *mface=NULL;
        MFace *mface=NULL;
-       float v[3],hull;
+       float v[3], hull;
        int i;
 
        cmd =(CollisionModifierData *)modifiers_findByType(ob, eModifierType_Collision);
 
        /* first some paranoia checks */
        int i;
 
        cmd =(CollisionModifierData *)modifiers_findByType(ob, eModifierType_Collision);
 
        /* first some paranoia checks */
-       if (!cmd) return ;
-       if (!cmd->numverts || !cmd->numfaces) return ;
+       if (!cmd) return;
+       if (!cmd->numverts || !cmd->numfaces) return;
 
        if ((pccd_M->totvert != cmd->numverts) ||
 
        if ((pccd_M->totvert != cmd->numverts) ||
-               (pccd_M->totface != cmd->numfaces)) return;
+           (pccd_M->totface != cmd->numfaces))
+       {
+               return;
+       }
 
        pccd_M->bbmin[0]=pccd_M->bbmin[1]=pccd_M->bbmin[2]=1e30f;
        pccd_M->bbmax[0]=pccd_M->bbmax[1]=pccd_M->bbmax[2]=-1e30f;
 
 
        /* blow it up with forcefield ranges */
 
        pccd_M->bbmin[0]=pccd_M->bbmin[1]=pccd_M->bbmin[2]=1e30f;
        pccd_M->bbmax[0]=pccd_M->bbmax[1]=pccd_M->bbmax[2]=-1e30f;
 
 
        /* blow it up with forcefield ranges */
-       hull = MAX2(ob->pd->pdef_sbift,ob->pd->pdef_sboft);
+       hull = MAX2(ob->pd->pdef_sbift, ob->pd->pdef_sboft);
 
        /* rotate current to previous */
 
        /* rotate current to previous */
-       if(pccd_M->mprevvert) MEM_freeN(pccd_M->mprevvert);
+       if (pccd_M->mprevvert) MEM_freeN(pccd_M->mprevvert);
        pccd_M->mprevvert = pccd_M->mvert;
        /* alloc and copy verts*/
        pccd_M->mvert = MEM_dupallocN(cmd->xnew);
        /* note that xnew coords are already in global space, */
        /* determine the ortho BB */
        pccd_M->mprevvert = pccd_M->mvert;
        /* alloc and copy verts*/
        pccd_M->mvert = MEM_dupallocN(cmd->xnew);
        /* note that xnew coords are already in global space, */
        /* determine the ortho BB */
-       for(i=0; i < pccd_M->totvert; i++){
+       for (i=0; i < pccd_M->totvert; i++) {
                /* evaluate limits */
                /* evaluate limits */
-               copy_v3_v3(v,pccd_M->mvert[i].co);
-               pccd_M->bbmin[0] = MIN2(pccd_M->bbmin[0],v[0]-hull);
-               pccd_M->bbmin[1] = MIN2(pccd_M->bbmin[1],v[1]-hull);
-               pccd_M->bbmin[2] = MIN2(pccd_M->bbmin[2],v[2]-hull);
+               copy_v3_v3(v, pccd_M->mvert[i].co);
+               pccd_M->bbmin[0] = MIN2(pccd_M->bbmin[0], v[0]-hull);
+               pccd_M->bbmin[1] = MIN2(pccd_M->bbmin[1], v[1]-hull);
+               pccd_M->bbmin[2] = MIN2(pccd_M->bbmin[2], v[2]-hull);
 
 
-               pccd_M->bbmax[0] = MAX2(pccd_M->bbmax[0],v[0]+hull);
-               pccd_M->bbmax[1] = MAX2(pccd_M->bbmax[1],v[1]+hull);
-               pccd_M->bbmax[2] = MAX2(pccd_M->bbmax[2],v[2]+hull);
+               pccd_M->bbmax[0] = MAX2(pccd_M->bbmax[0], v[0]+hull);
+               pccd_M->bbmax[1] = MAX2(pccd_M->bbmax[1], v[1]+hull);
+               pccd_M->bbmax[2] = MAX2(pccd_M->bbmax[2], v[2]+hull);
 
                /* evaluate limits */
 
                /* evaluate limits */
-               copy_v3_v3(v,pccd_M->mprevvert[i].co);
-               pccd_M->bbmin[0] = MIN2(pccd_M->bbmin[0],v[0]-hull);
-               pccd_M->bbmin[1] = MIN2(pccd_M->bbmin[1],v[1]-hull);
-               pccd_M->bbmin[2] = MIN2(pccd_M->bbmin[2],v[2]-hull);
+               copy_v3_v3(v, pccd_M->mprevvert[i].co);
+               pccd_M->bbmin[0] = MIN2(pccd_M->bbmin[0], v[0]-hull);
+               pccd_M->bbmin[1] = MIN2(pccd_M->bbmin[1], v[1]-hull);
+               pccd_M->bbmin[2] = MIN2(pccd_M->bbmin[2], v[2]-hull);
 
 
-               pccd_M->bbmax[0] = MAX2(pccd_M->bbmax[0],v[0]+hull);
-               pccd_M->bbmax[1] = MAX2(pccd_M->bbmax[1],v[1]+hull);
-               pccd_M->bbmax[2] = MAX2(pccd_M->bbmax[2],v[2]+hull);
+               pccd_M->bbmax[0] = MAX2(pccd_M->bbmax[0], v[0]+hull);
+               pccd_M->bbmax[1] = MAX2(pccd_M->bbmax[1], v[1]+hull);
+               pccd_M->bbmax[2] = MAX2(pccd_M->bbmax[2], v[2]+hull);
 
        }
 
 
        }
 
@@ -445,77 +450,77 @@ static void ccd_mesh_update(Object *ob,ccd_Mesh *pccd_M)
 
 
        /* anyhoo we need to walk the list of faces and find OBB they live in */
 
 
        /* anyhoo we need to walk the list of faces and find OBB they live in */
-       for(i=0; i < pccd_M->totface; i++){
+       for (i=0; i < pccd_M->totface; i++) {
                mima->minx=mima->miny=mima->minz=1e30f;
                mima->maxx=mima->maxy=mima->maxz=-1e30f;
 
                mima->minx=mima->miny=mima->minz=1e30f;
                mima->maxx=mima->maxy=mima->maxz=-1e30f;
 
-               copy_v3_v3(v,pccd_M->mvert[mface->v1].co);
-               mima->minx = MIN2(mima->minx,v[0]-hull);
-               mima->miny = MIN2(mima->miny,v[1]-hull);
-               mima->minz = MIN2(mima->minz,v[2]-hull);
-               mima->maxx = MAX2(mima->maxx,v[0]+hull);
-               mima->maxy = MAX2(mima->maxy,v[1]+hull);
-               mima->maxz = MAX2(mima->maxz,v[2]+hull);
-
-               copy_v3_v3(v,pccd_M->mvert[mface->v2].co);
-               mima->minx = MIN2(mima->minx,v[0]-hull);
-               mima->miny = MIN2(mima->miny,v[1]-hull);
-               mima->minz = MIN2(mima->minz,v[2]-hull);
-               mima->maxx = MAX2(mima->maxx,v[0]+hull);
-               mima->maxy = MAX2(mima->maxy,v[1]+hull);
-               mima->maxz = MAX2(mima->maxz,v[2]+hull);
-
-               copy_v3_v3(v,pccd_M->mvert[mface->v3].co);
-               mima->minx = MIN2(mima->minx,v[0]-hull);
-               mima->miny = MIN2(mima->miny,v[1]-hull);
-               mima->minz = MIN2(mima->minz,v[2]-hull);
-               mima->maxx = MAX2(mima->maxx,v[0]+hull);
-               mima->maxy = MAX2(mima->maxy,v[1]+hull);
-               mima->maxz = MAX2(mima->maxz,v[2]+hull);
-
-               if(mface->v4){
-                       copy_v3_v3(v,pccd_M->mvert[mface->v4].co);
-               mima->minx = MIN2(mima->minx,v[0]-hull);
-               mima->miny = MIN2(mima->miny,v[1]-hull);
-               mima->minz = MIN2(mima->minz,v[2]-hull);
-               mima->maxx = MAX2(mima->maxx,v[0]+hull);
-               mima->maxy = MAX2(mima->maxy,v[1]+hull);
-               mima->maxz = MAX2(mima->maxz,v[2]+hull);
+               copy_v3_v3(v, pccd_M->mvert[mface->v1].co);
+               mima->minx = MIN2(mima->minx, v[0]-hull);
+               mima->miny = MIN2(mima->miny, v[1]-hull);
+               mima->minz = MIN2(mima->minz, v[2]-hull);
+               mima->maxx = MAX2(mima->maxx, v[0]+hull);
+               mima->maxy = MAX2(mima->maxy, v[1]+hull);
+               mima->maxz = MAX2(mima->maxz, v[2]+hull);
+
+               copy_v3_v3(v, pccd_M->mvert[mface->v2].co);
+               mima->minx = MIN2(mima->minx, v[0]-hull);
+               mima->miny = MIN2(mima->miny, v[1]-hull);
+               mima->minz = MIN2(mima->minz, v[2]-hull);
+               mima->maxx = MAX2(mima->maxx, v[0]+hull);
+               mima->maxy = MAX2(mima->maxy, v[1]+hull);
+               mima->maxz = MAX2(mima->maxz, v[2]+hull);
+
+               copy_v3_v3(v, pccd_M->mvert[mface->v3].co);
+               mima->minx = MIN2(mima->minx, v[0]-hull);
+               mima->miny = MIN2(mima->miny, v[1]-hull);
+               mima->minz = MIN2(mima->minz, v[2]-hull);
+               mima->maxx = MAX2(mima->maxx, v[0]+hull);
+               mima->maxy = MAX2(mima->maxy, v[1]+hull);
+               mima->maxz = MAX2(mima->maxz, v[2]+hull);
+
+               if (mface->v4) {
+                       copy_v3_v3(v, pccd_M->mvert[mface->v4].co);
+               mima->minx = MIN2(mima->minx, v[0]-hull);
+               mima->miny = MIN2(mima->miny, v[1]-hull);
+               mima->minz = MIN2(mima->minz, v[2]-hull);
+               mima->maxx = MAX2(mima->maxx, v[0]+hull);
+               mima->maxy = MAX2(mima->maxy, v[1]+hull);
+               mima->maxz = MAX2(mima->maxz, v[2]+hull);
                }
 
 
                }
 
 
-               copy_v3_v3(v,pccd_M->mprevvert[mface->v1].co);
-               mima->minx = MIN2(mima->minx,v[0]-hull);
-               mima->miny = MIN2(mima->miny,v[1]-hull);
-               mima->minz = MIN2(mima->minz,v[2]-hull);
-               mima->maxx = MAX2(mima->maxx,v[0]+hull);
-               mima->maxy = MAX2(mima->maxy,v[1]+hull);
-               mima->maxz = MAX2(mima->maxz,v[2]+hull);
-
-               copy_v3_v3(v,pccd_M->mprevvert[mface->v2].co);
-               mima->minx = MIN2(mima->minx,v[0]-hull);
-               mima->miny = MIN2(mima->miny,v[1]-hull);
-               mima->minz = MIN2(mima->minz,v[2]-hull);
-               mima->maxx = MAX2(mima->maxx,v[0]+hull);
-               mima->maxy = MAX2(mima->maxy,v[1]+hull);
-               mima->maxz = MAX2(mima->maxz,v[2]+hull);
-
-               copy_v3_v3(v,pccd_M->mprevvert[mface->v3].co);
-               mima->minx = MIN2(mima->minx,v[0]-hull);
-               mima->miny = MIN2(mima->miny,v[1]-hull);
-               mima->minz = MIN2(mima->minz,v[2]-hull);
-               mima->maxx = MAX2(mima->maxx,v[0]+hull);
-               mima->maxy = MAX2(mima->maxy,v[1]+hull);
-               mima->maxz = MAX2(mima->maxz,v[2]+hull);
-
-               if(mface->v4){
-                       copy_v3_v3(v,pccd_M->mprevvert[mface->v4].co);
-               mima->minx = MIN2(mima->minx,v[0]-hull);
-               mima->miny = MIN2(mima->miny,v[1]-hull);
-               mima->minz = MIN2(mima->minz,v[2]-hull);
-               mima->maxx = MAX2(mima->maxx,v[0]+hull);
-               mima->maxy = MAX2(mima->maxy,v[1]+hull);
-               mima->maxz = MAX2(mima->maxz,v[2]+hull);
+               copy_v3_v3(v, pccd_M->mprevvert[mface->v1].co);
+               mima->minx = MIN2(mima->minx, v[0]-hull);
+               mima->miny = MIN2(mima->miny, v[1]-hull);
+               mima->minz = MIN2(mima->minz, v[2]-hull);
+               mima->maxx = MAX2(mima->maxx, v[0]+hull);
+               mima->maxy = MAX2(mima->maxy, v[1]+hull);
+               mima->maxz = MAX2(mima->maxz, v[2]+hull);
+
+               copy_v3_v3(v, pccd_M->mprevvert[mface->v2].co);
+               mima->minx = MIN2(mima->minx, v[0]-hull);
+               mima->miny = MIN2(mima->miny, v[1]-hull);
+               mima->minz = MIN2(mima->minz, v[2]-hull);
+               mima->maxx = MAX2(mima->maxx, v[0]+hull);
+               mima->maxy = MAX2(mima->maxy, v[1]+hull);
+               mima->maxz = MAX2(mima->maxz, v[2]+hull);
+
+               copy_v3_v3(v, pccd_M->mprevvert[mface->v3].co);
+               mima->minx = MIN2(mima->minx, v[0]-hull);
+               mima->miny = MIN2(mima->miny, v[1]-hull);
+               mima->minz = MIN2(mima->minz, v[2]-hull);
+               mima->maxx = MAX2(mima->maxx, v[0]+hull);
+               mima->maxy = MAX2(mima->maxy, v[1]+hull);
+               mima->maxz = MAX2(mima->maxz, v[2]+hull);
+
+               if (mface->v4) {
+                       copy_v3_v3(v, pccd_M->mprevvert[mface->v4].co);
+               mima->minx = MIN2(mima->minx, v[0]-hull);
+               mima->miny = MIN2(mima->miny, v[1]-hull);
+               mima->minz = MIN2(mima->minz, v[2]-hull);
+               mima->maxx = MAX2(mima->maxx, v[0]+hull);
+               mima->maxy = MAX2(mima->maxy, v[1]+hull);
+               mima->maxz = MAX2(mima->maxz, v[2]+hull);
                }
 
 
                }
 
 
@@ -523,12 +528,12 @@ static void ccd_mesh_update(Object *ob,ccd_Mesh *pccd_M)
        mface++;
 
        }
        mface++;
 
        }
-       return ;
+       return;
 }
 
 static void ccd_mesh_free(ccd_Mesh *ccdm)
 {
 }
 
 static void ccd_mesh_free(ccd_Mesh *ccdm)
 {
-       if(ccdm && (ccdm->savety == CCD_SAVETY )){ /*make sure we're not nuking objects we don't know*/
+       if (ccdm && (ccdm->savety == CCD_SAVETY )) { /*make sure we're not nuking objects we don't know*/
                MEM_freeN(ccdm->mface);
                MEM_freeN(ccdm->mvert);
                if (ccdm->mprevvert) MEM_freeN(ccdm->mprevvert);
                MEM_freeN(ccdm->mface);
                MEM_freeN(ccdm->mvert);
                if (ccdm->mprevvert) MEM_freeN(ccdm->mprevvert);
@@ -546,22 +551,22 @@ static void ccd_build_deflector_hash(Scene *scene, Object *vertexowner, GHash *h
        if (!hash) return;
        while (base) {
                /*Only proceed for mesh object in same layer */
        if (!hash) return;
        while (base) {
                /*Only proceed for mesh object in same layer */
-               if(base->object->type==OB_MESH && (base->lay & vertexowner->lay)) {
+               if (base->object->type==OB_MESH && (base->lay & vertexowner->lay)) {
                        ob= base->object;
                        ob= base->object;
-                       if((vertexowner) && (ob == vertexowner)) {
+                       if ((vertexowner) && (ob == vertexowner)) {
                                /* if vertexowner is given  we don't want to check collision with owner object */
                                base = base->next;
                                continue;
                        }
 
                        /*+++ only with deflecting set */
                                /* if vertexowner is given  we don't want to check collision with owner object */
                                base = base->next;
                                continue;
                        }
 
                        /*+++ only with deflecting set */
-                       if(ob->pd && ob->pd->deflect && BLI_ghash_lookup(hash, ob) == NULL) {
+                       if (ob->pd && ob->pd->deflect && BLI_ghash_lookup(hash, ob) == NULL) {
                                ccd_Mesh *ccdmesh = ccd_mesh_make(ob);
                                BLI_ghash_insert(hash, ob, ccdmesh);
                        }/*--- only with deflecting set */
 
                }/* mesh && layer*/
                                ccd_Mesh *ccdmesh = ccd_mesh_make(ob);
                                BLI_ghash_insert(hash, ob, ccdmesh);
                        }/*--- only with deflecting set */
 
                }/* mesh && layer*/
-          base = base->next;
+               base = base->next;
        } /* while (base) */
 }
 
        } /* while (base) */
 }
 
@@ -573,23 +578,23 @@ static void ccd_update_deflector_hash(Scene *scene, Object *vertexowner, GHash *
        if ((!hash) || (!vertexowner)) return;
        while (base) {
                /*Only proceed for mesh object in same layer */
        if ((!hash) || (!vertexowner)) return;
        while (base) {
                /*Only proceed for mesh object in same layer */
-               if(base->object->type==OB_MESH && (base->lay & vertexowner->lay)) {
+               if (base->object->type==OB_MESH && (base->lay & vertexowner->lay)) {
                        ob= base->object;
                        ob= base->object;
-                       if(ob == vertexowner){
+                       if (ob == vertexowner) {
                                /* if vertexowner is given  we don't want to check collision with owner object */
                                base = base->next;
                                continue;
                        }
 
                        /*+++ only with deflecting set */
                                /* if vertexowner is given  we don't want to check collision with owner object */
                                base = base->next;
                                continue;
                        }
 
                        /*+++ only with deflecting set */
-                       if(ob->pd && ob->pd->deflect) {
-                               ccd_Mesh *ccdmesh = BLI_ghash_lookup(hash,ob);
+                       if (ob->pd && ob->pd->deflect) {
+                               ccd_Mesh *ccdmesh = BLI_ghash_lookup(hash, ob);
                                if (ccdmesh)
                                if (ccdmesh)
-                                       ccd_mesh_update(ob,ccdmesh);
+                                       ccd_mesh_update(ob, ccdmesh);
                        }/*--- only with deflecting set */
 
                }/* mesh && layer*/
                        }/*--- only with deflecting set */
 
                }/* mesh && layer*/
-          base = base->next;
+               base = base->next;
        } /* while (base) */
 }
 
        } /* while (base) */
 }
 
@@ -601,12 +606,12 @@ static void ccd_update_deflector_hash(Scene *scene, Object *vertexowner, GHash *
 
 static int count_mesh_quads(Mesh *me)
 {
 
 static int count_mesh_quads(Mesh *me)
 {
-       int a,result = 0;
+       int a, result = 0;
        MFace *mface= me->mface;
 
        MFace *mface= me->mface;
 
-       if(mface) {
-               for(a=me->totface; a>0; a--, mface++) {
-                       if(mface->v4) result++;
+       if (mface) {
+               for (a=me->totface; a>0; a--, mface++) {
+                       if (mface->v4) result++;
                }
        }
        return result;
                }
        }
        return result;
@@ -618,19 +623,19 @@ static void add_mesh_quad_diag_springs(Object *ob)
        MFace *mface= me->mface;
        /*BodyPoint *bp;*/ /*UNUSED*/
        BodySpring *bs, *bs_new;
        MFace *mface= me->mface;
        /*BodyPoint *bp;*/ /*UNUSED*/
        BodySpring *bs, *bs_new;
-       int a ;
+       int a;
 
 
-       if (ob->soft){
+       if (ob->soft) {
                int nofquads;
                //float s_shear = ob->soft->shearstiff*ob->soft->shearstiff;
 
                nofquads = count_mesh_quads(me);
                if (nofquads) {
                        /* resize spring-array to hold additional quad springs */
                int nofquads;
                //float s_shear = ob->soft->shearstiff*ob->soft->shearstiff;
 
                nofquads = count_mesh_quads(me);
                if (nofquads) {
                        /* resize spring-array to hold additional quad springs */
-                       bs_new= MEM_callocN( (ob->soft->totspring + nofquads *2 )*sizeof(BodySpring), "bodyspring");
-                       memcpy(bs_new,ob->soft->bspring,(ob->soft->totspring )*sizeof(BodySpring));
+                       bs_new= MEM_callocN((ob->soft->totspring + nofquads *2 )*sizeof(BodySpring), "bodyspring");
+                       memcpy(bs_new, ob->soft->bspring, (ob->soft->totspring )*sizeof(BodySpring));
 
 
-                       if(ob->soft->bspring)
+                       if (ob->soft->bspring)
                                MEM_freeN(ob->soft->bspring); /* do this before reassigning the pointer  or have a 1st class memory leak */
                        ob->soft->bspring = bs_new;
 
                                MEM_freeN(ob->soft->bspring); /* do this before reassigning the pointer  or have a 1st class memory leak */
                        ob->soft->bspring = bs_new;
 
@@ -638,16 +643,16 @@ static void add_mesh_quad_diag_springs(Object *ob)
                        a = 0;
                        bs = bs_new+ob->soft->totspring;
                        /*bp= ob->soft->bpoint; */ /*UNUSED*/
                        a = 0;
                        bs = bs_new+ob->soft->totspring;
                        /*bp= ob->soft->bpoint; */ /*UNUSED*/
-                       if(mface ) {
-                               for(a=me->totface; a>0; a--, mface++) {
-                                       if(mface->v4) {
+                       if (mface ) {
+                               for (a=me->totface; a>0; a--, mface++) {
+                                       if (mface->v4) {
                                                bs->v1= mface->v1;
                                                bs->v2= mface->v3;
                                                bs->v1= mface->v1;
                                                bs->v2= mface->v3;
-                                               bs->springtype   =SB_STIFFQUAD;
+                                               bs->springtype   = SB_STIFFQUAD;
                                                bs++;
                                                bs->v1= mface->v2;
                                                bs->v2= mface->v4;
                                                bs++;
                                                bs->v1= mface->v2;
                                                bs->v2= mface->v4;
-                                               bs->springtype   =SB_STIFFQUAD;
+                                               bs->springtype   = SB_STIFFQUAD;
                                                bs++;
 
                                        }
                                                bs++;
 
                                        }
@@ -660,54 +665,54 @@ static void add_mesh_quad_diag_springs(Object *ob)
        }
 }
 
        }
 }
 
-static void add_2nd_order_roller(Object *ob,float UNUSED(stiffness), int *counter, int addsprings)
+static void add_2nd_order_roller(Object *ob, float UNUSED(stiffness), int *counter, int addsprings)
 {
        /*assume we have a softbody*/
        SoftBody *sb= ob->soft; /* is supposed to be there */
 {
        /*assume we have a softbody*/
        SoftBody *sb= ob->soft; /* is supposed to be there */
-       BodyPoint *bp,*bpo;
-       BodySpring *bs,*bs2,*bs3= NULL;
-       int a,b,c,notthis= 0,v0;
-       if (!sb->bspring){return;} /* we are 2nd order here so 1rst should have been build :) */
+       BodyPoint *bp, *bpo;
+       BodySpring *bs, *bs2, *bs3= NULL;
+       int a, b, c, notthis= 0, v0;
+       if (!sb->bspring) {return;} /* we are 2nd order here so 1rst should have been build :) */
        /* first run counting  second run adding */
        *counter = 0;
        if (addsprings) bs3 = ob->soft->bspring+ob->soft->totspring;
        /* first run counting  second run adding */
        *counter = 0;
        if (addsprings) bs3 = ob->soft->bspring+ob->soft->totspring;
-       for(a=sb->totpoint, bp= sb->bpoint; a>0; a--, bp++) {
+       for (a=sb->totpoint, bp= sb->bpoint; a>0; a--, bp++) {
                /*scan for neighborhood*/
                bpo = NULL;
                v0  = (sb->totpoint-a);
                /*scan for neighborhood*/
                bpo = NULL;
                v0  = (sb->totpoint-a);
-               for(b=bp->nofsprings;b>0;b--){
+               for (b=bp->nofsprings;b>0;b--) {
                        bs = sb->bspring + bp->springs[b-1];
                        /*nasty thing here that springs have two ends
                        so here we have to make sure we examine the other */
                        bs = sb->bspring + bp->springs[b-1];
                        /*nasty thing here that springs have two ends
                        so here we have to make sure we examine the other */
-                       if (v0 == bs->v1){
-                               bpo =sb->bpoint+bs->v2;
+                       if (v0 == bs->v1) {
+                               bpo = sb->bpoint+bs->v2;
                                notthis = bs->v2;
                        }
                        else {
                                notthis = bs->v2;
                        }
                        else {
-                       if (v0 == bs->v2){
-                               bpo =sb->bpoint+bs->v1;
+                       if (v0 == bs->v2) {
+                               bpo = sb->bpoint+bs->v1;
                                notthis = bs->v1;
                        }
                        else {printf("oops we should not get here -  add_2nd_order_springs");}
                        }
                                notthis = bs->v1;
                        }
                        else {printf("oops we should not get here -  add_2nd_order_springs");}
                        }
-                       if (bpo){/* so now we have a 2nd order humpdidump */
-                               for(c=bpo->nofsprings;c>0;c--){
+                       if (bpo) {/* so now we have a 2nd order humpdidump */
+                               for (c=bpo->nofsprings;c>0;c--) {
                                        bs2 = sb->bspring + bpo->springs[c-1];
                                        bs2 = sb->bspring + bpo->springs[c-1];
-                                       if ((bs2->v1 != notthis)  && (bs2->v1 > v0)){
+                                       if ((bs2->v1 != notthis) && (bs2->v1 > v0)) {
                                                (*counter)++;/*hit */
                                                (*counter)++;/*hit */
-                                               if (addsprings){
+                                               if (addsprings) {
                                                        bs3->v1= v0;
                                                        bs3->v2= bs2->v1;
                                                        bs3->v1= v0;
                                                        bs3->v2= bs2->v1;
-                                                       bs3->springtype   =SB_BEND;
+                                                       bs3->springtype   = SB_BEND;
                                                        bs3++;
                                                }
                                        }
                                                        bs3++;
                                                }
                                        }
-                                       if ((bs2->v2 !=notthis)&&(bs2->v2 > v0)){
+                                       if ((bs2->v2 !=notthis)&&(bs2->v2 > v0)) {
                                        (*counter)++;/*hit */
                                        (*counter)++;/*hit */
-                                               if (addsprings){
+                                               if (addsprings) {
                                                        bs3->v1= v0;
                                                        bs3->v2= bs2->v2;
                                                        bs3->v1= v0;
                                                        bs3->v2= bs2->v2;
-                                                       bs3->springtype   =SB_BEND;
+                                                       bs3->springtype   = SB_BEND;
                                                        bs3++;
                                                }
 
                                                        bs3++;
                                                }
 
@@ -722,40 +727,40 @@ static void add_2nd_order_roller(Object *ob,float UNUSED(stiffness), int *counte
 }
 
 
 }
 
 
-static void add_2nd_order_springs(Object *ob,float stiffness)
+static void add_2nd_order_springs(Object *ob, float stiffness)
 {
        int counter = 0;
        BodySpring *bs_new;
        stiffness *=stiffness;
 
 {
        int counter = 0;
        BodySpring *bs_new;
        stiffness *=stiffness;
 
-       add_2nd_order_roller(ob,stiffness,&counter,0); /* counting */
+       add_2nd_order_roller(ob, stiffness, &counter, 0); /* counting */
        if (counter) {
                /* resize spring-array to hold additional springs */
        if (counter) {
                /* resize spring-array to hold additional springs */
-               bs_new= MEM_callocN( (ob->soft->totspring + counter )*sizeof(BodySpring), "bodyspring");
-               memcpy(bs_new,ob->soft->bspring,(ob->soft->totspring )*sizeof(BodySpring));
+               bs_new= MEM_callocN((ob->soft->totspring + counter )*sizeof(BodySpring), "bodyspring");
+               memcpy(bs_new, ob->soft->bspring, (ob->soft->totspring )*sizeof(BodySpring));
 
 
-               if(ob->soft->bspring)
+               if (ob->soft->bspring)
                        MEM_freeN(ob->soft->bspring);
                ob->soft->bspring = bs_new;
 
                        MEM_freeN(ob->soft->bspring);
                ob->soft->bspring = bs_new;
 
-               add_2nd_order_roller(ob,stiffness,&counter,1); /* adding */
-               ob->soft->totspring +=counter ;
+               add_2nd_order_roller(ob, stiffness, &counter, 1); /* adding */
+               ob->soft->totspring += counter;
        }
 }
 
        }
 }
 
-static void add_bp_springlist(BodyPoint *bp,int springID)
+static void add_bp_springlist(BodyPoint *bp, int springID)
 {
        int *newlist;
 
        if (bp->springs == NULL) {
 {
        int *newlist;
 
        if (bp->springs == NULL) {
-               bp->springs = MEM_callocN( sizeof(int), "bpsprings");
+               bp->springs = MEM_callocN(sizeof(int), "bpsprings");
                bp->springs[0] = springID;
                bp->nofsprings = 1;
        }
        else {
                bp->nofsprings++;
                newlist = MEM_callocN(bp->nofsprings * sizeof(int), "bpsprings");
                bp->springs[0] = springID;
                bp->nofsprings = 1;
        }
        else {
                bp->nofsprings++;
                newlist = MEM_callocN(bp->nofsprings * sizeof(int), "bpsprings");
-               memcpy(newlist,bp->springs,(bp->nofsprings-1)* sizeof(int));
+               memcpy(newlist, bp->springs, (bp->nofsprings-1)* sizeof(int));
                MEM_freeN(bp->springs);
                bp->springs = newlist;
                bp->springs[bp->nofsprings-1] = springID;
                MEM_freeN(bp->springs);
                bp->springs = newlist;
                bp->springs[bp->nofsprings-1] = springID;
@@ -770,23 +775,23 @@ static void build_bps_springlist(Object *ob)
        SoftBody *sb= ob->soft; /* is supposed to be there */
        BodyPoint *bp;
        BodySpring *bs;
        SoftBody *sb= ob->soft; /* is supposed to be there */
        BodyPoint *bp;
        BodySpring *bs;
-       int a,b;
+       int a, b;
 
        if (sb==NULL) return; /* paranoya check */
 
 
        if (sb==NULL) return; /* paranoya check */
 
-       for(a=sb->totpoint, bp= sb->bpoint; a>0; a--, bp++) {
+       for (a=sb->totpoint, bp= sb->bpoint; a>0; a--, bp++) {
                /* throw away old list */
                if (bp->springs) {
                        MEM_freeN(bp->springs);
                        bp->springs=NULL;
                }
                /* scan for attached inner springs */
                /* throw away old list */
                if (bp->springs) {
                        MEM_freeN(bp->springs);
                        bp->springs=NULL;
                }
                /* scan for attached inner springs */
-               for(b=sb->totspring, bs= sb->bspring; b>0; b--, bs++) {
-                       if (( (sb->totpoint-a) == bs->v1) ){
-                               add_bp_springlist(bp,sb->totspring -b);
+               for (b=sb->totspring, bs= sb->bspring; b>0; b--, bs++) {
+                       if (( (sb->totpoint-a) == bs->v1) ) {
+                               add_bp_springlist(bp, sb->totspring -b);
                        }
                        }
-                       if (( (sb->totpoint-a) == bs->v2) ){
-                               add_bp_springlist(bp,sb->totspring -b);
+                       if (( (sb->totpoint-a) == bs->v2) ) {
+                               add_bp_springlist(bp, sb->totspring -b);
                        }
                }/*for springs*/
        }/*for bp*/
                        }
                }/*for springs*/
        }/*for bp*/
@@ -797,42 +802,42 @@ static void calculate_collision_balls(Object *ob)
        SoftBody *sb= ob->soft; /* is supposed to be there */
        BodyPoint *bp;
        BodySpring *bs;
        SoftBody *sb= ob->soft; /* is supposed to be there */
        BodyPoint *bp;
        BodySpring *bs;
-       int a,b,akku_count;
-       float min,max,akku;
+       int a, b, akku_count;
+       float min, max, akku;
 
        if (sb==NULL) return; /* paranoya check */
 
 
        if (sb==NULL) return; /* paranoya check */
 
-       for(a=sb->totpoint, bp= sb->bpoint; a>0; a--, bp++) {
+       for (a=sb->totpoint, bp= sb->bpoint; a>0; a--, bp++) {
                bp->colball=0;
                akku =0.0f;
                akku_count=0;
                min = 1e22f;
                max = -1e22f;
                /* first estimation based on attached */
                bp->colball=0;
                akku =0.0f;
                akku_count=0;
                min = 1e22f;
                max = -1e22f;
                /* first estimation based on attached */
-               for(b=bp->nofsprings;b>0;b--){
+               for (b=bp->nofsprings;b>0;b--) {
                        bs = sb->bspring + bp->springs[b-1];
                        bs = sb->bspring + bp->springs[b-1];
-                       if (bs->springtype == SB_EDGE){
+                       if (bs->springtype == SB_EDGE) {
                        akku += bs->len;
                        akku_count++,
                        akku += bs->len;
                        akku_count++,
-                       min = MIN2(bs->len,min);
-                       max = MAX2(bs->len,max);
+                       min = MIN2(bs->len, min);
+                       max = MAX2(bs->len, max);
                        }
                }
 
                if (akku_count > 0) {
                        }
                }
 
                if (akku_count > 0) {
-                       if (sb->sbc_mode == SBC_MODE_MANUAL){
+                       if (sb->sbc_mode == SBC_MODE_MANUAL) {
                                bp->colball=sb->colball;
                        }
                                bp->colball=sb->colball;
                        }
-                       if (sb->sbc_mode == SBC_MODE_AVG){
+                       if (sb->sbc_mode == SBC_MODE_AVG) {
                                bp->colball = akku/(float)akku_count*sb->colball;
                        }
                                bp->colball = akku/(float)akku_count*sb->colball;
                        }
-                       if (sb->sbc_mode == SBC_MODE_MIN){
+                       if (sb->sbc_mode == SBC_MODE_MIN) {
                                bp->colball=min*sb->colball;
                        }
                                bp->colball=min*sb->colball;
                        }
-                       if (sb->sbc_mode == SBC_MODE_MAX){
+                       if (sb->sbc_mode == SBC_MODE_MAX) {
                                bp->colball=max*sb->colball;
                        }
                                bp->colball=max*sb->colball;
                        }
-                       if (sb->sbc_mode == SBC_MODE_AVGMINMAX){
+                       if (sb->sbc_mode == SBC_MODE_AVGMINMAX) {
                                bp->colball = (min + max)/2.0f*sb->colball;
                        }
                }
                                bp->colball = (min + max)/2.0f*sb->colball;
                        }
                }
@@ -847,20 +852,20 @@ static void renew_softbody(Scene *scene, Object *ob, int totpoint, int totspring
        SoftBody *sb;
        int i;
        short softflag;
        SoftBody *sb;
        int i;
        short softflag;
-       if(ob->soft==NULL) ob->soft= sbNew(scene);
+       if (ob->soft==NULL) ob->soft= sbNew(scene);
        else free_softbody_intern(ob->soft);
        sb= ob->soft;
        softflag=ob->softflag;
 
        else free_softbody_intern(ob->soft);
        sb= ob->soft;
        softflag=ob->softflag;
 
-       if(totpoint) {
+       if (totpoint) {
                sb->totpoint= totpoint;
                sb->totspring= totspring;
 
                sb->totpoint= totpoint;
                sb->totspring= totspring;
 
-               sb->bpoint= MEM_mallocN( totpoint*sizeof(BodyPoint), "bodypoint");
-               if(totspring)
-                       sb->bspring= MEM_mallocN( totspring*sizeof(BodySpring), "bodyspring");
+               sb->bpoint= MEM_mallocN(totpoint*sizeof(BodyPoint), "bodypoint");
+               if (totspring)
+                       sb->bspring= MEM_mallocN(totspring*sizeof(BodySpring), "bodyspring");
 
 
-                       /* initialise BodyPoint array */
+                       /* initialize BodyPoint array */
                for (i=0; i<totpoint; i++) {
                        BodyPoint *bp = &sb->bpoint[i];
 
                for (i=0; i<totpoint; i++) {
                        BodyPoint *bp = &sb->bpoint[i];
 
@@ -868,7 +873,7 @@ static void renew_softbody(Scene *scene, Object *ob, int totpoint, int totspring
                        /* hum as far as i see this is overridden by _final_goal() now jow_go_for2_5 */
                        /* sadly breaks compatibility with older versions */
                        /* but makes goals behave the same for meshes, lattices and curves */
                        /* hum as far as i see this is overridden by _final_goal() now jow_go_for2_5 */
                        /* sadly breaks compatibility with older versions */
                        /* but makes goals behave the same for meshes, lattices and curves */
-                       if(softflag & OB_SB_GOAL) {
+                       if (softflag & OB_SB_GOAL) {
                                bp->goal= sb->defgoal;
                        }
                        else {
                                bp->goal= sb->defgoal;
                        }
                        else {
@@ -894,28 +899,28 @@ static void free_softbody_baked(SoftBody *sb)
        SBVertex *key;
        int k;
 
        SBVertex *key;
        int k;
 
-       for(k=0; k<sb->totkey; k++) {
+       for (k=0; k<sb->totkey; k++) {
                key= *(sb->keys + k);
                key= *(sb->keys + k);
-               if(key) MEM_freeN(key);
+               if (key) MEM_freeN(key);
        }
        }
-       if(sb->keys) MEM_freeN(sb->keys);
+       if (sb->keys) MEM_freeN(sb->keys);
 
        sb->keys= NULL;
        sb->totkey= 0;
 }
 static void free_scratch(SoftBody *sb)
 {
 
        sb->keys= NULL;
        sb->totkey= 0;
 }
 static void free_scratch(SoftBody *sb)
 {
-       if(sb->scratch){
+       if (sb->scratch) {
                /* todo make sure everything is cleaned up nicly */
                /* todo make sure everything is cleaned up nicly */
-               if (sb->scratch->colliderhash){
+               if (sb->scratch->colliderhash) {
                        BLI_ghash_free(sb->scratch->colliderhash, NULL,
                                        (GHashValFreeFP) ccd_mesh_free); /*this hoepfully will free all caches*/
                        sb->scratch->colliderhash = NULL;
                }
                        BLI_ghash_free(sb->scratch->colliderhash, NULL,
                                        (GHashValFreeFP) ccd_mesh_free); /*this hoepfully will free all caches*/
                        sb->scratch->colliderhash = NULL;
                }
-               if (sb->scratch->bodyface){
+               if (sb->scratch->bodyface) {
                        MEM_freeN(sb->scratch->bodyface);
                }
                        MEM_freeN(sb->scratch->bodyface);
                }
-               if (sb->scratch->Ref.ivert){
+               if (sb->scratch->Ref.ivert) {
                        MEM_freeN(sb->scratch->Ref.ivert);
                }
                MEM_freeN(sb->scratch);
                        MEM_freeN(sb->scratch->Ref.ivert);
                }
                MEM_freeN(sb->scratch);
@@ -927,12 +932,12 @@ static void free_scratch(SoftBody *sb)
 /* only frees internal data */
 static void free_softbody_intern(SoftBody *sb)
 {
 /* only frees internal data */
 static void free_softbody_intern(SoftBody *sb)
 {
-       if(sb) {
+       if (sb) {
                int a;
                BodyPoint *bp;
 
                int a;
                BodyPoint *bp;
 
-               if(sb->bpoint){
-                       for(a=sb->totpoint, bp= sb->bpoint; a>0; a--, bp++) {
+               if (sb->bpoint) {
+                       for (a=sb->totpoint, bp= sb->bpoint; a>0; a--, bp++) {
                                /* free spring list */
                                if (bp->springs != NULL) {
                                        MEM_freeN(bp->springs);
                                /* free spring list */
                                if (bp->springs != NULL) {
                                        MEM_freeN(bp->springs);
@@ -941,7 +946,7 @@ static void free_softbody_intern(SoftBody *sb)
                        MEM_freeN(sb->bpoint);
                }
 
                        MEM_freeN(sb->bpoint);
                }
 
-               if(sb->bspring) MEM_freeN(sb->bspring);
+               if (sb->bspring) MEM_freeN(sb->bspring);
 
                sb->totpoint= sb->totspring= 0;
                sb->bpoint= NULL;
 
                sb->totpoint= sb->totspring= 0;
                sb->bpoint= NULL;
@@ -974,7 +979,7 @@ static void free_softbody_intern(SoftBody *sb)
 ** Q: why not use 'simple' collision here like bouncing back a particle
 **   --> reverting is velocity on the face normal
 ** A: because our particles are not alone here
 ** Q: why not use 'simple' collision here like bouncing back a particle
 **   --> reverting is velocity on the face normal
 ** A: because our particles are not alone here
-**    and need to tell their neighbours exactly what happens via spring forces
+**    and need to tell their neighbors exactly what happens via spring forces
 ** unless sbObjectStep( .. ) is called on sub frame timing level
 ** BTW that also questions the use of a 'implicit' solvers on softbodies
 ** since that would only valid for 'slow' moving collision targets and dito particles
 ** unless sbObjectStep( .. ) is called on sub frame timing level
 ** BTW that also questions the use of a 'implicit' solvers on softbodies
 ** since that would only valid for 'slow' moving collision targets and dito particles
@@ -988,15 +993,15 @@ static void Vec3PlusStVec(float *v, float s, float *v1)
        v[2] += s*v1[2];
 }
 
        v[2] += s*v1[2];
 }
 
-/* +++ dependancy information functions*/
+/* +++ dependency information functions*/
 
 static int are_there_deflectors(Scene *scene, unsigned int layer)
 {
        Base *base;
 
 
 static int are_there_deflectors(Scene *scene, unsigned int layer)
 {
        Base *base;
 
-       for(base = scene->base.first; base; base= base->next) {
-               if( (base->lay & layer) && base->object->pd) {
-                       if(base->object->pd->deflect)
+       for (base = scene->base.first; base; base= base->next) {
+               if ( (base->lay & layer) && base->object->pd) {
+                       if (base->object->pd->deflect)
                                return 1;
                }
        }
                                return 1;
                }
        }
@@ -1007,41 +1012,41 @@ static int query_external_colliders(Scene *scene, Object *me)
 {
        return(are_there_deflectors(scene, me->lay));
 }
 {
        return(are_there_deflectors(scene, me->lay));
 }
-/* --- dependancy information functions*/
+/* --- dependency information functions*/
 
 
 /* +++ the aabb "force" section*/
 
 
 /* +++ the aabb "force" section*/
-static int sb_detect_aabb_collisionCached(     float UNUSED(force[3]), unsigned int UNUSED(par_layer),struct Object *vertexowner,float UNUSED(time))
+static int sb_detect_aabb_collisionCached(float UNUSED(force[3]), unsigned int UNUSED(par_layer), struct Object *vertexowner, float UNUSED(time))
 {
        Object *ob;
        SoftBody *sb=vertexowner->soft;
        GHash *hash;
        GHashIterator *ihash;
 {
        Object *ob;
        SoftBody *sb=vertexowner->soft;
        GHash *hash;
        GHashIterator *ihash;
-       float  aabbmin[3],aabbmax[3];
+       float  aabbmin[3], aabbmax[3];
        int deflected=0;
 #if 0
        int a;
 #endif
 
        if ((sb == NULL) || (sb->scratch ==NULL)) return 0;
        int deflected=0;
 #if 0
        int a;
 #endif
 
        if ((sb == NULL) || (sb->scratch ==NULL)) return 0;
-       copy_v3_v3(aabbmin,sb->scratch->aabbmin);
-       copy_v3_v3(aabbmax,sb->scratch->aabbmax);
+       copy_v3_v3(aabbmin, sb->scratch->aabbmin);
+       copy_v3_v3(aabbmax, sb->scratch->aabbmax);
 
        hash  = vertexowner->soft->scratch->colliderhash;
        ihash = BLI_ghashIterator_new(hash);
 
        hash  = vertexowner->soft->scratch->colliderhash;
        ihash = BLI_ghashIterator_new(hash);
-       while (!BLI_ghashIterator_isDone(ihash) ) {
+       while (BLI_ghashIterator_notDone(ihash) ) {
 
                ccd_Mesh *ccdm = BLI_ghashIterator_getValue     (ihash);
                ob             = BLI_ghashIterator_getKey       (ihash);
                        /* only with deflecting set */
 
                ccd_Mesh *ccdm = BLI_ghashIterator_getValue     (ihash);
                ob             = BLI_ghashIterator_getKey       (ihash);
                        /* only with deflecting set */
-                       if(ob->pd && ob->pd->deflect) {
+                       if (ob->pd && ob->pd->deflect) {
 #if 0                  /* UNUSED */
                                MFace *mface= NULL;
                                MVert *mvert= NULL;
                                MVert *mprevvert= NULL;
                                ccdf_minmax *mima= NULL;
 #endif
 #if 0                  /* UNUSED */
                                MFace *mface= NULL;
                                MVert *mvert= NULL;
                                MVert *mprevvert= NULL;
                                ccdf_minmax *mima= NULL;
 #endif
-                               if(ccdm){
+                               if (ccdm) {
 #if 0                          /* UNUSED */
                                        mface= ccdm->mface;
                                        mvert= ccdm->mvert;
 #if 0                          /* UNUSED */
                                        mface= ccdm->mface;
                                        mvert= ccdm->mvert;
@@ -1050,12 +1055,13 @@ static int sb_detect_aabb_collisionCached(      float UNUSED(force[3]), unsigned int
                                        a = ccdm->totface;
 #endif
                                        if ((aabbmax[0] < ccdm->bbmin[0]) ||
                                        a = ccdm->totface;
 #endif
                                        if ((aabbmax[0] < ccdm->bbmin[0]) ||
-                                               (aabbmax[1] < ccdm->bbmin[1]) ||
-                                               (aabbmax[2] < ccdm->bbmin[2]) ||
-                                               (aabbmin[0] > ccdm->bbmax[0]) ||
-                                               (aabbmin[1] > ccdm->bbmax[1]) ||
-                                               (aabbmin[2] > ccdm->bbmax[2]) ) {
-                                               /* boxes dont intersect */
+                                           (aabbmax[1] < ccdm->bbmin[1]) ||
+                                           (aabbmax[2] < ccdm->bbmin[2]) ||
+                                           (aabbmin[0] > ccdm->bbmax[0]) ||
+                                           (aabbmin[1] > ccdm->bbmax[1]) ||
+                                           (aabbmin[2] > ccdm->bbmax[2]) )
+                                       {
+                                               /* boxes don't intersect */
                                                BLI_ghashIterator_step(ihash);
                                                continue;
                                        }
                                                BLI_ghashIterator_step(ihash);
                                                continue;
                                        }
@@ -1065,13 +1071,13 @@ static int sb_detect_aabb_collisionCached(      float UNUSED(force[3]), unsigned int
                                        deflected = 2;
 
                                }
                                        deflected = 2;
 
                                }
-                               else{
+                               else {
                                        /*aye that should be cached*/
                                        /*aye that should be cached*/
-                                       printf("missing cache error \n");
+                                       printf("missing cache error\n");
                                        BLI_ghashIterator_step(ihash);
                                        continue;
                                }
                                        BLI_ghashIterator_step(ihash);
                                        continue;
                                }
-                       } /* if(ob->pd && ob->pd->deflect) */
+                       } /* if (ob->pd && ob->pd->deflect) */
                        BLI_ghashIterator_step(ihash);
        } /* while () */
        BLI_ghashIterator_free(ihash);
                        BLI_ghashIterator_step(ihash);
        } /* while () */
        BLI_ghashIterator_free(ihash);
@@ -1081,22 +1087,22 @@ static int sb_detect_aabb_collisionCached(      float UNUSED(force[3]), unsigned int
 
 
 /* +++ the face external section*/
 
 
 /* +++ the face external section*/
-static int sb_detect_face_pointCached(float face_v1[3],float face_v2[3],float face_v3[3],float *damp,
-                                                                  float force[3], unsigned int UNUSED(par_layer),struct Object *vertexowner,float time)
-                                                                  {
+static int sb_detect_face_pointCached(float face_v1[3], float face_v2[3], float face_v3[3], float *damp,
+                                      float force[3], unsigned int UNUSED(par_layer), struct Object *vertexowner, float time)
+{
        Object *ob;
        GHash *hash;
        GHashIterator *ihash;
        Object *ob;
        GHash *hash;
        GHashIterator *ihash;
-       float nv1[3], edge1[3], edge2[3], d_nvect[3], aabbmin[3],aabbmax[3];
-       float facedist,outerfacethickness,tune = 10.f;
+       float nv1[3], edge1[3], edge2[3], d_nvect[3], aabbmin[3], aabbmax[3];
+       float facedist, outerfacethickness, tune = 10.f;
        int a, deflected=0;
 
        int a, deflected=0;
 
-       aabbmin[0] = MIN3(face_v1[0],face_v2[0],face_v3[0]);
-       aabbmin[1] = MIN3(face_v1[1],face_v2[1],face_v3[1]);
-       aabbmin[2] = MIN3(face_v1[2],face_v2[2],face_v3[2]);
-       aabbmax[0] = MAX3(face_v1[0],face_v2[0],face_v3[0]);
-       aabbmax[1] = MAX3(face_v1[1],face_v2[1],face_v3[1]);
-       aabbmax[2] = MAX3(face_v1[2],face_v2[2],face_v3[2]);
+       aabbmin[0] = min_fff(face_v1[0], face_v2[0], face_v3[0]);
+       aabbmin[1] = min_fff(face_v1[1], face_v2[1], face_v3[1]);
+       aabbmin[2] = min_fff(face_v1[2], face_v2[2], face_v3[2]);
+       aabbmax[0] = max_fff(face_v1[0], face_v2[0], face_v3[0]);
+       aabbmax[1] = max_fff(face_v1[1], face_v2[1], face_v3[1]);
+       aabbmax[2] = max_fff(face_v1[2], face_v2[2], face_v3[2]);
 
        /* calculate face normal once again SIGH */
        sub_v3_v3v3(edge1, face_v1, face_v2);
 
        /* calculate face normal once again SIGH */
        sub_v3_v3v3(edge1, face_v1, face_v2);
@@ -1107,34 +1113,35 @@ static int sb_detect_face_pointCached(float face_v1[3],float face_v2[3],float fa
 
        hash  = vertexowner->soft->scratch->colliderhash;
        ihash = BLI_ghashIterator_new(hash);
 
        hash  = vertexowner->soft->scratch->colliderhash;
        ihash = BLI_ghashIterator_new(hash);
-       while (!BLI_ghashIterator_isDone(ihash) ) {
+       while (BLI_ghashIterator_notDone(ihash) ) {
 
                ccd_Mesh *ccdm = BLI_ghashIterator_getValue     (ihash);
                ob             = BLI_ghashIterator_getKey       (ihash);
                        /* only with deflecting set */
 
                ccd_Mesh *ccdm = BLI_ghashIterator_getValue     (ihash);
                ob             = BLI_ghashIterator_getKey       (ihash);
                        /* only with deflecting set */
-                       if(ob->pd && ob->pd->deflect) {
+                       if (ob->pd && ob->pd->deflect) {
                                MVert *mvert= NULL;
                                MVert *mprevvert= NULL;
                                MVert *mvert= NULL;
                                MVert *mprevvert= NULL;
-                               if(ccdm){
+                               if (ccdm) {
                                        mvert= ccdm->mvert;
                                        a    = ccdm->totvert;
                                        mprevvert= ccdm->mprevvert;
                                        mvert= ccdm->mvert;
                                        a    = ccdm->totvert;
                                        mprevvert= ccdm->mprevvert;
-                                       outerfacethickness =ob->pd->pdef_sboft;
+                                       outerfacethickness = ob->pd->pdef_sboft;
                                        if ((aabbmax[0] < ccdm->bbmin[0]) ||
                                        if ((aabbmax[0] < ccdm->bbmin[0]) ||
-                                               (aabbmax[1] < ccdm->bbmin[1]) ||
-                                               (aabbmax[2] < ccdm->bbmin[2]) ||
-                                               (aabbmin[0] > ccdm->bbmax[0]) ||
-                                               (aabbmin[1] > ccdm->bbmax[1]) ||
-                                               (aabbmin[2] > ccdm->bbmax[2]) ) {
-                                               /* boxes dont intersect */
+                                           (aabbmax[1] < ccdm->bbmin[1]) ||
+                                           (aabbmax[2] < ccdm->bbmin[2]) ||
+                                           (aabbmin[0] > ccdm->bbmax[0]) ||
+                                           (aabbmin[1] > ccdm->bbmax[1]) ||
+                                           (aabbmin[2] > ccdm->bbmax[2]) )
+                                       {
+                                               /* boxes don't intersect */
                                                BLI_ghashIterator_step(ihash);
                                                continue;
                                        }
 
                                }
                                                BLI_ghashIterator_step(ihash);
                                                continue;
                                        }
 
                                }
-                               else{
+                               else {
                                        /*aye that should be cached*/
                                        /*aye that should be cached*/
-                                       printf("missing cache error \n");
+                                       printf("missing cache error\n");
                                        BLI_ghashIterator_step(ihash);
                                        continue;
                                }
                                        BLI_ghashIterator_step(ihash);
                                        continue;
                                }
@@ -1142,19 +1149,19 @@ static int sb_detect_face_pointCached(float face_v1[3],float face_v2[3],float fa
 
                                /* use mesh*/
                                if (mvert) {
 
                                /* use mesh*/
                                if (mvert) {
-                                       while(a){
-                                               copy_v3_v3(nv1,mvert[a-1].co);
-                                               if(mprevvert){
-                                                       mul_v3_fl(nv1,time);
-                                                       Vec3PlusStVec(nv1,(1.0f-time),mprevvert[a-1].co);
+                                       while (a) {
+                                               copy_v3_v3(nv1, mvert[a-1].co);
+                                               if (mprevvert) {
+                                                       mul_v3_fl(nv1, time);
+                                                       Vec3PlusStVec(nv1, (1.0f-time), mprevvert[a-1].co);
                                                }
                                                /* origin to face_v2*/
                                                sub_v3_v3(nv1, face_v2);
                                                }
                                                /* origin to face_v2*/
                                                sub_v3_v3(nv1, face_v2);
-                                               facedist = dot_v3v3(nv1,d_nvect);
-                                               if (ABS(facedist)<outerfacethickness){
-                                                       if (isect_point_tri_prism_v3(nv1, face_v1,face_v2,face_v3) ){
+                                               facedist = dot_v3v3(nv1, d_nvect);
+                                               if (ABS(facedist)<outerfacethickness) {
+                                                       if (isect_point_tri_prism_v3(nv1, face_v1, face_v2, face_v3) ) {
                                                                float df;
                                                                float df;
-                                                               if (facedist > 0){
+                                                               if (facedist > 0) {
                                                                        df = (outerfacethickness-facedist)/outerfacethickness;
                                                                }
                                                                else {
                                                                        df = (outerfacethickness-facedist)/outerfacethickness;
                                                                }
                                                                else {
@@ -1163,15 +1170,15 @@ static int sb_detect_face_pointCached(float face_v1[3],float face_v2[3],float fa
 
                                                                *damp=df*tune*ob->pd->pdef_sbdamp;
 
 
                                                                *damp=df*tune*ob->pd->pdef_sbdamp;
 
-                                                               df = 0.01f*exp(- 100.0f*df);
-                                                               Vec3PlusStVec(force,-df,d_nvect);
+                                                               df = 0.01f * expf(-100.0f * df);
+                                                               Vec3PlusStVec(force, -df, d_nvect);
                                                                deflected = 3;
                                                        }
                                                }
                                                a--;
                                                                deflected = 3;
                                                        }
                                                }
                                                a--;
-                                       }/* while(a)*/
+                                       }/* while (a)*/
                                } /* if (mvert) */
                                } /* if (mvert) */
-                       } /* if(ob->pd && ob->pd->deflect) */
+                       } /* if (ob->pd && ob->pd->deflect) */
                        BLI_ghashIterator_step(ihash);
        } /* while () */
        BLI_ghashIterator_free(ihash);
                        BLI_ghashIterator_step(ihash);
        } /* while () */
        BLI_ghashIterator_free(ihash);
@@ -1179,36 +1186,36 @@ static int sb_detect_face_pointCached(float face_v1[3],float face_v2[3],float fa
 }
 
 
 }
 
 
-static int sb_detect_face_collisionCached(float face_v1[3],float face_v2[3],float face_v3[3],float *damp,
-                                                                  float force[3], unsigned int UNUSED(par_layer),struct Object *vertexowner,float time)
+static int sb_detect_face_collisionCached(float face_v1[3], float face_v2[3], float face_v3[3], float *damp,
+                                          float force[3], unsigned int UNUSED(par_layer), struct Object *vertexowner, float time)
 {
        Object *ob;
        GHash *hash;
        GHashIterator *ihash;
 {
        Object *ob;
        GHash *hash;
        GHashIterator *ihash;
-       float nv1[3], nv2[3], nv3[3], nv4[3], edge1[3], edge2[3], d_nvect[3], aabbmin[3],aabbmax[3];
-       float t,tune = 10.0f;
+       float nv1[3], nv2[3], nv3[3], nv4[3], edge1[3], edge2[3], d_nvect[3], aabbmin[3], aabbmax[3];
+       float t, tune = 10.0f;
        int a, deflected=0;
 
        int a, deflected=0;
 
-       aabbmin[0] = MIN3(face_v1[0],face_v2[0],face_v3[0]);
-       aabbmin[1] = MIN3(face_v1[1],face_v2[1],face_v3[1]);
-       aabbmin[2] = MIN3(face_v1[2],face_v2[2],face_v3[2]);
-       aabbmax[0] = MAX3(face_v1[0],face_v2[0],face_v3[0]);
-       aabbmax[1] = MAX3(face_v1[1],face_v2[1],face_v3[1]);
-       aabbmax[2] = MAX3(face_v1[2],face_v2[2],face_v3[2]);
+       aabbmin[0] = min_fff(face_v1[0], face_v2[0], face_v3[0]);
+       aabbmin[1] = min_fff(face_v1[1], face_v2[1], face_v3[1]);
+       aabbmin[2] = min_fff(face_v1[2], face_v2[2], face_v3[2]);
+       aabbmax[0] = max_fff(face_v1[0], face_v2[0], face_v3[0]);
+       aabbmax[1] = max_fff(face_v1[1], face_v2[1], face_v3[1]);
+       aabbmax[2] = max_fff(face_v1[2], face_v2[2], face_v3[2]);
 
        hash  = vertexowner->soft->scratch->colliderhash;
        ihash = BLI_ghashIterator_new(hash);
 
        hash  = vertexowner->soft->scratch->colliderhash;
        ihash = BLI_ghashIterator_new(hash);
-       while (!BLI_ghashIterator_isDone(ihash) ) {
+       while (BLI_ghashIterator_notDone(ihash) ) {
 
                ccd_Mesh *ccdm = BLI_ghashIterator_getValue     (ihash);
                ob             = BLI_ghashIterator_getKey       (ihash);
                        /* only with deflecting set */
 
                ccd_Mesh *ccdm = BLI_ghashIterator_getValue     (ihash);
                ob             = BLI_ghashIterator_getKey       (ihash);
                        /* only with deflecting set */
-                       if(ob->pd && ob->pd->deflect) {
+                       if (ob->pd && ob->pd->deflect) {
                                MFace *mface= NULL;
                                MVert *mvert= NULL;
                                MVert *mprevvert= NULL;
                                ccdf_minmax *mima= NULL;
                                MFace *mface= NULL;
                                MVert *mvert= NULL;
                                MVert *mprevvert= NULL;
                                ccdf_minmax *mima= NULL;
-                               if(ccdm){
+                               if (ccdm) {
                                        mface= ccdm->mface;
                                        mvert= ccdm->mvert;
                                        mprevvert= ccdm->mprevvert;
                                        mface= ccdm->mface;
                                        mvert= ccdm->mvert;
                                        mprevvert= ccdm->mprevvert;
@@ -1216,20 +1223,21 @@ static int sb_detect_face_collisionCached(float face_v1[3],float face_v2[3],floa
                                        a = ccdm->totface;
 
                                        if ((aabbmax[0] < ccdm->bbmin[0]) ||
                                        a = ccdm->totface;
 
                                        if ((aabbmax[0] < ccdm->bbmin[0]) ||
-                                               (aabbmax[1] < ccdm->bbmin[1]) ||
-                                               (aabbmax[2] < ccdm->bbmin[2]) ||
-                                               (aabbmin[0] > ccdm->bbmax[0]) ||
-                                               (aabbmin[1] > ccdm->bbmax[1]) ||
-                                               (aabbmin[2] > ccdm->bbmax[2]) ) {
-                                               /* boxes dont intersect */
+                                           (aabbmax[1] < ccdm->bbmin[1]) ||
+                                           (aabbmax[2] < ccdm->bbmin[2]) ||
+                                           (aabbmin[0] > ccdm->bbmax[0]) ||
+                                           (aabbmin[1] > ccdm->bbmax[1]) ||
+                                           (aabbmin[2] > ccdm->bbmax[2]) )
+                                       {
+                                               /* boxes don't intersect */
                                                BLI_ghashIterator_step(ihash);
                                                continue;
                                        }
 
                                }
                                                BLI_ghashIterator_step(ihash);
                                                continue;
                                        }
 
                                }
-                               else{
+                               else {
                                        /*aye that should be cached*/
                                        /*aye that should be cached*/
-                                       printf("missing cache error \n");
+                                       printf("missing cache error\n");
                                        BLI_ghashIterator_step(ihash);
                                        continue;
                                }
                                        BLI_ghashIterator_step(ihash);
                                        continue;
                                }
@@ -1251,27 +1259,27 @@ static int sb_detect_face_collisionCached(float face_v1[3],float face_v2[3],floa
                                        }
 
 
                                        }
 
 
-                                       if (mvert){
+                                       if (mvert) {
 
 
-                                               copy_v3_v3(nv1,mvert[mface->v1].co);
-                                               copy_v3_v3(nv2,mvert[mface->v2].co);
-                                               copy_v3_v3(nv3,mvert[mface->v3].co);
-                                               if (mface->v4){
-                                                       copy_v3_v3(nv4,mvert[mface->v4].co);
+                                               copy_v3_v3(nv1, mvert[mface->v1].co);
+                                               copy_v3_v3(nv2, mvert[mface->v2].co);
+                                               copy_v3_v3(nv3, mvert[mface->v3].co);
+                                               if (mface->v4) {
+                                                       copy_v3_v3(nv4, mvert[mface->v4].co);
                                                }
                                                }
-                                               if (mprevvert){
-                                                       mul_v3_fl(nv1,time);
-                                                       Vec3PlusStVec(nv1,(1.0f-time),mprevvert[mface->v1].co);
+                                               if (mprevvert) {
+                                                       mul_v3_fl(nv1, time);
+                                                       Vec3PlusStVec(nv1, (1.0f-time), mprevvert[mface->v1].co);
 
 
-                                                       mul_v3_fl(nv2,time);
-                                                       Vec3PlusStVec(nv2,(1.0f-time),mprevvert[mface->v2].co);
+                                                       mul_v3_fl(nv2, time);
+                                                       Vec3PlusStVec(nv2, (1.0f-time), mprevvert[mface->v2].co);
 
 
-                                                       mul_v3_fl(nv3,time);
-                                                       Vec3PlusStVec(nv3,(1.0f-time),mprevvert[mface->v3].co);
+                                                       mul_v3_fl(nv3, time);
+                                                       Vec3PlusStVec(nv3, (1.0f-time), mprevvert[mface->v3].co);
 
 
-                                                       if (mface->v4){
-                                                               mul_v3_fl(nv4,time);
-                                                               Vec3PlusStVec(nv4,(1.0f-time),mprevvert[mface->v4].co);
+                                                       if (mface->v4) {
+                                                               mul_v3_fl(nv4, time);
+                                                               Vec3PlusStVec(nv4, (1.0f-time), mprevvert[mface->v4].co);
                                                        }
                                                }
                                        }
                                                        }
                                                }
                                        }
@@ -1284,12 +1292,12 @@ static int sb_detect_face_collisionCached(float face_v1[3],float face_v2[3],floa
                                        if (
                                                isect_line_tri_v3(nv1, nv2, face_v1, face_v2, face_v3, &t, NULL) ||
                                                isect_line_tri_v3(nv2, nv3, face_v1, face_v2, face_v3, &t, NULL) ||
                                        if (
                                                isect_line_tri_v3(nv1, nv2, face_v1, face_v2, face_v3, &t, NULL) ||
                                                isect_line_tri_v3(nv2, nv3, face_v1, face_v2, face_v3, &t, NULL) ||
-                                               isect_line_tri_v3(nv3, nv1, face_v1, face_v2, face_v3, &t, NULL) ){
-                                               Vec3PlusStVec(force,-0.5f,d_nvect);
+                                               isect_line_tri_v3(nv3, nv1, face_v1, face_v2, face_v3, &t, NULL) ) {
+                                               Vec3PlusStVec(force, -0.5f, d_nvect);
                                                *damp=tune*ob->pd->pdef_sbdamp;
                                                deflected = 2;
                                        }
                                                *damp=tune*ob->pd->pdef_sbdamp;
                                                deflected = 2;
                                        }
-                                       if (mface->v4){ /* quad */
+                                       if (mface->v4) { /* quad */
                                                /* switch origin to be nv4 */
                                                sub_v3_v3v3(edge1, nv3, nv4);
                                                sub_v3_v3v3(edge2, nv1, nv4);
                                                /* switch origin to be nv4 */
                                                sub_v3_v3v3(edge1, nv3, nv4);
                                                sub_v3_v3v3(edge2, nv1, nv4);
@@ -1299,8 +1307,8 @@ static int sb_detect_face_collisionCached(float face_v1[3],float face_v2[3],floa
                                                        /* isect_line_tri_v3(nv1, nv3, face_v1, face_v2, face_v3, &t, NULL) ||
                                                         we did that edge already */
                                                        isect_line_tri_v3(nv3, nv4, face_v1, face_v2, face_v3, &t, NULL) ||
                                                        /* isect_line_tri_v3(nv1, nv3, face_v1, face_v2, face_v3, &t, NULL) ||
                                                         we did that edge already */
                                                        isect_line_tri_v3(nv3, nv4, face_v1, face_v2, face_v3, &t, NULL) ||
-                                                       isect_line_tri_v3(nv4, nv1, face_v1, face_v2, face_v3, &t, NULL) ){
-                                                       Vec3PlusStVec(force,-0.5f,d_nvect);
+                                                       isect_line_tri_v3(nv4, nv1, face_v1, face_v2, face_v3, &t, NULL) ) {
+                                                       Vec3PlusStVec(force, -0.5f, d_nvect);
                                                        *damp=tune*ob->pd->pdef_sbdamp;
                                                        deflected = 2;
                                                }
                                                        *damp=tune*ob->pd->pdef_sbdamp;
                                                        deflected = 2;
                                                }
@@ -1308,7 +1316,7 @@ static int sb_detect_face_collisionCached(float face_v1[3],float face_v2[3],floa
                                        mface++;
                                        mima++;
                                }/* while a */
                                        mface++;
                                        mima++;
                                }/* while a */
-                       } /* if(ob->pd && ob->pd->deflect) */
+                       } /* if (ob->pd && ob->pd->deflect) */
                        BLI_ghashIterator_step(ihash);
        } /* while () */
        BLI_ghashIterator_free(ihash);
                        BLI_ghashIterator_step(ihash);
        } /* while () */
        BLI_ghashIterator_free(ihash);
@@ -1317,83 +1325,82 @@ static int sb_detect_face_collisionCached(float face_v1[3],float face_v2[3],floa
 
 
 
 
 
 
-static void scan_for_ext_face_forces(Object *ob,float timenow)
+static void scan_for_ext_face_forces(Object *ob, float timenow)
 {
        SoftBody *sb = ob->soft;
        BodyFace *bf;
        int a;
 {
        SoftBody *sb = ob->soft;
        BodyFace *bf;
        int a;
-       float damp=0.0f,choke=1.0f;
+       float damp=0.0f, choke=1.0f;
        float tune = -10.0f;
        float feedback[3];
 
        float tune = -10.0f;
        float feedback[3];
 
-       if (sb && sb->scratch->totface){
+       if (sb && sb->scratch->totface) {
 
 
                bf = sb->scratch->bodyface;
 
 
                bf = sb->scratch->bodyface;
-               for(a=0; a<sb->scratch->totface; a++, bf++) {
+               for (a=0; a<sb->scratch->totface; a++, bf++) {
                        bf->ext_force[0]=bf->ext_force[1]=bf->ext_force[2]=0.0f;
 /*+++edges intruding*/
                        bf->flag &= ~BFF_INTERSECT;
                        feedback[0]=feedback[1]=feedback[2]=0.0f;
                        bf->ext_force[0]=bf->ext_force[1]=bf->ext_force[2]=0.0f;
 /*+++edges intruding*/
                        bf->flag &= ~BFF_INTERSECT;
                        feedback[0]=feedback[1]=feedback[2]=0.0f;
-                       if (sb_detect_face_collisionCached(sb->bpoint[bf->v1].pos,sb->bpoint[bf->v2].pos, sb->bpoint[bf->v3].pos,
-                               &damp,  feedback, ob->lay ,ob , timenow)){
-                               Vec3PlusStVec(sb->bpoint[bf->v1].force,tune,feedback);
-                               Vec3PlusStVec(sb->bpoint[bf->v2].force,tune,feedback);
-                               Vec3PlusStVec(sb->bpoint[bf->v3].force,tune,feedback);
-//                             Vec3PlusStVec(bf->ext_force,tune,feedback);
+                       if (sb_detect_face_collisionCached(sb->bpoint[bf->v1].pos, sb->bpoint[bf->v2].pos, sb->bpoint[bf->v3].pos,
+                               &damp,  feedback, ob->lay, ob, timenow)) {
+                               Vec3PlusStVec(sb->bpoint[bf->v1].force, tune, feedback);
+                               Vec3PlusStVec(sb->bpoint[bf->v2].force, tune, feedback);
+                               Vec3PlusStVec(sb->bpoint[bf->v3].force, tune, feedback);
+//                             Vec3PlusStVec(bf->ext_force, tune, feedback);
                                bf->flag |= BFF_INTERSECT;
                                bf->flag |= BFF_INTERSECT;
-                               choke = MIN2(MAX2(damp,choke),1.0f);
+                               choke = MIN2(MAX2(damp, choke), 1.0f);
                        }
 
                        feedback[0]=feedback[1]=feedback[2]=0.0f;
                        }
 
                        feedback[0]=feedback[1]=feedback[2]=0.0f;
-                       if ((bf->v4) && (sb_detect_face_collisionCached(sb->bpoint[bf->v1].pos,sb->bpoint[bf->v3].pos, sb->bpoint[bf->v4].pos,
-                               &damp,  feedback, ob->lay ,ob , timenow))){
-                               Vec3PlusStVec(sb->bpoint[bf->v1].force,tune,feedback);
-                               Vec3PlusStVec(sb->bpoint[bf->v3].force,tune,feedback);
-                               Vec3PlusStVec(sb->bpoint[bf->v4].force,tune,feedback);
-//                             Vec3PlusStVec(bf->ext_force,tune,feedback);
+                       if ((bf->v4) && (sb_detect_face_collisionCached(sb->bpoint[bf->v1].pos, sb->bpoint[bf->v3].pos, sb->bpoint[bf->v4].pos,
+                               &damp,  feedback, ob->lay, ob, timenow))) {
+                               Vec3PlusStVec(sb->bpoint[bf->v1].force, tune, feedback);
+                               Vec3PlusStVec(sb->bpoint[bf->v3].force, tune, feedback);
+                               Vec3PlusStVec(sb->bpoint[bf->v4].force, tune, feedback);
+//                             Vec3PlusStVec(bf->ext_force, tune, feedback);
                                bf->flag |= BFF_INTERSECT;
                                bf->flag |= BFF_INTERSECT;
-                               choke = MIN2(MAX2(damp,choke),1.0f);
+                               choke = MIN2(MAX2(damp, choke), 1.0f);
                        }
 /*---edges intruding*/
 
 /*+++ close vertices*/
                        }
 /*---edges intruding*/
 
 /*+++ close vertices*/
-                       if  (( bf->flag & BFF_INTERSECT)==0){
+                       if (( bf->flag & BFF_INTERSECT)==0) {
                                bf->flag &= ~BFF_CLOSEVERT;
                                tune = -1.0f;
                                feedback[0]=feedback[1]=feedback[2]=0.0f;
                                bf->flag &= ~BFF_CLOSEVERT;
                                tune = -1.0f;
                                feedback[0]=feedback[1]=feedback[2]=0.0f;
-                               if (sb_detect_face_pointCached(sb->bpoint[bf->v1].pos,sb->bpoint[bf->v2].pos, sb->bpoint[bf->v3].pos,
-                                       &damp,  feedback, ob->lay ,ob , timenow)){
-                               Vec3PlusStVec(sb->bpoint[bf->v1].force,tune,feedback);
-                               Vec3PlusStVec(sb->bpoint[bf->v2].force,tune,feedback);
-                               Vec3PlusStVec(sb->bpoint[bf->v3].force,tune,feedback);
-//                                             Vec3PlusStVec(bf->ext_force,tune,feedback);
+                               if (sb_detect_face_pointCached(sb->bpoint[bf->v1].pos, sb->bpoint[bf->v2].pos, sb->bpoint[bf->v3].pos,
+                                       &damp,  feedback, ob->lay, ob, timenow)) {
+                               Vec3PlusStVec(sb->bpoint[bf->v1].force, tune, feedback);
+                               Vec3PlusStVec(sb->bpoint[bf->v2].force, tune, feedback);
+                               Vec3PlusStVec(sb->bpoint[bf->v3].force, tune, feedback);
+//                                             Vec3PlusStVec(bf->ext_force, tune, feedback);
                                                bf->flag |= BFF_CLOSEVERT;
                                                bf->flag |= BFF_CLOSEVERT;
-                                               choke = MIN2(MAX2(damp,choke),1.0f);
+                                               choke = MIN2(MAX2(damp, choke), 1.0f);
                                }
 
                                feedback[0]=feedback[1]=feedback[2]=0.0f;
                                }
 
                                feedback[0]=feedback[1]=feedback[2]=0.0f;
-                               if ((bf->v4) && (sb_detect_face_pointCached(sb->bpoint[bf->v1].pos,sb->bpoint[bf->v3].pos, sb->bpoint[bf->v4].pos,
-                                       &damp,  feedback, ob->lay ,ob , timenow))){
-                               Vec3PlusStVec(sb->bpoint[bf->v1].force,tune,feedback);
-                               Vec3PlusStVec(sb->bpoint[bf->v3].force,tune,feedback);
-                               Vec3PlusStVec(sb->bpoint[bf->v4].force,tune,feedback);
-//                                             Vec3PlusStVec(bf->ext_force,tune,feedback);
+                               if ((bf->v4) && (sb_detect_face_pointCached(sb->bpoint[bf->v1].pos, sb->bpoint[bf->v3].pos, sb->bpoint[bf->v4].pos,
+                                       &damp,  feedback, ob->lay, ob, timenow))) {
+                               Vec3PlusStVec(sb->bpoint[bf->v1].force, tune, feedback);
+                               Vec3PlusStVec(sb->bpoint[bf->v3].force, tune, feedback);
+                               Vec3PlusStVec(sb->bpoint[bf->v4].force, tune, feedback);
+//                                             Vec3PlusStVec(bf->ext_force, tune, feedback);
                                                bf->flag |= BFF_CLOSEVERT;
                                                bf->flag |= BFF_CLOSEVERT;
-                                               choke = MIN2(MAX2(damp,choke),1.0f);
+                                               choke = MIN2(MAX2(damp, choke), 1.0f);
                                }
                        }
 /*--- close vertices*/
                }
                bf = sb->scratch->bodyface;
                                }
                        }
 /*--- close vertices*/
                }
                bf = sb->scratch->bodyface;
-               for(a=0; a<sb->scratch->totface; a++, bf++) {
-                       if (( bf->flag & BFF_INTERSECT) || ( bf->flag & BFF_CLOSEVERT))
-                       {
-                               sb->bpoint[bf->v1].choke2=MAX2(sb->bpoint[bf->v1].choke2,choke);
-                               sb->bpoint[bf->v2].choke2=MAX2(sb->bpoint[bf->v2].choke2,choke);
-                               sb->bpoint[bf->v3].choke2=MAX2(sb->bpoint[bf->v3].choke2,choke);
-                               if (bf->v4){
-                               sb->bpoint[bf->v2].choke2=MAX2(sb->bpoint[bf->v2].choke2,choke);
+               for (a=0; a<sb->scratch->totface; a++, bf++) {
+                       if (( bf->flag & BFF_INTERSECT) || ( bf->flag & BFF_CLOSEVERT)) {
+                               sb->bpoint[bf->v1].choke2=MAX2(sb->bpoint[bf->v1].choke2, choke);
+                               sb->bpoint[bf->v2].choke2=MAX2(sb->bpoint[bf->v2].choke2, choke);
+                               sb->bpoint[bf->v3].choke2=MAX2(sb->bpoint[bf->v3].choke2, choke);
+                               if (bf->v4) {
+                               sb->bpoint[bf->v2].choke2=MAX2(sb->bpoint[bf->v2].choke2, choke);
                                }
                        }
                }
                                }
                        }
                }
@@ -1405,38 +1412,38 @@ static void scan_for_ext_face_forces(Object *ob,float timenow)
 
 /* +++ the spring external section*/
 
 
 /* +++ the spring external section*/
 
-static int sb_detect_edge_collisionCached(float edge_v1[3],float edge_v2[3],float *damp,
-                                                                  float force[3], unsigned int UNUSED(par_layer),struct Object *vertexowner,float time)
+static int sb_detect_edge_collisionCached(float edge_v1[3], float edge_v2[3], float *damp,
+                                                                  float force[3], unsigned int UNUSED(par_layer), struct Object *vertexowner, float time)
 {
        Object *ob;
        GHash *hash;
        GHashIterator *ihash;
 {
        Object *ob;
        GHash *hash;
        GHashIterator *ihash;
-       float nv1[3], nv2[3], nv3[3], nv4[3], edge1[3], edge2[3], d_nvect[3], aabbmin[3],aabbmax[3];
-       float t,el;
+       float nv1[3], nv2[3], nv3[3], nv4[3], edge1[3], edge2[3], d_nvect[3], aabbmin[3], aabbmax[3];
+       float t, el;
        int a, deflected=0;
 
        int a, deflected=0;
 
-       aabbmin[0] = MIN2(edge_v1[0],edge_v2[0]);
-       aabbmin[1] = MIN2(edge_v1[1],edge_v2[1]);
-       aabbmin[2] = MIN2(edge_v1[2],edge_v2[2]);
-       aabbmax[0] = MAX2(edge_v1[0],edge_v2[0]);
-       aabbmax[1] = MAX2(edge_v1[1],edge_v2[1]);
-       aabbmax[2] = MAX2(edge_v1[2],edge_v2[2]);
+       aabbmin[0] = MIN2(edge_v1[0], edge_v2[0]);
+       aabbmin[1] = MIN2(edge_v1[1], edge_v2[1]);
+       aabbmin[2] = MIN2(edge_v1[2], edge_v2[2]);
+       aabbmax[0] = MAX2(edge_v1[0], edge_v2[0]);
+       aabbmax[1] = MAX2(edge_v1[1], edge_v2[1]);
+       aabbmax[2] = MAX2(edge_v1[2], edge_v2[2]);
 
 
-       el = len_v3v3(edge_v1,edge_v2);
+       el = len_v3v3(edge_v1, edge_v2);
 
        hash  = vertexowner->soft->scratch->colliderhash;
        ihash = BLI_ghashIterator_new(hash);
 
        hash  = vertexowner->soft->scratch->colliderhash;
        ihash = BLI_ghashIterator_new(hash);
-       while (!BLI_ghashIterator_isDone(ihash) ) {
+       while (BLI_ghashIterator_notDone(ihash) ) {
 
                ccd_Mesh *ccdm = BLI_ghashIterator_getValue     (ihash);
                ob             = BLI_ghashIterator_getKey       (ihash);
                        /* only with deflecting set */
 
                ccd_Mesh *ccdm = BLI_ghashIterator_getValue     (ihash);
                ob             = BLI_ghashIterator_getKey       (ihash);
                        /* only with deflecting set */
-                       if(ob->pd && ob->pd->deflect) {
+                       if (ob->pd && ob->pd->deflect) {
                                MFace *mface= NULL;
                                MVert *mvert= NULL;
                                MVert *mprevvert= NULL;
                                ccdf_minmax *mima= NULL;
                                MFace *mface= NULL;
                                MVert *mvert= NULL;
                                MVert *mprevvert= NULL;
                                ccdf_minmax *mima= NULL;
-                               if(ccdm){
+                               if (ccdm) {
                                        mface= ccdm->mface;
                                        mvert= ccdm->mvert;
                                        mprevvert= ccdm->mprevvert;
                                        mface= ccdm->mface;
                                        mvert= ccdm->mvert;
                                        mprevvert= ccdm->mprevvert;
@@ -1444,20 +1451,21 @@ static int sb_detect_edge_collisionCached(float edge_v1[3],float edge_v2[3],floa
                                        a = ccdm->totface;
 
                                        if ((aabbmax[0] < ccdm->bbmin[0]) ||
                                        a = ccdm->totface;
 
                                        if ((aabbmax[0] < ccdm->bbmin[0]) ||
-                                               (aabbmax[1] < ccdm->bbmin[1]) ||
-                                               (aabbmax[2] < ccdm->bbmin[2]) ||
-                                               (aabbmin[0] > ccdm->bbmax[0]) ||
-                                               (aabbmin[1] > ccdm->bbmax[1]) ||
-                                               (aabbmin[2] > ccdm->bbmax[2]) ) {
-                                               /* boxes dont intersect */
+                                           (aabbmax[1] < ccdm->bbmin[1]) ||
+                                           (aabbmax[2] < ccdm->bbmin[2]) ||
+                                           (aabbmin[0] > ccdm->bbmax[0]) ||
+                                           (aabbmin[1] > ccdm->bbmax[1]) ||
+                                           (aabbmin[2] > ccdm->bbmax[2]) )
+                                       {
+                                               /* boxes don't intersect */
                                                BLI_ghashIterator_step(ihash);
                                                continue;
                                        }
 
                                }
                                                BLI_ghashIterator_step(ihash);
                                                continue;
                                        }
 
                                }
-                               else{
+                               else {
                                        /*aye that should be cached*/
                                        /*aye that should be cached*/
-                                       printf("missing cache error \n");
+                                       printf("missing cache error\n");
                                        BLI_ghashIterator_step(ihash);
                                        continue;
                                }
                                        BLI_ghashIterator_step(ihash);
                                        continue;
                                }
@@ -1479,27 +1487,27 @@ static int sb_detect_edge_collisionCached(float edge_v1[3],float edge_v2[3],floa
                                        }
 
 
                                        }
 
 
-                                       if (mvert){
+                                       if (mvert) {
 
 
-                                               copy_v3_v3(nv1,mvert[mface->v1].co);
-                                               copy_v3_v3(nv2,mvert[mface->v2].co);
-                                               copy_v3_v3(nv3,mvert[mface->v3].co);
-                                               if (mface->v4){
-                                                       copy_v3_v3(nv4,mvert[mface->v4].co);
+                                               copy_v3_v3(nv1, mvert[mface->v1].co);
+                                               copy_v3_v3(nv2, mvert[mface->v2].co);
+                                               copy_v3_v3(nv3, mvert[mface->v3].co);
+                                               if (mface->v4) {
+                                                       copy_v3_v3(nv4, mvert[mface->v4].co);
                                                }
                                                }
-                                               if (mprevvert){
-                                                       mul_v3_fl(nv1,time);
-                                                       Vec3PlusStVec(nv1,(1.0f-time),mprevvert[mface->v1].co);
+                                               if (mprevvert) {
+                                                       mul_v3_fl(nv1, time);
+                                                       Vec3PlusStVec(nv1, (1.0f-time), mprevvert[mface->v1].co);
 
 
-                                                       mul_v3_fl(nv2,time);
-                                                       Vec3PlusStVec(nv2,(1.0f-time),mprevvert[mface->v2].co);
+                                                       mul_v3_fl(nv2, time);
+                                                       Vec3PlusStVec(nv2, (1.0f-time), mprevvert[mface->v2].co);
 
 
-                                                       mul_v3_fl(nv3,time);
-                                                       Vec3PlusStVec(nv3,(1.0f-time),mprevvert[mface->v3].co);
+                                                       mul_v3_fl(nv3, time);
+                                                       Vec3PlusStVec(nv3, (1.0f-time), mprevvert[mface->v3].co);
 
 
-                                                       if (mface->v4){
-                                                               mul_v3_fl(nv4,time);
-                                                               Vec3PlusStVec(nv4,(1.0f-time),mprevvert[mface->v4].co);
+                                                       if (mface->v4) {
+                                                               mul_v3_fl(nv4, time);
+                                                               Vec3PlusStVec(nv4, (1.0f-time), mprevvert[mface->v4].co);
                                                        }
                                                }
                                        }
                                                        }
                                                }
                                        }
@@ -1510,36 +1518,36 @@ static int sb_detect_edge_collisionCached(float edge_v1[3],float edge_v2[3],floa
 
                                        cross_v3_v3v3(d_nvect, edge2, edge1);
                                        normalize_v3(d_nvect);
 
                                        cross_v3_v3v3(d_nvect, edge2, edge1);
                                        normalize_v3(d_nvect);
-                                       if ( isect_line_tri_v3(edge_v1, edge_v2, nv1, nv2, nv3, &t, NULL)){
-                                               float v1[3],v2[3];
-                                               float intrusiondepth,i1,i2;
+                                       if ( isect_line_tri_v3(edge_v1, edge_v2, nv1, nv2, nv3, &t, NULL)) {
+                                               float v1[3], v2[3];
+                                               float intrusiondepth, i1, i2;
                                                sub_v3_v3v3(v1, edge_v1, nv2);
                                                sub_v3_v3v3(v2, edge_v2, nv2);
                                                sub_v3_v3v3(v1, edge_v1, nv2);
                                                sub_v3_v3v3(v2, edge_v2, nv2);
-                                               i1 = dot_v3v3(v1,d_nvect);
-                                               i2 = dot_v3v3(v2,d_nvect);
-                                               intrusiondepth = -MIN2(i1,i2)/el;
-                                               Vec3PlusStVec(force,intrusiondepth,d_nvect);
+                                               i1 = dot_v3v3(v1, d_nvect);
+                                               i2 = dot_v3v3(v2, d_nvect);
+                                               intrusiondepth = -MIN2(i1, i2)/el;
+                                               Vec3PlusStVec(force, intrusiondepth, d_nvect);
                                                *damp=ob->pd->pdef_sbdamp;
                                                deflected = 2;
                                        }
                                                *damp=ob->pd->pdef_sbdamp;
                                                deflected = 2;
                                        }
-                                       if (mface->v4){ /* quad */
+                                       if (mface->v4) { /* quad */
                                                /* switch origin to be nv4 */
                                                sub_v3_v3v3(edge1, nv3, nv4);
                                                sub_v3_v3v3(edge2, nv1, nv4);
 
                                                cross_v3_v3v3(d_nvect, edge2, edge1);
                                                normalize_v3(d_nvect);
                                                /* switch origin to be nv4 */
                                                sub_v3_v3v3(edge1, nv3, nv4);
                                                sub_v3_v3v3(edge2, nv1, nv4);
 
                                                cross_v3_v3v3(d_nvect, edge2, edge1);
                                                normalize_v3(d_nvect);
-                                               if (isect_line_tri_v3( edge_v1, edge_v2,nv1, nv3, nv4, &t, NULL)){
-                                                       float v1[3],v2[3];
-                                                       float intrusiondepth,i1,i2;
+                                               if (isect_line_tri_v3( edge_v1, edge_v2, nv1, nv3, nv4, &t, NULL)) {
+                                                       float v1[3], v2[3];
+                                                       float intrusiondepth, i1, i2;
                                                        sub_v3_v3v3(v1, edge_v1, nv4);
                                                        sub_v3_v3v3(v2, edge_v2, nv4);
                                                        sub_v3_v3v3(v1, edge_v1, nv4);
                                                        sub_v3_v3v3(v2, edge_v2, nv4);
-                                               i1 = dot_v3v3(v1,d_nvect);
-                                               i2 = dot_v3v3(v2,d_nvect);
-                                               intrusiondepth = -MIN2(i1,i2)/el;
+                                               i1 = dot_v3v3(v1, d_nvect);
+                                               i2 = dot_v3v3(v2, d_nvect);
+                                               intrusiondepth = -MIN2(i1, i2)/el;
 
 
 
 
-                                                       Vec3PlusStVec(force,intrusiondepth,d_nvect);
+                                                       Vec3PlusStVec(force, intrusiondepth, d_nvect);
                                                        *damp=ob->pd->pdef_sbdamp;
                                                        deflected = 2;
                                                }
                                                        *damp=ob->pd->pdef_sbdamp;
                                                        deflected = 2;
                                                }
@@ -1547,7 +1555,7 @@ static int sb_detect_edge_collisionCached(float edge_v1[3],float edge_v2[3],floa
                                        mface++;
                                        mima++;
                                }/* while a */
                                        mface++;
                                        mima++;
                                }/* while a */
-                       } /* if(ob->pd && ob->pd->deflect) */
+                       } /* if (ob->pd && ob->pd->deflect) */
                        BLI_ghashIterator_step(ihash);
        } /* while () */
        BLI_ghashIterator_free(ihash);
                        BLI_ghashIterator_step(ihash);
        } /* while () */
        BLI_ghashIterator_free(ihash);
@@ -1561,18 +1569,18 @@ static void _scan_for_ext_spring_forces(Scene *scene, Object *ob, float timenow,
        float damp;
        float feedback[3];
 
        float damp;
        float feedback[3];
 
-       if (sb && sb->totspring){
-               for(a=ifirst; a<ilast; a++) {
+       if (sb && sb->totspring) {
+               for (a=ifirst; a<ilast; a++) {
                        BodySpring *bs = &sb->bspring[a];
                        bs->ext_force[0]=bs->ext_force[1]=bs->ext_force[2]=0.0f;
                        feedback[0]=feedback[1]=feedback[2]=0.0f;
                        bs->flag &= ~BSF_INTERSECT;
 
                        BodySpring *bs = &sb->bspring[a];
                        bs->ext_force[0]=bs->ext_force[1]=bs->ext_force[2]=0.0f;
                        feedback[0]=feedback[1]=feedback[2]=0.0f;
                        bs->flag &= ~BSF_INTERSECT;
 
-                       if (bs->springtype == SB_EDGE){
+                       if (bs->springtype == SB_EDGE) {
                                /* +++ springs colliding */
                                /* +++ springs colliding */
-                               if (ob->softflag & OB_SB_EDGECOLL){
-                                       if ( sb_detect_edge_collisionCached (sb->bpoint[bs->v1].pos , sb->bpoint[bs->v2].pos,
-                                               &damp,feedback,ob->lay,ob,timenow)){
+                               if (ob->softflag & OB_SB_EDGECOLL) {
+                                       if ( sb_detect_edge_collisionCached (sb->bpoint[bs->v1].pos, sb->bpoint[bs->v2].pos,
+                                               &damp, feedback, ob->lay, ob, timenow)) {
                                                        add_v3_v3(bs->ext_force, feedback);
                                                        bs->flag |= BSF_INTERSECT;
                                                        //bs->cf=damp;
                                                        add_v3_v3(bs->ext_force, feedback);
                                                        bs->flag |= BSF_INTERSECT;
                                                        //bs->cf=damp;
@@ -1584,41 +1592,41 @@ static void _scan_for_ext_spring_forces(Scene *scene, Object *ob, float timenow,
 
                                /* +++ springs seeing wind ... n stuff depending on their orientation*/
                                /* note we don't use sb->mediafrict but use sb->aeroedge for magnitude of effect*/
 
                                /* +++ springs seeing wind ... n stuff depending on their orientation*/
                                /* note we don't use sb->mediafrict but use sb->aeroedge for magnitude of effect*/
-                               if(sb->aeroedge){
-                                       float vel[3],sp[3],pr[3],force[3];
-                                       float f,windfactor  = 0.25f;
+                               if (sb->aeroedge) {
+                                       float vel[3], sp[3], pr[3], force[3];
+                                       float f, windfactor  = 0.25f;
                                        /*see if we have wind*/
                                        /*see if we have wind*/
-                                       if(do_effector) {
+                                       if (do_effector) {
                                                EffectedPoint epoint;
                                                EffectedPoint epoint;
-                                               float speed[3]={0.0f,0.0f,0.0f};
+                                               float speed[3] = {0.0f, 0.0f, 0.0f};
                                                float pos[3];
                                                float pos[3];
-                                               mid_v3_v3v3(pos, sb->bpoint[bs->v1].pos , sb->bpoint[bs->v2].pos);
-                                               mid_v3_v3v3(vel, sb->bpoint[bs->v1].vec , sb->bpoint[bs->v2].vec);
+                                               mid_v3_v3v3(pos, sb->bpoint[bs->v1].pos, sb->bpoint[bs->v2].pos);
+                                               mid_v3_v3v3(vel, sb->bpoint[bs->v1].vec, sb->bpoint[bs->v2].vec);
                                                pd_point_from_soft(scene, pos, vel, -1, &epoint);
                                                pdDoEffectors(do_effector, NULL, sb->effector_weights, &epoint, force, speed);
 
                                                pd_point_from_soft(scene, pos, vel, -1, &epoint);
                                                pdDoEffectors(do_effector, NULL, sb->effector_weights, &epoint, force, speed);
 
-                                               mul_v3_fl(speed,windfactor);
+                                               mul_v3_fl(speed, windfactor);
                                                add_v3_v3(vel, speed);
                                        }
                                        /* media in rest */
                                                add_v3_v3(vel, speed);
                                        }
                                        /* media in rest */
-                                       else{
-                                               add_v3_v3v3(vel, sb->bpoint[bs->v1].vec , sb->bpoint[bs->v2].vec);
+                                       else {
+                                               add_v3_v3v3(vel, sb->bpoint[bs->v1].vec, sb->bpoint[bs->v2].vec);
                                        }
                                        f = normalize_v3(vel);
                                        f = -0.0001f*f*f*sb->aeroedge;
                                        }
                                        f = normalize_v3(vel);
                                        f = -0.0001f*f*f*sb->aeroedge;
-                                       /* (todo) add a nice angle dependant function done for now BUT */
+                                       /* (todo) add a nice angle dependent function done for now BUT */
                                        /* still there could be some nice drag/lift function, but who needs it */
 
                                        /* still there could be some nice drag/lift function, but who needs it */
 
-                                       sub_v3_v3v3(sp, sb->bpoint[bs->v1].pos , sb->bpoint[bs->v2].pos);
-                                       project_v3_v3v3(pr,vel,sp);
+                                       sub_v3_v3v3(sp, sb->bpoint[bs->v1].pos, sb->bpoint[bs->v2].pos);
+                                       project_v3_v3v3(pr, vel, sp);
                                        sub_v3_v3(vel, pr);
                                        normalize_v3(vel);
                                        sub_v3_v3(vel, pr);
                                        normalize_v3(vel);
-                                       if (ob->softflag & OB_SB_AERO_ANGLE){
+                                       if (ob->softflag & OB_SB_AERO_ANGLE) {
                                                normalize_v3(sp);
                                                normalize_v3(sp);
-                                               Vec3PlusStVec(bs->ext_force,f*(1.0f-ABS(dot_v3v3(vel,sp))),vel);
+                                               Vec3PlusStVec(bs->ext_force, f*(1.0f-ABS(dot_v3v3(vel, sp))), vel);
                                        }
                                        }
-                                       else{
-                                               Vec3PlusStVec(bs->ext_force,f,vel); // to keep compatible with 2.45 release files
+                                       else {
+                                               Vec3PlusStVec(bs->ext_force, f, vel); // to keep compatible with 2.45 release files
                                        }
                                }
                                /* --- springs seeing wind */
                                        }
                                }
                                /* --- springs seeing wind */
@@ -1645,18 +1653,18 @@ static void *exec_scan_for_ext_spring_forces(void *data)
        return NULL;
 }
 
        return NULL;
 }
 
-static void sb_sfesf_threads_run(Scene *scene, struct Object *ob, float timenow,int totsprings,int *UNUSED(ptr_to_break_func(void)))
+static void sb_sfesf_threads_run(Scene *scene, struct Object *ob, float timenow, int totsprings, int *UNUSED(ptr_to_break_func(void)))
 {
        ListBase *do_effector = NULL;
        ListBase threads;
        SB_thread_context *sb_threads;
 {
        ListBase *do_effector = NULL;
        ListBase threads;
        SB_thread_context *sb_threads;
-       int i, totthread,left,dec;
+       int i, totthread, left, dec;
        int lowsprings =100; /* wild guess .. may increase with better thread management 'above' or even be UI option sb->spawn_cf_threads_nopts */
 
        do_effector= pdInitEffectors(scene, ob, NULL, ob->soft->effector_weights);
 
        /* figure the number of threads while preventing pretty pointless threading overhead */
        int lowsprings =100; /* wild guess .. may increase with better thread management 'above' or even be UI option sb->spawn_cf_threads_nopts */
 
        do_effector= pdInitEffectors(scene, ob, NULL, ob->soft->effector_weights);
 
        /* figure the number of threads while preventing pretty pointless threading overhead */
-       if(scene->r.mode & R_FIXED_THREADS)
+       if (scene->r.mode & R_FIXED_THREADS)
                totthread= scene->r.threads;
        else
                totthread= BLI_system_thread_count();
                totthread= scene->r.threads;
        else
                totthread= BLI_system_thread_count();
@@ -1669,29 +1677,29 @@ static void sb_sfesf_threads_run(Scene *scene, struct Object *ob, float timenow,
        memset(sb_threads, 0, sizeof(SB_thread_context)*totthread);
        left = totsprings;
        dec = totsprings/totthread +1;
        memset(sb_threads, 0, sizeof(SB_thread_context)*totthread);
        left = totsprings;
        dec = totsprings/totthread +1;
-       for(i=0; i<totthread; i++) {
+       for (i=0; i<totthread; i++) {
                sb_threads[i].scene = scene;
                sb_threads[i].ob = ob;
                sb_threads[i].forcetime = 0.0; // not used here
                sb_threads[i].timenow = timenow;
                sb_threads[i].ilast   = left;
                left = left - dec;
                sb_threads[i].scene = scene;
                sb_threads[i].ob = ob;
                sb_threads[i].forcetime = 0.0; // not used here
                sb_threads[i].timenow = timenow;
                sb_threads[i].ilast   = left;
                left = left - dec;
-               if (left >0){
+               if (left >0) {
                        sb_threads[i].ifirst  = left;
                }
                else
                        sb_threads[i].ifirst  = 0;
                sb_threads[i].do_effector = do_effector;
                        sb_threads[i].ifirst  = left;
                }
                else
                        sb_threads[i].ifirst  = 0;
                sb_threads[i].do_effector = do_effector;
-               sb_threads[i].do_deflector = 0;// not used here
+               sb_threads[i].do_deflector = FALSE;// not used here
                sb_threads[i].fieldfactor = 0.0f;// not used here
                sb_threads[i].windfactor  = 0.0f;// not used here
                sb_threads[i].nr= i;
                sb_threads[i].tot= totthread;
        }
                sb_threads[i].fieldfactor = 0.0f;// not used here
                sb_threads[i].windfactor  = 0.0f;// not used here
                sb_threads[i].nr= i;
                sb_threads[i].tot= totthread;
        }
-       if(totthread > 1) {
+       if (totthread > 1) {
                BLI_init_threads(&threads, exec_scan_for_ext_spring_forces, totthread);
 
                BLI_init_threads(&threads, exec_scan_for_ext_spring_forces, totthread);
 
-               for(i=0; i<totthread; i++)
+               for (i=0; i<totthread; i++)
                        BLI_insert_thread(&threads, &sb_threads[i]);
 
                BLI_end_threads(&threads);
                        BLI_insert_thread(&threads, &sb_threads[i]);
 
                BLI_end_threads(&threads);
@@ -1707,27 +1715,27 @@ static void sb_sfesf_threads_run(Scene *scene, struct Object *ob, float timenow,
 
 /* --- the spring external section*/
 
 
 /* --- the spring external section*/
 
-static int choose_winner(float*w, float* pos,float*a,float*b,float*c,float*ca,float*cb,float*cc)
+static int choose_winner(float*w, float* pos, float*a, float*b, float*c, float*ca, float*cb, float*cc)
 {
 {
-       float mindist,cp;
+       float mindist, cp;
        int winner =1;
        int winner =1;
-       mindist = ABS(dot_v3v3(pos,a));
+       mindist = fabsf(dot_v3v3(pos, a));
 
 
-       cp = ABS(dot_v3v3(pos,b));
-       if ( mindist < cp ){
+       cp = fabsf(dot_v3v3(pos, b));
+       if ( mindist < cp ) {
                mindist = cp;
                winner =2;
        }
 
                mindist = cp;
                winner =2;
        }
 
-       cp = ABS(dot_v3v3(pos,c));
-       if (mindist < cp ){
+       cp = fabsf(dot_v3v3(pos, c));
+       if (mindist < cp ) {
                mindist = cp;
                winner =3;
        }
                mindist = cp;
                winner =3;
        }
-       switch (winner){
-               case 1: copy_v3_v3(w,ca); break;
-               case 2: copy_v3_v3(w,cb); break;
-               case 3: copy_v3_v3(w,cc);
+       switch (winner) {
+               case 1: copy_v3_v3(w, ca); break;
+               case 2: copy_v3_v3(w, cb); break;
+               case 3: copy_v3_v3(w, cc);
        }
        return(winner);
 }
        }
        return(winner);
 }
@@ -1736,13 +1744,13 @@ static int choose_winner(float*w, float* pos,float*a,float*b,float*c,float*ca,fl
 
 static int sb_detect_vertex_collisionCached(float opco[3], float facenormal[3], float *damp,
                                                                         float force[3], unsigned int UNUSED(par_layer), struct Object *vertexowner,
 
 static int sb_detect_vertex_collisionCached(float opco[3], float facenormal[3], float *damp,
                                                                         float force[3], unsigned int UNUSED(par_layer), struct Object *vertexowner,
-                                                                        float time,float vel[3], float *intrusion)
+                                                                        float time, float vel[3], float *intrusion)
 {
        Object *ob= NULL;
        GHash *hash;
        GHashIterator *ihash;
 {
        Object *ob= NULL;
        GHash *hash;
        GHashIterator *ihash;
-       float nv1[3], nv2[3], nv3[3], nv4[3], edge1[3], edge2[3],d_nvect[3], dv1[3],ve[3],avel[3]={0.0,0.0,0.0},
-             vv1[3], vv2[3], vv3[3], vv4[3], coledge[3]={0.0f, 0.0f, 0.0f}, mindistedge = 1000.0f,
+       float nv1[3], nv2[3], nv3[3], nv4[3], edge1[3], edge2[3], d_nvect[3], dv1[3], ve[3], avel[3] = {0.0, 0.0, 0.0},
+             vv1[3], vv2[3], vv3[3], vv4[3], coledge[3] = {0.0f, 0.0f, 0.0f}, mindistedge = 1000.0f,
              outerforceaccu[3], innerforceaccu[3],
              facedist, /* n_mag, */ /* UNUSED */ force_mag_norm, minx, miny, minz, maxx, maxy, maxz,
              innerfacethickness = -0.5f, outerfacethickness = 0.2f,
              outerforceaccu[3], innerforceaccu[3],
              facedist, /* n_mag, */ /* UNUSED */ force_mag_norm, minx, miny, minz, maxx, maxy, maxz,
              innerfacethickness = -0.5f, outerfacethickness = 0.2f,
@@ -1755,54 +1763,55 @@ static int sb_detect_vertex_collisionCached(float opco[3], float facenormal[3],
        outerforceaccu[0]=outerforceaccu[1]=outerforceaccu[2]=0.0f;
        innerforceaccu[0]=innerforceaccu[1]=innerforceaccu[2]=0.0f;
 /* go */
        outerforceaccu[0]=outerforceaccu[1]=outerforceaccu[2]=0.0f;
        innerforceaccu[0]=innerforceaccu[1]=innerforceaccu[2]=0.0f;
 /* go */
-       while (!BLI_ghashIterator_isDone(ihash) ) {
+       while (BLI_ghashIterator_notDone(ihash) ) {
 
                ccd_Mesh *ccdm = BLI_ghashIterator_getValue     (ihash);
                ob             = BLI_ghashIterator_getKey       (ihash);
                        /* only with deflecting set */
 
                ccd_Mesh *ccdm = BLI_ghashIterator_getValue     (ihash);
                ob             = BLI_ghashIterator_getKey       (ihash);
                        /* only with deflecting set */
-                       if(ob->pd && ob->pd->deflect) {
+                       if (ob->pd && ob->pd->deflect) {
                                MFace *mface= NULL;
                                MVert *mvert= NULL;
                                MVert *mprevvert= NULL;
                                ccdf_minmax *mima= NULL;
 
                                MFace *mface= NULL;
                                MVert *mvert= NULL;
                                MVert *mprevvert= NULL;
                                ccdf_minmax *mima= NULL;
 
-                               if(ccdm){
+                               if (ccdm) {
                                        mface= ccdm->mface;
                                        mvert= ccdm->mvert;
                                        mprevvert= ccdm->mprevvert;
                                        mima= ccdm->mima;
                                        a = ccdm->totface;
 
                                        mface= ccdm->mface;
                                        mvert= ccdm->mvert;
                                        mprevvert= ccdm->mprevvert;
                                        mima= ccdm->mima;
                                        a = ccdm->totface;
 
-                                       minx =ccdm->bbmin[0];
-                                       miny =ccdm->bbmin[1];
-                                       minz =ccdm->bbmin[2];
+                                       minx = ccdm->bbmin[0];
+                                       miny = ccdm->bbmin[1];
+                                       minz = ccdm->bbmin[2];
 
 
-                                       maxx =ccdm->bbmax[0];
-                                       maxy =ccdm->bbmax[1];
-                                       maxz =ccdm->bbmax[2];
+                                       maxx = ccdm->bbmax[0];
+                                       maxy = ccdm->bbmax[1];
+                                       maxz = ccdm->bbmax[2];
 
                                        if ((opco[0] < minx) ||
 
                                        if ((opco[0] < minx) ||
-                                               (opco[1] < miny) ||
-                                               (opco[2] < minz) ||
-                                               (opco[0] > maxx) ||
-                                               (opco[1] > maxy) ||
-                                               (opco[2] > maxz) ) {
-                                                       /* outside the padded boundbox --> collision object is too far away */
-                                                                                               BLI_ghashIterator_step(ihash);
-                                                       continue;
+                                           (opco[1] < miny) ||
+                                           (opco[2] < minz) ||
+                                           (opco[0] > maxx) ||
+                                           (opco[1] > maxy) ||
+                                           (opco[2] > maxz) )
+                                       {
+                                               /* outside the padded boundbox --> collision object is too far away */
+                                               BLI_ghashIterator_step(ihash);
+                                               continue;
                                        }
                                }
                                        }
                                }
-                               else{
+                               else {
                                        /*aye that should be cached*/
                                        /*aye that should be cached*/
-                                       printf("missing cache error \n");
+                                       printf("missing cache error\n");
                                                BLI_ghashIterator_step(ihash);
                                        continue;
                                }
 
                                /* do object level stuff */
                                /* need to have user control for that since it depends on model scale */
                                                BLI_ghashIterator_step(ihash);
                                        continue;
                                }
 
                                /* do object level stuff */
                                /* need to have user control for that since it depends on model scale */
-                               innerfacethickness =-ob->pd->pdef_sbift;
-                               outerfacethickness =ob->pd->pdef_sboft;
+                               innerfacethickness = -ob->pd->pdef_sbift;
+                               outerfacethickness =  ob->pd->pdef_sboft;
                                fa = (ff*outerfacethickness-outerfacethickness);
                                fa *= fa;
                                fa = 1.0f/fa;
                                fa = (ff*outerfacethickness-outerfacethickness);
                                fa *= fa;
                                fa = 1.0f/fa;
@@ -1822,41 +1831,41 @@ static int sb_detect_vertex_collisionCached(float opco[3], float facenormal[3],
                                                        continue;
                                        }
 
                                                        continue;
                                        }
 
-                                       if (mvert){
+                                       if (mvert) {
 
 
-                                               copy_v3_v3(nv1,mvert[mface->v1].co);
-                                               copy_v3_v3(nv2,mvert[mface->v2].co);
-                                               copy_v3_v3(nv3,mvert[mface->v3].co);
-                                               if (mface->v4){
-                                                       copy_v3_v3(nv4,mvert[mface->v4].co);
+                                               copy_v3_v3(nv1, mvert[mface->v1].co);
+                                               copy_v3_v3(nv2, mvert[mface->v2].co);
+                                               copy_v3_v3(nv3, mvert[mface->v3].co);
+                                               if (mface->v4) {
+                                                       copy_v3_v3(nv4, mvert[mface->v4].co);
                                                }
 
                                                }
 
-                                               if (mprevvert){
+                                               if (mprevvert) {
                                                        /* grab the average speed of the collider vertices
                                                        before we spoil nvX
                                                        humm could be done once a SB steps but then we' need to store that too
                                                        since the AABB reduced propabitlty to get here drasticallly
                                                        it might be a nice tradeof CPU <--> memory
                                                        */
                                                        /* grab the average speed of the collider vertices
                                                        before we spoil nvX
                                                        humm could be done once a SB steps but then we' need to store that too
                                                        since the AABB reduced propabitlty to get here drasticallly
                                                        it might be a nice tradeof CPU <--> memory
                                                        */
-                                                       sub_v3_v3v3(vv1,nv1,mprevvert[mface->v1].co);
-                                                       sub_v3_v3v3(vv2,nv2,mprevvert[mface->v2].co);
-                                                       sub_v3_v3v3(vv3,nv3,mprevvert[mface->v3].co);
-                                                       if (mface->v4){
-                                                               sub_v3_v3v3(vv4,nv4,mprevvert[mface->v4].co);
+                                                       sub_v3_v3v3(vv1, nv1, mprevvert[mface->v1].co);
+                                                       sub_v3_v3v3(vv2, nv2, mprevvert[mface->v2].co);
+                                                       sub_v3_v3v3(vv3, nv3, mprevvert[mface->v3].co);
+                                                       if (mface->v4) {
+                                                               sub_v3_v3v3(vv4, nv4, mprevvert[mface->v4].co);
                                                        }
 
                                                        }
 
-                                                       mul_v3_fl(nv1,time);
-                                                       Vec3PlusStVec(nv1,(1.0f-time),mprevvert[mface->v1].co);
+                                                       mul_v3_fl(nv1, time);
+                                                       Vec3PlusStVec(nv1, (1.0f-time), mprevvert[mface->v1].co);
 
 
-                                                       mul_v3_fl(nv2,time);
-                                                       Vec3PlusStVec(nv2,(1.0f-time),mprevvert[mface->v2].co);
+                                                       mul_v3_fl(nv2, time);
+                                                       Vec3PlusStVec(nv2, (1.0f-time), mprevvert[mface->v2].co);
 
 
-                                                       mul_v3_fl(nv3,time);
-                                                       Vec3PlusStVec(nv3,(1.0f-time),mprevvert[mface->v3].co);
+                                                       mul_v3_fl(nv3, time);
+                                                       Vec3PlusStVec(nv3, (1.0f-time), mprevvert[mface->v3].co);
 
 
-                                                       if (mface->v4){
-                                                               mul_v3_fl(nv4,time);
-                                                               Vec3PlusStVec(nv4,(1.0f-time),mprevvert[mface->v4].co);
+                                                       if (mface->v4) {
+                                                               mul_v3_fl(nv4, time);
+                                                               Vec3PlusStVec(nv4, (1.0f-time), mprevvert[mface->v4].co);
                                                        }
                                                }
                                        }
                                                        }
                                                }
                                        }
@@ -1864,32 +1873,32 @@ static int sb_detect_vertex_collisionCached(float opco[3], float facenormal[3],
                                        /* switch origin to be nv2*/
                                        sub_v3_v3v3(edge1, nv1, nv2);
                                        sub_v3_v3v3(edge2, nv3, nv2);
                                        /* switch origin to be nv2*/
                                        sub_v3_v3v3(edge1, nv1, nv2);
                                        sub_v3_v3v3(edge2, nv3, nv2);
-                                       sub_v3_v3v3(dv1,opco,nv2); /* abuse dv1 to have vertex in question at *origin* of triangle */
+                                       sub_v3_v3v3(dv1, opco, nv2); /* abuse dv1 to have vertex in question at *origin* of triangle */
 
                                        cross_v3_v3v3(d_nvect, edge2, edge1);
                                        /* n_mag = */ /* UNUSED */ normalize_v3(d_nvect);
 
                                        cross_v3_v3v3(d_nvect, edge2, edge1);
                                        /* n_mag = */ /* UNUSED */ normalize_v3(d_nvect);
-                                       facedist = dot_v3v3(dv1,d_nvect);
+                                       facedist = dot_v3v3(dv1, d_nvect);
                                        // so rules are
                                        //
 
                                        // so rules are
                                        //
 
-                                       if ((facedist > innerfacethickness) && (facedist < outerfacethickness)){
-                                               if (isect_point_tri_prism_v3(opco, nv1, nv2, nv3) ){
+                                       if ((facedist > innerfacethickness) && (facedist < outerfacethickness)) {
+                                               if (isect_point_tri_prism_v3(opco, nv1, nv2, nv3) ) {
                                                        force_mag_norm =(float)exp(-ee*facedist);
                                                        if (facedist > outerfacethickness*ff)
                                                                force_mag_norm =(float)force_mag_norm*fa*(facedist - outerfacethickness)*(facedist - outerfacethickness);
                                                        *damp=ob->pd->pdef_sbdamp;
                                                        force_mag_norm =(float)exp(-ee*facedist);
                                                        if (facedist > outerfacethickness*ff)
                                                                force_mag_norm =(float)force_mag_norm*fa*(facedist - outerfacethickness)*(facedist - outerfacethickness);
                                                        *damp=ob->pd->pdef_sbdamp;
-                                                       if (facedist > 0.0f){
+                                                       if (facedist > 0.0f) {
                                                                *damp *= (1.0f - facedist/outerfacethickness);
                                                                *damp *= (1.0f - facedist/outerfacethickness);
-                                                               Vec3PlusStVec(outerforceaccu,force_mag_norm,d_nvect);
+                                                               Vec3PlusStVec(outerforceaccu, force_mag_norm, d_nvect);
                                                                deflected = 3;
 
                                                        }
                                                        else {
                                                                deflected = 3;
 
                                                        }
                                                        else {
-                                                               Vec3PlusStVec(innerforceaccu,force_mag_norm,d_nvect);
+                                                               Vec3PlusStVec(innerforceaccu, force_mag_norm, d_nvect);
                                                                if (deflected < 2) deflected = 2;
                                                        }
                                                                if (deflected < 2) deflected = 2;
                                                        }
-                                                       if ((mprevvert) && (*damp > 0.0f)){
-                                                               choose_winner(ve,opco,nv1,nv2,nv3,vv1,vv2,vv3);
+                                                       if ((mprevvert) && (*damp > 0.0f)) {
+                                                               choose_winner(ve, opco, nv1, nv2, nv3, vv1, vv2, vv3);
                                                                add_v3_v3(avel, ve);
                                                                cavel ++;
                                                        }
                                                                add_v3_v3(avel, ve);
                                                                cavel ++;
                                                        }
@@ -1897,35 +1906,35 @@ static int sb_detect_vertex_collisionCached(float opco[3], float facenormal[3],
                                                        ci++;
                                                }
                                        }
                                                        ci++;
                                                }
                                        }
-                                       if (mface->v4){ /* quad */
+                                       if (mface->v4) { /* quad */
                                                /* switch origin to be nv4 */
                                                sub_v3_v3v3(edge1, nv3, nv4);
                                                sub_v3_v3v3(edge2, nv1, nv4);
                                                /* switch origin to be nv4 */
                                                sub_v3_v3v3(edge1, nv3, nv4);
                                                sub_v3_v3v3(edge2, nv1, nv4);
-                                               sub_v3_v3v3(dv1,opco,nv4); /* abuse dv1 to have vertex in question at *origin* of triangle */
+                                               sub_v3_v3v3(dv1, opco, nv4); /* abuse dv1 to have vertex in question at *origin* of triangle */
 
                                                cross_v3_v3v3(d_nvect, edge2, edge1);
                                                /* n_mag = */ /* UNUSED */ normalize_v3(d_nvect);
 
                                                cross_v3_v3v3(d_nvect, edge2, edge1);
                                                /* n_mag = */ /* UNUSED */ normalize_v3(d_nvect);
-                                               facedist = dot_v3v3(dv1,d_nvect);
+                                               facedist = dot_v3v3(dv1, d_nvect);
 
 
-                                               if ((facedist > innerfacethickness) && (facedist < outerfacethickness)){
-                                                       if (isect_point_tri_prism_v3(opco, nv1, nv3, nv4) ){
+                                               if ((facedist > innerfacethickness) && (facedist < outerfacethickness)) {
+                                                       if (isect_point_tri_prism_v3(opco, nv1, nv3, nv4) ) {
                                                                force_mag_norm =(float)exp(-ee*facedist);
                                                                if (facedist > outerfacethickness*ff)
                                                                        force_mag_norm =(float)force_mag_norm*fa*(facedist - outerfacethickness)*(facedist - outerfacethickness);
                                                                *damp=ob->pd->pdef_sbdamp;
                                                                force_mag_norm =(float)exp(-ee*facedist);
                                                                if (facedist > outerfacethickness*ff)
                                                                        force_mag_norm =(float)force_mag_norm*fa*(facedist - outerfacethickness)*(facedist - outerfacethickness);
                                                                *damp=ob->pd->pdef_sbdamp;
-                                                       if (facedist > 0.0f){
+                                                       if (facedist > 0.0f) {
                                                                *damp *= (1.0f - facedist/outerfacethickness);
                                                                *damp *= (1.0f - facedist/outerfacethickness);
-                                                               Vec3PlusStVec(outerforceaccu,force_mag_norm,d_nvect);
+                                                               Vec3PlusStVec(outerforceaccu, force_mag_norm, d_nvect);
                                                                deflected = 3;
 
                                                        }
                                                        else {
                                                                deflected = 3;
 
                                                        }
                                                        else {
-                                                               Vec3PlusStVec(innerforceaccu,force_mag_norm,d_nvect);
+                                                               Vec3PlusStVec(innerforceaccu, force_mag_norm, d_nvect);
                                                                if (deflected < 2) deflected = 2;
                                                        }
 
                                                                if (deflected < 2) deflected = 2;
                                                        }
 
-                                                               if ((mprevvert) && (*damp > 0.0f)){
-                                                                       choose_winner(ve,opco,nv1,nv3,nv4,vv1,vv3,vv4);
+                                                               if ((mprevvert) && (*damp > 0.0f)) {
+                                                                       choose_winner(ve, opco, nv1, nv3, nv4, vv1, vv3, vv4);
                                                                        add_v3_v3(avel, ve);
                                                                        cavel ++;
                                                                }
                                                                        add_v3_v3(avel, ve);
                                                                        cavel ++;
                                                                }
@@ -1934,51 +1943,51 @@ static int sb_detect_vertex_collisionCached(float opco[3], float facenormal[3],
                                                        }
 
                                                }
                                                        }
 
                                                }
-                                               if ((deflected < 2)&& (G.rt != 444)) // we did not hit a face until now
-                                               { // see if 'outer' hits an edge
+                                               if ((deflected < 2)&& (G.debug_value != 444)) { /* we did not hit a face until now */
+                                                       /* see if 'outer' hits an edge */
                                                        float dist;
 
                                                        closest_to_line_segment_v3(ve, opco, nv1, nv2);
                                                        float dist;
 
                                                        closest_to_line_segment_v3(ve, opco, nv1, nv2);
-                                                        sub_v3_v3v3(ve,opco,ve);
+                                                        sub_v3_v3v3(ve, opco, ve);
                                                        dist = normalize_v3(ve);
                                                        dist = normalize_v3(ve);
-                                                       if ((dist < outerfacethickness)&&(dist < mindistedge )){
-                                                               copy_v3_v3(coledge,ve);
+                                                       if ((dist < outerfacethickness)&&(dist < mindistedge )) {
+                                                               copy_v3_v3(coledge, ve);
                                                                mindistedge = dist,
                                                                deflected=1;
                                                        }
 
                                                        closest_to_line_segment_v3(ve, opco, nv2, nv3);
                                                                mindistedge = dist,
                                                                deflected=1;
                                                        }
 
                                                        closest_to_line_segment_v3(ve, opco, nv2, nv3);
-                                                        sub_v3_v3v3(ve,opco,ve);
+                                                       sub_v3_v3v3(ve, opco, ve);
                                                        dist = normalize_v3(ve);
                                                        dist = normalize_v3(ve);
-                                                       if ((dist < outerfacethickness)&&(dist < mindistedge )){
-                                                               copy_v3_v3(coledge,ve);
+                                                       if ((dist < outerfacethickness)&&(dist < mindistedge )) {
+                                                               copy_v3_v3(coledge, ve);
                                                                mindistedge = dist,
                                                                deflected=1;
                                                        }
 
                                                        closest_to_line_segment_v3(ve, opco, nv3, nv1);
                                                                mindistedge = dist,
                                                                deflected=1;
                                                        }
 
                                                        closest_to_line_segment_v3(ve, opco, nv3, nv1);
-                                                        sub_v3_v3v3(ve,opco,ve);
+                                                       sub_v3_v3v3(ve, opco, ve);
                                                        dist = normalize_v3(ve);
                                                        dist = normalize_v3(ve);
-                                                       if ((dist < outerfacethickness)&&(dist < mindistedge )){
-                                                               copy_v3_v3(coledge,ve);
+                                                       if ((dist < outerfacethickness)&&(dist < mindistedge )) {
+                                                               copy_v3_v3(coledge, ve);
                                                                mindistedge = dist,
                                                                deflected=1;
                                                        }
                                                                mindistedge = dist,
                                                                deflected=1;
                                                        }
-                                                       if (mface->v4){ /* quad */
+                                                       if (mface->v4) { /* quad */
                                                                closest_to_line_segment_v3(ve, opco, nv3, nv4);
                                                                closest_to_line_segment_v3(ve, opco, nv3, nv4);
-                                                               sub_v3_v3v3(ve,opco,ve);
+                                                               sub_v3_v3v3(ve, opco, ve);
                                                                dist = normalize_v3(ve);
                                                                dist = normalize_v3(ve);
-                                                               if ((dist < outerfacethickness)&&(dist < mindistedge )){
-                                                                       copy_v3_v3(coledge,ve);
+                                                               if ((dist < outerfacethickness)&&(dist < mindistedge )) {
+                                                                       copy_v3_v3(coledge, ve);
                                                                        mindistedge = dist,
                                                                                deflected=1;
                                                                }
 
                                                                closest_to_line_segment_v3(ve, opco, nv1, nv4);
                                                                        mindistedge = dist,
                                                                                deflected=1;
                                                                }
 
                                                                closest_to_line_segment_v3(ve, opco, nv1, nv4);
-                                                               sub_v3_v3v3(ve,opco,ve);
+                                                               sub_v3_v3v3(ve, opco, ve);
                                                                dist = normalize_v3(ve);
                                                                dist = normalize_v3(ve);
-                                                               if ((dist < outerfacethickness)&&(dist < mindistedge )){
-                                                                       copy_v3_v3(coledge,ve);
+                                                               if ((dist < outerfacethickness)&&(dist < mindistedge )) {
+                                                                       copy_v3_v3(coledge, ve);
                                                                        mindistedge = dist,
                                                                                deflected=1;
                                                                }
                                                                        mindistedge = dist,
                                                                                deflected=1;
                                                                }
@@ -1991,33 +2000,33 @@ static int sb_detect_vertex_collisionCached(float opco[3], float facenormal[3],
                                        mface++;
                                        mima++;
                                }/* while a */
                                        mface++;
                                        mima++;
                                }/* while a */
-                       } /* if(ob->pd && ob->pd->deflect) */
+                       } /* if (ob->pd && ob->pd->deflect) */
                        BLI_ghashIterator_step(ihash);
        } /* while () */
 
                        BLI_ghashIterator_step(ihash);
        } /* while () */
 
-       if (deflected == 1){ // no face but 'outer' edge cylinder sees vert
+       if (deflected == 1) { // no face but 'outer' edge cylinder sees vert
                force_mag_norm =(float)exp(-ee*mindistedge);
                if (mindistedge > outerfacethickness*ff)
                        force_mag_norm =(float)force_mag_norm*fa*(mindistedge - outerfacethickness)*(mindistedge - outerfacethickness);
                force_mag_norm =(float)exp(-ee*mindistedge);
                if (mindistedge > outerfacethickness*ff)
                        force_mag_norm =(float)force_mag_norm*fa*(mindistedge - outerfacethickness)*(mindistedge - outerfacethickness);
-               Vec3PlusStVec(force,force_mag_norm,coledge);
+               Vec3PlusStVec(force, force_mag_norm, coledge);
                *damp=ob->pd->pdef_sbdamp;
                *damp=ob->pd->pdef_sbdamp;
-               if (mindistedge > 0.0f){
+               if (mindistedge > 0.0f) {
                        *damp *= (1.0f - mindistedge/outerfacethickness);
                }
 
        }
                        *damp *= (1.0f - mindistedge/outerfacethickness);
                }
 
        }
-       if (deflected == 2){ //  face inner detected
+       if (deflected == 2) { //  face inner detected
                add_v3_v3(force, innerforceaccu);
        }
                add_v3_v3(force, innerforceaccu);
        }
-       if (deflected == 3){ //  face outer detected
+       if (deflected == 3) { //  face outer detected
                add_v3_v3(force, outerforceaccu);
        }
 
        BLI_ghashIterator_free(ihash);
                add_v3_v3(force, outerforceaccu);
        }
 
        BLI_ghashIterator_free(ihash);
-       if (cavel) mul_v3_fl(avel,1.0f/(float)cavel);
-       copy_v3_v3(vel,avel);
+       if (cavel) mul_v3_fl(avel, 1.0f/(float)cavel);
+       copy_v3_v3(vel, avel);
        if (ci) *intrusion /= ci;
        if (ci) *intrusion /= ci;
-       if (deflected){
+       if (deflected) {
                normalize_v3_v3(facenormal, force);
        }
        return deflected;
                normalize_v3_v3(facenormal, force);
        }
        return deflected;
@@ -2025,34 +2034,34 @@ static int sb_detect_vertex_collisionCached(float opco[3], float facenormal[3],
 
 
 /* sandbox to plug in various deflection algos */
 
 
 /* sandbox to plug in various deflection algos */
-static int sb_deflect_face(Object *ob,float *actpos,float *facenormal,float *force,float *cf,float time,float *vel,float *intrusion)
+static int sb_deflect_face(Object *ob, float *actpos, float *facenormal, float *force, float *cf, float time, float *vel, float *intrusion)
 {
        float s_actpos[3];
        int deflected;
 {
        float s_actpos[3];
        int deflected;
-       copy_v3_v3(s_actpos,actpos);
-       deflected= sb_detect_vertex_collisionCached(s_actpos, facenormal, cf, force , ob->lay, ob,time,vel,intrusion);
-       //deflected= sb_detect_vertex_collisionCachedEx(s_actpos, facenormal, cf, force , ob->lay, ob,time,vel,intrusion);
+       copy_v3_v3(s_actpos, actpos);
+       deflected= sb_detect_vertex_collisionCached(s_actpos, facenormal, cf, force, ob->lay, ob, time, vel, intrusion);
+       //deflected= sb_detect_vertex_collisionCachedEx(s_actpos, facenormal, cf, force, ob->lay, ob, time, vel, intrusion);
        return(deflected);
 }
 
 /* hiding this for now .. but the jacobian may pop up on other tasks .. so i'd like to keep it
        return(deflected);
 }
 
 /* hiding this for now .. but the jacobian may pop up on other tasks .. so i'd like to keep it
-static void dfdx_spring(int ia, int ic, int op, float dir[3],float L,float len,float factor)
+static void dfdx_spring(int ia, int ic, int op, float dir[3], float L, float len, float factor)
 {
 {
-       float m,delta_ij;
-       int i ,j;
-       if (L < len){
-               for(i=0;i<3;i++)
-                       for(j=0;j<3;j++){
+       float m, delta_ij;
+       int ij;
+       if (L < len) {
+               for (i=0;i<3;i++)
+                       for (j=0;j<3;j++) {
                                delta_ij = (i==j ? (1.0f): (0.0f));
                                m=factor*(dir[i]*dir[j] + (1-L/len)*(delta_ij - dir[i]*dir[j]));
                                delta_ij = (i==j ? (1.0f): (0.0f));
                                m=factor*(dir[i]*dir[j] + (1-L/len)*(delta_ij - dir[i]*dir[j]));
-                               nlMatrixAdd(ia+i,op+ic+j,m);
+                               nlMatrixAdd(ia+i, op+ic+j, m);
                        }
        }
                        }
        }
-       else{
-               for(i=0;i<3;i++)
-                       for(j=0;j<3;j++){
+       else {
+               for (i=0;i<3;i++)
+                       for (j=0;j<3;j++) {
                                m=factor*dir[i]*dir[j];
                                m=factor*dir[i]*dir[j];
-                               nlMatrixAdd(ia+i,op+ic+j,m);
+                               nlMatrixAdd(ia+i, op+ic+j, m);
                        }
        }
 }
                        }
        }
 }
@@ -2061,27 +2070,27 @@ static void dfdx_spring(int ia, int ic, int op, float dir[3],float L,float len,f
 static void dfdx_goal(int ia, int ic, int op, float factor)
 {
        int i;
 static void dfdx_goal(int ia, int ic, int op, float factor)
 {
        int i;
-       for(i=0;i<3;i++) nlMatrixAdd(ia+i,op+ic+i,factor);
+       for (i=0;i<3;i++) nlMatrixAdd(ia+i, op+ic+i, factor);
 }
 
 }
 
-static void dfdv_goal(int ia, int ic,float factor)
+static void dfdv_goal(int ia, int ic, float factor)
 {
        int i;
 {
        int i;
-       for(i=0;i<3;i++) nlMatrixAdd(ia+i,ic+i,factor);
+       for (i=0;i<3;i++) nlMatrixAdd(ia+i, ic+i, factor);
 }
 */
 }
 */
-static void sb_spring_force(Object *ob,int bpi,BodySpring *bs,float iks,float UNUSED(forcetime), int nl_flags)
+static void sb_spring_force(Object *ob, int bpi, BodySpring *bs, float iks, float UNUSED(forcetime), int nl_flags)
 {
        SoftBody *sb= ob->soft; /* is supposed to be there */
 {
        SoftBody *sb= ob->soft; /* is supposed to be there */
-       BodyPoint  *bp1,*bp2;
+       BodyPoint  *bp1, *bp2;
 
 
-       float dir[3],dvel[3];
-       float distance,forcefactor,kd,absvel,projvel,kw;
+       float dir[3], dvel[3];
+       float distance, forcefactor, kd, absvel, projvel, kw;
 #if 0  /* UNUSED */
 #if 0  /* UNUSED */
-       int ia,ic;
+       int ia, ic;
 #endif
        /* prepare depending on which side of the spring we are on */
 #endif
        /* prepare depending on which side of the spring we are on */
-       if (bpi == bs->v1){
+       if (bpi == bs->v1) {
                bp1 = &sb->bpoint[bs->v1];
                bp2 = &sb->bpoint[bs->v2];
 #if 0  /* UNUSED */
                bp1 = &sb->bpoint[bs->v1];
                bp2 = &sb->bpoint[bs->v2];
 #if 0  /* UNUSED */
@@ -2089,7 +2098,7 @@ static void sb_spring_force(Object *ob,int bpi,BodySpring *bs,float iks,float UN
                ic =3*bs->v2;
 #endif
        }
                ic =3*bs->v2;
 #endif
        }
-       else if (bpi == bs->v2){
+       else if (bpi == bs->v2) {
                bp1 = &sb->bpoint[bs->v2];
                bp2 = &sb->bpoint[bs->v1];
 #if 0  /* UNUSED */
                bp1 = &sb->bpoint[bs->v2];
                bp2 = &sb->bpoint[bs->v1];
 #if 0  /* UNUSED */
@@ -2097,7 +2106,7 @@ static void sb_spring_force(Object *ob,int bpi,BodySpring *bs,float iks,float UN
                ic =3*bs->v1;
 #endif
        }
                ic =3*bs->v1;
 #endif
        }
-       else{
+       else {
                /* TODO make this debug option */
                /**/
                printf("bodypoint <bpi> is not attached to spring  <*bs> --> sb_spring_force()\n");
                /* TODO make this debug option */
                /**/
                printf("bodypoint <bpi> is not attached to spring  <*bs> --> sb_spring_force()\n");
@@ -2105,21 +2114,21 @@ static void sb_spring_force(Object *ob,int bpi,BodySpring *bs,float iks,float UN
        }
 
        /* do bp1 <--> bp2 elastic */
        }
 
        /* do bp1 <--> bp2 elastic */
-       sub_v3_v3v3(dir,bp1->pos,bp2->pos);
+       sub_v3_v3v3(dir, bp1->pos, bp2->pos);
        distance = normalize_v3(dir);
        if (bs->len < distance)
                iks  = 1.0f/(1.0f-sb->inspring)-1.0f ;/* inner spring constants function */
        else
                iks  = 1.0f/(1.0f-sb->inpush)-1.0f ;/* inner spring constants function */
 
        distance = normalize_v3(dir);
        if (bs->len < distance)
                iks  = 1.0f/(1.0f-sb->inspring)-1.0f ;/* inner spring constants function */
        else
                iks  = 1.0f/(1.0f-sb->inpush)-1.0f ;/* inner spring constants function */
 
-       if(bs->len > 0.0f) /* check for degenerated springs */
+       if (bs->len > 0.0f) /* check for degenerated springs */
                forcefactor = iks/bs->len;
        else
                forcefactor = iks;
        kw = (bp1->springweight+bp2->springweight)/2.0f;
        kw = kw * kw;
        kw = kw * kw;
                forcefactor = iks/bs->len;
        else
                forcefactor = iks;
        kw = (bp1->springweight+bp2->springweight)/2.0f;
        kw = kw * kw;
        kw = kw * kw;
-       switch (bs->springtype){
+       switch (bs->springtype) {
                case SB_EDGE:
                case SB_HANDLE:
                        forcefactor *=  kw;
                case SB_EDGE:
                case SB_HANDLE:
                        forcefactor *=  kw;
@@ -2135,30 +2144,30 @@ static void sb_spring_force(Object *ob,int bpi,BodySpring *bs,float iks,float UN
        }
 
 
        }
 
 
-       Vec3PlusStVec(bp1->force,(bs->len - distance)*forcefactor,dir);
+       Vec3PlusStVec(bp1->force, (bs->len - distance)*forcefactor, dir);
 
        /* do bp1 <--> bp2 viscous */
 
        /* do bp1 <--> bp2 viscous */
-       sub_v3_v3v3(dvel,bp1->vec,bp2->vec);
+       sub_v3_v3v3(dvel, bp1->vec, bp2->vec);
        kd = sb->infrict * sb_fric_force_scale(ob);
        absvel  = normalize_v3(dvel);
        kd = sb->infrict * sb_fric_force_scale(ob);
        absvel  = normalize_v3(dvel);
-       projvel = dot_v3v3(dir,dvel);
+       projvel = dot_v3v3(dir, dvel);
        kd     *= absvel * projvel;
        kd     *= absvel * projvel;
-       Vec3PlusStVec(bp1->force,-kd,dir);
+       Vec3PlusStVec(bp1->force, -kd, dir);
 
        /* do jacobian stuff if needed */
 
        /* do jacobian stuff if needed */
-       if(nl_flags & NLF_BUILD){
+       if (nl_flags & NLF_BUILD) {
                //int op =3*sb->totpoint;
                //float mvel = -forcetime*kd;
                //float mpos = -forcetime*forcefactor;
                /* depending on my pos */
                //int op =3*sb->totpoint;
                //float mvel = -forcetime*kd;
                //float mpos = -forcetime*forcefactor;
                /* depending on my pos */
-               // dfdx_spring(ia,ia,op,dir,bs->len,distance,-mpos);
+               // dfdx_spring(ia, ia, op, dir, bs->len, distance, -mpos);
                /* depending on my vel */
                /* depending on my vel */
-               // dfdv_goal(ia,ia,mvel); // well that ignores geometie
-               if(bp2->goal < SOFTGOALSNAP){ /* ommit this bp when it snaps */
+               // dfdv_goal(ia, ia, mvel); // well that ignores geometie
+               if (bp2->goal < SOFTGOALSNAP) { /* ommit this bp when it snaps */
                        /* depending on other pos */
                        /* depending on other pos */
-                       // dfdx_spring(ia,ic,op,dir,bs->len,distance,mpos);
+                       // dfdx_spring(ia, ic, op, dir, bs->len, distance, mpos);
                        /* depending on other vel */
                        /* depending on other vel */
-                       // dfdv_goal(ia,ia,-mvel); // well that ignores geometie
+                       // dfdv_goal(ia, ia, -mvel); // well that ignores geometie
                }
        }
 }
                }
        }
 }
@@ -2167,10 +2176,10 @@ static void sb_spring_force(Object *ob,int bpi,BodySpring *bs,float iks,float UN
 /* since this is definitely the most CPU consuming task here .. try to spread it */
 /* core function _softbody_calc_forces_slice_in_a_thread */
 /* result is int to be able to flag user break */
 /* since this is definitely the most CPU consuming task here .. try to spread it */
 /* core function _softbody_calc_forces_slice_in_a_thread */
 /* result is int to be able to flag user break */
-static int _softbody_calc_forces_slice_in_a_thread(Scene *scene, Object *ob, float forcetime, float timenow,int ifirst,int ilast,int *UNUSED(ptr_to_break_func(void)),ListBase *do_effector,int do_deflector,float fieldfactor, float windfactor)
+static int _softbody_calc_forces_slice_in_a_thread(Scene *scene, Object *ob, float forcetime, float timenow, int ifirst, int ilast, int *UNUSED(ptr_to_break_func(void)), ListBase *do_effector, int do_deflector, float fieldfactor, float windfactor)
 {
        float iks;
 {
        float iks;
-       int bb,do_selfcollision,do_springcollision,do_aero;
+       int bb, do_selfcollision, do_springcollision, do_aero;
        int number_of_points_here = ilast - ifirst;
        SoftBody *sb= ob->soft; /* is supposed to be there */
        BodyPoint  *bp;
        int number_of_points_here = ilast - ifirst;
        SoftBody *sb= ob->soft; /* is supposed to be there */
        BodyPoint  *bp;
@@ -2185,12 +2194,12 @@ static int _softbody_calc_forces_slice_in_a_thread(Scene *scene, Object *ob, flo
        /* --- could be done on object level to squeeze out the last bits of it */
        }
        else {
        /* --- could be done on object level to squeeze out the last bits of it */
        }
        else {
-               printf("Error expected a SB here \n");
+               printf("Error expected a SB here\n");
                return (999);
        }
 
 /* debugerin */
                return (999);
        }
 
 /* debugerin */
-       if  (sb->totpoint < ifirst) {
+       if (sb->totpoint < ifirst) {
                printf("Aye 998");
                return (998);
        }
                printf("Aye 998");
                return (998);
        }
@@ -2198,79 +2207,79 @@ static int _softbody_calc_forces_slice_in_a_thread(Scene *scene, Object *ob, flo
 
 
        bp = &sb->bpoint[ifirst];
 
 
        bp = &sb->bpoint[ifirst];
-       for(bb=number_of_points_here; bb>0; bb--, bp++) {
+       for (bb=number_of_points_here; bb>0; bb--, bp++) {
                /* clear forces  accumulator */
                /* clear forces  accumulator */
-               bp->force[0]= bp->force[1]= bp->force[2]= 0.0;
+               bp->force[0] = bp->force[1] = bp->force[2] = 0.0;
                /* naive ball self collision */
                /* needs to be done if goal snaps or not */
                /* naive ball self collision */
                /* needs to be done if goal snaps or not */
-               if(do_selfcollision){
+               if (do_selfcollision) {
                        int attached;
                        BodyPoint   *obp;
                        BodySpring *bs;
                        int attached;
                        BodyPoint   *obp;
                        BodySpring *bs;
-                       int c,b;
-                       float velcenter[3],dvel[3],def[3];
+                       int c, b;
+                       float velcenter[3], dvel[3], def[3];
                        float distance;
                        float compare;
                        float bstune = sb->ballstiff;
 
                        float distance;
                        float compare;
                        float bstune = sb->ballstiff;
 
-                       for(c=sb->totpoint, obp= sb->bpoint; c>=ifirst+bb; c--, obp++) {
+                       for (c=sb->totpoint, obp= sb->bpoint; c>=ifirst+bb; c--, obp++) {
                                compare = (obp->colball + bp->colball);
                                sub_v3_v3v3(def, bp->pos, obp->pos);
                                /* rather check the AABBoxes before ever calulating the real distance */
                                compare = (obp->colball + bp->colball);
                                sub_v3_v3v3(def, bp->pos, obp->pos);
                                /* rather check the AABBoxes before ever calulating the real distance */
-                               /* mathematically it is completly nuts, but performace is pretty much (3) times faster */
+                               /* mathematically it is completly nuts, but performance is pretty much (3) times faster */
                                if ((ABS(def[0]) > compare) || (ABS(def[1]) > compare) || (ABS(def[2]) > compare)) continue;
                                distance = normalize_v3(def);
                                if ((ABS(def[0]) > compare) || (ABS(def[1]) > compare) || (ABS(def[2]) > compare)) continue;
                                distance = normalize_v3(def);
-                               if (distance < compare ){
+                               if (distance < compare ) {
                                        /* exclude body points attached with a spring */
                                        attached = 0;
                                        /* exclude body points attached with a spring */
                                        attached = 0;
-                                       for(b=obp->nofsprings;b>0;b--){
+                                       for (b=obp->nofsprings;b>0;b--) {
                                                bs = sb->bspring + obp->springs[b-1];
                                                bs = sb->bspring + obp->springs[b-1];
-                                               if (( ilast-bb == bs->v2)  || ( ilast-bb == bs->v1)){
+                                               if (( ilast-bb == bs->v2) || ( ilast-bb == bs->v1)) {
                                                        attached=1;
                                                        continue;}
                                        }
                                                        attached=1;
                                                        continue;}
                                        }
-                                       if (!attached){
-                                               float f = bstune/(distance) + bstune/(compare*compare)*distance - 2.0f*bstune/compare ;
+                                       if (!attached) {
+                                               float f = bstune / (distance) + bstune / (compare * compare) * distance - 2.0f * bstune / compare;
 
                                                mid_v3_v3v3(velcenter, bp->vec, obp->vec);
 
                                                mid_v3_v3v3(velcenter, bp->vec, obp->vec);
-                                               sub_v3_v3v3(dvel,velcenter,bp->vec);
-                                               mul_v3_fl(dvel,_final_mass(ob,bp));
+                                               sub_v3_v3v3(dvel, velcenter, bp->vec);
+                                               mul_v3_fl(dvel, _final_mass(ob, bp));
 
 
-                                               Vec3PlusStVec(bp->force,f*(1.0f-sb->balldamp),def);
-                                               Vec3PlusStVec(bp->force,sb->balldamp,dvel);
+                                               Vec3PlusStVec(bp->force, f*(1.0f-sb->balldamp), def);
+                                               Vec3PlusStVec(bp->force, sb->balldamp, dvel);
 
 
-                                               /* exploit force(a,b) == -force(b,a) part2/2 */
-                                               sub_v3_v3v3(dvel,velcenter,obp->vec);
-                                               mul_v3_fl(dvel,_final_mass(ob,bp));
+                                               /* exploit force(a, b) == -force(b, a) part2/2 */
+                                               sub_v3_v3v3(dvel, velcenter, obp->vec);
+                                               mul_v3_fl(dvel, _final_mass(ob, bp));
 
 
-                                               Vec3PlusStVec(obp->force,sb->balldamp,dvel);
-                                               Vec3PlusStVec(obp->force,-f*(1.0f-sb->balldamp),def);
+                                               Vec3PlusStVec(obp->force, sb->balldamp, dvel);
+                                               Vec3PlusStVec(obp->force, -f*(1.0f-sb->balldamp), def);
                                        }
                                }
                        }
                }
                /* naive ball self collision done */
 
                                        }
                                }
                        }
                }
                /* naive ball self collision done */
 
-               if(_final_goal(ob,bp) < SOFTGOALSNAP){ /* ommit this bp when it snaps */
+               if (_final_goal(ob, bp) < SOFTGOALSNAP) { /* ommit this bp when it snaps */
                        float auxvect[3];
                        float velgoal[3];
 
                        /* do goal stuff */
                        float auxvect[3];
                        float velgoal[3];
 
                        /* do goal stuff */
-                       if(ob->softflag & OB_SB_GOAL) {
+                       if (ob->softflag & OB_SB_GOAL) {
                                /* true elastic goal */
                                /* true elastic goal */
-                               float ks,kd;
-                               sub_v3_v3v3(auxvect,bp->pos,bp->origT);
-                               ks  = 1.0f/(1.0f- _final_goal(ob,bp)*sb->goalspring)-1.0f ;
+                               float ks, kd;
+                               sub_v3_v3v3(auxvect, bp->pos, bp->origT);
+                               ks  = 1.0f / (1.0f - _final_goal(ob, bp) * sb->goalspring) - 1.0f;
                                bp->force[0]+= -ks*(auxvect[0]);
                                bp->force[1]+= -ks*(auxvect[1]);
                                bp->force[2]+= -ks*(auxvect[2]);
 
                                /* calulate damping forces generated by goals*/
                                bp->force[0]+= -ks*(auxvect[0]);
                                bp->force[1]+= -ks*(auxvect[1]);
                                bp->force[2]+= -ks*(auxvect[2]);
 
                                /* calulate damping forces generated by goals*/
-                               sub_v3_v3v3(velgoal,bp->origS, bp->origE);
-                               kd =  sb->goalfrict * sb_fric_force_scale(ob) ;
-                               add_v3_v3v3(auxvect,velgoal,bp->vec);
+                               sub_v3_v3v3(velgoal, bp->origS, bp->origE);
+                               kd =  sb->goalfrict * sb_fric_force_scale(ob);
+                               add_v3_v3v3(auxvect, velgoal, bp->vec);
 
 
-                               if (forcetime > 0.0 ) { /* make sure friction does not become rocket motor on time reversal */
+                               if (forcetime > 0.0f) { /* make sure friction does not become rocket motor on time reversal */
                                        bp->force[0]-= kd * (auxvect[0]);
                                        bp->force[1]-= kd * (auxvect[1]);
                                        bp->force[2]-= kd * (auxvect[2]);
                                        bp->force[0]-= kd * (auxvect[0]);
                                        bp->force[1]-= kd * (auxvect[1]);
                                        bp->force[2]-= kd * (auxvect[2]);
@@ -2284,25 +2293,25 @@ static int _softbody_calc_forces_slice_in_a_thread(Scene *scene, Object *ob, flo
                        /* done goal stuff */
 
                        /* gravitation */
                        /* done goal stuff */
 
                        /* gravitation */
-                       if (sb && scene->physics_settings.flag & PHYS_GLOBAL_GRAVITY){
+                       if (scene->physics_settings.flag & PHYS_GLOBAL_GRAVITY) {
                                float gravity[3];
                                copy_v3_v3(gravity, scene->physics_settings.gravity);
                                float gravity[3];
                                copy_v3_v3(gravity, scene->physics_settings.gravity);
-                               mul_v3_fl(gravity, sb_grav_force_scale(ob)*_final_mass(ob,bp)*sb->effector_weights->global_gravity); /* individual mass of node here */
+                               mul_v3_fl(gravity, sb_grav_force_scale(ob)*_final_mass(ob, bp)*sb->effector_weights->global_gravity); /* individual mass of node here */
                                add_v3_v3(bp->force, gravity);
                        }
 
                        /* particle field & vortex */
                                add_v3_v3(bp->force, gravity);
                        }
 
                        /* particle field & vortex */
-                       if(do_effector) {
+                       if (do_effector) {
                                EffectedPoint epoint;
                                float kd;
                                EffectedPoint epoint;
                                float kd;
-                               float force[3]= {0.0f, 0.0f, 0.0f};
-                               float speed[3]= {0.0f, 0.0f, 0.0f};
+                               float force[3] = {0.0f, 0.0f, 0.0f};
+                               float speed[3] = {0.0f, 0.0f, 0.0f};
                                float eval_sb_fric_force_scale = sb_fric_force_scale(ob); /* just for calling function once */
                                pd_point_from_soft(scene, bp->pos, bp->vec, sb->bpoint-bp, &epoint);
                                pdDoEffectors(do_effector, NULL, sb->effector_weights, &epoint, force, speed);
 
                                /* apply forcefield*/
                                float eval_sb_fric_force_scale = sb_fric_force_scale(ob); /* just for calling function once */
                                pd_point_from_soft(scene, bp->pos, bp->vec, sb->bpoint-bp, &epoint);
                                pdDoEffectors(do_effector, NULL, sb->effector_weights, &epoint, force, speed);
 
                                /* apply forcefield*/
-                               mul_v3_fl(force,fieldfactor* eval_sb_fric_force_scale);
+                               mul_v3_fl(force, fieldfactor* eval_sb_fric_force_scale);
                                add_v3_v3(bp->force, force);
 
                                /* BP friction in moving media */
                                add_v3_v3(bp->force, force);
 
                                /* BP friction in moving media */
@@ -2326,21 +2335,21 @@ static int _softbody_calc_forces_slice_in_a_thread(Scene *scene, Object *ob, flo
                        bp->choke = 0.0f;
                        bp->choke2 = 0.0f;
                        bp->loc_flag &= ~SBF_DOFUZZY;
                        bp->choke = 0.0f;
                        bp->choke2 = 0.0f;
                        bp->loc_flag &= ~SBF_DOFUZZY;
-                       if(do_deflector && !(bp->loc_flag & SBF_OUTOFCOLLISION) ) {
-                               float cfforce[3],defforce[3] ={0.0f,0.0f,0.0f}, vel[3] = {0.0f,0.0f,0.0f}, facenormal[3], cf = 1.0f,intrusion;
+                       if (do_deflector && !(bp->loc_flag & SBF_OUTOFCOLLISION) ) {
+                               float cfforce[3], defforce[3] ={0.0f, 0.0f, 0.0f}, vel[3] = {0.0f, 0.0f, 0.0f}, facenormal[3], cf = 1.0f, intrusion;
                                float kd = 1.0f;
 
                                float kd = 1.0f;
 
-                               if (sb_deflect_face(ob,bp->pos,facenormal,defforce,&cf,timenow,vel,&intrusion)){
-                                               if (intrusion < 0.0f){
+                               if (sb_deflect_face(ob, bp->pos, facenormal, defforce, &cf, timenow, vel, &intrusion)) {
+                                               if (intrusion < 0.0f) {
                                                        sb->scratch->flag |= SBF_DOFUZZY;
                                                        bp->loc_flag |= SBF_DOFUZZY;
                                                        bp->choke = sb->choke*0.01f;
                                                }
 
                                                        sb->scratch->flag |= SBF_DOFUZZY;
                                                        bp->loc_flag |= SBF_DOFUZZY;
                                                        bp->choke = sb->choke*0.01f;
                                                }
 
-                                                       sub_v3_v3v3(cfforce,bp->vec,vel);
-                                                       Vec3PlusStVec(bp->force,-cf*50.0f,cfforce);
+                                                       sub_v3_v3v3(cfforce, bp->vec, vel);
+                                                       Vec3PlusStVec(bp->force, -cf*50.0f, cfforce);
 
 
-                                       Vec3PlusStVec(bp->force,kd,defforce);
+                                       Vec3PlusStVec(bp->force, kd, defforce);
                                }
 
                        }
                                }
 
                        }
@@ -2348,45 +2357,45 @@ static int _softbody_calc_forces_slice_in_a_thread(Scene *scene, Object *ob, flo
 
                        /* +++springs */
                        iks  = 1.0f/(1.0f-sb->inspring)-1.0f ;/* inner spring constants function */
 
                        /* +++springs */
                        iks  = 1.0f/(1.0f-sb->inspring)-1.0f ;/* inner spring constants function */
-                       if(ob->softflag & OB_SB_EDGES) {
-                               if (sb->bspring){ /* spring list exists at all ? */
+                       if (ob->softflag & OB_SB_EDGES) {
+                               if (sb->bspring) { /* spring list exists at all ? */
                                        int b;
                                        BodySpring *bs;
                                        int b;
                                        BodySpring *bs;
-                                       for(b=bp->nofsprings;b>0;b--){
+                                       for (b=bp->nofsprings;b>0;b--) {
                                                bs = sb->bspring + bp->springs[b-1];
                                                bs = sb->bspring + bp->springs[b-1];
-                                               if (do_springcollision || do_aero){
+                                               if (do_springcollision || do_aero) {
                                                        add_v3_v3(bp->force, bs->ext_force);
                                                        if (bs->flag & BSF_INTERSECT)
                                                                bp->choke = bs->cf;
 
                                                }
                                                        add_v3_v3(bp->force, bs->ext_force);
                                                        if (bs->flag & BSF_INTERSECT)
                                                                bp->choke = bs->cf;
 
                                                }
-                                               // sb_spring_force(Object *ob,int bpi,BodySpring *bs,float iks,float forcetime,int nl_flags)
-                                               sb_spring_force(ob,ilast-bb,bs,iks,forcetime,0);
+                                               // sb_spring_force(Object *ob, int bpi, BodySpring *bs, float iks, float forcetime, int nl_flags)
+                                               sb_spring_force(ob, ilast-bb, bs, iks, forcetime, 0);
                                        }/* loop springs */
                                }/* existing spring list */
                        }/*any edges*/
                        /* ---springs */
                }/*omit on snap */
        }/*loop all bp's*/
                                        }/* loop springs */
                                }/* existing spring list */
                        }/*any edges*/
                        /* ---springs */
                }/*omit on snap */
        }/*loop all bp's*/
-return 0; /*done fine*/
+       return 0; /*done fine*/
 }
 
 static void *exec_softbody_calc_forces(void *data)
 {
        SB_thread_context *pctx = (SB_thread_context*)data;
 }
 
 static void *exec_softbody_calc_forces(void *data)
 {
        SB_thread_context *pctx = (SB_thread_context*)data;
-       _softbody_calc_forces_slice_in_a_thread(pctx->scene, pctx->ob, pctx->forcetime, pctx->timenow, pctx->ifirst, pctx->ilast, NULL, pctx->do_effector,pctx->do_deflector,pctx->fieldfactor,pctx->windfactor);
+       _softbody_calc_forces_slice_in_a_thread(pctx->scene, pctx->ob, pctx->forcetime, pctx->timenow, pctx->ifirst, pctx->ilast, NULL, pctx->do_effector, pctx->do_deflector, pctx->fieldfactor, pctx->windfactor);
        return NULL;
 }
 
        return NULL;
 }
 
-static void sb_cf_threads_run(Scene *scene, Object *ob, float forcetime, float timenow,int totpoint,int *UNUSED(ptr_to_break_func(void)),struct ListBase *do_effector,int do_deflector,float fieldfactor, float windfactor)
+static void sb_cf_threads_run(Scene *scene, Object *ob, float forcetime, float timenow, int totpoint, int *UNUSED(ptr_to_break_func(void)), struct ListBase *do_effector, int do_deflector, float fieldfactor, float windfactor)
 {
        ListBase threads;
        SB_thread_context *sb_threads;
 {
        ListBase threads;
        SB_thread_context *sb_threads;
-       int i, totthread,left,dec;
+       int i, totthread, left, dec;
        int lowpoints =100; /* wild guess .. may increase with better thread management 'above' or even be UI option sb->spawn_cf_threads_nopts */
 
        /* figure the number of threads while preventing pretty pointless threading overhead */
        int lowpoints =100; /* wild guess .. may increase with better thread management 'above' or even be UI option sb->spawn_cf_threads_nopts */
 
        /* figure the number of threads while preventing pretty pointless threading overhead */
-       if(scene->r.mode & R_FIXED_THREADS)
+       if (scene->r.mode & R_FIXED_THREADS)
                totthread= scene->r.threads;
        else
                totthread= BLI_system_thread_count();
                totthread= scene->r.threads;
        else
                totthread= BLI_system_thread_count();
@@ -2395,20 +2404,20 @@ static void sb_cf_threads_run(Scene *scene, Object *ob, float forcetime, float t
                totthread--;
        }
 
                totthread--;
        }
 
-       /* printf("sb_cf_threads_run spawning %d threads \n",totthread); */
+       /* printf("sb_cf_threads_run spawning %d threads\n", totthread); */
 
        sb_threads= MEM_callocN(sizeof(SB_thread_context)*totthread, "SBThread");
        memset(sb_threads, 0, sizeof(SB_thread_context)*totthread);
        left = totpoint;
        dec = totpoint/totthread +1;
 
        sb_threads= MEM_callocN(sizeof(SB_thread_context)*totthread, "SBThread");
        memset(sb_threads, 0, sizeof(SB_thread_context)*totthread);
        left = totpoint;
        dec = totpoint/totthread +1;
-       for(i=0; i<totthread; i++) {
+       for (i=0; i<totthread; i++) {
                sb_threads[i].scene = scene;
                sb_threads[i].ob = ob;
                sb_threads[i].forcetime = forcetime;
                sb_threads[i].timenow = timenow;
                sb_threads[i].ilast   = left;
                left = left - dec;
                sb_threads[i].scene = scene;
                sb_threads[i].ob = ob;
                sb_threads[i].forcetime = forcetime;
                sb_threads[i].timenow = timenow;
                sb_threads[i].ilast   = left;
                left = left - dec;
-               if (left >0){
+               if (left >0) {
                        sb_threads[i].ifirst  = left;
                }
                else
                        sb_threads[i].ifirst  = left;
                }
                else
@@ -2422,10 +2431,10 @@ static void sb_cf_threads_run(Scene *scene, Object *ob, float forcetime, float t
        }
 
 
        }
 
 
-       if(totthread > 1) {
+       if (totthread > 1) {
                BLI_init_threads(&threads, exec_softbody_calc_forces, totthread);
 
                BLI_init_threads(&threads, exec_softbody_calc_forces, totthread);
 
-               for(i=0; i<totthread; i++)
+               for (i=0; i<totthread; i++)
                        BLI_insert_thread(&threads, &sb_threads[i]);
 
                BLI_end_threads(&threads);
                        BLI_insert_thread(&threads, &sb_threads[i]);
 
                BLI_end_threads(&threads);
@@ -2447,7 +2456,7 @@ static void softbody_calc_forcesEx(Scene *scene, Object *ob, float forcetime, fl
        /* float gravity; */ /* UNUSED */
        /* float iks; */
        float fieldfactor = -1.0f, windfactor  = 0.25;
        /* float gravity; */ /* UNUSED */
        /* float iks; */
        float fieldfactor = -1.0f, windfactor  = 0.25;
-       int   do_deflector /*,do_selfcollision*/ ,do_springcollision,do_aero;
+       int   do_deflector /*, do_selfcollision*/, do_springcollision, do_aero;
 
        /* gravity = sb->grav * sb_grav_force_scale(ob); */ /* UNUSED */
 
 
        /* gravity = sb->grav * sb_grav_force_scale(ob); */ /* UNUSED */
 
@@ -2461,20 +2470,20 @@ static void softbody_calc_forcesEx(Scene *scene, Object *ob, float forcetime, fl
        /* bproot= sb->bpoint; */ /* need this for proper spring addressing */ /* UNUSED */
 
        if (do_springcollision || do_aero)
        /* bproot= sb->bpoint; */ /* need this for proper spring addressing */ /* UNUSED */
 
        if (do_springcollision || do_aero)
-       sb_sfesf_threads_run(scene, ob, timenow,sb->totspring,NULL);
+       sb_sfesf_threads_run(scene, ob, timenow, sb->totspring, NULL);
 
        /* after spring scan because it uses Effoctors too */
        do_effector= pdInitEffectors(scene, ob, NULL, sb->effector_weights);
 
        if (do_deflector) {
                float defforce[3];
 
        /* after spring scan because it uses Effoctors too */
        do_effector= pdInitEffectors(scene, ob, NULL, sb->effector_weights);
 
        if (do_deflector) {
                float defforce[3];
-               do_deflector = sb_detect_aabb_collisionCached(defforce,ob->lay,ob,timenow);
+               do_deflector = sb_detect_aabb_collisionCached(defforce, ob->lay, ob, timenow);
        }
 
        sb_cf_threads_run(scene, ob, forcetime, timenow, sb->totpoint, NULL, do_effector, do_deflector, fieldfactor, windfactor);
 
        /* finally add forces caused by face collision */
        }
 
        sb_cf_threads_run(scene, ob, forcetime, timenow, sb->totpoint, NULL, do_effector, do_deflector, fieldfactor, windfactor);
 
        /* finally add forces caused by face collision */
-       if (ob->softflag & OB_SB_FACECOLL) scan_for_ext_face_forces(ob,timenow);
+       if (ob->softflag & OB_SB_FACECOLL) scan_for_ext_face_forces(ob, timenow);
 
        /* finish matrix and solve */
        pdEndEffectors(&do_effector);
 
        /* finish matrix and solve */
        pdEndEffectors(&do_effector);
@@ -2486,18 +2495,18 @@ static void softbody_calc_forcesEx(Scene *scene, Object *ob, float forcetime, fl
 static void softbody_calc_forces(Scene *scene, Object *ob, float forcetime, float timenow, int nl_flags)
 {
        /* redirection to the new threaded Version */
 static void softbody_calc_forces(Scene *scene, Object *ob, float forcetime, float timenow, int nl_flags)
 {
        /* redirection to the new threaded Version */
-       if (!(G.rt & 0x10)){ // 16
+       if (!(G.debug_value & 0x10)) { // 16
                softbody_calc_forcesEx(scene, ob, forcetime, timenow, nl_flags);
                return;
        }
                softbody_calc_forcesEx(scene, ob, forcetime, timenow, nl_flags);
                return;
        }
-       else{
+       else {
                /* so the following will die  */
                /* |||||||||||||||||||||||||| */
                /* VVVVVVVVVVVVVVVVVVVVVVVVVV */
                /*backward compatibility note:
                fixing bug [17428] which forces adaptive step size to tiny steps
                in some situations
                /* so the following will die  */
                /* |||||||||||||||||||||||||| */
                /* VVVVVVVVVVVVVVVVVVVVVVVVVV */
                /*backward compatibility note:
                fixing bug [17428] which forces adaptive step size to tiny steps
                in some situations
-               .. keeping G.rt==17 0x11 option for old files 'needing' the bug*/
+               .. keeping G.debug_value==17 0x11 option for old files 'needing' the bug*/
 
                /* rule we never alter free variables :bp->vec bp->pos in here !
                * this will ruin adaptive stepsize AKA heun! (BM)
 
                /* rule we never alter free variables :bp->vec bp->pos in here !
                * this will ruin adaptive stepsize AKA heun! (BM)
@@ -2507,23 +2516,23 @@ static void softbody_calc_forces(Scene *scene, Object *ob, float forcetime, floa
                /* BodyPoint *bproot; */ /* UNUSED */
                BodySpring *bs;
                ListBase *do_effector = NULL;
                /* BodyPoint *bproot; */ /* UNUSED */
                BodySpring *bs;
                ListBase *do_effector = NULL;
-               float iks, ks, kd, gravity[3] = {0.0f,0.0f,0.0f};
+               float iks, ks, kd, gravity[3] = {0.0f, 0.0f, 0.0f};
                float fieldfactor = -1.0f, windfactor  = 0.25f;
                float tune = sb->ballstiff;
                float fieldfactor = -1.0f, windfactor  = 0.25f;
                float tune = sb->ballstiff;
-               int a, b,  do_deflector,do_selfcollision,do_springcollision,do_aero;
+               int a, b,  do_deflector, do_selfcollision, do_springcollision, do_aero;
 
 
                /* jacobian
                NLboolean success;
 
 
 
                /* jacobian
                NLboolean success;
 
-               if(nl_flags){
+               if (nl_flags) {
                nlBegin(NL_SYSTEM);
                nlBegin(NL_MATRIX);
                }
                */
 
 
                nlBegin(NL_SYSTEM);
                nlBegin(NL_MATRIX);
                }
                */
 
 
-               if (scene->physics_settings.flag & PHYS_GLOBAL_GRAVITY){
+               if (scene->physics_settings.flag & PHYS_GLOBAL_GRAVITY) {
                        copy_v3_v3(gravity, scene->physics_settings.gravity);
                        mul_v3_fl(gravity, sb_grav_force_scale(ob)*sb->effector_weights->global_gravity);
                }
                        copy_v3_v3(gravity, scene->physics_settings.gravity);
                        mul_v3_fl(gravity, sb_grav_force_scale(ob)*sb->effector_weights->global_gravity);
                }
@@ -2543,28 +2552,28 @@ static void softbody_calc_forces(Scene *scene, Object *ob, float forcetime, floa
 
                if (do_deflector) {
                        float defforce[3];
 
                if (do_deflector) {
                        float defforce[3];
-                       do_deflector = sb_detect_aabb_collisionCached(defforce,ob->lay,ob,timenow);
+                       do_deflector = sb_detect_aabb_collisionCached(defforce, ob->lay, ob, timenow);
                }
 
                }
 
-               for(a=sb->totpoint, bp= sb->bpoint; a>0; a--, bp++) {
+               for (a=sb->totpoint, bp= sb->bpoint; a>0; a--, bp++) {
                        /* clear forces  accumulator */
                        /* clear forces  accumulator */
-                       bp->force[0]= bp->force[1]= bp->force[2]= 0.0;
-                       if(nl_flags & NLF_BUILD){
+                       bp->force[0] = bp->force[1] = bp->force[2] = 0.0;
+                       if (nl_flags & NLF_BUILD) {
                                //int ia =3*(sb->totpoint-a);
                                //int op =3*sb->totpoint;
                                /* dF/dV = v */
                                /* jacobioan
                                //int ia =3*(sb->totpoint-a);
                                //int op =3*sb->totpoint;
                                /* dF/dV = v */
                                /* jacobioan
-                               nlMatrixAdd(op+ia,ia,-forcetime);
-                               nlMatrixAdd(op+ia+1,ia+1,-forcetime);
-                               nlMatrixAdd(op+ia+2,ia+2,-forcetime);
+                               nlMatrixAdd(op+ia, ia, -forcetime);
+                               nlMatrixAdd(op+ia+1, ia+1, -forcetime);
+                               nlMatrixAdd(op+ia+2, ia+2, -forcetime);
 
 
-                               nlMatrixAdd(ia,ia,1);
-                               nlMatrixAdd(ia+1,ia+1,1);
-                               nlMatrixAdd(ia+2,ia+2,1);
+                               nlMatrixAdd(ia, ia, 1);
+                               nlMatrixAdd(ia+1, ia+1, 1);
+                               nlMatrixAdd(ia+2, ia+2, 1);
 
 
-                               nlMatrixAdd(op+ia,op+ia,1);
-                               nlMatrixAdd(op+ia+1,op+ia+1,1);
-                               nlMatrixAdd(op+ia+2,op+ia+2,1);
+                               nlMatrixAdd(op+ia, op+ia, 1);
+                               nlMatrixAdd(op+ia+1, op+ia+1, 1);
+                               nlMatrixAdd(op+ia+2, op+ia+2, 1);
                                */
 
 
                                */
 
 
@@ -2572,15 +2581,15 @@ static void softbody_calc_forces(Scene *scene, Object *ob, float forcetime, floa
 
                        /* naive ball self collision */
                        /* needs to be done if goal snaps or not */
 
                        /* naive ball self collision */
                        /* needs to be done if goal snaps or not */
-                       if(do_selfcollision){
+                       if (do_selfcollision) {
                                int attached;
                                BodyPoint   *obp;
                                int attached;
                                BodyPoint   *obp;
-                               int c,b;
-                               float velcenter[3],dvel[3],def[3];
+                               int c, b;
+                               float velcenter[3], dvel[3], def[3];
                                float distance;
                                float compare;
 
                                float distance;
                                float compare;
 
-                               for(c=sb->totpoint, obp= sb->bpoint; c>=a; c--, obp++) {
+                               for (c=sb->totpoint, obp= sb->bpoint; c>=a; c--, obp++) {
 
                                        //if ((bp->octantflag & obp->octantflag) == 0) continue;
 
 
                                        //if ((bp->octantflag & obp->octantflag) == 0) continue;
 
@@ -2588,41 +2597,41 @@ static void softbody_calc_forces(Scene *scene, Object *ob, float forcetime, floa
                                        sub_v3_v3v3(def, bp->pos, obp->pos);
 
                                        /* rather check the AABBoxes before ever calulating the real distance */
                                        sub_v3_v3v3(def, bp->pos, obp->pos);
 
                                        /* rather check the AABBoxes before ever calulating the real distance */
-                                       /* mathematically it is completly nuts, but performace is pretty much (3) times faster */
+                                       /* mathematically it is completly nuts, but performance is pretty much (3) times faster */
                                        if ((ABS(def[0]) > compare) || (ABS(def[1]) > compare) || (ABS(def[2]) > compare)) continue;
 
                                        distance = normalize_v3(def);
                                        if ((ABS(def[0]) > compare) || (ABS(def[1]) > compare) || (ABS(def[2]) > compare)) continue;
 
                                        distance = normalize_v3(def);
-                                       if (distance < compare ){
+                                       if (distance < compare ) {
                                                /* exclude body points attached with a spring */
                                                attached = 0;
                                                /* exclude body points attached with a spring */
                                                attached = 0;
-                                               for(b=obp->nofsprings;b>0;b--){
+                                               for (b=obp->nofsprings;b>0;b--) {
                                                        bs = sb->bspring + obp->springs[b-1];
                                                        bs = sb->bspring + obp->springs[b-1];
-                                                       if (( sb->totpoint-a == bs->v2)  || ( sb->totpoint-a == bs->v1)){
+                                                       if (( sb->totpoint-a == bs->v2) || ( sb->totpoint-a == bs->v1)) {
                                                                attached=1;
                                                                continue;}
                                                }
                                                                attached=1;
                                                                continue;}
                                                }
-                                               if (!attached){
-                                                       float f = tune/(distance) + tune/(compare*compare)*distance - 2.0f*tune/compare ;
+                                               if (!attached) {
+                                                       float f = tune / (distance) + tune / (compare * compare) * distance - 2.0f * tune/compare;
 
                                                        mid_v3_v3v3(velcenter, bp->vec, obp->vec);
 
                                                        mid_v3_v3v3(velcenter, bp->vec, obp->vec);
-                                                       sub_v3_v3v3(dvel,velcenter,bp->vec);
-                                                       mul_v3_fl(dvel,_final_mass(ob,bp));
+                                                       sub_v3_v3v3(dvel, velcenter, bp->vec);
+                                                       mul_v3_fl(dvel, _final_mass(ob, bp));
 
 
-                                                       Vec3PlusStVec(bp->force,f*(1.0f-sb->balldamp),def);
-                                                       Vec3PlusStVec(bp->force,sb->balldamp,dvel);
+                                                       Vec3PlusStVec(bp->force, f*(1.0f-sb->balldamp), def);
+                                                       Vec3PlusStVec(bp->force, sb->balldamp, dvel);
 
 
-                                                       if(nl_flags & NLF_BUILD){
+                                                       if (nl_flags & NLF_BUILD) {
                                                                //int ia =3*(sb->totpoint-a);
                                                                //int ic =3*(sb->totpoint-c);
                                                                //int op =3*sb->totpoint;
                                                                //float mvel = forcetime*sb->nodemass*sb->balldamp;
                                                                //float mpos = forcetime*tune*(1.0f-sb->balldamp);
                                                                /*some quick and dirty entries to the jacobian*/
                                                                //int ia =3*(sb->totpoint-a);
                                                                //int ic =3*(sb->totpoint-c);
                                                                //int op =3*sb->totpoint;
                                                                //float mvel = forcetime*sb->nodemass*sb->balldamp;
                                                                //float mpos = forcetime*tune*(1.0f-sb->balldamp);
                                                                /*some quick and dirty entries to the jacobian*/
-                                                               //dfdx_goal(ia,ia,op,mpos);
-                                                               //dfdv_goal(ia,ia,mvel);
-                                                               /* exploit force(a,b) == -force(b,a) part1/2 */
-                                                               //dfdx_goal(ic,ic,op,mpos);
-                                                               //dfdv_goal(ic,ic,mvel);
+                                                               //dfdx_goal(ia, ia, op, mpos);
+                                                               //dfdv_goal(ia, ia, mvel);
+                                                               /* exploit force(a, b) == -force(b, a) part1/2 */
+                                                               //dfdx_goal(ic, ic, op, mpos);
+                                                               //dfdv_goal(ic, ic, mvel);
 
 
                                                                /*TODO sit down an X-out the true jacobian entries*/
 
 
                                                                /*TODO sit down an X-out the true jacobian entries*/
@@ -2635,12 +2644,12 @@ static void softbody_calc_forces(Scene *scene, Object *ob, float forcetime, floa
                                                                */
                                                        }
 
                                                                */
                                                        }
 
-                                                       /* exploit force(a,b) == -force(b,a) part2/2 */
-                                                       sub_v3_v3v3(dvel,velcenter,obp->vec);
-                                                       mul_v3_fl(dvel,(_final_mass(ob,bp)+_final_mass(ob,obp))/2.0f);
+                                                       /* exploit force(a, b) == -force(b, a) part2/2 */
+                                                       sub_v3_v3v3(dvel, velcenter, obp->vec);
+                                                       mul_v3_fl(dvel, (_final_mass(ob, bp)+_final_mass(ob, obp))/2.0f);
 
 
-                                                       Vec3PlusStVec(obp->force,sb->balldamp,dvel);
-                                                       Vec3PlusStVec(obp->force,-f*(1.0f-sb->balldamp),def);
+                                                       Vec3PlusStVec(obp->force, sb->balldamp, dvel);
+                                                       Vec3PlusStVec(obp->force, -f*(1.0f-sb->balldamp), def);
 
 
                                                }
 
 
                                                }
@@ -2649,41 +2658,41 @@ static void softbody_calc_forces(Scene *scene, Object *ob, float forcetime, floa
                        }
                        /* naive ball self collision done */
 
                        }
                        /* naive ball self collision done */
 
-                       if(_final_goal(ob,bp) < SOFTGOALSNAP){ /* ommit this bp when it snaps */
+                       if (_final_goal(ob, bp) < SOFTGOALSNAP) { /* ommit this bp when it snaps */
                                float auxvect[3];
                                float velgoal[3];
 
                                /* do goal stuff */
                                float auxvect[3];
                                float velgoal[3];
 
                                /* do goal stuff */
-                               if(ob->softflag & OB_SB_GOAL) {
+                               if (ob->softflag & OB_SB_GOAL) {
                                        /* true elastic goal */
                                        /* true elastic goal */
-                                       sub_v3_v3v3(auxvect,bp->pos,bp->origT);
-                                       ks  = 1.0f/(1.0f- _final_goal(ob,bp)*sb->goalspring)-1.0f ;
+                                       sub_v3_v3v3(auxvect, bp->pos, bp->origT);
+                                       ks  = 1.0f / (1.0f- _final_goal(ob, bp) * sb->goalspring) - 1.0f;
                                        bp->force[0]+= -ks*(auxvect[0]);
                                        bp->force[1]+= -ks*(auxvect[1]);
                                        bp->force[2]+= -ks*(auxvect[2]);
 
                                        bp->force[0]+= -ks*(auxvect[0]);
                                        bp->force[1]+= -ks*(auxvect[1]);
                                        bp->force[2]+= -ks*(auxvect[2]);
 
-                                       if(nl_flags & NLF_BUILD){
+                                       if (nl_flags & NLF_BUILD) {
                                                //int ia =3*(sb->totpoint-a);
                                                //int op =3*(sb->totpoint);
                                                /* depending on my pos */
                                                //int ia =3*(sb->totpoint-a);
                                                //int op =3*(sb->totpoint);
                                                /* depending on my pos */
-                                               //dfdx_goal(ia,ia,op,ks*forcetime);
+                                               //dfdx_goal(ia, ia, op, ks*forcetime);
                                        }
 
 
                                        /* calulate damping forces generated by goals*/
                                        }
 
 
                                        /* calulate damping forces generated by goals*/
-                                       sub_v3_v3v3(velgoal,bp->origS, bp->origE);
-                                       kd =  sb->goalfrict * sb_fric_force_scale(ob) ;
-                                       add_v3_v3v3(auxvect,velgoal,bp->vec);
+                                       sub_v3_v3v3(velgoal, bp->origS, bp->origE);
+                                       kd = sb->goalfrict * sb_fric_force_scale(ob);
+                                       add_v3_v3v3(auxvect, velgoal, bp->vec);
 
 
-                                       if (forcetime > 0.0 ) { /* make sure friction does not become rocket motor on time reversal */
+                                       if (forcetime > 0.0f) { /* make sure friction does not become rocket motor on time reversal */
                                                bp->force[0]-= kd * (auxvect[0]);
                                                bp->force[1]-= kd * (auxvect[1]);
                                                bp->force[2]-= kd * (auxvect[2]);
                                                bp->force[0]-= kd * (auxvect[0]);
                                                bp->force[1]-= kd * (auxvect[1]);
                                                bp->force[2]-= kd * (auxvect[2]);
-                                               if(nl_flags & NLF_BUILD){
+                                               if (nl_flags & NLF_BUILD) {
                                                        //int ia =3*(sb->totpoint-a);
                                                        normalize_v3(auxvect);
                                                        /* depending on my vel */
                                                        //int ia =3*(sb->totpoint-a);
                                                        normalize_v3(auxvect);
                                                        /* depending on my vel */
-                                                       //dfdv_goal(ia,ia,kd*forcetime);
+                                                       //dfdv_goal(ia, ia, kd*forcetime);
                                                }
 
                                        }
                                                }
 
                                        }
@@ -2697,20 +2706,20 @@ static void softbody_calc_forces(Scene *scene, Object *ob, float forcetime, floa
 
 
                                /* gravitation */
 
 
                                /* gravitation */
-                               madd_v3_v3fl(bp->force, gravity, _final_mass(ob,bp)); /* individual mass of node here */
+                               madd_v3_v3fl(bp->force, gravity, _final_mass(ob, bp)); /* individual mass of node here */
 
 
                                /* particle field & vortex */
 
 
                                /* particle field & vortex */
-                               if(do_effector) {
+                               if (do_effector) {
                                        EffectedPoint epoint;
                                        EffectedPoint epoint;
-                                       float force[3]= {0.0f, 0.0f, 0.0f};
-                                       float speed[3]= {0.0f, 0.0f, 0.0f};
+                                       float force[3] = {0.0f, 0.0f, 0.0f};
+                                       float speed[3] = {0.0f, 0.0f, 0.0f};
                                        float eval_sb_fric_force_scale = sb_fric_force_scale(ob); /* just for calling function once */
                                        pd_point_from_soft(scene, bp->pos, bp->vec, sb->bpoint-bp, &epoint);
                                        pdDoEffectors(do_effector, NULL, sb->effector_weights, &epoint, force, speed);
 
                                        /* apply forcefield*/
                                        float eval_sb_fric_force_scale = sb_fric_force_scale(ob); /* just for calling function once */
                                        pd_point_from_soft(scene, bp->pos, bp->vec, sb->bpoint-bp, &epoint);
                                        pdDoEffectors(do_effector, NULL, sb->effector_weights, &epoint, force, speed);
 
                                        /* apply forcefield*/
-                                       mul_v3_fl(force,fieldfactor* eval_sb_fric_force_scale);
+                                       mul_v3_fl(force, fieldfactor* eval_sb_fric_force_scale);
                                        add_v3_v3(bp->force, force);
 
                                        /* BP friction in moving media */
                                        add_v3_v3(bp->force, force);
 
                                        /* BP friction in moving media */
@@ -2729,13 +2738,13 @@ static void softbody_calc_forces(Scene *scene, Object *ob, float forcetime, floa
                                        bp->force[1]-= bp->vec[1]*kd;
                                        bp->force[2]-= bp->vec[2]*kd;
                                        /* friction in media done */
                                        bp->force[1]-= bp->vec[1]*kd;
                                        bp->force[2]-= bp->vec[2]*kd;
                                        /* friction in media done */
-                                       if(nl_flags & NLF_BUILD){
+                                       if (nl_flags & NLF_BUILD) {
                                                //int ia =3*(sb->totpoint-a);
                                                /* da/dv =  */
 
                                                //int ia =3*(sb->totpoint-a);
                                                /* da/dv =  */
 
-                                               //                                      nlMatrixAdd(ia,ia,forcetime*kd);
-                                               //                                      nlMatrixAdd(ia+1,ia+1,forcetime*kd);
-                                               //                                      nlMatrixAdd(ia+2,ia+2,forcetime*kd);
+                                               //                                      nlMatrixAdd(ia, ia, forcetime*kd);
+                                               //                                      nlMatrixAdd(ia+1, ia+1, forcetim