# [C#] Quaternion methods giving slightly different results

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

## Recommended Posts

Why do I get slightly different values for the following two methods using Quaternions? METHOD 1:
            // AddScaledVector (consise XNA method)
orientation += new Quaternion(rotation * duration * 0.5f, 0) * orientation;   // Don't forget the 0.5f, otherwise you will rotate at 2x radians per second

METHOD 2:
            Quaternion p = orientation;


public Quaternion AddScaledVector(Vector3 vector, float scale, Quaternion quaternion)
{
Quaternion q = new Quaternion(
0,
vector.X * scale,
vector.Y * scale,
vector.Z * scale
);

q *= quaternion;

quaternion.W += q.W * 0.5f;
quaternion.X += q.X * 0.5f;
quaternion.Y += q.Y * 0.5f;
quaternion.Z += q.Z * 0.5f;

return quaternion;
}


METHOD 1 returns: {X:0.05399736 Y:0.04901603 Z:0.04899952 W:0.9961349} METHOD 2 returns: {X:0.05322018 Y:0.04909545 Z:0.04899951 W:0.9969998} Thank you.

##### Share on other sites
XNA? the constructor for the quaternion is (x, y, z, w), you have (0, x, y, z) but those values do seem very close so maybe what your doing is the right order.

I guess they could be normalising at some point. I tried normalising your returned Quaternion but the vaues are still off.

I'm having trouble seeing whats going wrong here. Out of curiosity, why are you trying to replace a perfectly readable line of code with a pretty ugly method that doesn't work as well?

If there's no other reason than the method name (readability) then just stick that Method 1 line intot he method 2 code.

##### Share on other sites
The reason why I am using the second method is because that is the exact method used in the Cyclone physics engine, which I am porting over to C#.

I still have concerns over the Quaternion methods found in the code though and I am slightly confused by the following lines in the source code to the Cyclone physics engine:

        Quaternion q;        body->getOrientation(&q);        q.addScaledVector(rotationDirection, rotationAmount * ((real)0.5));        body->setOrientation(q);

As I understand the q.addScaledVector method is as follows:

        void addScaledVector(const Vector3& vector, real scale)        {            Quaternion q(0,                vector.x * scale,                vector.y * scale,                vector.z * scale);            q *= *this;            r += q.r * ((real)0.5);            i += q.i * ((real)0.5);            j += q.j * ((real)0.5);            k += q.k * ((real)0.5);        }

So I was wondering why there is another 0.5 being fed into the method and why it would be necessary, or whether it is a typo.

Also, considering the following line:

orientation.addScaledVector(rotation, duration);

can be coded in XNA as:
orientation += new Quaternion(rotation * duration * 0.5f, 0) * orientation;

then can the line:
q.addScaledVector(rotationDirection, rotationAmount * ((real)0.5));

be coded as:
orientation += new Quaternion(rotation * duration * 0.5f * 0.5f, 0) * orientation;

OR

orientation += new Quaternion(rotation * duration * 0.25f, 0) * orientation;

I guess in all cases in don't need to normalize the Quaternion either?

• 18
• 11
• 17
• 9
• 51
• ### Forum Statistics

• Total Topics
631397
• Total Posts
2999795
×