Skip to content

Commit bfd78ef

Browse files
author
FreehandBlock51
committed
Added cube-on-cube collision detection
1 parent fa4fcac commit bfd78ef

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

include/sim/cube.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,8 @@ bool ccube_is_bbox_inside(ccube_t cube, bbox_t box);
4747
* Checks if a sphere and a cube are overlapping
4848
*/
4949
bool ccube_is_sphere_inside(ccube_t cube, csphere_t sphere);
50+
51+
/**
52+
* Checks if two cubes are overlapping
53+
*/
54+
bool ccube_is_ccube_inside(ccube_t a, ccube_t b);

src/sim/cube.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,18 @@ bool ccube_is_sphere_inside(ccube_t cube, csphere_t sphere) {
6969
ccube_clamp_point_within_cube(cube, &closest_to_cube_center);
7070
return csphere_is_point_inside(sphere, closest_to_cube_center);
7171
}
72+
73+
bool ccube_is_ccube_inside(ccube_t a, ccube_t b) {
74+
// cancel out a's rotation so we can treat this like a cube-bbox check
75+
quaternion_t a_rot_reversed = a.rotation;
76+
quaternion_conjugate(&a_rot_reversed);
77+
vec4_cross_product(&b.rotation, b.rotation, a_rot_reversed);
78+
vec3_rotate_by_quaternion(&b.position, b.position, a.rotation);
79+
a.rotation = VEC4_ZERO;
80+
81+
// Do the cube-bbox check. We inline it here so we don't have to
82+
// initialize an actual bbox
83+
vec3_t a_closest_b = b.position;
84+
ccube_clamp_point_within_cube(a, &a_closest_b);
85+
return ccube_is_point_inside(b, a_closest_b);
86+
}

0 commit comments

Comments
 (0)