Correcting circle intersection code to not return virtual intersections.

This commit is contained in:
2025-08-28 16:07:48 -05:00
parent 2e340b7d28
commit 1b9c842077

View File

@@ -358,10 +358,7 @@ fn extract_point_pair(pair: &CGA) -> (CGA, CGA) {
(a,b) (a,b)
} }
fn get_circle_intersection(circle_1: &CGA, circle_2: &CGA) -> CircleIntersection { fn circle_intersection(circle_1: &CGA, circle_2: &CGA) -> CircleIntersection {
if !circle_intersection(circle_1, circle_2) {
return CircleIntersection::None;
}
//Assumes circles intersect in two places. //Assumes circles intersect in two places.
let circle_1 = grade_selection(circle_1, 3); //Explicitly check only trivector part let circle_1 = grade_selection(circle_1, 3); //Explicitly check only trivector part
let circle_2 = grade_selection(circle_2, 3); let circle_2 = grade_selection(circle_2, 3);
@@ -370,21 +367,12 @@ fn get_circle_intersection(circle_1: &CGA, circle_2: &CGA) -> CircleIntersection
let ba = &circle_2 * &circle_1; let ba = &circle_2 * &circle_1;
let comm = &ab - &ba; //Twice the commutator of circle_1 and circle_2. Unnormalized, but that isn't important. let comm = &ab - &ba; //Twice the commutator of circle_1 and circle_2. Unnormalized, but that isn't important.
let inter = &i4 * &comm; //The bivector of the pair of intersection points. let inter = &i4 * &comm; //The bivector of the pair of intersection points.
let (a,b) = extract_point_pair(&inter); let sqr = (&inter * &inter)[0];
CircleIntersection::TwoPoints(a,b) if sqr > ERROR {
} let (a,b) = extract_point_pair(&inter);
CircleIntersection::TwoPoints(a,b)
fn circle_intersection(circle_1: &CGA, circle_2: &CGA) -> bool {
let circle_1 = grade_selection(circle_1, 4);
let circle_2 = grade_selection(circle_2, 4);
let meet = circle_1 & circle_2;
let sqr = &meet * &meet;
let mag = magnitude(&sqr);
if mag < ERROR {
true
} else { } else {
false CircleIntersection::None //Technically could have one intersection, but that's not tested yet
} }
} }
@@ -586,8 +574,9 @@ impl Game {
draw_line(&self.context, &self.boundary); draw_line(&self.context, &self.boundary);
self.ship.draw(&self.context); self.ship.draw(&self.context);
let line = 100.*self.ship.fixed_to_world(&self.ship.verts[0]) ^ self.ship.fixed_to_world(&self.ship.verts[1]) ^ CGA::e4(); //Scaled to keep above floating point error
for asteroid in &self.asteroids { for asteroid in &self.asteroids {
let pair = get_circle_intersection(&asteroid.circle, &self.boundary); let pair = circle_intersection(&asteroid.circle, &self.boundary);
match pair { match pair {
CircleIntersection::TwoPoints(a,b) => { CircleIntersection::TwoPoints(a,b) => {
draw_point(&self.context, &a); draw_point(&self.context, &a);
@@ -596,6 +585,15 @@ impl Game {
_ => {} _ => {}
} }
asteroid.draw(&self.context); asteroid.draw(&self.context);
let pair = circle_intersection(&asteroid.circle, &line);
match pair {
CircleIntersection::TwoPoints(a,b) => {
draw_point(&self.context, &a);
draw_point(&self.context, &b);
draw_line(&self.context, &line);
}
_ => {},
}
} }
} }