Using Quaternions

I have been trying to convert euler angles to quaternions and have not been able to do ether. I would like to be able to take a vector and apply a quaternion to it. I have searched google many times but could not find a solution. Please help.
xyzw to a xyz could be difficult though I have never tried someone else might be more help but here is a link that might help http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles
I decided not to use quaternions. Please tell me if there is a reason I shouldn't use this code.
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
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <vector>
#include <math.h>

using namespace std;
struct Vector3 {
	float x;
	float y;
	float z;
	float w;
};

Vector3 rotate(Vector3 pos1, float angle);
int main() {
	//Output: -0.585905, -3.55763, 4
	Vector3 pos1;
	pos1.x = 2;
	pos1.y = 3;
	pos1.z = 4;

	pos1 = rotate(pos1, 9);
	cout<<pos1.x<<'\n';
	cout<<pos1.y<<'\n';
	cout<<pos1.z<<'\n';
	getchar();
	return 0;
}
Vector3 rotate(Vector3 pos1, float angle) {
	//Vector3 pos1;
	pos1.x = 2;
	pos1.y = 3;
	pos1.z = 4;
	Vector3 pos2;
	//float angle = 9;
	pos2.x = pos1.x * cos(angle) + pos1.y * sin(angle);
	pos2.y = pos1.y * cos(angle) - pos1.x * sin(angle);
	pos2.z = pos1.z;
	return pos2;
}
Well...
1. rotate() ignores pos1.
2. rotate() only rotates around the Z axis. You'd normally want to at least be able to rotate around any of the three axes, and ideally around an arbitrary non-orthogonal axis.
Would this correctly work with quaternions?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Vector3 rotate(Vector3 euler, Vector3 point) {
	
	Vector3 returnV;
	Vector3 quat;
	quat.x = euler.x;
	quat.y = euler.y;
	quat.z = euler.z;
	float t = 1.0f - ( quat.x * quat.x ) - ( quat.y * quat.y ) - ( quat.z * quat.z );
	if ( t < 0.0f )
		quat.w = 0.0f;
	else
		quat.w = -sqrtf(t);
	returnV.x = quat.x * point.x * -quat.x;
	returnV.y = quat.y * point.y * -quat.y;
	returnV.z = quat.z * point.z * -quat.z;
	return returnV;
}
What do you mean by "with quaternions"?
I would like to be able to take a vector and apply a quaternion to it.

What do you mean by "apply"?


http://www.cprogramming.com/tutorial/3d/quaternions.html


You had:
1
2
	pos2.x = pos1.x * cos(angle) + pos1.y * sin(angle);
	pos2.y = pos1.y * cos(angle) - pos1.x * sin(angle);

That is actually a multiplication of a 2D vector with a 2x2 matrix:
http://en.wikipedia.org/wiki/Rotation_matrix
However, as you should notice, your matrix had odd values and therefore won't rotate as usual.
Last edited on
What I meant was would that rotate correctly to rotate something.
Would this correctly rotate something? I found the code online:

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <vector>
#include <math.h>

using namespace std;
struct Vector3 {
	float X;
	float Y;
	float Z;
	float W;
};
struct Quat {
	float X;
	float Y;
	float Z;
	float W;
};
Vector3 Transform(Vector3 value, Quat rotation);
Vector3 rotate(Vector3 pos1, float angle);
int main() {
	Vector3 pos1;
	Vector3 euler;
	Quat quat;
	pos1.X = 2;
	pos1.Y = 3;
	pos1.Z = 4;
	quat.X = 5;
	quat.Y = 0;
	quat.Z = 0;
	float t = 1.0f - ( quat.X * quat.X ) - ( quat.Y * quat.Y ) - ( quat.Z * quat.Z );
	if ( t < 0.0f )
		quat.W = 0.0f;
	else
		quat.W = -sqrtf(t);
	pos1 = Transform(pos1, quat);
	cout<<pos1.X<<'\n';
	cout<<pos1.Y<<'\n';
	cout<<pos1.Z<<'\n';
	
	getchar();
	return 0;
}
Vector3 rotate(Vector3 pos1, float angle) {
	Vector3 pos2;
	pos2.X = pos1.X * cos(angle) + pos1.X * sin(angle);
	pos2.Y = pos1.Y * cos(angle) - pos1.Y * sin(angle);
	pos2.Z = pos1.Z;

	
	return pos2;
}
Vector3 Transform(Vector3 value, Quat rotation)
{
    Vector3 vector;
    float num12 = rotation.X + rotation.X;
    float num2 = rotation.Y + rotation.Y;
    float num = rotation.Z + rotation.Z;
    float num11 = rotation.W * num12;
    float num10 = rotation.W * num2;
    float num9 = rotation.W * num;
    float num8 = rotation.X * num12;
    float num7 = rotation.X * num2;
    float num6 = rotation.X * num;
    float num5 = rotation.Y * num2;
    float num4 = rotation.Y * num;
    float num3 = rotation.Z * num;
    float num15 = ((value.X * ((1 - num5) - num3)) + (value.Y * (num7 - num9))) + (value.Z * (num6 + num10));
    float num14 = ((value.X * (num7 + num9)) + (value.Y * ((1 - num8) - num3))) + (value.Z * (num4 - num11));
    float num13 = ((value.X * (num6 - num10)) + (value.Y * (num4 + num11))) + (value.Z * ((1 - num8) - num5));
    vector.X = num15;
    vector.Y = num14;
    vector.Z = num13;
    return vector;
}
Last edited on
No.

The rotate() is definitely wrong: Lets take (1,0,0) and turn it 90 degrees around Z. That should yield (0,1,0). However, you compute new Y as k*Y, and since Y is 0, no k will turn it to 1. Same with X.

Your method of calculating the quat does not create a valid quaternion.

The Transform() looks suspicious too.
Topic archived. No new replies allowed.