diff --git a/src/main.rs b/src/main.rs index 4f421d3..51c153f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -44,10 +44,11 @@ impl Rect { } pub fn point_collides(&self, pos: (f64, f64), point: (f64, f64)) -> bool { - pos.0 + self.width > point.0 - && pos.0 <= point.0 - && pos.1 + self.height > point.1 - && pos.1 <= point.1 + println!("point: {point:?}"); + pos.0 + self.width < point.0 + && pos.0 >= point.0 + && pos.1 + self.height < point.1 + && pos.1 >= point.1 } } // @@ -91,6 +92,7 @@ fn vertical_line_intersect(p0: (f64, f64), vel: (f64, f64), line_x: f64) -> (f64 (line_x, y) } +#[derive(Debug)] enum Surface { Top, Bottom, @@ -116,12 +118,11 @@ fn closest_surface_for_point_and_rectangle_and_your_mom( ), (vertical_line_intersect(p0, vel, rect_pos.0), Surface::Left), ( - horizontal_line_intersect(p0, vel, rect_pos.0 + rect.width), + vertical_line_intersect(p0, vel, rect_pos.0 + rect.width), Surface::Right, ), ] .into_iter() - .filter(|(point, _)| rect.point_collides(rect_pos, *point)) .map(|(point, surface)| (point_distance(p0, point), surface)) .min_by(|(dist_a, _), (dist_b, _)| dist_a.total_cmp(&dist_b)) } @@ -140,10 +141,9 @@ impl System for CollisionSystem { if id == other_id { continue; } - let other_rect = ctx.entity_component::(id).clone(); - let other_body = ctx.entity_component::(id).clone(); + let other_rect = ctx.entity_component::(other_id).clone(); + let other_body = ctx.entity_component::(other_id).clone(); if rect.rect_collides(body.pos, &other_rect, other_body.pos) { - println!("collider vi?"); let last_pos = ( body.pos.0 - body.vel.0 * delta, body.pos.1 - body.vel.1 * delta, @@ -163,11 +163,14 @@ impl System for CollisionSystem { &other_rect, ) }) + .inspect(|v| println!("item: {v:?}")) .filter_map(std::convert::identity) .min_by(|(dist_a, _), (dist_b, _)| dist_a.total_cmp(&dist_b)) .map(|(_, surface)| surface) .ok_or_else(|| "we already checked if collision happens")?; + println!("surface found: {closest_surface:?}"); + let body = ctx.entity_component::(id); match closest_surface { Surface::Top => { @@ -313,8 +316,8 @@ fn main() { ..Default::default() }, Rect { - width: 256.0, - height: 256.0 + width: 128.0, + height: 128.0 }, Collider { resolve: true }, PlayerMovement, @@ -323,7 +326,7 @@ fn main() { spawn!( &mut context, RigidBody { - pos: (0.0, 650.0), + pos: (0.0, 720.0), ..Default::default() }, Rect {