From 9ac727f2a19ad1727fbaf3acfeac15d6daa65629 Mon Sep 17 00:00:00 2001 From: SimonFJ20 Date: Thu, 11 Apr 2024 22:58:27 +0200 Subject: [PATCH] skillissues --- src/collision.rs | 104 +++++++++++++++++++++++++++++++---------------- src/main.rs | 2 +- 2 files changed, 71 insertions(+), 35 deletions(-) diff --git a/src/collision.rs b/src/collision.rs index 73c8487..bee8482 100644 --- a/src/collision.rs +++ b/src/collision.rs @@ -33,16 +33,16 @@ enum Diagonal { BottomLeft, } -enum DiagonalCommonResult { +enum DiagonalComparison { None, Direction(Direction), Diagonal(Diagonal), } impl Diagonal { - pub fn common(&self, other: &Diagonal) -> DiagonalCommonResult { + pub fn common(&self, other: &Diagonal) -> DiagonalComparison { use Diagonal::*; - use DiagonalCommonResult as R; + use DiagonalComparison as R; use Direction::*; match (self, other) { (TopLeft, TopRight) => R::Direction(Top), @@ -57,24 +57,52 @@ impl Diagonal { _ => R::None, } } + pub fn opposing(&self, other: &Diagonal) -> DiagonalComparison { + use Diagonal::*; + use DiagonalComparison as R; + use Direction::*; + match (self, other) { + (TopLeft, TopRight) => R::Direction(Left), + (TopLeft, BottomLeft) => R::Direction(Top), + (TopRight, TopLeft) => R::Direction(Right), + (TopRight, BottomRight) => R::Direction(Top), + (BottomRight, TopRight) => R::Direction(Bottom), + (BottomRight, BottomLeft) => R::Direction(Right), + (BottomLeft, TopLeft) => R::Direction(Bottom), + (BottomLeft, BottomRight) => R::Direction(Left), + (left, right) if left == right => R::Diagonal(left.opposite()), + _ => R::None, + } + } pub fn contains(&self, dir: Direction) -> bool { + use Diagonal::*; + use Direction::*; match (self, dir) { - (Diagonal::TopLeft, Direction::Top) - | (Diagonal::TopLeft, Direction::Left) - | (Diagonal::TopRight, Direction::Top) - | (Diagonal::TopRight, Direction::Left) - | (Diagonal::TopRight, Direction::Right) - | (Diagonal::BottomRight, Direction::Bottom) - | (Diagonal::BottomRight, Direction::Right) - | (Diagonal::BottomLeft, Direction::Bottom) - | (Diagonal::BottomLeft, Direction::Left) => true, - (Diagonal::TopLeft, Direction::Bottom) - | (Diagonal::TopLeft, Direction::Right) - | (Diagonal::TopRight, Direction::Bottom) - | (Diagonal::BottomRight, Direction::Top) - | (Diagonal::BottomRight, Direction::Left) - | (Diagonal::BottomLeft, Direction::Top) - | (Diagonal::BottomLeft, Direction::Right) => false, + (TopLeft, Top) + | (TopLeft, Left) + | (TopRight, Top) + | (TopRight, Left) + | (TopRight, Right) + | (BottomRight, Bottom) + | (BottomRight, Right) + | (BottomLeft, Bottom) + | (BottomLeft, Left) => true, + (TopLeft, Bottom) + | (TopLeft, Right) + | (TopRight, Bottom) + | (BottomRight, Top) + | (BottomRight, Left) + | (BottomLeft, Top) + | (BottomLeft, Right) => false, + } + } + pub fn opposite(&self) -> Self { + use Diagonal::*; + match self { + TopLeft => BottomRight, + TopRight => BottomLeft, + BottomRight => TopLeft, + BottomLeft => TopRight, } } } @@ -185,34 +213,42 @@ fn rects_closet_points( lowest.1 } -fn point_vel_rect_collision(pos: V2, vel: V2, other_pos: V2, rect: V2) -> Option { +fn point_delta_rect_collision(pos: V2, delta: V2, other_pos: V2, rect: V2) -> Option { let c1 = point_rect_closest_point(pos, other_pos, rect); let (c0, c2) = rect_adjacent_corners(other_pos, rect, c1); - let intersection_c1_c0 = line_intersection(pos, vel, c1, (c0.0 - c1.0, c0.1 - c1.0))?; - if point_between_points(intersection_c1_c0, c1, c0) { - return Some(intersection_c1_c0); + if let Some(intersection_c1_c0) = line_intersection(pos, delta, c1, (c0.0 - c1.0, c0.1 - c1.0)) + { + println!("x intersect"); + if point_between_points(intersection_c1_c0, c1, c0) { + println!(" x between points"); + return Some(intersection_c1_c0); + } } - let intersection_c1_c2 = line_intersection(pos, vel, c1, (c2.0 - c1.0, c2.1 - c1.0))?; - if point_between_points(intersection_c1_c2, c1, c2) { - return Some(intersection_c1_c2); + if let Some(intersection_c1_c2) = line_intersection(pos, delta, c1, (c2.0 - c1.0, c2.1 - c1.0)) + { + println!("y intersect"); + if point_between_points(intersection_c1_c2, c1, c2) { + println!(" y between points"); + return Some(intersection_c1_c2); + } } None } fn rect_collision( pos: V2, - vel: V2, + delta: V2, rect: V2, other_pos: V2, other_rect: V2, ) -> Option<(V2, Direction)> { let ((p0, d0), (_, d1)) = rects_closet_points(pos, rect, other_pos, other_rect); - let common = match d0.common(&d1) { - DiagonalCommonResult::Direction(dir) => dir, + let dir = match d0.opposing(&d1) { + DiagonalComparison::Direction(dir) => dir, _ => return None, }; - let new_pos = point_vel_rect_collision(p0, vel, other_pos, other_rect)?; - Some((new_pos, common)) + let new_pos = point_delta_rect_collision(p0, delta, other_pos, other_rect)?; + Some((new_pos, dir)) } #[derive(Component, Clone, Default)] @@ -247,10 +283,10 @@ impl System for CollisionSystem { continue; }; let body = ctx.entity_component::(id); - body.pos = new_pos; + // body.pos = new_pos; match dir { - Direction::Top | Direction::Bottom => body.vel.0 = 0.0, - Direction::Left | Direction::Right => body.vel.1 = 0.0, + Direction::Top | Direction::Bottom => body.vel.1 = 0.0, + Direction::Left | Direction::Right => body.vel.0 = 0.0, } } } diff --git a/src/main.rs b/src/main.rs index 342d270..52e3127 100644 --- a/src/main.rs +++ b/src/main.rs @@ -141,7 +141,7 @@ fn main() { context.add_system(CloudSystem); let player = context.load_sprite("textures/player.png").unwrap(); let background = context.load_sprite("textures/literally_dprk.png").unwrap(); - let nope = context.load_sprite("textures/nuh-uh.png").unwrap(); + // let nope = context.load_sprite("textures/nuh-uh.png").unwrap(); spawn!( &mut context,