[Bf-blender-cvs] [5eb27c071ae] shrinkwrap-features: Resolve the opposite vector ambiguity in Damped Track constraint.

Alexander Gavrilov noreply at git.blender.org
Sat Jul 7 22:21:29 CEST 2018


Commit: 5eb27c071aed9f66255faf8813929bb1b91ac7f1
Author: Alexander Gavrilov
Date:   Sat Jul 7 23:21:20 2018 +0300
Branches: shrinkwrap-features
https://developer.blender.org/rB5eb27c071aed9f66255faf8813929bb1b91ac7f1

Resolve the opposite vector ambiguity in Damped Track constraint.

Damped Track by specification attempts to arrive at the desired
direction via the shortest rotation. However with opposite vectors
there are infinitely many valid 180 degree rotations. Currently
it gives up and does nothing.

I think that it would be more reasonable to resolve the ambiguity
arbitrarily. The effect would be not much different from a floating
point precision error just happening to nudge the original vector
in a random direction, but Damped Track won't have a weird dead zone.

===================================================================

M	source/blender/blenkernel/intern/constraint.c

===================================================================

diff --git a/source/blender/blenkernel/intern/constraint.c b/source/blender/blenkernel/intern/constraint.c
index 8d832158cf7..d7218aebe3a 100644
--- a/source/blender/blenkernel/intern/constraint.c
+++ b/source/blender/blenkernel/intern/constraint.c
@@ -3791,10 +3791,35 @@ static void damptrack_do_transform(float matrix[4][4], float tarvec[3], int trac
 		rangle = dot_v3v3(obvec, tarvec);
 		rangle = acosf(max_ff(-1.0f, min_ff(1.0f, rangle)));
 
+		if (rangle == 0.0f) {
+			/* nothing to do */
+			return;
+		}
+
 		/* construct rotation matrix from the axis-angle rotation found above
 		 *	- this call takes care to make sure that the axis provided is a unit vector first
 		 */
-		axis_angle_to_mat3(rmat, raxis, rangle);
+		if (normalize_v3_v3(raxis, raxis) == 0.0f) {
+			/* if dot product is nonzero, while cross is zero, we have two opposite vectors!
+			 *  - this is an ambiguity in the math that needs to be resolved arbitrarily,
+			 *    or there will be a case where damped track strangely does nothing
+			 *  - to do that, rotate through the Z axis if possible, otherwise use X axis
+			 */
+			rangle = M_PI;
+			zero_v3(obvec);
+
+			if (fabsf(tarvec[2]) >= 1.0f - FLT_EPSILON)
+				obvec[0] = 1.0f;
+			else
+				obvec[2] = 1.0f;
+
+			cross_v3_v3v3(raxis, obvec, tarvec);
+
+			if (normalize_v3_v3(raxis, raxis) == 0.0f)
+				return;
+		}
+
+		axis_angle_normalized_to_mat3(rmat, raxis, rangle);
 
 		/* rotate the owner in the way defined by this rotation matrix, then reapply the location since
 		 * we may have destroyed that in the process of multiplying the matrix



More information about the Bf-blender-cvs mailing list