diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2020-08-25 18:39:33 +0200 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2020-08-25 18:39:33 +0200 |
| commit | 4dccf6a313908a5d63ba01b375804e9ebef4687e (patch) | |
| tree | d158385782a02a577f3a47cb1da96e3479d8c957 /src/vector2.rs | |
| parent | c0e7c121e96713549dce67b01f8d86ef79e6ab80 (diff) | |
| download | boids-4dccf6a313908a5d63ba01b375804e9ebef4687e.tar.gz boids-4dccf6a313908a5d63ba01b375804e9ebef4687e.tar.bz2 boids-4dccf6a313908a5d63ba01b375804e9ebef4687e.zip | |
Added Vector2 for direction, steering behavior gut draft not working
Diffstat (limited to 'src/vector2.rs')
| -rw-r--r-- | src/vector2.rs | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/vector2.rs b/src/vector2.rs new file mode 100644 index 0000000..e991a2a --- /dev/null +++ b/src/vector2.rs @@ -0,0 +1,55 @@ +use sdl2::rect::Point; + +#[derive(PartialEq, Clone, Copy)] +pub struct Vector2 { + pub y: f32, + pub x: f32, +} + +impl Vector2 { + pub fn new(x: f32, y: f32) -> Vector2 { + Vector2 { x, y } + } + + pub fn from_point(point: Point) -> Vector2 { + Vector2::new(point.x as f32, point.y as f32) + } + + pub fn norm(&self) -> f32 { + (self.x * self.x + self.y * self.y).sqrt() + } + + pub fn normalize(&mut self) { + *self = *self / self.norm(); + } +} + +use std::ops::{Add, Mul, MulAssign, Div}; + +impl Add for Vector2 { + type Output = Self; + fn add(self, other: Self) -> Self::Output { + Vector2::new(self.x + other.x, self.y + other.y) + } +} + +impl Mul<f32> for Vector2 { + type Output = Vector2; + fn mul(self, scalar: f32) -> Self::Output { + Vector2::new(self.x * scalar, self.y * scalar) + } +} + +impl MulAssign<f32> for Vector2 { + fn mul_assign(&mut self, scalar: f32) { + self.x *= scalar; + self.y *= scalar; + } +} + +impl Div<f32> for Vector2 { + type Output = Vector2; + fn div(self, scalar: f32) -> Self::Output { + Vector2::new(self.x / scalar, self.y / scalar) + } +} |
