1 #include "anglestransform.qh"
4 #include "../dpdefs/csprogsdefs.qh"
7 #include "../dpdefs/progsdefs.qh"
8 #include "../dpdefs/dpextensions.qh"
11 #ifdef POSITIVE_PITCH_IS_DOWN
12 vector fixedvectoangles(vector a)
19 vector fixedvectoangles2(vector a, vector b)
22 ang = vectoangles2(a, b);
27 void fixedmakevectors(vector a)
29 // a makevectors that actually inverts vectoangles
36 // angles in fixedmakevectors/fixedvectoangles space
37 vector AnglesTransform_Apply(vector transform, vector v)
39 fixedmakevectors(transform);
40 return v_forward * v.x
45 vector AnglesTransform_Multiply(vector t1, vector t2)
47 vector m_forward, m_up;
48 fixedmakevectors(t2); m_forward = v_forward; m_up = v_up;
49 m_forward = AnglesTransform_Apply(t1, m_forward); m_up = AnglesTransform_Apply(t1, m_up);
50 return fixedvectoangles2(m_forward, m_up);
53 vector AnglesTransform_Invert(vector transform)
55 vector i_forward, i_up;
56 fixedmakevectors(transform);
57 // we want angles that turn v_forward into '1 0 0', v_right into '0 1 0' and v_up into '0 0 1'
58 // but these are orthogonal unit vectors!
59 // so to invert, we can simply fixedvectoangles the TRANSPOSED matrix
60 // TODO is this always -transform?
61 i_forward.x = v_forward.x;
62 i_forward.y = -v_right.x;
67 return fixedvectoangles2(i_forward, i_up);
70 vector AnglesTransform_TurnDirectionFR(vector transform)
72 // turn 180 degrees around v_up
73 // changes in-direction to out-direction
74 //fixedmakevectors(transform);
75 //return fixedvectoangles2(-1 * v_forward, 1 * v_up);
76 transform.x = -transform.x;
77 transform.y = 180 + transform.y;
78 transform.z = -transform.z;
85 vector AnglesTransform_TurnDirectionFU(vector transform)
87 // turn 180 degrees around v_up
88 // changes in-direction to out-direction
89 //fixedmakevectors(transform);
90 //return fixedvectoangles2(-1 * v_forward, 1 * v_up);
91 transform.x = -transform.x;
92 transform.y = 180 + transform.y;
93 transform.z = 180 - transform.z;
97 vector AnglesTransform_RightDivide(vector to_transform, vector from_transform)
99 return AnglesTransform_Multiply(to_transform, AnglesTransform_Invert(from_transform));
102 vector AnglesTransform_LeftDivide(vector from_transform, vector to_transform)
104 return AnglesTransform_Multiply(AnglesTransform_Invert(from_transform), to_transform);
107 vector AnglesTransform_Normalize(vector t, float minimize_roll)
110 // first, bring all angles in their range...
111 t.x = t.x - 360 * rint(t.x / 360);
112 t.y = t.y - 360 * rint(t.y / 360);
113 t.z = t.z - 360 * rint(t.z / 360);
115 need_flip = (t.z > 90 || t.z <= -90);
117 need_flip = (t.x > 90 || t.x < -90); // for pitch we prefer to allow exactly -90 degrees for looking straight down
120 if(t.x >= 0) t.x = 180 - t.x; else t.x = -180 - t.x;
121 if(t.y > 0) t.y -= 180; else t.y += 180;
122 if(t.z > 0) t.z -= 180; else t.z += 180;
127 vector AnglesTransform_CancelRoll(vector t)
129 const float epsilon = 30;
133 // forward vector (NOT SO important)
134 // right vector, up vector: screen rotation (MORE important)
135 // choose best match among all pitch-yaw only rotations
137 // FIXME find a better method
139 f = fabs(t.x - (-90)) / epsilon;
148 f = fabs(t.x - 90) / epsilon;
159 #ifdef POSITIVE_PITCH_IS_DOWN
160 vector AnglesTransform_ApplyToAngles(vector transform, vector v)
163 v = AnglesTransform_Multiply(transform, v);
167 vector AnglesTransform_ApplyToVAngles(vector transform, vector v)
169 v = AnglesTransform_Multiply(transform, v);
172 vector AnglesTransform_FromAngles(vector v)
177 vector AnglesTransform_ToAngles(vector v)
182 vector AnglesTransform_FromVAngles(vector v)
186 vector AnglesTransform_ToVAngles(vector v)
191 vector AnglesTransform_ApplyToAngles(vector transform, vector v)
193 v = AnglesTransform_Multiply(transform, v);
196 vector AnglesTransform_ApplyToVAngles(vector transform, vector v)
199 v = AnglesTransform_Multiply(transform, v);
203 vector AnglesTransform_FromAngles(vector v)
207 vector AnglesTransform_ToAngles(vector v)
211 vector AnglesTransform_FromVAngles(vector v)
216 vector AnglesTransform_ToVAngles(vector v)
223 vector AnglesTransform_Multiply_GetPostShift(vector t0, vector st0, vector t1, vector st1)
225 // we want the result of:
226 // t0 * (t1 * p + st1) + st0
227 // t0 * t1 * p + t0 * st1 + st0
228 return st0 + AnglesTransform_Apply(t0, st1);
230 vector AnglesTransform_PrePostShift_GetPostShift(vector sf, vector t, vector st)
232 return st - AnglesTransform_Apply(t, sf);