Skip to content

Commit

Permalink
finish commenting tools module
Browse files Browse the repository at this point in the history
  • Loading branch information
cdsupina committed Jul 1, 2021
1 parent d374dd1 commit d1d5cad
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
25 changes: 15 additions & 10 deletions thetawave_lib/src/tools/math.rs
Original file line number Diff line number Diff line change
@@ -1,7 1,7 @@
use amethyst::{
core::math::Vector3
};
use amethyst::core::math::Vector3;

/// Calculates distance between two points
// TODO: change Vector3 to Point3 (across entire project where appropriate)
pub fn distance(point_a: Vector3<f32>, point_b: Vector3<f32>, is_2d: bool) -> f32 {
let mut value = (point_a.x - point_b.x).powi(2) (point_a.y - point_b.y).powi(2);

Expand All @@ -12,14 12,17 @@ pub fn distance(point_a: Vector3<f32>, point_b: Vector3<f32>, is_2d: bool) -> f3
value.sqrt()
}

/// Calculate the signed modulo of two numbers
pub fn signed_modulo(a: f32, n: f32) -> f32 {
a - (a / n).floor() * n
}

/// Rotate x coordinate by an angle
pub fn rotate_x(x: f32, y: f32, angle: f32) -> f32 {
(x * angle.cos()) (y * angle.sin())
}

/// Rotate y coordinate by an angle
pub fn rotate_y(x: f32, y: f32, angle: f32) -> f32 {
(-x * angle.sin()) (y * angle.cos())
}
Expand Down Expand Up @@ -50,9 53,11 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

/// Simple vector struct
#[derive(Clone, Copy, Debug)]
pub struct Vector(pub f32, pub f32);

/// Use separating axis theorem to check for collisions between two polygons
pub fn sat_is_colliding(poly1: &[Vector], poly2: &[Vector], max_dist: &Option<f32>) -> bool {
let estimated_dist = (poly1[1].0 - poly2[0].0).powi(2) (poly1[1].1 - poly2[0].1).powi(2);
match max_dist {
Expand All @@ -61,7 66,7 @@ pub fn sat_is_colliding(poly1: &[Vector], poly2: &[Vector], max_dist: &Option<f3
}
}

pub fn run_sat(poly1: &[Vector], poly2: &[Vector]) -> bool {
fn run_sat(poly1: &[Vector], poly2: &[Vector]) -> bool {
let mut edges = Vec::new();
edges.append(&mut poly_to_edges(&poly1));
edges.append(&mut poly_to_edges(&poly2));
Expand All @@ -77,11 82,11 @@ pub fn run_sat(poly1: &[Vector], poly2: &[Vector]) -> bool {
true
}

pub fn edge_vector(point1: Vector, point2: Vector) -> Vector {
fn edge_vector(point1: Vector, point2: Vector) -> Vector {
Vector(point2.0 - point1.0, point2.1 - point1.1)
}

pub fn poly_to_edges(poly: &[Vector]) -> Vec<Vector> {
fn poly_to_edges(poly: &[Vector]) -> Vec<Vector> {
// Returns a Vec of the edges of the poly as vectors
let mut edges = Vec::with_capacity(poly.len());

Expand All @@ -92,15 97,15 @@ pub fn poly_to_edges(poly: &[Vector]) -> Vec<Vector> {
edges
}

pub fn orthogonal(vector: Vector) -> Vector {
fn orthogonal(vector: Vector) -> Vector {
Vector(vector.1, -vector.0)
}

pub fn dot_product(vector1: Vector, vector2: Vector) -> f32 {
fn dot_product(vector1: Vector, vector2: Vector) -> f32 {
vector1.0 * vector2.0 vector1.1 * vector2.1
}

pub fn project(poly: &[Vector], axis: Vector) -> Vector {
fn project(poly: &[Vector], axis: Vector) -> Vector {
let mut min: Option<f32> = None;
let mut max: Option<f32> = None;

Expand All @@ -120,6 125,6 @@ pub fn project(poly: &[Vector], axis: Vector) -> Vector {
Vector(min.unwrap(), max.unwrap())
}

pub fn overlap(projection1: Vector, projection2: Vector) -> bool {
fn overlap(projection1: Vector, projection2: Vector) -> bool {
projection1.0 <= projection2.1 && projection2.0 <= projection1.1
}
5 changes: 1 addition & 4 deletions thetawave_lib/src/tools/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 3,7 @@ mod random;
mod timer;

pub use self::{
math::{
distance, dot_product, edge_vector, orthogonal, overlap, poly_to_edges, project, rotate_x,
rotate_y, run_sat, sat_is_colliding, signed_modulo, Vector,
},
math::{distance, rotate_x, rotate_y, sat_is_colliding, signed_modulo, Vector},
random::weighted_rng,
timer::Timer,
};
9 changes: 9 additions & 0 deletions thetawave_lib/src/tools/timer.rs
Original file line number Diff line number Diff line change
@@ -1,16 1,22 @@
/// Counts down a set amount of time then resets
pub struct Timer {
period: f32,
countdown: f32,
}

impl Timer {
/// Creates a new instance of Timer
/// Sets both the period and countdown to given value
pub fn new(period: f32) -> Self {
Timer {
period,
countdown: period,
}
}

/// Updates the timer
/// Subtracts value from countdown
/// Resets if countdown reaches 0 and returns true
pub fn update(&mut self, delta_time: f32) -> bool {
self.countdown -= delta_time;

Expand All @@ -21,14 27,17 @@ impl Timer {
false
}

/// Resets the countdown value to the period
pub fn reset(&mut self) {
self.countdown = self.period;
}

/// Returns the period of the timer
pub fn get_period(&self) -> f32 {
self.period
}

/// Returns the difference between the period and countdown
pub fn get_time_left(&self) -> f32 {
self.period - self.countdown
}
Expand Down

0 comments on commit d1d5cad

Please sign in to comment.