Hello all,
I am new to C# and XNA and one of my tasks (for uni) is to make a console application where i have two quaternions (A and B) and i need to compute the dot product of these two quaternions. My problem is, i don't know how to represent a quaternion in C#. I am also assuming once i have assigned the two quaternions to variables i can then go ahead and use the code:
Quaternion.Dot(a, b);
and then print it to the screen. Oh and just as a heads up, i know how to do this mathematically, i know how to get the answer but that isn't the point of this task. I just don't know how to do this in code as opposed to pen and paper, working it out.
Any help is greatly appreciated!
Representing a quaternion in C#?
A quaternion is a 4D vector, so the easiest and most flexible way is to just store it as 4 floats
However, as some quaternion operations are better represented as 3D vector operations, it also makes sense to store it as a 3D vector and a scalar. This also neatly matches the quaternion as a 3D complex number interpretation.
Which representation to pick depends on your usage and whether you already have a 3D vector class (which you probably have if you are using quaternions.)
Note that I'm using struct's. Be sure you understand the difference between classes and structs in C#.
struct Quaternion {
float X;
float Y;
float Z;
float W;
public static float Dot(Quaternion q, Quaternion r) {
// ...
}
};
However, as some quaternion operations are better represented as 3D vector operations, it also makes sense to store it as a 3D vector and a scalar. This also neatly matches the quaternion as a 3D complex number interpretation.
struct Quaternion {
Vector3 xyz;
float w;
public float X { get { return xyz.X; } set { xyz.X = value; } }
public float Y { get { return xyz.Y; } set { xyz.Y = value; } }
public float Z { get { return xyz.Z; } set { xyz.Z = value; } }
}
Which representation to pick depends on your usage and whether you already have a 3D vector class (which you probably have if you are using quaternions.)
Note that I'm using struct's. Be sure you understand the difference between classes and structs in C#.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement