fix(Rectangle2D): now the collider will treat its position as the center and not a corner much like textures in the renderer

This commit is contained in:
lisk77 2025-10-20 02:41:33 +02:00
parent 2d4484a617
commit dfdffed745

View file

@ -237,9 +237,11 @@ impl Rectangle2D {
pub fn position(&self) -> Position2D { pub fn position(&self) -> Position2D {
self.position self.position
} }
pub fn set_position(&mut self, position: Position2D) { pub fn set_position(&mut self, position: Position2D) {
self.position = position; self.position = position;
} }
pub fn size(&self) -> v2 { pub fn size(&self) -> v2 {
self.size self.size
} }
@ -251,17 +253,13 @@ impl Rectangle2D {
impl Collider for Rectangle2D { impl Collider for Rectangle2D {
fn is_colliding(&self, other: &Self) -> bool { fn is_colliding(&self, other: &Self) -> bool {
let x1 = self.position().x(); let half_size1 = self.size() * 0.5;
let y1 = self.position().y(); let half_size2 = other.size() * 0.5;
let w1 = self.size().x();
let h1 = self.size().y();
let x2 = other.position().x(); let dx = (self.position().x() - other.position().x()).abs();
let y2 = other.position().y(); let dy = (self.position().y() - other.position().y()).abs();
let w2 = other.size().x();
let h2 = other.size().y();
x1 < x2 + w2 && x1 + w1 > x2 && y1 < y2 + h2 && y1 + h1 > y2 dx < (half_size1.x() + half_size2.x()) && dy < (half_size1.y() + half_size2.y())
} }
} }