mirror of
https://github.com/recp/cglm.git
synced 2025-12-24 12:32:40 +00:00
aabb intersect functions
* AABB vs AABB * AABB vs Point * AABB vs Sphere * AABB contains AABB
This commit is contained in:
@@ -212,4 +212,69 @@ glm_aabb_center(vec3 box[2], vec3 dest) {
|
||||
glm_vec_center(box[0], box[1], dest);
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief check if two AABB intersects
|
||||
*
|
||||
* @param[in] box bounding box
|
||||
* @param[in] other other bounding box
|
||||
*/
|
||||
CGLM_INLINE
|
||||
bool
|
||||
glm_aabb_aabb(vec3 box[2], vec3 other[2]) {
|
||||
return (box[0][0] <= other[1][0] && box[1][0] >= other[0][0])
|
||||
&& (box[0][1] <= other[1][1] && box[1][1] >= other[0][1])
|
||||
&& (box[0][2] <= other[1][2] && box[1][2] >= other[0][2]);
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief check if AABB intersects with sphere
|
||||
*
|
||||
* @param[in] box bounding box
|
||||
* @param[in] other other bounding box
|
||||
*/
|
||||
CGLM_INLINE
|
||||
bool
|
||||
glm_aabb_sphere(vec3 box[2], vec4 sph) {
|
||||
vec3 v;
|
||||
float dist;
|
||||
|
||||
/* 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]));
|
||||
|
||||
/* this is the same as glm_sphere_point */
|
||||
dist = glm_vec_distance(v, sph);
|
||||
|
||||
return dist < sph[3];
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief check if point is inside of AABB
|
||||
*
|
||||
* @param[in] box bounding box
|
||||
* @param[in] point point
|
||||
*/
|
||||
CGLM_INLINE
|
||||
bool
|
||||
glm_aabb_point(vec3 box[2], vec3 point) {
|
||||
return (point[0] >= box[0][0] && point[0] <= box[1][0])
|
||||
&& (point[1] >= box[0][1] && point[1] <= box[1][1])
|
||||
&& (point[2] >= box[0][2] && point[2] <= box[1][2]);
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief check if AABB contains other AABB
|
||||
*
|
||||
* @param[in] box bounding box
|
||||
* @param[in] other other bounding box
|
||||
*/
|
||||
CGLM_INLINE
|
||||
bool
|
||||
glm_aabb_contains(vec3 box[2], vec3 other[2]) {
|
||||
return (box[0][0] <= other[0][0] && box[1][0] >= other[1][0])
|
||||
&& (box[0][1] <= other[0][1] && box[1][1] >= other[1][1])
|
||||
&& (box[0][2] <= other[0][2] && box[1][2] >= other[1][2]);
|
||||
}
|
||||
|
||||
#endif /* cglm_box_h */
|
||||
|
||||
@@ -56,6 +56,22 @@ CGLM_EXPORT
|
||||
void
|
||||
glmc_aabb_center(vec3 box[2], vec3 dest);
|
||||
|
||||
CGLM_EXPORT
|
||||
bool
|
||||
glmc_aabb_aabb(vec3 box[2], vec3 other[2]);
|
||||
|
||||
CGLM_EXPORT
|
||||
bool
|
||||
glmc_aabb_point(vec3 box[2], vec3 point);
|
||||
|
||||
CGLM_EXPORT
|
||||
bool
|
||||
glmc_aabb_contains(vec3 box[2], vec3 other[2]);
|
||||
|
||||
CGLM_EXPORT
|
||||
bool
|
||||
glmc_aabb_sphere(vec3 box[2], vec4 sph);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
24
src/box.c
24
src/box.c
@@ -70,3 +70,27 @@ void
|
||||
glmc_aabb_center(vec3 box[2], vec3 dest) {
|
||||
glm_aabb_center(box, dest);
|
||||
}
|
||||
|
||||
CGLM_EXPORT
|
||||
bool
|
||||
glmc_aabb_aabb(vec3 box[2], vec3 other[2]) {
|
||||
return glm_aabb_aabb(box, other);
|
||||
}
|
||||
|
||||
CGLM_EXPORT
|
||||
bool
|
||||
glmc_aabb_point(vec3 box[2], vec3 point) {
|
||||
return glm_aabb_point(box, point);
|
||||
}
|
||||
|
||||
CGLM_EXPORT
|
||||
bool
|
||||
glmc_aabb_contains(vec3 box[2], vec3 other[2]) {
|
||||
return glm_aabb_contains(box, other);
|
||||
}
|
||||
|
||||
CGLM_EXPORT
|
||||
bool
|
||||
glmc_aabb_sphere(vec3 box[2], vec4 sph) {
|
||||
return glm_aabb_sphere(box, sph);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user