263 lines
6.9 KiB
Rust
263 lines
6.9 KiB
Rust
use crate::{Sprite, SpriteCollection};
|
|
use std::cmp::max;
|
|
|
|
// https://github.com/pythonarcade/arcade/blob/d2ce45a9b965020cde57a2a88536311e04504e6e/arcade/sprite_list/spatial_hash.py#L356
|
|
|
|
fn collided(sprite1: &Sprite, sprite2: &Sprite) -> bool {
|
|
let coll_rad1 = max(sprite1.rect.width(), sprite1.rect.height()) as i32;
|
|
let coll_rad2 = max(sprite2.rect.width(), sprite2.rect.height()) as i32;
|
|
|
|
let collision_radius = coll_rad1 + coll_rad2;
|
|
let collision_diameter = collision_radius * collision_radius;
|
|
|
|
let diff_x = sprite1.position().0 - sprite2.position().0;
|
|
let diff_x2 = diff_x * diff_x;
|
|
|
|
if diff_x2 > collision_diameter {
|
|
return false;
|
|
}
|
|
|
|
let diff_y = sprite1.position().1 - sprite2.position().1;
|
|
let diff_y2 = diff_y * diff_y;
|
|
|
|
if diff_y2 > collision_diameter {
|
|
return false;
|
|
}
|
|
|
|
return sprite1.rect.has_intersection(sprite2.rect);
|
|
}
|
|
|
|
/// Check if two sprites are touching or overlapping.
|
|
pub fn check_for_collision(sprite1: &Sprite, sprite2: &Sprite) -> bool {
|
|
collided(sprite1, sprite2)
|
|
}
|
|
|
|
/// Check if the sprite is colliding with any sprite in the collection, and return a list of
|
|
/// references to the sprites which are colliding
|
|
pub fn check_for_collision_with_collection<'a>(
|
|
sprite: &Sprite,
|
|
list: &'a SpriteCollection,
|
|
) -> Vec<&'a Sprite> {
|
|
list.inner()
|
|
.iter()
|
|
.filter(|s| check_for_collision(sprite, s))
|
|
.collect()
|
|
}
|
|
/// A 2-dimensional vector.
|
|
#[derive(Clone, Copy, PartialEq, PartialOrd, Debug, Default)]
|
|
#[repr(C)]
|
|
pub struct Vec2(pub(crate) f32, pub(crate) f32);
|
|
|
|
/// Creates a `Vec2`.
|
|
#[inline]
|
|
pub fn vec2(x: f32, y: f32) -> Vec2 {
|
|
Vec2(x, y)
|
|
}
|
|
|
|
impl Vec2 {
|
|
/// Performs `is_nan` on each element of self, returning a `Vec2Mask` of the results.
|
|
///
|
|
/// In other words, this computes `[x.is_nan(), y.is_nan()]`.
|
|
|
|
|
|
|
|
|
|
/// Returns a `Vec2` with elements representing the sign of `self`.
|
|
///
|
|
/// - `1.0` if the number is positive, `+0.0` or `INFINITY`
|
|
/// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
|
|
/// - `NAN` if the number is `NAN`
|
|
#[inline]
|
|
pub fn signum(self) -> Self {
|
|
Self(self.0.signum(), self.1.signum())
|
|
}
|
|
|
|
#[deprecated(since = "0.9.5", note = "please use `Vec2::recip` instead")]
|
|
#[inline(always)]
|
|
pub fn reciprocal(self) -> Self {
|
|
self.recip()
|
|
}
|
|
|
|
/// Returns a `Vec2` containing the reciprocal `1.0/n` of each element of `self`.
|
|
#[inline]
|
|
pub fn recip(self) -> Self {
|
|
Self(self.0.recip(), self.1.recip())
|
|
}
|
|
/// Creates a new `Vec2`.
|
|
#[inline]
|
|
pub fn new(x: f32, y: f32) -> Vec2 {
|
|
Vec2(x, y)
|
|
}
|
|
|
|
|
|
/// Creates a `Vec2` with all elements set to `v`.
|
|
#[inline]
|
|
pub fn splat(v: f32) -> Vec2 {
|
|
Vec2(v, v)
|
|
}
|
|
|
|
|
|
/// Returns element `x`.
|
|
#[inline]
|
|
pub fn x(self) -> f32 {
|
|
self.0
|
|
}
|
|
|
|
/// Returns element `y`.
|
|
#[inline]
|
|
pub fn y(self) -> f32 {
|
|
self.1
|
|
}
|
|
|
|
/// Returns a mutable reference to element `x`.
|
|
#[inline]
|
|
pub fn x_mut(&mut self) -> &mut f32 {
|
|
&mut self.0
|
|
}
|
|
|
|
/// Returns a mutable reference to element `y`.
|
|
#[inline]
|
|
pub fn y_mut(&mut self) -> &mut f32 {
|
|
&mut self.1
|
|
}
|
|
|
|
/// Sets element `x`.
|
|
#[inline]
|
|
pub fn set_x(&mut self, x: f32) {
|
|
self.0 = x;
|
|
}
|
|
|
|
/// Sets element `y`.
|
|
#[inline]
|
|
pub fn set_y(&mut self, y: f32) {
|
|
self.1 = y;
|
|
}
|
|
|
|
/// Computes the dot product of `self` and `other`.
|
|
#[inline]
|
|
pub fn dot(self, other: Vec2) -> f32 {
|
|
(self.0 * other.0) + (self.1 * other.1)
|
|
}
|
|
|
|
/// Computes the length of `self`.
|
|
#[inline]
|
|
pub fn length(self) -> f32 {
|
|
self.dot(self).sqrt()
|
|
}
|
|
|
|
/// Computes the squared length of `self`.
|
|
///
|
|
/// This is generally faster than `Vec2::length()` as it avoids a square
|
|
/// root operation.
|
|
#[inline]
|
|
pub fn length_squared(self) -> f32 {
|
|
self.dot(self)
|
|
}
|
|
|
|
#[deprecated(since = "0.9.5", note = "please use `Vec2::length_recip` instead")]
|
|
#[inline(always)]
|
|
pub fn length_reciprocal(self) -> f32 {
|
|
self.length_recip()
|
|
}
|
|
|
|
/// Computes `1.0 / Vec2::length()`.
|
|
///
|
|
/// For valid results, `self` must _not_ be of length zero.
|
|
#[inline]
|
|
pub fn length_recip(self) -> f32 {
|
|
self.length().recip()
|
|
}
|
|
|
|
|
|
/// Returns the vertical minimum of `self` and `other`.
|
|
///
|
|
/// In other words, this computes
|
|
/// `[x: min(x1, x2), y: min(y1, y2)]`,
|
|
/// taking the minimum of each element individually.
|
|
#[inline]
|
|
pub fn min(self, other: Vec2) -> Vec2 {
|
|
Vec2(self.0.min(other.0), self.1.min(other.1))
|
|
}
|
|
|
|
/// Returns the vertical maximum of `self` and `other`.
|
|
///
|
|
/// In other words, this computes
|
|
/// `[x: max(x1, x2), y: max(y1, y2)]`,
|
|
/// taking the maximum of each element individually.
|
|
#[inline]
|
|
pub fn max(self, other: Vec2) -> Vec2 {
|
|
Vec2(self.0.max(other.0), self.1.max(other.1))
|
|
}
|
|
|
|
/// Returns the horizontal minimum of `self`'s elements.
|
|
///
|
|
/// In other words, this computes `min(x, y)`.
|
|
#[inline]
|
|
pub fn min_element(self) -> f32 {
|
|
self.0.min(self.1)
|
|
}
|
|
|
|
/// Returns the horizontal maximum of `self`'s elements.
|
|
///
|
|
/// In other words, this computes `max(x, y)`.
|
|
#[inline]
|
|
pub fn max_element(self) -> f32 {
|
|
self.0.max(self.1)
|
|
}
|
|
|
|
/// Creates a `Vec2` from the first two values in `slice`.
|
|
///
|
|
/// # Panics
|
|
///
|
|
/// Panics if `slice` is less than two elements long.
|
|
#[inline]
|
|
pub fn from_slice_unaligned(slice: &[f32]) -> Self {
|
|
Self(slice[0], slice[1])
|
|
}
|
|
|
|
/// Writes the elements of `self` to the first two elements in `slice`.
|
|
///
|
|
/// # Panics
|
|
///
|
|
/// Panics if `slice` is less than two elements long.
|
|
#[inline]
|
|
pub fn write_to_slice_unaligned(self, slice: &mut [f32]) {
|
|
slice[0] = self.0;
|
|
slice[1] = self.1;
|
|
}
|
|
|
|
/// Returns a `Vec2` containing the absolute value of each element of `self`.
|
|
#[inline]
|
|
pub fn abs(self) -> Self {
|
|
Self(self.0.abs(), self.1.abs())
|
|
}
|
|
|
|
/// Returns a `Vec2` containing the nearest integer to a number for each element of `self`.
|
|
/// Round half-way cases away from 0.0.
|
|
#[inline]
|
|
pub fn round(self) -> Self {
|
|
Self(self.0.round(), self.1.round())
|
|
}
|
|
|
|
/// Returns a `Vec2` containing the largest integer less than or equal to a number for each
|
|
/// element of `self`.
|
|
#[inline]
|
|
pub fn floor(self) -> Self {
|
|
Self(self.0.floor(), self.1.floor())
|
|
}
|
|
|
|
/// Returns a `Vec2` containing the smallest integer greater than or equal to a number for each
|
|
/// element of `self`.
|
|
#[inline]
|
|
pub fn ceil(self) -> Self {
|
|
Self(self.0.ceil(), self.1.ceil())
|
|
}
|
|
|
|
/// The perpendicular dot product of the vector and `other`.
|
|
#[inline]
|
|
pub fn perp_dot(self, other: Vec2) -> f32 {
|
|
(self.0 * other.1) - (self.1 * other.0)
|
|
}
|
|
|
|
|
|
} |