Skip to content

Commit

Permalink
Merge pull request #84 from AryanpurTech/Object-Rotation-Improvement
Browse files Browse the repository at this point in the history
Object Rotation Improvements
  • Loading branch information
ElhamAryanpur authored Nov 9, 2024
2 parents 9791f93 + 903a87a commit bab9e7d
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "blue_engine"
version = "0.5.20"
version = "0.5.21"
authors = ["Elham Aryanpur <[email protected]>"]
edition = "2021"
description = "General-Purpose, Easy-to-use, Fast, and Portable graphics engine"
Expand Down
13 changes: 12 additions & 1 deletion src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,17 @@ pub enum RotateAxis {
unsafe impl Send for RotateAxis {}
unsafe impl Sync for RotateAxis {}

/// Defines how the rotation amount is
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum RotateAmount {
#[doc(hidden)]
Radians(f32),
#[doc(hidden)]
Degrees(f32),
}
unsafe impl Send for RotateAmount {}
unsafe impl Sync for RotateAmount {}

/// Defines how the texture data is
#[derive(Debug, Clone)]
pub enum TextureData {
Expand Down Expand Up @@ -755,7 +766,7 @@ pub enum ExecuteOrder {
pub struct Window {
/// The winit window itself.
pub window: Option<std::sync::Arc<crate::winit::window::Window>>,
/// Default attributes of the window\
/// Default attributes of the window
pub default_attributes: winit::window::WindowAttributes,
/// Whether the engine should close.
pub should_close: bool,
Expand Down
71 changes: 66 additions & 5 deletions src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::header::{
};
use crate::uniform_type::{Array4, Matrix};
use crate::utils::default_resources::{DEFAULT_MATRIX_4, DEFAULT_SHADER, DEFAULT_TEXTURE};
use crate::{ObjectStorage, StringBuffer, UnsignedIntType};
use crate::{ObjectStorage, RotateAmount, StringBuffer, UnsignedIntType};

impl Renderer {
/// Creates a new object
Expand Down Expand Up @@ -234,11 +234,10 @@ impl Object {
}

/// Rotates the object in the axis you specify
///
/// THIS METHOD IS DEPRECATED, USE [crate::Object::set_rotation] or [crate::Object::rotate]
#[deprecated]
pub fn set_rotatation(&mut self, angle: f32, axis: RotateAxis) -> &mut Self {
// The reason for using different transformation matrix is because
// of alteration of translation that happens due to rotation. The
// solution suggested by https://github.com/tksuoran fixed this through
// separating the matrices and multiplying them back at the end.
let mut rotation_matrix = self.rotation_matrix;
let axis = match axis {
RotateAxis::X => {
Expand All @@ -263,6 +262,68 @@ impl Object {
self
}

/// Sets the rotation of the object in the axis you specify
pub fn set_rotation(&mut self, amount: RotateAmount, axis: RotateAxis) -> &mut Self {
let mut rotation_matrix = self.rotation_matrix;

let amount_radians = match amount {
RotateAmount::Radians(amount) => amount,
RotateAmount::Degrees(amount) => amount.to_radians(),
};
let axis = match axis {
RotateAxis::X => {
self.rotation.x = amount_radians;
nalgebra_glm::Vec3::x_axis()
}
RotateAxis::Y => {
self.rotation.y = amount_radians;
nalgebra_glm::Vec3::y_axis()
}
RotateAxis::Z => {
self.rotation.z = amount_radians;
nalgebra_glm::Vec3::z_axis()
}
};

rotation_matrix = nalgebra_glm::rotate(&rotation_matrix, amount_radians, &axis);
self.rotation_matrix = rotation_matrix;
self.inverse_matrices();

self.changed = true;
self
}

/// Rotates the object in the axis you specify
pub fn rotate(&mut self, amount: RotateAmount, axis: RotateAxis) -> &mut Self {
let mut rotation_matrix = self.rotation_matrix;

let amount_radians = match amount {
RotateAmount::Radians(amount) => amount,
RotateAmount::Degrees(amount) => amount.to_radians(),
};
let axis = match axis {
RotateAxis::X => {
self.rotation.x += amount_radians;
nalgebra_glm::Vec3::x_axis()
}
RotateAxis::Y => {
self.rotation.y += amount_radians;
nalgebra_glm::Vec3::y_axis()
}
RotateAxis::Z => {
self.rotation.z += amount_radians;
nalgebra_glm::Vec3::z_axis()
}
};

rotation_matrix = nalgebra_glm::rotate(&rotation_matrix, amount_radians, &axis);
self.rotation_matrix = rotation_matrix;
self.inverse_matrices();

self.changed = true;
self
}

/// Moves the object by the amount you specify in the axis you specify
pub fn set_translation(&mut self, x: f32, y: f32, z: f32) -> &mut Self {
self.position.x -= x;
Expand Down

0 comments on commit bab9e7d

Please sign in to comment.