It looks like you're new here. If you want to get involved, click one of these buttons!
/*
* Common.h
*
* Created on: Apr 12, 2013
* Author: epoulsen
*/
#ifndef COMMON_H_
#define COMMON_H_
#define _USE_MATH_DEFINES
#include <math.h>
#include "orx.h"
#define M_TAU 6.283185307
//
// Python prototype
//
// def normalizeradians(rad):
// if rad < 0:
// rad += math.ceil(abs(rad) / math.tau) * math.tau
// else:
// return rad % math.tau
// return rad
inline float normalizeRadians(float rad) {
if (rad < 0)
rad += M_TAU * ceil(fabs(rad) / M_TAU); else
rad = fmod(rad, M_TAU);
return rad;
}
inline float rad2deg(float rad) {
return rad * (180 / M_PI);
}
inline float vec2rad(orxVECTOR & vec) {
return normalizeRadians(atan2(vec.fY, vec.fX));
}
inline void rad2vec(orxVECTOR & out, float rad) {
out.fX = cos(rad);
out.fY = sin(rad);
}
#endif /* COMMON_H_ */
Comments
As fmod is famous to be a (relatively) expensive function, here's an alternative that should work just as well, without any conditional branching (which is an added bonus!
Also, if you use orx, the whole vec2rad/rad2vec conversion already exists as part of the cartesian<->spherical translations:
I'm not that up on coordinate spaces; I didn't realize that those vector functions did what I wanted.