working obb collision, test in progress

This commit is contained in:
Lillian Vixe 2024-03-08 02:16:22 -08:00
parent f48aff81b3
commit 0d1690d7b1
2 changed files with 92 additions and 3 deletions

View file

@ -22,7 +22,7 @@ pub struct OctTree<T> {
}
#[derive(Debug, Default, Clone, PartialEq, Eq)]
struct Path {
pub struct Path {
packed: u64,
length: usize,
}
@ -589,6 +589,71 @@ impl OBB {
}
}
//***************
pub fn ray_obb_intersection(
ray_origin: Vec3,
ray_direction_normalized: Vec3,
bounds: &OBB,
) -> Option<Vec3> {
let transform: Transform = Transform::from_translation(bounds.position)
.with_rotation(bounds.orientation)
.with_scale(bounds.half_size * 2.);
let matrix = transform.compute_matrix().inverse();
let ro_trans: Vec3 = matrix.transform_point3(ray_origin);
let rd_trans: Vec3 = matrix.transform_vector3(ray_direction_normalized); // normalizing?
let passthru =
distance_to_ray_bounds_intersection(ro_trans, rd_trans, Vec3::ZERO, Vec3::splat(0.5));
if passthru.is_some() {
let unwrapped = passthru.unwrap();
return Some(ray_origin + (ray_direction_normalized * unwrapped));
} else {
return None;
}
}
pub fn distance_to_ray_bounds_intersection(
ray_origin: Vec3,
ray_direction_normalized: Vec3,
bound_center: Vec3,
bound_half_size: Vec3,
) -> Option<f32> {
let bound_min: Vec3 = bound_center - bound_half_size;
let bound_max: Vec3 = bound_center + bound_half_size;
let tmin: Vec3 = (bound_min - ray_origin) / ray_direction_normalized;
let tmax: Vec3 = (bound_max - ray_origin) / ray_direction_normalized;
let sc = tmin.min(tmax);
let sf = tmin.max(tmax);
let t0: f32 = sc.x.max(sc.y).max(sc.z);
let t1: f32 = sf.x.min(sf.y).min(sf.z);
if !(t0 <= t1 && t1 > 0.0) {
return None;
}
return Some(t0);
}
pub fn ray_aabb_intersection(
ray_origin: Vec3,
ray_direction_normalized: Vec3,
bound_center: Vec3,
bound_half_size: Vec3,
) -> Option<Vec3> {
let passthru = distance_to_ray_bounds_intersection(
ray_origin,
ray_direction_normalized,
bound_center,
bound_half_size,
);
if passthru.is_some() {
return Some(ray_origin + (ray_direction_normalized * passthru.unwrap()));
} else {
return None;
}
}
//***************
pub fn obb_obb_overlap(first: &OBB, second: &OBB) -> bool {
let r_pos = second.position - first.position;
return !(get_seperating_plane_obb(r_pos, first.axis_x(), first, second)
@ -843,7 +908,7 @@ pub fn test_obb_collisions() {
);
let second = OBB::new(
Vec3::new(11., 0., 0.),
Quat::from_rotation_y(45.),
Quat::from_rotation_y(0.785398),
Vec3::splat(5.),
);
assert!(obb_obb_overlap(&first, &second));
@ -862,6 +927,30 @@ pub fn test_obb_collisions() {
assert!(!obb_obb_overlap(&first, &second));
}
}
#[test]
pub fn test_ray_x_bounding_collisions() {
let abounds: OBB = OBB::new(
Vec3::new(10., 0., 0.),
Quat::from_rotation_z(1.5708),
Vec3::splat(5.),
); // rotated 90 degrees (approximate in radians), should match aabb_test at 5x 0yz
let aabb_test: Option<Vec3> =
ray_aabb_intersection(Vec3::ZERO, Vec3::X, abounds.position, abounds.half_size);
let obb_test: Option<Vec3> = ray_obb_intersection(Vec3::ZERO, Vec3::X, &abounds);
assert_eq!(aabb_test.unwrap(), Vec3::new(5., 0., 0.));
assert_eq!(obb_test.unwrap(), Vec3::new(5., 0., 0.));
let rotated_bounds: OBB = OBB::new(
Vec3::new(10., 0., 0.),
Quat::from_rotation_z(0.785398), // 45 degrees rotation in radians (approximate)
Vec3::splat(5.),
);
let rotated_obb_test = ray_obb_intersection(Vec3::ZERO, Vec3::X, &rotated_bounds).unwrap();
println!("{rotated_obb_test}");
panic!();
}
/*
use rand::Rng;
#[test]

View file

@ -194,7 +194,7 @@ pub mod mesh_plugin {
/*
in the send mesh event will be the handle of what we update if it is already present, and the mesh data in an octtree or other voxel format
*/
for ev in events.iter() {
for ev in events.read() {
let mut vertices = Vec::new();
let mut normals = Vec::new();
let mut indices = Vec::new();