Skip to content

Commit

Permalink
de_messages: Add nalgebra conversion impls
Browse files Browse the repository at this point in the history
  • Loading branch information
Indy2222 committed Oct 12, 2023
1 parent 72681cc commit 73440c5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/messages/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ de_types.workspace = true
bevy = { workspace = true, optional = true }
bincode.workspace = true
glam.workspace = true
nalgebra.workspace = true
thiserror.workspace = true
49 changes: 49 additions & 0 deletions crates/messages/src/players/geom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use bincode::{Decode, Encode};
#[cfg(feature = "bevy")]
use glam::Quat;
use glam::{Vec2, Vec3, Vec4};
use nalgebra::{Point2, Point3, Point4};

/// Network representation of translation and rotation. Note that scale is
/// assumed to be always 1.0 along all axes.
Expand Down Expand Up @@ -58,12 +59,27 @@ impl From<Vec2> for Vec2Net {
}
}

impl From<Point2<f32>> for Vec2Net {
fn from(point: Point2<f32>) -> Self {
Self {
x: point.x,
y: point.y,
}
}
}

impl From<Vec2Net> for Vec2 {
fn from(vec: Vec2Net) -> Self {
Self::new(vec.x, vec.y)
}
}

impl From<Vec2Net> for Point2<f32> {
fn from(vec: Vec2Net) -> Self {
Self::new(vec.x, vec.y)
}
}

#[derive(Clone, Copy, Debug, Encode, Decode)]
pub struct Vec3Net {
x: f32,
Expand All @@ -81,12 +97,28 @@ impl From<Vec3> for Vec3Net {
}
}

impl From<Point3<f32>> for Vec3Net {
fn from(point: Point3<f32>) -> Self {
Self {
x: point.x,
y: point.y,
z: point.z,
}
}
}

impl From<Vec3Net> for Vec3 {
fn from(vec: Vec3Net) -> Self {
Self::new(vec.x, vec.y, vec.z)
}
}

impl From<Vec3Net> for Point3<f32> {
fn from(vec: Vec3Net) -> Self {
Self::new(vec.x, vec.y, vec.z)
}
}

#[derive(Clone, Copy, Debug, Encode, Decode)]
pub struct Vec4Net {
x: f32,
Expand All @@ -106,8 +138,25 @@ impl From<Vec4> for Vec4Net {
}
}

impl From<Point4<f32>> for Vec4Net {
fn from(point: Point4<f32>) -> Self {
Self {
x: point.x,
y: point.y,
z: point.z,
w: point.w,
}
}
}

impl From<Vec4Net> for Vec4 {
fn from(vec: Vec4Net) -> Self {
Self::new(vec.x, vec.y, vec.z, vec.w)
}
}

impl From<Vec4Net> for Point4<f32> {
fn from(vec: Vec4Net) -> Self {
Self::new(vec.x, vec.y, vec.z, vec.w)
}
}

0 comments on commit 73440c5

Please sign in to comment.