vec2 momento
This commit is contained in:
parent
be9397089e
commit
e5287f65ff
|
@ -190,6 +190,7 @@ pub struct Sprite {
|
|||
is_platform: bool,
|
||||
}
|
||||
|
||||
|
||||
impl Sprite {
|
||||
/// Create a new Sprite. The `path` is relative to the current directory while running.
|
||||
///
|
||||
|
|
218
src/physics.rs
218
src/physics.rs
|
@ -43,3 +43,221 @@ pub fn check_for_collision_with_collection<'a>(
|
|||
.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)
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in a new issue