   [.net] Quaternion Multiplication Help
  Hi! I'm trying to create a random variant vector from another vector given a variant limit angle. ie, I have a vector and want to create a random vector that is offset from the original vector by no more than a certain angle.Luckily, since I'm not very good with the math, I've found that the Orge engine does just that! They have a Vector3 function called "RandomDeviant" that looks like this:inline Vector3 randomDeviant(   const Radian& angle,   const Vector3& up = Vector3::ZERO ) const    {   Vector3 newUp;    if (up == Vector3::ZERO)   {  // Generate an up vector  newUp = this->perpendicular();   }   else   {  newUp = up;   }    // Rotate up vector by random amount around this   Quaternion q;   q.FromAngleAxis( Radian(Math::UnitRandom() * Math::TWO_PI), *this );   newUp = q * newUp;    // Finally rotate this by given angle around randomised up   q.FromAngleAxis( angle, newUp );   return q * (*this);    }So I figured I could use this as a base and implement the same functionality in C# / XNA land. However, I found that I'm unable to multiply Quaternions by Vector3 in C#. It gives me errors on the line: newUp = q * newUp; and return q * (*this );. I'm very unfamiliar with Quaternions, so I was hoping someone might be able to point me in the right direction as to how to port this to C#?Any help would be great! Thanks in advance!PS - I also have posted this in the official XNA forums, but no responses so far... so I'm trying GameDev :)   
  You can't "really" multiply a quaternion by a vector.  But quaternions are used to represent rotation matrices and you can multiply a matrix by a vector.  Multiplying a quaternion by a vector like that is an abuse of operator overloading, IMO.I would say try using Matrix.CreateFromQuaternion(quaternion) * myvector to do the multiplication.Oh, and you can't do *this in C#...   ;  Yea, you're right. The *this pointer won't work in C# land. I should have posted my ported code so far. It doesn't compile because of the Vector3 * Quaternion that I'm trying to do of course:   /// <summary>   /// Get a random variant vector from a particular vector given a   /// specific variant limit angle.   /// </summary>   /// <param name="vector">The vector to get a variant from.</param>   /// <param name="angle">The variant limit angle.</param>   /// <returns></returns>   public static Vector3 RandomVectorVariant( Vector3 vector, float angle )   {  // Thanks to OGRE engine for this logic:  Vector3 vUp = Vector3.Cross( vector, new Vector3( 0, 1, 0 ) );  vUp.Normalize();  float randAngle = ( float )Random.NextDouble() * MathHelper.TwoPi;  Quaternion q = Quaternion.CreateFromAxisAngle( vector, randAngle );  vUp = vUp * q;  q = Quaternion.CreateFromAxisAngle( vUp, MathHelper.ToRadians( angle ) );  return vector * q;   }Maybe there is an easier way of doing what I'm trying to do. Instead of using Quaternions, maybe there is a way to get a random vector deviant using regular old linear algebra? Any ideas?   ;  Ok, I figured it out. I was able to port the OGRE Quatenrion - Vector3 multiplication operator. Everything works great now!Here's the final code I used for anyone who's interested:/// <summary>   /// Get a random variant vector from a particular vector given a   /// specific variant limit angle.   /// </summary>   /// <param name="vector">The vector to get a variant from.</param>   /// <param name="angle">The variant limit angle.</param>   /// <returns></returns>   public static Vector3 RandomVectorVariant( Vector3 vector, float angle )   {  // Thanks to OGRE engine for this logic:  Vector3 workVector = vector;  workVector.Normalize();  Vector3 vUp = Vector3.Cross( vector, new Vector3( 0, 1, 0 ) );  vUp.Normalize();  float randAngle = ( float )Random.NextDouble() * MathHelper.TwoPi;  Quaternion q = Quaternion.CreateFromAxisAngle( workVector, randAngle );  vUp = Vector3QuaternionMultiplication( vUp, q );  q = Quaternion.CreateFromAxisAngle( vUp, MathHelper.ToRadians( angle ) );  return Vector3QuaternionMultiplication( workVector, q );   }   /// <summary>   /// Multiplay a Vector3 by a quatenrion.   /// </summary>   /// <param name="v">The vector.</param>   /// <param name="q">The quaternion.</param>   /// <returns>The resulting vector.</returns>   public static Vector3 Vector3QuaternionMultiplication( Vector3 v, Quaternion q )   {  // nVidia SDK implementation    Vector3 uv, uuv;    Vector3 qvec = new Vector3( q.X, q.Y, q.Z );  uv = Vector3.Cross( qvec, v );    uuv = Vector3.Cross( qvec, uv );    uv *= ( 2.0f * q.W );    uuv *= 2.0f;    return v + uv + uuv;   }