Quaternion Rounding

Hello there, I need help developing an algorithm which will round a quaternion to the nearest x degrees/radians.
Here is my algorithm so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//GridAngle: the degrees to snap to, such as 90deg, 180deg, 270deg
//CurrentAngle: the angle of which to round
Quaternion RoundQuaternion(const Quaternion& CurrentAngle, float GridAngle)
{
	float Angle = acos(TargetAngle.w) * 2.0f;

	Angle = ((float)(int)((Angle + GridAngle) / GridAngle)) * GridAngle;

	float SinHalf = sin(Angle * 0.5f);

	float CosAngle = cos(GridAngle);
	float HalfCosAngle = CosAngle * 0.5f;
	float SinAngle = sin(GridAngle);
	float HalfSinAngle = SinAngle * 0.5f;

	float _w = cos(Angle * 0.5f);
	float _x = TargetAngle.x / SinHalf;
	float _y = TargetAngle.y / SinHalf;
	float _z = TargetAngle.z / SinHalf;
	_x = (float)(int)((_x + HalfCosAngle) / CosAngle);
	_y = (float)(int)((_y + HalfSinAngle) / SinAngle);
	_z = (float)(int)((_z + HalfCosAngle) / CosAngle);

	_x *= CosAngle;
	_y *= SinAngle;
	_z *= CosAngle;

	Ogre::Quaternion ReturnValue(_w, SinHalf * _x, SinHalf * _y, SinHalf * _z);
	return ReturnValue;
}

At the moment, this algorithm does not snap it to the angle I want. Any help / solutions would be appreciated.
Your function doesn't use the CurrentAngle parameter. Line 5 should probably use it.

Look at round() and trunc() for rounding and truncating floating point numbers.

http://www.cplusplus.com/reference/cmath/round/
http://www.cplusplus.com/reference/cmath/trunc/

Thanks
Topic archived. No new replies allowed.