feat: added a camera with orthographic projection and did some work restructuring the comet_app to make the setup system optional. Input handling is moved to the app

This commit is contained in:
lisk77 2024-11-13 03:33:02 +01:00
parent 780365aeb8
commit 5a9f632e3a
22 changed files with 1173 additions and 349 deletions

View file

@ -1,7 +1,9 @@
[package]
name = "comet_math"
version = "0.1.0"
version = "0.1.1"
edition = "2021"
[dependencies]
comet_log = { path = "../comet_log" }
num-traits = "0.2.19"
chrono = "0.4.0"

View file

@ -11,7 +11,7 @@ trait LinearTransformation {
// ##################################################
/// Representation of a 2x2 Matrix in row major
/// Representation of a 2x2 Matrix
#[repr(C)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialEq)]
@ -39,13 +39,20 @@ impl Mat2 {
}
}
pub fn from_vec(row1: Vec2, row2: Vec2) -> Self {
pub fn from_rows(row1: Vec2, row2: Vec2) -> Self {
Self {
x00: row1.x(), x01: row1.y(),
x10: row2.x(), x11: row2.y()
}
}
pub fn from_cols(col1: Vec2, col2: Vec2) -> Self {
Self {
x00: col1.x(), x01: col2.x(),
x10: col1.y(), x11: col2.y()
}
}
pub fn get(&self, row: usize, col: usize) -> Option<f32> {
assert!(row <= 1, "This row ({}) is out of bounds! Bounds: 0..1", row);
assert!(col <= 1, "This row ({}) is out of bounds! Bounds: 0..1", col);
@ -68,6 +75,15 @@ impl Mat2 {
}
}
pub fn get_col(&self, col: usize) -> Option<Vec2> {
assert!(col <= 1, "This row ({}) is out of bounds! Bounds: 0..1", col);
match col {
0 => Some(Vec2::new(self.x00, self.x10)),
1 => Some(Vec2::new(self.x01, self.x11)),
_ => None
}
}
pub fn set(&mut self, row: usize, col: usize, element: f32) {
assert!(row <= 1, "This row ({}) is out of bounds! Bounds: 0..1", row);
assert!(col <= 1, "This row ({}) is out of bounds! Bounds: 0..1", col);
@ -91,6 +107,16 @@ impl Mat2 {
}
}
pub fn set_col(&mut self, col: usize, col_content: Vec2) {
assert!(col <= 1, "This row ({}) is out of bounds! Bounds: 0..1", col);
match col {
0 => { self.x00 = col_content.x(); self.x10 = col_content.y(); },
1 => { self.x01 = col_content.x(); self.x11 = col_content.y(); },
_ => {}
}
}
pub fn det(&self) -> f32 {
self.x00 * self.x11
- self.x01 * self.x10
@ -108,6 +134,12 @@ impl Mat2 {
self.set_row(row1, self.get_row(row2).expect(format!("This row ({}) is out of bounds! Bounds: 0..1", row2).as_str()));
self.set_row(row2, tmp);
}
pub fn swap_cols(&mut self, col1: usize, col2: usize) {
let tmp = self.get_col(col1).expect(format!("This row ({}) is out of bounds! Bounds: 0..1", col1).as_str());
self.set_col(col1, self.get_col(col2).expect(format!("This row ({}) is out of bounds! Bounds: 0..1", col2).as_str()));
self.set_col(col2, tmp);
}
}
impl Add<Mat2> for Mat2 {
@ -187,11 +219,12 @@ impl Div<f32> for Mat2 {
}
}
/// [WARN]: This will return a column-major array for wgpu use!
impl Into<[[f32; 2]; 2]> for Mat2 {
fn into(self) -> [[f32; 2]; 2] {
[
[self.x00, self.x01],
[self.x10, self.x11],
[self.x00, self.x10],
[self.x01, self.x11],
]
}
}
@ -235,7 +268,7 @@ impl Mat3 {
}
}
pub fn from_vec(row1: Vec3, row2: Vec3, row3: Vec3) -> Self {
pub fn from_rows(row1: Vec3, row2: Vec3, row3: Vec3) -> Self {
Self {
x00: row1.x(), x01: row1.y(), x02: row1.z(),
x10: row2.x(), x11: row2.y(), x12: row2.z(),
@ -243,6 +276,14 @@ impl Mat3 {
}
}
pub fn from_cols(col1: Vec3, col2: Vec3, col3: Vec3) -> Self {
Self {
x00: col1.x(), x01: col2.x(), x02: col3.x(),
x10: col1.y(), x11: col2.y(), x12: col3.y(),
x20: col1.z(), x21: col2.z(), x22: col3.z()
}
}
pub fn get(&self, row: usize, col: usize) -> Option<f32> {
assert!(row <= 2, "This row ({}) is out of bounds! Bounds: 0..2", row);
assert!(col <= 2, "This row ({}) is out of bounds! Bounds: 0..2", col);
@ -270,6 +311,16 @@ impl Mat3 {
}
}
pub fn get_col(&self, col: usize) -> Option<Vec3> {
assert!(col <= 2, "This row ({}) is out of bounds! Bounds: 0..2", col);
match col {
0 => Some(Vec3::new(self.x00, self.x10, self.x20)),
1 => Some(Vec3::new(self.x01, self.x11, self.x21)),
2 => Some(Vec3::new(self.x02, self.x12, self.x22)),
_ => None
}
}
pub fn set(&mut self, row: usize, col: usize, element: f32) {
assert!(row <= 2, "This row ({}) is out of bounds! Bounds: 0..2", row);
assert!(col <= 2, "This row ({}) is out of bounds! Bounds: 0..2", col);
@ -298,6 +349,16 @@ impl Mat3 {
}
}
pub fn set_col(&mut self, col: usize, col_content: Vec3) {
assert!(col <= 2, "This row ({}) is out of bounds! Bounds: 0..2", col);
match col {
0 => { self.x00 = col_content.x; self.x10 = col_content.y; self.x20 = col_content.z; },
1 => { self.x01 = col_content.x; self.x11 = col_content.y; self.x21 = col_content.z; },
2 => { self.x02 = col_content.x; self.x12 = col_content.y; self.x22 = col_content.z; }
_ => {}
}
}
pub fn det(&self) -> f32 {
self.x00 * self.x11 * self.x22
+ self.x01 * self.x12 * self.x20
@ -320,6 +381,12 @@ impl Mat3 {
self.set_row(row1, self.get_row(row2).expect(format!("This row ({}) is out of bounds! Bounds: 0..2", row2).as_str()));
self.set_row(row2, tmp);
}
pub fn swap_cols(&mut self, col1: usize, col2: usize) {
let tmp = self.get_col(col1).expect(format!("This row ({}) is out of bounds! Bounds: 0..2", col1).as_str());
self.set_col(col1, self.get_col(col2).expect(format!("This row ({}) is out of bounds! Bounds: 0..2", col2).as_str()));
self.set_col(col2, tmp);
}
}
impl Add<Mat3> for Mat3 {
@ -412,9 +479,9 @@ impl Div<f32> for Mat3 {
impl Into<[[f32; 3]; 3]> for Mat3 {
fn into(self) -> [[f32; 3]; 3] {
[
[self.x00, self.x01, self.x02],
[self.x10, self.x11, self.x12],
[self.x20, self.x21, self.x22],
[self.x00, self.x10, self.x20],
[self.x01, self.x11, self.x21],
[self.x02, self.x12, self.x22],
]
}
}
@ -470,7 +537,7 @@ impl Mat4 {
}
}
pub fn from_vec(row1: Vec4, row2: Vec4, row3: Vec4, row4: Vec4) -> Self {
pub fn from_rows(row1: Vec4, row2: Vec4, row3: Vec4, row4: Vec4) -> Self {
Self {
x00: row1.x(), x01: row1.y(), x02: row1.z(), x03: row1.w(),
x10: row2.x(), x11: row2.y(), x12: row2.z(), x13: row2.w(),
@ -479,6 +546,15 @@ impl Mat4 {
}
}
pub fn from_cols(col1: Vec4, col2: Vec4, col3: Vec4, col4: Vec4) -> Self {
Self {
x00: col1.x(), x01: col2.x(), x02: col3.x(), x03: col4.x(),
x10: col1.y(), x11: col2.y(), x12: col3.y(), x13: col4.y(),
x20: col1.z(), x21: col2.z(), x22: col3.z(), x23: col4.z(),
x30: col1.w(), x31: col2.w(), x32: col3.w(), x33: col4.w()
}
}
pub fn rh_look_to(camera: Point3, dir: Vec3, up: Vec3) -> Self {
let f = dir.normalize();
let s = cross(f, up).normalize();
@ -486,19 +562,19 @@ impl Mat4 {
let cam = camera.to_vec();
/*Mat4::new(
Mat4::new(
s.x().clone(), u.x().clone(), -f.x().clone(), 0.0,
s.y().clone(), u.y().clone(), -f.y().clone(), 0.0,
s.z().clone(), u.z().clone(), -f.z().clone(), 0.0,
-dot(&cam, &s), -dot(&cam, &u), dot(&cam, &f),1.0
)*/
)
Mat4::new(
/*Mat4::new(
s.x().clone(), s.y().clone(), s.z().clone(), 0.0,
u.x().clone(), u.y().clone(), u.z().clone(), 0.0,
-f.x().clone(), -f.y().clone(), -f.z().clone(), 0.0,
-dot(&cam, &s), -dot(&cam, &u), dot(&cam, &f), 1.0
)
)*/
}
@ -524,22 +600,31 @@ impl Mat4 {
let bottom = -ymax;
let top = ymax;
/*Mat4::new(
Mat4::new(
(2.0 * near) / (right - left), 0.0, (right + left) / (right - left), 0.0,
0.0, (2.0 * near) / (top - bottom), (top + bottom) / (top - bottom), 0.0,
0.0, 0.0, -(far + near) / (far - near), -(2.0 * far * near) / (far - near),
0.0, 0.0, -1.0, 0.0
)*/
)
Mat4::new(
/*Mat4::new(
(2.0 * near) / (right - left), 0.0, 0.0, 0.0,
0.0, (2.0 * near) / (top - bottom), 0.0, 0.0,
(right + left) / (right - left), (top + bottom) / (top - bottom), -(far + near) / (far - near), -1.0,
0.0, 0.0, -(2.0 * far * near) / (far - near), 0.0
)
)*/
}
pub fn orthographic_matrix(left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32) -> Self {
Mat4::new(
2.0 / (right - left), 0.0, 0.0, 0.0,
0.0, 2.0 / (top - bottom), 0.0, 0.0,
0.0, 0.0, -2.0 / (far - near), 0.0,
-(right + left) / (right - left), -(top + bottom) / (top - bottom), -(far + near) / (far - near), 1.0
)
}
pub fn get(&self, row: usize, col: usize) -> Option<f32> {
assert!(row <= 3, "This row ({}) is out of bounds! Bounds: 0..3", row);
assert!(col <= 3, "This row ({}) is out of bounds! Bounds: 0..3", col);
@ -575,6 +660,17 @@ impl Mat4 {
}
}
pub fn get_col(&self, col: usize) -> Option<Vec4> {
assert!(col <= 3, "This row ({}) is out of bounds! Bounds: 0..3", col);
match col {
0 => Some(Vec4::new(self.x00, self.x10, self.x20, self.x30)),
1 => Some(Vec4::new(self.x01, self.x11, self.x21, self.x31)),
2 => Some(Vec4::new(self.x02, self.x12, self.x22, self.x32)),
3 => Some(Vec4::new(self.x03, self.x13, self.x23, self.x33)),
_ => None
}
}
pub fn set(&mut self, row: usize, col: usize, element: f32) {
assert!(row <= 3, "The given row ({}) is out of bounds! Bounds: 0..3", row);
assert!(col <= 3, "The given column ({}) is out of bounds! Bounds: 0..3", col);
@ -610,6 +706,17 @@ impl Mat4 {
}
}
pub fn set_col(&mut self, col: usize, col_content: Vec4) {
assert!(col <= 3, "This column ({}) is out of bounds! Bounds: 0..3", col);
match col {
0 => { self.x00 = col_content.x(); self.x10 = col_content.y(); self.x20 = col_content.z(); self.x30 = col_content.w(); },
1 => { self.x01 = col_content.x(); self.x11 = col_content.y(); self.x21 = col_content.z(); self.x31 = col_content.w(); },
2 => { self.x02 = col_content.x(); self.x12 = col_content.y(); self.x22 = col_content.z(); self.x32 = col_content.w(); },
3 => { self.x03 = col_content.x(); self.x13 = col_content.y(); self.x23 = col_content.z(); self.x33 = col_content.w(); }
_ => {}
}
}
pub fn det(&self) -> f32 {
self.x00 * (self.x11 * (self.x22* self.x33 - self.x23 * self.x32)
- self.x21 * (self.x12 * self.x33 - self.x13 * self.x32)
@ -633,6 +740,18 @@ impl Mat4 {
x30: self.x03, x31: self.x13, x32: self.x23, x33: self.x33
}
}
pub fn swap_rows(&mut self, row1: usize, row2: usize) {
let tmp = self.get_row(row1).expect(format!("This row ({}) is out of bounds! Bounds: 0..2", row1).as_str());
self.set_row(row1, self.get_row(row2).expect(format!("This row ({}) is out of bounds! Bounds: 0..2", row2).as_str()));
self.set_row(row2, tmp);
}
pub fn swap_cols(&mut self, col1: usize, col2: usize) {
let tmp = self.get_col(col1).expect(format!("This row ({}) is out of bounds! Bounds: 0..2", col1).as_str());
self.set_col(col1, self.get_col(col2).expect(format!("This row ({}) is out of bounds! Bounds: 0..2", col2).as_str()));
self.set_col(col2, tmp);
}
}
impl Add<Mat4> for Mat4 {
@ -715,10 +834,10 @@ impl Mul<Mat4> for Mat4 {
impl Into<[[f32; 4]; 4]> for Mat4 {
fn into(self) -> [[f32; 4]; 4] {
[
[self.x00, self.x01, self.x02, self.x03],
[self.x10, self.x11, self.x12, self.x13],
[self.x20, self.x21, self.x22, self.x23],
[self.x30, self.x31, self.x32, self.x33],
[self.x00, self.x10, self.x20, self.x30],
[self.x01, self.x11, self.x21, self.x31],
[self.x02, self.x12, self.x22, self.x32],
[self.x03, self.x13, self.x23, self.x33],
]
}
}

View file

@ -1,12 +1,13 @@
use crate::point::{Point2, Point3};
use crate::quaternion::Quat;
use crate::utilities::acos;
use std::ops::{Add, Div, Mul, Sub};
use std::ops::{Add, AddAssign, Div, Mul, Sub, SubAssign};
use comet_log::*;
pub trait InnerSpace {
fn dot(&self, other: &Self) -> f32;
fn dist(&self, other: &Self) -> f32;
fn vAngle(&self, other: &Self) -> f32;
fn v_angle(&self, other: &Self) -> f32;
}
// ##################################################
@ -103,6 +104,13 @@ impl Add<Vec2> for Vec2 {
}
}
impl AddAssign for Vec2 {
fn add_assign(&mut self, other: Vec2) {
self.x += other.x;
self.y += other.y;
}
}
impl Sub<Vec2> for Vec2 {
type Output = Vec2;
@ -114,6 +122,13 @@ impl Sub<Vec2> for Vec2 {
}
}
impl SubAssign for Vec2 {
fn sub_assign(&mut self, other: Vec2) {
self.x -= other.x;
self.y -= other.y;
}
}
impl Mul<f32> for Vec2 {
type Output = Vec2;
@ -125,6 +140,228 @@ impl Mul<f32> for Vec2 {
}
}
impl Into<[f32;2]> for Vec2 {
fn into(self) -> [f32;2] {
[self.x, self.y]
}
}
impl Into<Vec2> for [f32;2] {
fn into(self) -> Vec2 {
Vec2 {
x: self[0],
y: self[1],
}
}
}
/// Representation of a 2D integer Vector
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct IVec2 {
x: i64,
y: i64,
}
impl IVec2 {
pub const X: IVec2 = IVec2 { x: 1, y: 0 };
pub const Y: IVec2 = IVec2 { x: 0, y: 1 };
pub const ZERO: IVec2 = IVec2 { x: 0, y: 0 };
pub const fn new(x: i64, y: i64) -> Self {
IVec2 { x, y }
}
pub fn from_point(p: Point2) -> Self {
Self { x: p.x() as i64, y: p.y() as i64 }
}
pub fn as_vec2(&self) -> Vec2 {
Vec2 {
x: self.x as f32,
y: self.y as f32,
}
}
pub fn x(&self) -> i64 {
self.x
}
pub fn y(&self) -> i64 {
self.y
}
pub fn set_x(&mut self, new_x: i64) {
self.x = new_x;
}
pub fn set_y(&mut self, new_y: i64) {
self.y = new_y;
}
pub fn length(&self) -> i64 {
((self.x * self.x + self.y * self.y) as f32).sqrt() as i64
}
pub fn normalize(&self) -> Self {
let factor = 1.0 / self.length() as f32;
IVec2 {
x: (factor * self.x as f32) as i64,
y: (factor * self.y as f32) as i64,
}
}
pub fn xx(&self) -> Self {
Self {
x: self.x,
y: self.x,
}
}
pub fn xy(&self) -> Self {
Self {
x: self.x,
y: self.y,
}
}
pub fn yx(&self) -> Self {
Self {
x: self.y,
y: self.x,
}
}
pub fn yy(&self) -> Self {
Self {
x: self.y,
y: self.y,
}
}
}
impl Add<IVec2> for IVec2 {
type Output = IVec2;
fn add(self, other: IVec2) -> IVec2 {
IVec2 {
x: self.x + other.x,
y: self.y + other.y,
}
}
}
impl Add<IVec2> for Vec2 {
type Output = Vec2;
fn add(self, other: IVec2) -> Vec2 {
Vec2 {
x: self.x + other.x as f32,
y: self.y + other.y as f32,
}
}
}
impl Add<Vec2> for IVec2 {
type Output = Vec2;
fn add(self, other: Vec2) -> Vec2 {
Vec2 {
x: self.x as f32 + other.x,
y: self.y as f32 + other.y,
}
}
}
impl AddAssign for IVec2 {
fn add_assign(&mut self, other: IVec2) {
self.x += other.x;
self.y += other.y;
}
}
impl Sub<IVec2> for IVec2 {
type Output = IVec2;
fn sub(self, other: IVec2) -> IVec2 {
IVec2 {
x: self.x - other.x,
y: self.y - other.y,
}
}
}
impl Sub<IVec2> for Vec2 {
type Output = Vec2;
fn sub(self, other: IVec2) -> Vec2 {
Vec2 {
x: self.x - other.x as f32,
y: self.y - other.y as f32,
}
}
}
impl Sub<Vec2> for IVec2 {
type Output = Vec2;
fn sub(self, other: Vec2) -> Vec2 {
Vec2 {
x: self.x as f32 - other.x,
y: self.y as f32 - other.y,
}
}
}
impl SubAssign for IVec2 {
fn sub_assign(&mut self, other: IVec2) {
self.x -= other.x;
self.y -= other.y;
}
}
impl Mul<f32> for IVec2 {
type Output = IVec2;
fn mul(self, other: f32) -> IVec2 {
IVec2 {
x: self.x * other as i64,
y: self.y * other as i64,
}
}
}
impl From<IVec2> for Vec2 {
fn from(v: IVec2) -> Vec2 {
Vec2 {
x: v.x as f32,
y: v.y as f32,
}
}
}
impl From<Vec2> for IVec2 {
fn from(v: Vec2) -> IVec2 {
IVec2 {
x: v.x as i64,
y: v.y as i64,
}
}
}
impl Into<[i64;2]> for IVec2 {
fn into(self) -> [i64;2] {
[self.x, self.y]
}
}
impl Into<[f32;2]> for IVec2 {
fn into(self) -> [f32;2] {
[self.x as f32, self.y as f32]
}
}
// ##################################################
// # VECTOR 3D #
// ##################################################
@ -181,17 +418,6 @@ impl Vec3 {
self.z = new_z;
}
pub fn into_quaternion(&self) -> Quat {
Quat {
s: 0.0,
v: Vec3 {
x: self.x,
y: self.y,
z: self.z,
}
}
}
pub fn length(&self) -> f32 {
(self.x * self.x + self.y * self.y + self.z * self.z).sqrt()
}
@ -408,6 +634,14 @@ impl Add<Vec3> for Vec3 {
}
}
impl AddAssign for Vec3 {
fn add_assign(&mut self, other: Vec3) {
self.x += other.x;
self.y += other.y;
self.z += other.z;
}
}
impl Sub<Vec3> for Vec3 {
type Output = Vec3;
@ -420,6 +654,14 @@ impl Sub<Vec3> for Vec3 {
}
}
impl SubAssign for Vec3 {
fn sub_assign(&mut self, other: Vec3) {
self.x -= other.x;
self.y -= other.y;
self.z -= other.z;
}
}
impl Mul<f32> for Vec3 {
type Output = Vec3;
@ -432,6 +674,202 @@ impl Mul<f32> for Vec3 {
}
}
impl Into<Quat> for Vec3 {
fn into(self) -> Quat {
Quat::new(0.0, self)
}
}
impl Into<[f32;3]> for Vec3 {
fn into(self) -> [f32;3] {
[self.x, self.y, self.z]
}
}
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct IVec3 {
pub x: i64,
pub y: i64,
pub z: i64,
}
impl IVec3 {
pub const X: IVec3 = IVec3 { x: 1, y: 0, z: 0 };
pub const Y: IVec3 = IVec3 { x: 0, y: 1, z: 0 };
pub const Z: IVec3 = IVec3 { x: 0, y: 0, z: 1 };
pub const ZERO: IVec3 = IVec3 { x: 0, y: 0, z: 0 };
pub const fn new(x: i64, y: i64, z: i64) -> Self {
IVec3 { x, y, z }
}
pub fn from_point(p: Point3) -> Self {
Self {
x: p.x() as i64,
y: p.y() as i64,
z: p.z() as i64,
}
}
pub fn x(&self) -> i64 {
self.x
}
pub fn y(&self) -> i64 {
self.y
}
pub fn z(&self) -> i64 {
self.z
}
pub fn set_x(&mut self, new_x: i64) {
self.x = new_x;
}
pub fn set_y(&mut self, new_y: i64) {
self.y = new_y;
}
pub fn set_z(&mut self, new_z: i64) {
self.z = new_z;
}
pub fn length(&self) -> i64 {
((self.x * self.x + self.y * self.y + self.z * self.z) as f32).sqrt() as i64
}
pub fn normalize(&self) -> Self {
let factor = 1 / self.length();
IVec3 {
x: factor * self.x,
y: factor * self.y,
z: factor * self.z,
}
}
}
impl Add<IVec3> for IVec3 {
type Output = IVec3;
fn add(self, other: IVec3) -> IVec3 {
IVec3 {
x: self.x + other.x,
y: self.y + other.y,
z: self.z + other.z,
}
}
}
impl Add<IVec3> for Vec3 {
type Output = Vec3;
fn add(self, other: IVec3) -> Vec3 {
Vec3 {
x: self.x + other.x as f32,
y: self.y + other.y as f32,
z: self.z + other.z as f32,
}
}
}
impl Add<Vec3> for IVec3 {
type Output = Vec3;
fn add(self, other: Vec3) -> Vec3 {
Vec3 {
x: self.x as f32 + other.x,
y: self.y as f32 + other.y,
z: self.z as f32 + other.z,
}
}
}
impl AddAssign for IVec3 {
fn add_assign(&mut self, other: IVec3) {
self.x += other.x;
self.y += other.y;
self.z += other.z;
}
}
impl Sub<IVec3> for IVec3 {
type Output = IVec3;
fn sub(self, other: IVec3) -> IVec3 {
IVec3 {
x: self.x - other.x,
y: self.y - other.y,
z: self.z - other.z,
}
}
}
impl Sub<IVec3> for Vec3 {
type Output = Vec3;
fn sub(self, other: IVec3) -> Vec3 {
Vec3 {
x: self.x - other.x as f32,
y: self.y - other.y as f32,
z: self.z - other.z as f32,
}
}
}
impl Sub<Vec3> for IVec3 {
type Output = Vec3;
fn sub(self, other: Vec3) -> Vec3 {
Vec3 {
x: self.x as f32 - other.x,
y: self.y as f32 - other.y,
z: self.z as f32 - other.z,
}
}
}
impl SubAssign for IVec3 {
fn sub_assign(&mut self, other: IVec3) {
self.x -= other.x;
self.y -= other.y;
}
}
impl Mul<f32> for IVec3 {
type Output = IVec3;
fn mul(self, other: f32) -> IVec3 {
IVec3 {
x: self.x * other as i64,
y: self.y * other as i64,
z: self.z * other as i64,
}
}
}
impl From<IVec3> for Vec3 {
fn from(v: IVec3) -> Vec3 {
Vec3 {
x: v.x as f32,
y: v.y as f32,
z: v.z as f32,
}
}
}
impl From<Vec3> for IVec3 {
fn from(v: Vec3) -> IVec3 {
IVec3 {
x: v.x as i64,
y: v.y as i64,
z: v.z as i64,
}
}
}
// ##################################################
// # VECTOR 4D #
// ##################################################
@ -2568,6 +3006,15 @@ impl Add<Vec4> for Vec4 {
}
}
impl AddAssign for Vec4 {
fn add_assign(&mut self, other: Vec4) {
self.x += other.x;
self.y += other.y;
self.z += other.z;
self.w += other.w;
}
}
impl Sub<Vec4> for Vec4 {
type Output = Vec4;
@ -2581,6 +3028,15 @@ impl Sub<Vec4> for Vec4 {
}
}
impl SubAssign for Vec4 {
fn sub_assign(&mut self, other: Vec4) {
self.x -= other.x;
self.y -= other.y;
self.z -= other.z;
self.w -= other.w;
}
}
impl Mul<f32> for Vec4 {
type Output = Vec4;
@ -2594,6 +3050,12 @@ impl Mul<f32> for Vec4 {
}
}
impl Into<[f32;4]> for Vec4 {
fn into(self) -> [f32;4] {
[self.x, self.y, self.z, self.w]
}
}
impl InnerSpace for Vec2 {
fn dot(&self, other: &Self) -> f32 {
self.x * other.x + self.y * other.y
@ -2607,7 +3069,8 @@ impl InnerSpace for Vec2 {
.length()
}
fn vAngle(&self, other: &Self) -> f32 {
fn v_angle(&self, other: &Self) -> f32 {
//debug!("{:?}", dot(self,other)/(self.length()*other.length()));
acos(dot(self, other) / (self.length() * other.length()))
}
}
@ -2626,7 +3089,7 @@ impl InnerSpace for Vec3 {
.length()
}
fn vAngle(&self, other: &Self) -> f32 {
fn v_angle(&self, other: &Self) -> f32 {
acos(dot(self, other) / (self.length() * other.length()))
}
}
@ -2646,7 +3109,7 @@ impl InnerSpace for Vec4 {
.length()
}
fn vAngle(&self, other: &Self) -> f32 {
fn v_angle(&self, other: &Self) -> f32 {
acos(dot(self, other) / (self.length() * other.length()))
}
}
@ -2672,5 +3135,5 @@ pub fn v_dist<T: InnerSpace>(v1: &T, v2: &T) -> f32 {
}
pub fn v_angle<T: InnerSpace>(v1: &T, v2: &T) -> f32 {
v1.vAngle(v2)
v1.v_angle(v2)
}