Method to create a rotation matrix from a direction vector

Discuss about anything related to the Irrlicht Engine, or read announcements about any significant features or usage changes.
Post Reply
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Method to create a rotation matrix from a direction vector

Post by rogerborg »

I've seen a couple of posts asking how to create a rotation matrix from a direction vector. It's just a matter of using setRotationDegrees(vector.getHorizontalAngle()) but that's not immediately obvious from the API. How about an explicit matrix method that creates a rotation matrix from a direction vector?

Code: Select all

Index: include/matrix4.h
===================================================================
--- include/matrix4.h	(revision 1286)
+++ include/matrix4.h	(working copy)
@@ -124,6 +124,9 @@
 			//! Make a rotation matrix from Euler angles. The 4th row and column are unmodified.
 			void setRotationDegrees( const vector3d<T>& rotation );
 
+			//! Make a rotation matrix from a direction vector (e.g. a plane normal)
+			void createRotationFromVector(const vector3d<T>& vector);
+
 			//! Returns the rotation, as set by setRotation(). This code was orginally written by by Chev.
 			core::vector3d<T> getRotationDegrees() const;
 
@@ -632,6 +635,13 @@
 	}
 
 	template <class T>
+	inline void CMatrix4<T>::createRotationFromVector(const vector3d<T>& vector)
+	{
+		setRotationDegrees(vector.getHorizontalAngle());
+	}
+
+
+	template <class T>
 	inline void CMatrix4<T>::setInverseRotationDegrees( const vector3d<T>& rotation )
 	{
 		setInverseRotationRadians( rotation * core::DEGTORAD );
Index: include/vector3d.h
===================================================================
--- include/vector3d.h	(revision 1286)
+++ include/vector3d.h	(working copy)
@@ -232,7 +232,7 @@
 		/** Thanks to Arras on the Irrlicht forums to add this method.
 		\return A vector representing the rotation in degrees of
 		this vector. The Z component of the vector will always be 0. */
-		vector3d<T> getHorizontalAngle()
+		vector3d<T> getHorizontalAngle() const
 		{
 			vector3d<T> angle;
 
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Post Reply