diff --git a/src/physics.rs b/src/physics.rs index 95d3289..55be716 100644 --- a/src/physics.rs +++ b/src/physics.rs @@ -84,17 +84,38 @@ fn resolve_aabb_collisions(mut query: Query<(&mut PhysicsBody2D, &AABBCollider)> let dy = b.pos.y - other_b.pos.y; if other_b.immovable { - let new_x = match dx { - ..0 => other_b.pos.x - (c.width / 2 + other_c.width / 2) as i64, - 0 => b.pos.x, - 1.. => other_b.pos.x + (c.width / 2 + other_c.width / 2) as i64, - }; - let new_y = match dy { - ..0 => other_b.pos.y - (c.height / 2 + other_c.height / 2) as i64, - 0 => b.pos.y, - 1.. => other_b.pos.y + (c.height / 2 + other_c.height / 2) as i64, + // move backwards along dx/dy until we're out + + let (new_x, new_y) = match dx.abs() > dy.abs() { + true => { + let dist = (c.width + other_c.width) as i64 / 2; + + match dx > 0 { + true => (other_b.pos.x + dist, b.pos.y), + false => (other_b.pos.x - dist, b.pos.y), + } + } + false => { + let dist = (c.height + other_c.height) as i64 / 2; + + match dy > 0 { + true => (b.pos.x, other_b.pos.y + dist), + false => (b.pos.x, other_b.pos.y - dist), + } + } }; + // let new_x = match dx { + // ..0 => other_b.pos.x - (c.width / 2 + other_c.width / 2) as i64, + // 0 => b.pos.x, + // 1.. => other_b.pos.x + (c.width / 2 + other_c.width / 2) as i64, + // }; + // let new_y = match dy { + // ..0 => other_b.pos.y - (c.height / 2 + other_c.height / 2) as i64, + // 0 => b.pos.y, + // 1.. => other_b.pos.y + (c.height / 2 + other_c.height / 2) as i64, + // }; + let updated_b = PhysicsBody2D { pos: I64Vec2 { x: new_x, y: new_y }, vel: b.vel,