NightmareX2000's picture

Adding Matrix4.CreateFromYawPitchRoll(...) and Quaternion.CreateFromYawPitchRoll(...)

You can think it as a feature request or useful extension codes. In XNA, Matrix and Quaternion classes has CreateFromYawPitchRoll methods which are useful when creating Arcball-Camera. I don't know if existing methods work the same way or can be used to achieve same effect, I gathered these together:

		public static Matrix4 MatrixFromYawPitchRoll(float yaw, float pitch, float roll)
		{
			return Matrix4.CreateFromQuaternion(QuaternionFromYawPitchRoll(yaw, pitch, roll));
		}
 
		public static Quaternion QuaternionFromYawPitchRoll(float yaw, float pitch, float roll)
		{
			Quaternion result = Quaternion.Identity;
			float num9 = roll * 0.5f;
			float num6 = (float) Math.Sin((double) num9);
			float num5 = (float) Math.Cos((double) num9);
			float num8 = pitch * 0.5f;
			float num4 = (float) Math.Sin((double) num8);
			float num3 = (float) Math.Cos((double) num8);
			float num7 = yaw * 0.5f;
			float num2 = (float) Math.Sin((double) num7);
			float num = (float) Math.Cos((double) num7);
			result.X = ((num * num4) * num5) + ((num2 * num3) * num6);
			result.Y = ((num2 * num3) * num5) - ((num * num4) * num6);
			result.Z = ((num * num3) * num6) - ((num2 * num4) * num5);
			result.W = ((num * num3) * num5) + ((num2 * num4) * num6);
			return result;
		}

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
flopoloco's picture

This is very useful indeed.