oml_game/math/
matrix32.rs

1use crate::math::{Matrix22, Matrix33, Vector2};
2
3#[derive(Copy, Clone, Default)]
4pub struct Matrix32 {
5	pub rot: Matrix22,
6	pub pos: Vector2,
7}
8
9impl Matrix32 {
10	pub fn identity() -> Self {
11		Self {
12			rot: Matrix22::identity(),
13			pos: Vector2::zero(),
14		}
15	}
16
17	pub fn scaling(scale: f32) -> Self {
18		Self {
19			rot: Matrix22::scaling(scale),
20			pos: Vector2::zero(),
21		}
22	}
23
24	pub fn with_scaling(mut self, scale: f32) -> Self {
25		self.rot = Matrix22::scaling(scale);
26		self
27	}
28
29	pub fn with_scaling_xy(mut self, xs: f32, ys: f32) -> Self {
30		self.rot = Matrix22::scaling_xy(xs, ys);
31		self
32	}
33
34	pub fn translation(t: &Vector2) -> Self {
35		Self {
36			rot: Matrix22::identity(),
37			pos: *t,
38		}
39	}
40	pub fn with_translation(mut self, t: &Vector2) -> Self {
41		self.pos = *t;
42		self
43	}
44
45	pub fn scaling_xy(x: f32, y: f32) -> Self {
46		Self {
47			rot: Matrix22::scaling_xy(x, y),
48			pos: Vector2::zero(),
49		}
50	}
51
52	// :UNTESTED:
53	pub fn from_matrix33(m: &Matrix33) -> Self {
54		Self {
55			rot: Matrix22::new(&Vector2::new(m.x.x, m.x.y), &Vector2::new(m.y.x, m.y.y)),
56			pos: Vector2::new(m.x.z, m.y.z),
57		}
58	}
59
60	pub fn mul_vector2(&self, v: &Vector2) -> Vector2 {
61		let x = v.x;
62		let y = v.y;
63
64		Vector2::new(
65			self.rot.x.x * x + self.rot.y.x * y + self.pos.x,
66			self.rot.x.y * x + self.rot.y.y * y + self.pos.y,
67		)
68	}
69}
70
71impl From<[f32; 6]> for Matrix32 {
72	fn from(m: [f32; 6]) -> Self {
73		Self {
74			rot: Matrix22::new(&Vector2::new(m[0], m[3]), &Vector2::new(m[1], m[4])),
75			pos: Vector2::new(m[2], m[5]),
76		}
77	}
78}
79
80impl std::fmt::Debug for Matrix32 {
81	fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
82		writeln!(
83			f,
84			"Matrix32:\n{} {} {}\n{} {} {}",
85			self.rot.x.x, self.rot.x.y, self.pos.x, self.rot.y.x, self.rot.y.y, self.pos.y,
86		)
87	}
88}