1 | |
---|
2 | |
---|
3 | /* |
---|
4 | orxonox - the future of 3D-vertical-scrollers |
---|
5 | |
---|
6 | Copyright (C) 2004 orx |
---|
7 | |
---|
8 | This program is free software; you can redistribute it and/or modify |
---|
9 | it under the terms of the GNU General Public License as published by |
---|
10 | the Free Software Foundation; either version 2, or (at your option) |
---|
11 | any later version. |
---|
12 | |
---|
13 | ### File Specific: |
---|
14 | main-programmer: Christian Meyer |
---|
15 | co-programmer: ... |
---|
16 | */ |
---|
17 | |
---|
18 | |
---|
19 | #include "rotation.h" |
---|
20 | |
---|
21 | |
---|
22 | using namespace std; |
---|
23 | |
---|
24 | /** |
---|
25 | \brief create a rotation from a vector |
---|
26 | \param v: a vector |
---|
27 | */ |
---|
28 | Rotation::Rotation (const Vector& v) |
---|
29 | { |
---|
30 | Vector x = Vector( 1, 0, 0); |
---|
31 | Vector axis = x.cross( v); |
---|
32 | axis.normalize(); |
---|
33 | float angle = angle_rad( x, v); |
---|
34 | float ca = cos(angle); |
---|
35 | float sa = sin(angle); |
---|
36 | m[0] = 1.0f+(1.0f-ca)*(axis.x*axis.x-1.0f); |
---|
37 | m[1] = -axis.z*sa+(1.0f-ca)*axis.x*axis.y; |
---|
38 | m[2] = axis.y*sa+(1.0f-ca)*axis.x*axis.z; |
---|
39 | m[3] = axis.z*sa+(1.0f-ca)*axis.x*axis.y; |
---|
40 | m[4] = 1.0f+(1.0f-ca)*(axis.y*axis.y-1.0f); |
---|
41 | m[5] = -axis.x*sa+(1.0f-ca)*axis.y*axis.z; |
---|
42 | m[6] = -axis.y*sa+(1.0f-ca)*axis.x*axis.z; |
---|
43 | m[7] = axis.x*sa+(1.0f-ca)*axis.y*axis.z; |
---|
44 | m[8] = 1.0f+(1.0f-ca)*(axis.z*axis.z-1.0f); |
---|
45 | } |
---|
46 | |
---|
47 | /** |
---|
48 | \brief creates a rotation from an axis and an angle (radians!) |
---|
49 | \param axis: the rotational axis |
---|
50 | \param angle: the angle in radians |
---|
51 | */ |
---|
52 | Rotation::Rotation (const Vector& axis, float angle) |
---|
53 | { |
---|
54 | float ca, sa; |
---|
55 | ca = cos(angle); |
---|
56 | sa = sin(angle); |
---|
57 | m[0] = 1.0f+(1.0f-ca)*(axis.x*axis.x-1.0f); |
---|
58 | m[1] = -axis.z*sa+(1.0f-ca)*axis.x*axis.y; |
---|
59 | m[2] = axis.y*sa+(1.0f-ca)*axis.x*axis.z; |
---|
60 | m[3] = axis.z*sa+(1.0f-ca)*axis.x*axis.y; |
---|
61 | m[4] = 1.0f+(1.0f-ca)*(axis.y*axis.y-1.0f); |
---|
62 | m[5] = -axis.x*sa+(1.0f-ca)*axis.y*axis.z; |
---|
63 | m[6] = -axis.y*sa+(1.0f-ca)*axis.x*axis.z; |
---|
64 | m[7] = axis.x*sa+(1.0f-ca)*axis.y*axis.z; |
---|
65 | m[8] = 1.0f+(1.0f-ca)*(axis.z*axis.z-1.0f); |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | \brief creates a rotation from euler angles (pitch/yaw/roll) |
---|
70 | \param pitch: rotation around z (in radians) |
---|
71 | \param yaw: rotation around y (in radians) |
---|
72 | \param roll: rotation around x (in radians) |
---|
73 | */ |
---|
74 | Rotation::Rotation ( float pitch, float yaw, float roll) |
---|
75 | { |
---|
76 | float cy, sy, cr, sr, cp, sp; |
---|
77 | cy = cos(yaw); |
---|
78 | sy = sin(yaw); |
---|
79 | cr = cos(roll); |
---|
80 | sr = sin(roll); |
---|
81 | cp = cos(pitch); |
---|
82 | sp = sin(pitch); |
---|
83 | m[0] = cy*cr; |
---|
84 | m[1] = -cy*sr; |
---|
85 | m[2] = sy; |
---|
86 | m[3] = cp*sr+sp*sy*cr; |
---|
87 | m[4] = cp*cr-sp*sr*sy; |
---|
88 | m[5] = -sp*cy; |
---|
89 | m[6] = sp*sr-cp*sy*cr; |
---|
90 | m[7] = sp*cr+cp*sy*sr; |
---|
91 | m[8] = cp*cy; |
---|
92 | } |
---|
93 | |
---|
94 | /** |
---|
95 | \brief creates a nullrotation |
---|
96 | */ |
---|
97 | Rotation::Rotation () |
---|
98 | { |
---|
99 | m[0] = 1.0f; |
---|
100 | m[1] = 0.0f; |
---|
101 | m[2] = 0.0f; |
---|
102 | m[3] = 0.0f; |
---|
103 | m[4] = 1.0f; |
---|
104 | m[5] = 0.0f; |
---|
105 | m[6] = 0.0f; |
---|
106 | m[7] = 0.0f; |
---|
107 | m[8] = 1.0f; |
---|
108 | } |
---|
109 | |
---|
110 | /** |
---|
111 | \brief rotates the vector by the given rotation |
---|
112 | \param v: a vector |
---|
113 | \param r: a rotation |
---|
114 | */ |
---|
115 | Vector rotate_vector( const Vector& v, const Rotation& r) |
---|
116 | { |
---|
117 | Vector t; |
---|
118 | |
---|
119 | t.x = v.x * r.m[0] + v.y * r.m[1] + v.z * r.m[2]; |
---|
120 | t.y = v.x * r.m[3] + v.y * r.m[4] + v.z * r.m[5]; |
---|
121 | t.z = v.x * r.m[6] + v.y * r.m[7] + v.z * r.m[8]; |
---|
122 | |
---|
123 | return t; |
---|
124 | } |
---|