[4578] | 1 | /* |
---|
[2043] | 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
[4578] | 12 | main-programmer: Christian Meyer |
---|
[6617] | 13 | co-programmer: ... |
---|
[2043] | 14 | */ |
---|
| 15 | |
---|
[3590] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_MATH |
---|
[2043] | 17 | |
---|
[6617] | 18 | #include "rotation_OBSOLETE.h" |
---|
[5662] | 19 | #ifdef DEBUG |
---|
[5672] | 20 | #include "debug.h" |
---|
[5662] | 21 | #else |
---|
[5672] | 22 | #include <stdio.h> |
---|
| 23 | #define PRINT(x) printf |
---|
[5662] | 24 | #endif |
---|
[2043] | 25 | |
---|
| 26 | using namespace std; |
---|
| 27 | |
---|
| 28 | /** |
---|
[4836] | 29 | * create a rotation from a vector |
---|
| 30 | * @param v: a vector |
---|
[2043] | 31 | */ |
---|
| 32 | Rotation::Rotation (const Vector& v) |
---|
| 33 | { |
---|
| 34 | Vector x = Vector( 1, 0, 0); |
---|
| 35 | Vector axis = x.cross( v); |
---|
| 36 | axis.normalize(); |
---|
[3234] | 37 | float angle = angleRad( x, v); |
---|
[2043] | 38 | float ca = cos(angle); |
---|
| 39 | float sa = sin(angle); |
---|
| 40 | m[0] = 1.0f+(1.0f-ca)*(axis.x*axis.x-1.0f); |
---|
| 41 | m[1] = -axis.z*sa+(1.0f-ca)*axis.x*axis.y; |
---|
| 42 | m[2] = axis.y*sa+(1.0f-ca)*axis.x*axis.z; |
---|
| 43 | m[3] = axis.z*sa+(1.0f-ca)*axis.x*axis.y; |
---|
| 44 | m[4] = 1.0f+(1.0f-ca)*(axis.y*axis.y-1.0f); |
---|
| 45 | m[5] = -axis.x*sa+(1.0f-ca)*axis.y*axis.z; |
---|
| 46 | m[6] = -axis.y*sa+(1.0f-ca)*axis.x*axis.z; |
---|
| 47 | m[7] = axis.x*sa+(1.0f-ca)*axis.y*axis.z; |
---|
| 48 | m[8] = 1.0f+(1.0f-ca)*(axis.z*axis.z-1.0f); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | /** |
---|
[4836] | 52 | * creates a rotation from an axis and an angle (radians!) |
---|
| 53 | * @param axis: the rotational axis |
---|
| 54 | * @param angle: the angle in radians |
---|
[2043] | 55 | */ |
---|
| 56 | Rotation::Rotation (const Vector& axis, float angle) |
---|
| 57 | { |
---|
| 58 | float ca, sa; |
---|
| 59 | ca = cos(angle); |
---|
| 60 | sa = sin(angle); |
---|
| 61 | m[0] = 1.0f+(1.0f-ca)*(axis.x*axis.x-1.0f); |
---|
| 62 | m[1] = -axis.z*sa+(1.0f-ca)*axis.x*axis.y; |
---|
| 63 | m[2] = axis.y*sa+(1.0f-ca)*axis.x*axis.z; |
---|
| 64 | m[3] = axis.z*sa+(1.0f-ca)*axis.x*axis.y; |
---|
| 65 | m[4] = 1.0f+(1.0f-ca)*(axis.y*axis.y-1.0f); |
---|
| 66 | m[5] = -axis.x*sa+(1.0f-ca)*axis.y*axis.z; |
---|
| 67 | m[6] = -axis.y*sa+(1.0f-ca)*axis.x*axis.z; |
---|
| 68 | m[7] = axis.x*sa+(1.0f-ca)*axis.y*axis.z; |
---|
| 69 | m[8] = 1.0f+(1.0f-ca)*(axis.z*axis.z-1.0f); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | /** |
---|
[4836] | 73 | * creates a rotation from euler angles (pitch/yaw/roll) |
---|
| 74 | * @param pitch: rotation around z (in radians) |
---|
| 75 | * @param yaw: rotation around y (in radians) |
---|
| 76 | * @param roll: rotation around x (in radians) |
---|
[2043] | 77 | */ |
---|
| 78 | Rotation::Rotation ( float pitch, float yaw, float roll) |
---|
| 79 | { |
---|
| 80 | float cy, sy, cr, sr, cp, sp; |
---|
| 81 | cy = cos(yaw); |
---|
| 82 | sy = sin(yaw); |
---|
| 83 | cr = cos(roll); |
---|
| 84 | sr = sin(roll); |
---|
| 85 | cp = cos(pitch); |
---|
| 86 | sp = sin(pitch); |
---|
| 87 | m[0] = cy*cr; |
---|
| 88 | m[1] = -cy*sr; |
---|
| 89 | m[2] = sy; |
---|
| 90 | m[3] = cp*sr+sp*sy*cr; |
---|
| 91 | m[4] = cp*cr-sp*sr*sy; |
---|
| 92 | m[5] = -sp*cy; |
---|
| 93 | m[6] = sp*sr-cp*sy*cr; |
---|
| 94 | m[7] = sp*cr+cp*sy*sr; |
---|
| 95 | m[8] = cp*cy; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | /** |
---|
[4836] | 99 | * creates a nullrotation (an identity rotation) |
---|
[2043] | 100 | */ |
---|
| 101 | Rotation::Rotation () |
---|
| 102 | { |
---|
| 103 | m[0] = 1.0f; |
---|
| 104 | m[1] = 0.0f; |
---|
| 105 | m[2] = 0.0f; |
---|
| 106 | m[3] = 0.0f; |
---|
| 107 | m[4] = 1.0f; |
---|
| 108 | m[5] = 0.0f; |
---|
| 109 | m[6] = 0.0f; |
---|
| 110 | m[7] = 0.0f; |
---|
| 111 | m[8] = 1.0f; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | /** |
---|
[4836] | 115 | * fills the specified buffer with a 4x4 glmatrix |
---|
| 116 | * @param buffer: Pointer to an array of 16 floats |
---|
[4578] | 117 | |
---|
[2190] | 118 | Use this to get the rotation in a gl-compatible format |
---|
| 119 | */ |
---|
| 120 | void Rotation::glmatrix (float* buffer) |
---|
| 121 | { |
---|
[4578] | 122 | buffer[0] = m[0]; |
---|
| 123 | buffer[1] = m[3]; |
---|
| 124 | buffer[2] = m[6]; |
---|
| 125 | buffer[3] = m[0]; |
---|
| 126 | buffer[4] = m[1]; |
---|
| 127 | buffer[5] = m[4]; |
---|
| 128 | buffer[6] = m[7]; |
---|
| 129 | buffer[7] = m[0]; |
---|
| 130 | buffer[8] = m[2]; |
---|
| 131 | buffer[9] = m[5]; |
---|
| 132 | buffer[10] = m[8]; |
---|
| 133 | buffer[11] = m[0]; |
---|
| 134 | buffer[12] = m[0]; |
---|
| 135 | buffer[13] = m[0]; |
---|
| 136 | buffer[14] = m[0]; |
---|
| 137 | buffer[15] = m[1]; |
---|
[2190] | 138 | } |
---|
| 139 | |
---|
| 140 | /** |
---|
[4836] | 141 | * multiplies two rotational matrices |
---|
| 142 | * @param r: another Rotation |
---|
| 143 | * @return the matrix product of the Rotations |
---|
[4578] | 144 | |
---|
[2190] | 145 | Use this to rotate one rotation by another |
---|
| 146 | */ |
---|
| 147 | Rotation Rotation::operator* (const Rotation& r) |
---|
| 148 | { |
---|
[4578] | 149 | Rotation p; |
---|
[2190] | 150 | |
---|
[4578] | 151 | p.m[0] = m[0]*r.m[0] + m[1]*r.m[3] + m[2]*r.m[6]; |
---|
| 152 | p.m[1] = m[0]*r.m[1] + m[1]*r.m[4] + m[2]*r.m[7]; |
---|
| 153 | p.m[2] = m[0]*r.m[2] + m[1]*r.m[5] + m[2]*r.m[8]; |
---|
| 154 | |
---|
| 155 | p.m[3] = m[3]*r.m[0] + m[4]*r.m[3] + m[5]*r.m[6]; |
---|
| 156 | p.m[4] = m[3]*r.m[1] + m[4]*r.m[4] + m[5]*r.m[7]; |
---|
| 157 | p.m[5] = m[3]*r.m[2] + m[4]*r.m[5] + m[5]*r.m[8]; |
---|
| 158 | |
---|
| 159 | p.m[6] = m[6]*r.m[0] + m[7]*r.m[3] + m[8]*r.m[6]; |
---|
| 160 | p.m[7] = m[6]*r.m[1] + m[7]*r.m[4] + m[8]*r.m[7]; |
---|
| 161 | p.m[8] = m[6]*r.m[2] + m[7]*r.m[5] + m[8]*r.m[8]; |
---|
| 162 | |
---|
| 163 | return p; |
---|
[2190] | 164 | } |
---|
| 165 | |
---|
| 166 | |
---|
| 167 | /** |
---|
[4836] | 168 | * rotates the vector by the given rotation |
---|
| 169 | * @param v: a vector |
---|
| 170 | * @param r: a rotation |
---|
| 171 | * @return the rotated vector |
---|
[2043] | 172 | */ |
---|
[3228] | 173 | Vector rotateVector( const Vector& v, const Rotation& r) |
---|
[2043] | 174 | { |
---|
| 175 | Vector t; |
---|
[4578] | 176 | |
---|
[2043] | 177 | t.x = v.x * r.m[0] + v.y * r.m[1] + v.z * r.m[2]; |
---|
| 178 | t.y = v.x * r.m[3] + v.y * r.m[4] + v.z * r.m[5]; |
---|
| 179 | t.z = v.x * r.m[6] + v.y * r.m[7] + v.z * r.m[8]; |
---|
| 180 | |
---|
| 181 | return t; |
---|
| 182 | } |
---|