implement glm_aabb_sphere as GraphicsGems Solid Box - Solid Sphere test

This commit is contained in:
Recep Aslantas
2018-05-30 23:00:18 +03:00
parent c8fc460ba1
commit 6b2b4b4f12
5 changed files with 23 additions and 19 deletions

View File

@@ -229,24 +229,27 @@ glm_aabb_aabb(vec3 box[2], vec3 other[2]) {
/*!
* @brief check if AABB intersects with sphere
*
* @param[in] box bounding box
* @param[in] other other bounding box
* https://github.com/erich666/GraphicsGems/blob/master/gems/BoxSphere.c
* Solid Box - Solid Sphere test.
*
* @param[in] box solid bounding box
* @param[in] s solid sphere
*/
CGLM_INLINE
bool
glm_aabb_sphere(vec3 box[2], vec4 sph) {
vec3 v;
float dist;
glm_aabb_sphere(vec3 box[2], vec4 s) {
float dmin;
int a, b, c;
/* get box closest point to sphere center by clamping */
v[0] = glm_max(box[0][0], glm_min(sph[0], box[1][0]));
v[1] = glm_max(box[0][1], glm_min(sph[1], box[1][1]));
v[2] = glm_max(box[0][2], glm_min(sph[2], box[1][2]));
a = s[0] >= box[0][0];
b = s[1] >= box[0][1];
c = s[2] >= box[0][2];
/* this is the same as glm_sphere_point */
dist = glm_vec_distance(v, sph);
dmin = glm_pow2(s[0] - box[a][0])
+ glm_pow2(s[1] - box[b][1])
+ glm_pow2(s[2] - box[c][2]);
return dist < sph[3];
return dmin <= glm_pow2(s[3]);
}
/*!

View File

@@ -70,7 +70,7 @@ glmc_aabb_contains(vec3 box[2], vec3 other[2]);
CGLM_EXPORT
bool
glmc_aabb_sphere(vec3 box[2], vec4 sph);
glmc_aabb_sphere(vec3 box[2], vec4 s);
#ifdef __cplusplus
}

View File

@@ -98,7 +98,6 @@ glm_make_deg(float *rad) {
CGLM_INLINE
float
glm_pow2(float x) {
return x * x;
}