Jump to content
  • Advertisement
Sign in to follow this  

Representing a quaternion in C#?

This topic is 2612 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post

Link to post
Share on other sites
A quaternion is a 4D vector, so the easiest and most flexible way is to just store it as 4 floats
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#.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!