qbsp mathlib: add helper for getting angle between vectors

This commit is contained in:
Eric Wasylishen 2017-03-22 01:07:32 -06:00
parent 8e1b7effca
commit 1f3d38a611
2 changed files with 25 additions and 0 deletions

View File

@ -139,3 +139,26 @@ VectorScale(const vec3_t v, const vec_t scale, vec3_t out)
out[1] = v[1] * scale;
out[2] = v[2] * scale;
}
/// Returns degrees of clockwise rotation from start to end, assuming `normal` is pointing towards the viewer
float SignedDegreesBetweenUnitVectors(const vec3_t start, const vec3_t end, const vec3_t normal)
{
const float cosangle = qmax(-1.0, qmin(1.0, DotProduct(start, end)));
const float unsigned_degrees = acos(cosangle) * (360.0 / (2.0 * Q_PI));
if (unsigned_degrees < ANGLEEPSILON)
return 0;
// get a normal for the rotation plane using the right-hand rule
vec3_t rotationNormal;
CrossProduct(start, end, rotationNormal);
VectorNormalize(rotationNormal);
const float normalsCosAngle = DotProduct(rotationNormal, normal);
if (normalsCosAngle >= 0) {
// counterclockwise rotation
return -unsigned_degrees;
}
// clockwise rotation
return unsigned_degrees;
}

View File

@ -235,6 +235,8 @@ vec_t VectorNormalize(vec3_t v);
void VectorInverse(vec3_t v);
void VectorScale(const vec3_t v, const vec_t scale, vec3_t out);
float SignedDegreesBetweenUnitVectors(const vec3_t start, const vec3_t end, const vec3_t normal);
#ifdef __GNUC__
/* min and max macros with type checking */
#define qmax(a,b) ({ \