fix some typos

This commit is contained in:
Theis Pieter Hollebeek 2024-04-10 20:10:02 +02:00
parent 7609eb3e4b
commit 1eab69d8ad

View File

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