Bugfix: Limit Distance constraint could be doing divide-by-zero in a
authorJoshua Leung <aligorith@gmail.com>
Tue, 24 May 2011 12:20:02 +0000 (12:20 +0000)
committerJoshua Leung <aligorith@gmail.com>
Tue, 24 May 2011 12:20:02 +0000 (12:20 +0000)
few cases, especially if the object and target are at the same
location when the constraint is created.

This manisfested as the constrained object disappearing when the
constraint was added, only reappearing after transforming it a bit.

source/blender/blenkernel/intern/constraint.c

index 35e1cae05239a50cc94edfa304ba3f986586d913..fe3286dcc2be1dbd7c841ae831871f0e36cddc75 100644 (file)
@@ -2642,15 +2642,13 @@ static void distlimit_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *
                /* set distance (flag is only set when user demands it) */
                if (data->dist == 0)
                        data->dist= dist;
-                       
-               // FIXME: dist may be 0!
                
                /* check if we're which way to clamp from, and calculate interpolation factor (if needed) */
                if (data->mode == LIMITDIST_OUTSIDE) {
                        /* if inside, then move to surface */
                        if (dist <= data->dist) {
                                clamp_surf= 1;
-                               sfac= data->dist / dist;
+                               if (dist != 0.0f) sfac= data->dist / dist;
                        }
                        /* if soft-distance is enabled, start fading once owner is dist+softdist from the target */
                        else if (data->flag & LIMITDIST_USESOFT) {
@@ -2663,14 +2661,14 @@ static void distlimit_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *
                        /* if outside, then move to surface */
                        if (dist >= data->dist) {
                                clamp_surf= 1;
-                               sfac= data->dist / dist;
+                               if (dist != 0.0f) sfac= data->dist / dist;
                        }
                        /* if soft-distance is enabled, start fading once owner is dist-soft from the target */
                        else if (data->flag & LIMITDIST_USESOFT) {
                                // FIXME: there's a problem with "jumping" when this kicks in
                                if (dist >= (data->dist - data->soft)) {
                                        sfac = (float)( data->soft*(1.0f - expf(-(dist - data->dist)/data->soft)) + data->dist );
-                                       sfac /= dist;
+                                       if (dist != 0.0f) sfac /= dist;
                                        
                                        clamp_surf= 1;
                                }
@@ -2679,7 +2677,7 @@ static void distlimit_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *
                else {
                        if (IS_EQF(dist, data->dist)==0) {
                                clamp_surf= 1;
-                               sfac= data->dist / dist;
+                               if (dist != 0.0f) sfac= data->dist / dist;
                        }
                }