-
-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic impls of
Rect2
, Rect2i
, Aabb
, Plane
Add `Mul<X>` impls for `Transform2/3D` for the new types Add `min/max` functions for `Vector2/3`
- Loading branch information
Showing
13 changed files
with
564 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
use godot_ffi as sys; | ||
use sys::{ffi_methods, GodotFfi}; | ||
|
||
use super::Vector3; | ||
|
||
/// Axis-aligned bounding box. | ||
/// | ||
/// `Aabb` consists of a position, a size, and several utility functions. It is typically used for | ||
/// fast overlap tests. | ||
/// | ||
/// Currently most methods are only available through [`InnerAabb`](super::inner::InnerAabb). | ||
/// | ||
/// The 2D counterpart to `Aabb` is [`Rect2`](super::Rect2). | ||
#[derive(Default, Copy, Clone, PartialEq, Debug)] | ||
#[repr(C)] | ||
pub struct Aabb { | ||
pub position: Vector3, | ||
pub size: Vector3, | ||
} | ||
|
||
impl Aabb { | ||
/// Create a new `Aabb` from a position and a size. | ||
/// | ||
/// _Godot equivalent: `Aabb(Vector3 position, Vector3 size)`_ | ||
#[inline] | ||
pub const fn new(position: Vector3, size: Vector3) -> Self { | ||
Self { position, size } | ||
} | ||
|
||
/// The end of the `Aabb` calculated as `position + size`. | ||
/// | ||
/// _Godot equivalent: `Aabb.size`_ | ||
#[inline] | ||
pub fn end(&self) -> Vector3 { | ||
self.position + self.size | ||
} | ||
|
||
/// Set size based on desired end-point. | ||
/// | ||
/// _Godot equivalent: `Aabb.size`_ | ||
#[inline] | ||
pub fn set_end(&mut self, end: Vector3) { | ||
self.size = end - self.position | ||
} | ||
|
||
/// Returns `true` if the two `Aabb`s are approximately equal, by calling `is_equal_approx` on | ||
/// `position` and `size`. | ||
/// | ||
/// _Godot equivalent: `Aabb.is_equal_approx()`_ | ||
#[inline] | ||
pub fn is_equal_approx(&self, other: &Self) -> bool { | ||
self.position.is_equal_approx(other.position) && self.size.is_equal_approx(other.size) | ||
} | ||
|
||
/* Add in when `Aabb::abs()` is implemented. | ||
/// Assert that the size of the `Aabb` is not negative. | ||
/// | ||
/// Certain functions will fail to give a correct result if the size is negative. | ||
#[inline] | ||
fn assert_nonnegative(&self) { | ||
assert!( | ||
self.size.x >= 0.0 && self.size.y >= 0.0 && self.size.z >= 0.0, | ||
"Aabb size is negative, this is not supported. Use Aabb.abs() to get an Aabb with a positive size." | ||
); | ||
} | ||
*/ | ||
} | ||
|
||
impl GodotFfi for Aabb { | ||
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
use std::ops::Neg; | ||
|
||
use godot_ffi as sys; | ||
use sys::{ffi_methods, GodotFfi}; | ||
|
||
use super::{is_equal_approx, real, Vector3}; | ||
|
||
/// 3D plane in Hessian form: `a*b + b*y + c*z + d = 0` | ||
/// | ||
/// Currently most methods are only available through [`InnerPlane`](super::inner::InnerPlane). | ||
/// | ||
/// Note: almost all methods on `Plane` require that the `normal` vector have | ||
/// unit length and will panic if this invariant is violated. This is not separately | ||
/// annotated for each method. | ||
#[derive(Copy, Clone, PartialEq, Debug)] | ||
#[repr(C)] | ||
pub struct Plane { | ||
pub normal: Vector3, | ||
pub d: real, | ||
} | ||
|
||
impl Plane { | ||
/// Creates a new `Plane` from the `normal` and the distance from the origin `d`. | ||
/// | ||
/// # Panics | ||
/// In contrast to construction via `Plane { normal, d }`, this verifies that `normal` has unit length, and will | ||
/// panic if this is not the case. | ||
/// | ||
/// _Godot equivalent: `Plane(Vector3 normal, float d)`_ | ||
#[inline] | ||
pub fn new(normal: Vector3, d: real) -> Self { | ||
let plane = Self { normal, d }; | ||
plane.assert_normalized(); | ||
plane | ||
} | ||
|
||
/// Create a new `Plane` through the origin from a normal. | ||
/// | ||
/// # Panics | ||
/// See [`Self::new()`]. | ||
/// | ||
/// _Godot equivalent: `Plane(Vector3 normal)`_ | ||
#[inline] | ||
pub fn new_origin(normal: Vector3) -> Self { | ||
Self::new(normal, 0.0) | ||
} | ||
|
||
/// Create a new `Plane` from normal and a point in the plane. | ||
/// | ||
/// # Panics | ||
/// See [`Self::new()`]. | ||
/// | ||
/// _Godot equivalent: `Plane(Vector3 normal, Vector3 point)`_ | ||
#[inline] | ||
pub fn new_normal_point(normal: Vector3, point: Vector3) -> Self { | ||
Self::new(normal, normal.dot(point)) | ||
} | ||
|
||
/// Creates a new `Plane` from normal and origin distance. | ||
/// | ||
/// `a`, `b`, `c` are used for the `normal` vector. | ||
/// `d` is the distance from the origin. | ||
/// | ||
/// # Panics | ||
/// See [`Self::new()`]. | ||
/// | ||
/// _Godot equivalent: `Plane(float a, float b, float c, float d)`_ | ||
#[inline] | ||
pub fn from_coordinates(a: real, b: real, c: real, d: real) -> Self { | ||
Self::new(Vector3::new(a, b, c), d) | ||
} | ||
|
||
/// Creates a new `Plane` from three points, given in clockwise order. | ||
/// | ||
/// If all three points are collinear, returns `None`. | ||
/// | ||
/// _Godot equivalent: `Plane(Vector3 point1, Vector3 point2, Vector3 point3)`_ | ||
#[inline] | ||
pub fn from_points(a: Vector3, b: Vector3, c: Vector3) -> Option<Self> { | ||
let normal = (a - c).cross(a - b).normalized(); | ||
|
||
if normal.is_finite() { | ||
Some(Self { | ||
normal, | ||
d: normal.dot(a), | ||
}) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
/// Returns `true` if the two `Plane`s are approximately equal, by calling `is_equal_approx` on | ||
/// `normal` and `d` or on `-normal` and `-d`. | ||
/// | ||
/// _Godot equivalent: `Plane.is_equal_approx()`_ | ||
#[inline] | ||
pub fn is_equal_approx(&self, other: &Self) -> bool { | ||
(self.normal.is_equal_approx(other.normal) && is_equal_approx(self.d, other.d)) | ||
|| (self.normal.is_equal_approx(-other.normal) && is_equal_approx(self.d, -other.d)) | ||
} | ||
|
||
#[inline] | ||
fn assert_normalized(self) { | ||
assert!( | ||
self.normal.is_normalized(), | ||
"Plane {:?} -- normal does not have unit length", | ||
self.normal | ||
); | ||
} | ||
} | ||
|
||
impl Neg for Plane { | ||
type Output = Plane; | ||
|
||
fn neg(self) -> Self::Output { | ||
Self::new(-self.normal, -self.d) | ||
} | ||
} | ||
|
||
impl GodotFfi for Plane { | ||
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. } | ||
} |
Oops, something went wrong.