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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
/*
Bullet for XNA Copyright (c) 2003-2007 Vsevolod Klementjev http://www.codeplex.com/xnadevru
Bullet original C++ version Copyright (c) 2003-2007 Erwin Coumans http://bulletphysics.com
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
using System;
using System.Collections.Generic;
using System.Text;
namespace XnaDevRu.BulletX.LinearMath
{
internal class Quaternion : QuadWord
{
public Quaternion() { }
public Quaternion(float x, float y, float z, float w)
: base(x, y, z, w) { }
public Quaternion(Vector3 axis, float angle)
{
SetRotation(axis, angle);
}
public Quaternion(float yaw, float pitch, float roll)
{
SetEuler(yaw, pitch, roll);
}
public void SetRotation(Vector3 axis, float angle)
{
float d = axis.Length();
if (d == 0) throw new DivideByZeroException();
float s = (float)Math.Sin(angle * 0.5f) / d;
X = axis.X * s;
Y = axis.Y * s;
Z = axis.Z * s;
W = (float)Math.Cos(angle * 0.5f);
}
public void SetEuler(float yaw, float pitch, float roll)
{
float halfYaw = yaw * 0.5f;
float halfPitch = pitch * 0.5f;
float halfRoll = roll * 0.5f;
float cosYaw = (float)Math.Cos(halfYaw);
float sinYaw = (float)Math.Sin(halfYaw);
float cosPitch = (float)Math.Cos(halfPitch);
float sinPitch = (float)Math.Sin(halfPitch);
float cosRoll = (float)Math.Cos(halfRoll);
float sinRoll = (float)Math.Sin(halfRoll);
X = cosRoll * sinPitch * cosYaw + sinRoll * cosPitch * sinYaw;
Y = cosRoll * cosPitch * sinYaw - sinRoll * sinPitch * cosYaw;
Z = sinRoll * cosPitch * cosYaw - cosRoll * sinPitch * sinYaw;
W = cosRoll * cosPitch * cosYaw + sinRoll * sinPitch * sinYaw;
}
public float LengthSquared()
{
return Dot(this, this);
}
public float Length()
{
return (float)Math.Sqrt(LengthSquared());
}
public float Angle()
{
return 2f * (float)Math.Acos(W);
}
public static float Angle(Quaternion a, Quaternion b)
{
float s = (float)Math.Sqrt(a.LengthSquared() * b.LengthSquared());
if (s == 0) throw new DivideByZeroException();
return (float)Math.Acos(Dot(a, b) / s);
}
public static Quaternion Farthest(Quaternion a, Quaternion b)
{
Quaternion diff, sum;
diff = a - b;
sum = a + b;
if (Dot(diff, diff) > Dot(sum, sum))
return b;
return -b;
}
public static Quaternion Slerp(Quaternion a, Quaternion b, float c)
{
float theta = Angle(a, b);
if (theta != 0)
{
float d = 1f / (float)Math.Sin(theta);
float s0 = (float)Math.Sin((1f - c) * theta);
float s1 = (float)Math.Sin(c * theta);
return new Quaternion(
(a.X * s0 + b.X * s1) * d,
(a.Y * s0 + b.Y * s1) * d,
(a.Z * s0 + b.Z * s1) * d,
(a.W * s0 + b.W * s1) * d);
}
else
{
return a;
}
}
public static Quaternion Inverse(Quaternion a)
{
return new Quaternion(a.X, a.Y, a.Z, -a.W);
}
public static Quaternion Normalize(Quaternion a)
{
return a / a.Length();
}
public static float Dot(Quaternion a, Quaternion b)
{
return a.X * b.X + a.Y * b.Y + a.Z * b.Z + a.W * b.W;
}
public static Quaternion operator +(Quaternion a, Quaternion b)
{
return new Quaternion(a.X + b.X, a.Y + b.Y, a.Z + b.Z, a.W + b.W);
}
public static Quaternion operator -(Quaternion a, Quaternion b)
{
return new Quaternion(a.X - b.X, a.Y - b.Y, a.Z - b.Z, a.W - b.W);
}
public static Quaternion operator -(Quaternion a)
{
return new Quaternion(-a.X, -a.Y, -a.Z, -a.W);
}
public static Quaternion operator *(Quaternion a, float b)
{
return new Quaternion(a.X * b, a.Y * b, a.Z * b, a.W * b);
}
public static Quaternion operator *(Quaternion a, Quaternion b)
{
return new Quaternion(
a.W * b.X + a.X * b.W + a.Y * b.Z - a.Z * b.Y,
a.W * b.Y + a.Y * b.W + a.Z * b.X - a.X * b.Z,
a.W * b.Z + a.Z * b.W + a.X * b.Y - a.Y * b.X,
a.W * b.W - a.X * b.X - a.Y * b.Y - a.Z * b.Z);
}
public static Quaternion operator *(Quaternion a, Vector3 b)
{
return new Quaternion(
a.W * b.X + a.Y * b.Z - a.Z * b.Y,
a.W * b.Y + a.Z * b.X - a.X * b.Z,
a.W * b.Z + a.X * b.Y - a.Y * b.X,
-a.X * b.X - a.Y * b.Y - a.Z * b.Z);
}
public static Quaternion operator *(Vector3 w, Quaternion q)
{
return new Quaternion(
w.X * q.W + w.Y * q.Z - w.Z * q.Y,
w.Y * q.W + w.Z * q.X - w.X * q.Z,
w.Z * q.W + w.X * q.Y - w.Y * q.X,
-w.X * q.X - w.Y * q.Y - w.Z * q.Z);
}
public static Quaternion operator /(Quaternion a, float b)
{
if (b == 0) throw new DivideByZeroException();
return new Quaternion(a.X / b, a.Y / b, a.Z / b, a.W / b);
}
public static explicit operator MonoXnaCompactMaths.Quaternion(Quaternion a)
{
return new MonoXnaCompactMaths.Quaternion(a.X, a.Y, a.Z, a.W);
}
}
}
|