AlignToVector()

BlitzMax Forums/BlitzMax Programming/AlignToVector()

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

If I just normalize the matrix at the beginning, the scale values get added back in when the rotation is performed.

This is awesome! You can align any axis, control the angle along the aligned axis, and use a rate:
	Method AlignToVector(dir:TVec3,axis=3,rate#=1.0,roll_value#=0.0)
		Local pitch#,yaw#,roll#,dist#
		Local i:TVec3,j:TVec3,k:TVec3
		Local mscale:TVec3=New TVec3
		dist#=Sqr(dir.x#*dir.x#+dir.z#*dir.z#)
		pitch#=ATan2(-dir.y#,dist#)
		yaw#=ATan2(-dir.x#,dir.z#)
		roll#=roll_value
		mat=mat.normalize()
		i=mat.i()
		j=mat.j()
		k=mat.k()
		If rate=1.0
			SetRotation vec3(pitch,yaw,roll),1
		Else
			Select axis
				Case 1
					mat.kx=i.x
					mat.ky=i.y
					mat.kz=i.z
					mat.ix=k.x
					mat.iy=k.y
					mat.iz=k.z
					mat.jx:*-1.0
					mat.jy:*-1.0
					mat.jz:*-1.0
				Case 2
					mat.kx=j.x
					mat.ky=j.y
					mat.kz=j.z
					mat.jx=k.x
					mat.jy=k.y
					mat.jz=k.z
					mat.ix:*-1.0
					mat.iy:*-1.0
					mat.iz:*-1.0
			EndSelect
			quaternion=mat.rotation().slerp(vec3(pitch,yaw,roll).toquat(),rate)
			SetRotation quaternion.toeuler(),1
		EndIf
		i=mat.i()
		j=mat.j()
		k=mat.k()
		Select axis
			Case 1
				mat.kx=i.x
				mat.ky=i.y
				mat.kz=i.z
				mat.ix=k.x
				mat.iy=k.y
				mat.iz=k.z
				mat.jx:*-1.0
				mat.jy:*-1.0
				mat.jz:*-1.0
			Case 2
				mat.kx=j.x
				mat.ky=j.y
				mat.kz=j.z
				mat.jx=k.x
				mat.jy=k.y
				mat.jz=k.z
				mat.ix:*-1.0
				mat.iy:*-1.0
				mat.iz:*-1.0
			Case 3
				Return
		EndSelect
		'Retrieve the local rotation and fill in the values:
		quaternion=TFormQuat(mat.rotation(),Null,parent)
		rotation=quaternion.toeuler()
	EndMethod