seemingly working unoptimized ray/x/octree:

This commit is contained in:
Lillian Vixe 2024-03-27 06:10:10 -07:00
parent c316533fea
commit a7df66d2df

View file

@ -454,24 +454,6 @@ pub fn obb_to_global_sphere(local_pos: Vec3, size: usize, trans: &OBB) -> (Vec3,
.transform_point(local_pos);
return (position, bound_radius);
}
#[test]
pub fn test_sphere_internal_transform() {
let pos = Vec3::new(0., 10., 0.);
let size = 10;
let obby = OBB::new(
Vec3::new(0., 10., 0.),
Quat::from_rotation_x(f32::to_radians(90.)),
Vec3::new(1., 1., 1.),
);
let (x, y) = obb_to_global_sphere(pos, size, &obby);
println!("{x}, {y:.3}");
let z = x.y;
println!("{z:.3}");
let x = x.round();
let y = y.round();
assert_eq!(x, Vec3::new(0., 10., 20.));
assert_eq!(y, 15.);
}
pub fn obb_obb_overlap(first: &OBB, second: &OBB) -> bool {
let r_pos = second.position - first.position;
@ -578,17 +560,18 @@ fn ray_octtree_visitor<T: Clone>(
}
} else if node.is_leaf() {
let mut node_bounds = transform.clone();
let position = transform
let tposition = transform
.as_transform()
.compute_matrix()
.transform_point3(position);
node_bounds.position = position;
node_bounds.position = tposition;
node_bounds.half_size = node_bounds.half_size; // * size as f32; size is 1 for leaves
let x = ray_obb_intersection(ray_origin, ray_normal, &node_bounds);
if x.is_some() {
dbg!(position);
return Some((x.unwrap(), parent_path));
let mut x = x.unwrap();
x.hitpoint.local += position;
return Some((x, parent_path));
}
}
//v nothing could be found, failing out so other searches can continue
@ -620,7 +603,7 @@ pub fn test_ray_x_octtree() {
let col = collision.unwrap();
let hitloc = col.hit_data.hitpoint.local.clone();
let hitnorm = col.hit_data.normal.local.clone();
let hitvox = hitloc - (hitnorm * 1.05);
let hitvox = hitloc - (hitnorm * 0.5);
dbg!(hitvox.round());
}
@ -643,12 +626,44 @@ pub fn test_ray_x_octtree() {
let col = collision.unwrap();
let hitloc = col.hit_data.hitpoint.local.clone();
let hitnorm = col.hit_data.normal.local.clone();
let hitvox = hitloc - (hitnorm * 1.05);
let hitvox = hitloc - (hitnorm * 0.5);
dbg!(hitvox.round());
}
}
panic!();
}
#[test]
pub fn test_ray_x_bounding_collisions() {
let abounds: OBB = OBB::new(
Vec3::new(10., 0., 0.),
Quat::from_rotation_y(f32::to_radians(90.)),
Vec3::splat(5.),
);
let aabb_test = ray_aabb_intersection(Vec3::ZERO, Vec3::X, abounds.position, abounds.half_size);
let obb_test = ray_obb_intersection(Vec3::ZERO, Vec3::X, &abounds);
assert_eq!(aabb_test.clone().unwrap().hitpoint, Vec3::new(5., 0., 0.));
assert_eq!(aabb_test.unwrap().normal, Vec3::new(-1., 0., 0.));
assert_eq!(
obb_test.unwrap().hitpoint.global.round(),
Vec3::new(5., 0., 0.)
);
let rotated_bounds: OBB = OBB::new(
Vec3::new(20., 0., 0.),
Quat::from_rotation_z(f32::to_radians(45.0)),
Vec3::splat(5.),
);
let rotated_obb_test = ray_obb_intersection(Vec3::ZERO, Vec3::X, &rotated_bounds).unwrap();
dbg!(rotated_obb_test.clone());
assert_eq!(rotated_obb_test.hitpoint.global.round().x, 13.);
assert_eq!(
rotated_obb_test.normal.local.normalize(),
Vec3::new(-0.7071068, 0.7071068, -0.0)
);
}
#[test]
pub fn test_sphere_collisions() {
let x = sphere_sphere_overlap(Vec3::new(0., 6., 0.), 4., Vec3::new(0., 12., 0.), 9.5);
@ -684,38 +699,6 @@ 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_y(f32::to_radians(90.)),
Vec3::splat(5.),
);
let aabb_test = ray_aabb_intersection(Vec3::ZERO, Vec3::X, abounds.position, abounds.half_size);
let obb_test = ray_obb_intersection(Vec3::ZERO, Vec3::X, &abounds);
assert_eq!(aabb_test.clone().unwrap().hitpoint, Vec3::new(5., 0., 0.));
assert_eq!(aabb_test.unwrap().normal, Vec3::new(-1., 0., 0.));
assert_eq!(
obb_test.unwrap().hitpoint.global.round(),
Vec3::new(5., 0., 0.)
);
let rotated_bounds: OBB = OBB::new(
Vec3::new(20., 0., 0.),
Quat::from_rotation_z(f32::to_radians(45.0)),
Vec3::splat(5.),
);
let rotated_obb_test = ray_obb_intersection(Vec3::ZERO, Vec3::X, &rotated_bounds).unwrap();
dbg!(rotated_obb_test.clone());
assert_eq!(rotated_obb_test.hitpoint.global.round().x, 13.);
assert_eq!(
rotated_obb_test.normal.local.normalize(),
Vec3::new(-0.7071068, 0.7071068, -0.0)
);
}
#[test]
pub fn test_ray_x_sphere_collisions() {
let ro = Vec3::ZERO;
@ -833,3 +816,22 @@ pub fn test_oct_x_oct_collisions() {
);
}
}
#[test]
pub fn test_sphere_internal_transform() {
let pos = Vec3::new(0., 10., 0.);
let size = 10;
let obby = OBB::new(
Vec3::new(0., 10., 0.),
Quat::from_rotation_x(f32::to_radians(90.)),
Vec3::new(1., 1., 1.),
);
let (x, y) = obb_to_global_sphere(pos, size, &obby);
println!("{x}, {y:.3}");
let z = x.y;
println!("{z:.3}");
let x = x.round();
let y = y.round();
assert_eq!(x, Vec3::new(0., 10., 20.));
assert_eq!(y, 15.);
}