From 1301ab4b0bb5e1438e2834aba5f4baea3ff588d9 Mon Sep 17 00:00:00 2001 From: gallant Date: Tue, 16 May 2023 09:45:13 -0500 Subject: [PATCH] necessary point collision; will revise later --- src/sprite/physics.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/sprite/physics.rs b/src/sprite/physics.rs index 853c369..7d76caf 100644 --- a/src/sprite/physics.rs +++ b/src/sprite/physics.rs @@ -39,6 +39,28 @@ pub fn check_for_collision(sprite1: &Sprite, sprite2: &Sprite) -> bool { collided(sprite1, sprite2) } +#[must_use] +pub fn check_for_collision_with_point(sprite1: &Sprite, point: &crate::math::vec2::Vec2Int) -> bool { + let coll_rad = max(sprite1.rect.width(), sprite1.rect.height()) as i32; + + let coll2 = coll_rad * coll_rad; + + let diff_x = sprite1.position().x - point.x; + let diff_x2 = diff_x * diff_x; + + if diff_x2 > coll2 { return false; } + + let diff_y = sprite1.position().y - point.y; + let diff_y2 = diff_y * diff_y; + + if diff_y2 > coll2 { + return false; + } + + return true; + +} + /// Check if the sprite is colliding with any sprite in the collection, and return a list of /// references to the sprites which are colliding #[must_use]