rotating a vector using matrices

(cyborgxs) #1

I’m trying tio create a function to rotate a vector by a given quaternion, I have come up with the following:

def RotateVectorQu(refveca, refqu):
	veca	= Mathutils.Vector(refveca)
	qu	= Mathutils.Quaternion(refqu)
	rotmat	= qu.toMatrix()
	vecb	= rotmat * veca 
	return vecb

But it seems to be only rotating around one axis, do I need to rotate each axis seperately? and if so what order? Is there a neater way of doing this maybe with a built in method/function?

(wah_tak) #2

i think, to multiply a matrix with a vector you have to use the function “MatMultVec()”

more here:

(cyborgxs) #3

to be honest I don’t think that is an issue, cos I started using MatMultVec() but got the following error message in the console:

Mathutils.MatMultVec(): Deprecated: use matrix * vec to perform column vector multiplication

which I did, :). I was kinda hoping for a “RotateVec()” function/method in the API, but if there is I can’t see it, :wink:

(RyeDin) #4

Take this with a grain of salt since I am just entering this amazing Blender community… (i.e., I is new)… but it might work better to first convert the quaternion rotation into a Euler rotation. It also might have something to with trying to rotate on the global axes instead of locally??

(Sutabi-old) #5

For eular I simple created this function, some be somewhat the same though:

def pivotDistance(mat,rot,dist):
        #Mat is the object's matrix
        #rot is (x,y,z) degrees to rotate
        #dist is the distance to rotate it
	"Used for finding the 'to' for renderman"
	rad = 180/pi
	(x,y,z) = mat[3][0],mat[3][1],mat[3][2]
	dPivot = [rot[0]*rad-90,rot[1]*rad,rot[2]*rad]
	result = Vector(0,dist,0)
	dx = RotationMatrix(dPivot[0],4,"x")
	dy = RotationMatrix(dPivot[1],4,"y")
	dz = RotationMatrix(dPivot[2],4,"z")
	dMat = dx*dy*dz
	return result*dMat
        #returns the ending vector position

as you can see I need to get for the x,y,z axis and multiply them together.