Skip to content

Commit

Permalink
Merge #718
Browse files Browse the repository at this point in the history
718: Vectors: Default, missing Vector2 constants, documentation r=toasteater a=Bromeon

Completes a part of the recent `Vector2`/`Vector3` refactoring:
* `impl Default` (zero vector) for both
* associated constants for `Vector2`
* documentation of constants

Let me know if there's other obvious things that could fit.

Co-authored-by: Jan Haller <bromeon@gmail.com>
  • Loading branch information
bors[bot] and Bromeon authored Apr 3, 2021
2 parents 3ad9b65 + 227fdda commit 4c5184c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
27 changes: 24 additions & 3 deletions gdnative-core/src/core_types/vector2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::IsEqualApprox;
use glam::Vec2;
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};

#[derive(Copy, Clone, Debug, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq, Default)]
#[repr(C)]
pub struct Vector2 {
pub x: f32,
Expand All @@ -13,6 +13,27 @@ pub struct Vector2 {
///
/// See the official [`Godot documentation`](https://docs.godotengine.org/en/3.1/classes/class_vector2.html).
impl Vector2 {
/// The zero vector.
pub const ZERO: Vector2 = Vector2::new(0.0, 0.0);

/// A vector with all components set to 1. Typically used for scaling.
pub const ONE: Vector2 = Vector2::new(1.0, 1.0);

/// A vector with all components set to +infinity.
pub const INF: Vector2 = Vector2::new(f32::INFINITY, f32::INFINITY);

/// Unit vector in -X direction.
pub const LEFT: Vector2 = Vector2::new(-1.0, 0.0);

/// Unit vector in +X direction.
pub const RIGHT: Vector2 = Vector2::new(1.0, 0.0);

/// Unit vector in -Y direction (the Y axis points down in 2D).
pub const UP: Vector2 = Vector2::new(0.0, -1.0);

/// Unit vector in +Y direction (the Y axis points down in 2D).
pub const DOWN: Vector2 = Vector2::new(0.0, 1.0);

/// Constructs a new Vector2 from the given x and y.
#[inline]
pub const fn new(x: f32, y: f32) -> Self {
Expand Down Expand Up @@ -455,7 +476,7 @@ mod tests {
V::new(-1.2, 0.8),
V::new(1.2, 10.3),
V::new(-5.4, 4.2),
0.2
0.2,
))
);

Expand All @@ -464,7 +485,7 @@ mod tests {
V::new(-3.7, 2.1),
V::new(5.4, -8.5),
V::new(-10.8, -6.6),
0.6
0.6,
))
);
}
Expand Down
19 changes: 18 additions & 1 deletion gdnative-core/src/core_types/vector3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::IsEqualApprox;
use glam::Vec3A;
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};

#[derive(Copy, Clone, Debug, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq, Default)]
#[repr(C)]
pub struct Vector3 {
pub x: f32,
Expand All @@ -23,14 +23,31 @@ pub enum Axis {
///
/// See the official [`Godot documentation`](https://docs.godotengine.org/en/3.1/classes/class_vector3.html).
impl Vector3 {
/// The zero vector.
pub const ZERO: Self = Self::new(0.0, 0.0, 0.0);

/// A vector with all components set to 1. Typically used for scaling.
pub const ONE: Self = Self::new(1.0, 1.0, 1.0);

/// A vector with all components set to +infinity.
pub const INF: Self = Self::new(f32::INFINITY, f32::INFINITY, f32::INFINITY);

/// Unit vector in -X direction.
pub const LEFT: Self = Self::new(-1.0, 0.0, 0.0);

/// Unit vector in +X direction.
pub const RIGHT: Self = Self::new(1.0, 0.0, 0.0);

/// Unit vector in +Y direction.
pub const UP: Self = Self::new(0.0, 1.0, 0.0);

/// Unit vector in -Y direction.
pub const DOWN: Self = Self::new(0.0, -1.0, 0.0);

/// Unit vector in -Z direction.
pub const FORWARD: Self = Self::new(0.0, 0.0, -1.0);

/// Unit vector in +Z direction.
pub const BACK: Self = Self::new(0.0, 0.0, 1.0);

/// Returns a Vector3 with the given components.
Expand Down

0 comments on commit 4c5184c

Please sign in to comment.