# [Bf-blender-cvs] SVN commit: /data/svn/bf-blender [15498] trunk/blender/source/gameengine/ Ketsji: added a factor argument for aligning to vector, this isn' t correct since it does linear interpolation of the vector and renormalizes .

Campbell Barton ideasman42 at gmail.com
Wed Jul 9 11:21:52 CEST 2008

```Revision: 15498
http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=15498
Author:   campbellbarton
Date:     2008-07-09 11:21:52 +0200 (Wed, 09 Jul 2008)

Log Message:
-----------
added a factor argument for aligning to vector, this isn't correct since it does linear interpolation of the vector and renormalizes.
(can be improved to rotate correctly but for our  use ist ok for now, would also be useful to have an argument to clamp the maximum rotation angle to get a constant rotation speed),

This will used to make franky upright when falling from an angle, to track to a surface when hanging onto a ledge and setting the glide pitch.
Without this rotation is instant and jerky.

currently this is done with Mathutils which isnt available in Blender Player.

def do_rotate_up(own):
own.alignAxisToVect([0,0,1], 2, 0.1)

replaces...

def do_rotate_up(own):
up_nor = Vector(0,0,1)
own_mat = Matrix(*own.getOrientation()).transpose()
own_up = up_nor * own_mat
ang = AngleBetweenVecs(own_up, up_nor)
if ang > 0.005:
# Set orientation
cross = CrossVecs(own_up, up_nor)
new_mat = own_mat * RotationMatrix(ang*0.1, 3, 'r', cross)
own.setOrientation(new_mat.transpose())

M    source/gameengine/Ketsji/KX_GameObject.cpp
M    source/gameengine/Ketsji/KX_GameObject.h

Modified Paths:
--------------
trunk/blender/source/gameengine/Ketsji/KX_GameObject.cpp
trunk/blender/source/gameengine/Ketsji/KX_GameObject.h

Modified: trunk/blender/source/gameengine/Ketsji/KX_GameObject.cpp
===================================================================
--- trunk/blender/source/gameengine/Ketsji/KX_GameObject.cpp	2008-07-09 08:24:13 UTC (rev 15497)
+++ trunk/blender/source/gameengine/Ketsji/KX_GameObject.cpp	2008-07-09 09:21:52 UTC (rev 15498)
@@ -572,7 +572,7 @@
m_objectColor = rgbavec;
}

-void KX_GameObject::AlignAxisToVect(const MT_Vector3& dir, int axis)
+void KX_GameObject::AlignAxisToVect(const MT_Vector3& dir, int axis, float fac)
{
MT_Matrix3x3 orimat;
MT_Vector3 vect,ori,z,x,y;
@@ -585,6 +585,11 @@
cout << "alignAxisToVect() Error: Null vector!\n";
return;
}
+
+	if (fac<=0.0) {
+		return;
+	}
+
// normalize
vect /= len;
orimat = GetSGNode()->GetWorldOrientation();
@@ -594,7 +599,14 @@
ori = MT_Vector3(orimat[0][2], orimat[1][2], orimat[2][2]); //pivot axis
if (MT_abs(vect.dot(ori)) > 1.0-3.0*MT_EPSILON) //is the vector paralell to the pivot?
ori = MT_Vector3(orimat[0][1], orimat[1][1], orimat[2][1]); //change the pivot!
-			x = vect;
+			if (fac == 1.0) {
+				x = vect;
+			} else {
+				x = (vect * fac) + ((orimat * MT_Vector3(1.0, 0.0, 0.0)) * (1-fac));
+				len = x.length();
+				if (MT_fuzzyZero(len)) x = vect;
+				else x /= len;
+			}
y = ori.cross(x);
z = x.cross(y);
break;
@@ -602,7 +614,14 @@
ori = MT_Vector3(orimat[0][0], orimat[1][0], orimat[2][0]);
if (MT_abs(vect.dot(ori)) > 1.0-3.0*MT_EPSILON)
ori = MT_Vector3(orimat[0][2], orimat[1][2], orimat[2][2]);
-			y = vect;
+			if (fac == 1.0) {
+				y = vect;
+			} else {
+				y = (vect * fac) + ((orimat * MT_Vector3(0.0, 1.0, 0.0)) * (1-fac));
+				len = y.length();
+				if (MT_fuzzyZero(len)) y = vect;
+				else y /= len;
+			}
z = ori.cross(y);
x = y.cross(z);
break;
@@ -610,7 +629,14 @@
ori = MT_Vector3(orimat[0][1], orimat[1][1], orimat[2][1]);
if (MT_abs(vect.dot(ori)) > 1.0-3.0*MT_EPSILON)
ori = MT_Vector3(orimat[0][0], orimat[1][0], orimat[2][0]);
-			z = vect;
+			if (fac == 1.0) {
+				z = vect;
+			} else {
+				z = (vect * fac) + ((orimat * MT_Vector3(0.0, 0.0, 1.0)) * (1-fac));
+				len = z.length();
+				if (MT_fuzzyZero(len)) z = vect;
+				else z /= len;
+			}
x = ori.cross(z);
y = z.cross(x);
break;
@@ -1386,13 +1412,14 @@
{
PyObject* pyvect;
int axis = 2; //z axis is the default
+	float fac = 1.0;

-	if (PyArg_ParseTuple(args,"O|i",&pyvect,&axis))
+	if (PyArg_ParseTuple(args,"O|if",&pyvect,&axis, &fac))
{
MT_Vector3 vect;
if (PyVecTo(pyvect, vect))
{
-			AlignAxisToVect(vect,axis);
+			AlignAxisToVect(vect,axis,fac);
Py_RETURN_NONE;
}
}

Modified: trunk/blender/source/gameengine/Ketsji/KX_GameObject.h
===================================================================
--- trunk/blender/source/gameengine/Ketsji/KX_GameObject.h	2008-07-09 08:24:13 UTC (rev 15497)
+++ trunk/blender/source/gameengine/Ketsji/KX_GameObject.h	2008-07-09 09:21:52 UTC (rev 15498)
@@ -278,7 +278,8 @@
void
AlignAxisToVect(
const MT_Vector3& vect,
-		int axis = 2
+		int axis = 2,
+		float fac = 1.0
);

/**

```