cat_box/
physics.rs

1//! Basic physics utilities for cat-box.
2//!
3//! Still ***very much work-in-progress***
4
5#![allow(clippy::cast_possible_wrap)]
6
7use crate::{Sprite, SpriteCollection};
8use std::cmp::max;
9
10// https://github.com/pythonarcade/arcade/blob/d2ce45a9b965020cde57a2a88536311e04504e6e/arcade/sprite_list/spatial_hash.py#L356
11
12fn collided(sprite1: &Sprite, sprite2: &Sprite) -> bool {
13    let coll_rad1 = max(sprite1.rect.width(), sprite1.rect.height()) as i32;
14    let coll_rad2 = max(sprite2.rect.width(), sprite2.rect.height()) as i32;
15
16    let collision_radius = coll_rad1 + coll_rad2;
17    let collision_diameter = collision_radius * collision_radius;
18
19    let diff_x = sprite1.position().x - sprite2.position().x;
20    let diff_x2 = diff_x * diff_x;
21
22    if diff_x2 > collision_diameter {
23        return false;
24    }
25
26    let diff_y = sprite1.position().y - sprite2.position().y;
27    let diff_y2 = diff_y * diff_y;
28
29    if diff_y2 > collision_diameter {
30        return false;
31    }
32
33    sprite1.rect.has_intersection(sprite2.rect)
34}
35
36/// Check if two sprites are touching or overlapping.
37#[must_use]
38pub fn check_for_collision(sprite1: &Sprite, sprite2: &Sprite) -> bool {
39    collided(sprite1, sprite2)
40}
41
42/// Check if the sprite is colliding with any sprite in the collection, and return a list of
43/// references to the sprites which are colliding
44#[must_use]
45pub fn check_for_collision_with_collection<'a>(
46    sprite: &Sprite,
47    list: &'a SpriteCollection,
48) -> Vec<&'a Sprite> {
49    list.inner()
50        .iter()
51        .filter(|s| check_for_collision(sprite, s))
52        .collect()
53}