Last active
February 19, 2018 15:14
-
-
Save srndpty/d3613eac74d4852490f43345b36cd9a7 to your computer and use it in GitHub Desktop.
Get angle between 2 objects ignoring Y axis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cmath> | |
struct vec3 | |
{ | |
float x, y, z; | |
}; | |
float length(vec3 v) | |
{ | |
return sqrt(v.x * v.x + v.y * v.y + v.z * v.z); | |
} | |
float dot(vec3 a, vec3 b) | |
{ | |
return a.x * b.x + a.y * b.y + a.z * b.z; | |
} | |
float cross(vec3 a, vec3 b) | |
{ | |
vec3 out; | |
out.x = a.y * b.z - b.y * a.z; | |
out.y = a.z * b.x - b.z * a.x; | |
out.z = a.x * b.y - b.x * a.y; | |
return out; | |
} | |
float getRelAngleBetweenXZ(vec3 a, vec3 b) | |
{ | |
a.y = 0.0f; | |
b.y = 0.0f; | |
float cosTheta = dot(a, b) / length(a) * length(b); | |
float rad = acosf(cosTheta); | |
vec3 cp = cross(a, b); | |
if (cp.y > 0) | |
{ | |
rad *= -1; | |
} | |
return rad; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment