Skip to content

Commit fce0a9b

Browse files
Demindirotoasteater
authored andcommitted
Replace euclid with glam
1 parent 9a703d2 commit fce0a9b

27 files changed

+938
-303
lines changed

examples/dodge_the_creeps/src/main_scene.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ impl Main {
116116
mob_owner
117117
.set_linear_velocity(Vector2::new(rng.gen_range(x.min_speed..x.max_speed), 0.0));
118118

119-
mob_owner
120-
.set_linear_velocity(mob_owner.linear_velocity().rotated(Angle { radians: d }));
119+
mob_owner.set_linear_velocity(mob_owner.linear_velocity().rotated(d));
121120

122121
let hud = unsafe { owner.get_node_as_instance::<hud::Hud>("hud").unwrap() };
123122

examples/dodge_the_creeps/src/player.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl Player {
3232
#[export]
3333
fn _ready(&mut self, owner: &Area2D) {
3434
let viewport = owner.get_viewport_rect();
35-
self.screen_size = viewport.size.to_vector();
35+
self.screen_size = viewport.size;
3636
owner.hide();
3737
}
3838

@@ -61,7 +61,7 @@ impl Player {
6161
}
6262

6363
if velocity.length() > 0.0 {
64-
velocity = velocity.normalize() * self.speed;
64+
velocity = velocity.normalized() * self.speed;
6565

6666
let animation;
6767

@@ -82,8 +82,11 @@ impl Player {
8282
}
8383

8484
let change = velocity * delta;
85-
let position =
86-
(owner.global_position() + change).clamp(Vector2::new(0.0, 0.0), self.screen_size);
85+
let position = owner.global_position() + change;
86+
let position = Vector2::new(
87+
position.x.max(0.0).min(self.screen_size.x),
88+
position.y.max(0.0).min(self.screen_size.y),
89+
);
8790
owner.set_global_position(position);
8891
}
8992

examples/scene_create/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ crate-type = ["cdylib"]
1010

1111
[dependencies]
1212
gdnative = { path = "../../gdnative" }
13-
euclid = "0.22.1"
13+
glam = "0.13.0"

examples/scene_create/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use euclid::vec3;
21
use gdnative::prelude::*;
32

43
#[derive(Debug, Clone, PartialEq)]
@@ -64,7 +63,7 @@ impl SceneCreate {
6463

6564
let x = (self.children_spawned % 10) as f32;
6665
let z = (self.children_spawned / 10) as f32;
67-
spatial.translate(vec3(-10.0 + x * 2.0, 0.0, -10.0 + z * 2.0));
66+
spatial.translate(Vector3::new(-10.0 + x * 2.0, 0.0, -10.0 + z * 2.0));
6867

6968
// You need to parent the new scene under some node if you want it in the scene.
7069
// We parent it under ourselves.

gdnative-core/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ type_tag_fallback = []
2020
gdnative-sys = { path = "../gdnative-sys", version = "0.9.3" }
2121
libc = "0.2"
2222
approx = "0.4.0"
23-
euclid = "0.22.1"
23+
glam = "0.13.0"
2424
indexmap = "1.6.0"
2525
ahash = "0.7.0"
2626

2727
gdnative-impl-proc-macros = { path = "../impl/proc_macros", version = "=0.9.3" }
2828

2929
bitflags = { version = "1.2", optional = true }
3030
parking_lot = { version = "0.11.0", optional = true }
31-
atomic-take = "1.0.0"
31+
atomic-take = "1.0.0"

gdnative-core/src/core_types/geom/basis.rs

Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::core_types::{Quat, Vector3};
2-
use euclid::{approxeq::ApproxEq, default, Transform3D, UnknownUnit, Vector3D};
1+
use crate::core_types::{IsEqualApprox, Quat, Vector3};
2+
use core::ops::Mul;
33

44
/// A 3x3 matrix.
55
#[repr(C)]
@@ -96,7 +96,7 @@ impl Basis {
9696
#[inline]
9797
pub fn from_axis_angle(axis: &Vector3, phi: f32) -> Self {
9898
assert!(
99-
axis.length().approx_eq(&1.0),
99+
axis.length().is_equal_approx(1.0),
100100
"The axis Vector3 must be normalized."
101101
);
102102

@@ -147,7 +147,7 @@ impl Basis {
147147
];
148148

149149
let det: f32 = x.x * co[0] + x.y * co[1] + x.z * co[2];
150-
assert!(!det.approx_eq(&0.0), "Determinant was zero");
150+
assert!(!det.is_equal_approx(0.0), "Determinant was zero");
151151

152152
let s: f32 = 1.0 / det;
153153

@@ -210,7 +210,7 @@ impl Basis {
210210
#[inline]
211211
pub fn orthonormalize(&mut self) {
212212
assert!(
213-
!self.determinant().approx_eq(&0.0),
213+
!self.determinant().is_equal_approx(0.0),
214214
"Determinant should not be zero."
215215
);
216216

@@ -219,11 +219,11 @@ impl Basis {
219219
let mut y = self.y();
220220
let mut z = self.z();
221221

222-
x = x.normalize();
222+
x = x.normalized();
223223
y = y - x * (x.dot(y));
224-
y = y.normalize();
224+
y = y.normalized();
225225
z = z - x * (x.dot(z)) - y * (y.dot(z));
226-
z = z.normalize();
226+
z = z.normalized();
227227

228228
self.set_x(x);
229229
self.set_y(y);
@@ -241,23 +241,23 @@ impl Basis {
241241

242242
/// Returns `true` if `self` and `other` are approximately equal.
243243
#[inline]
244-
pub fn approx_eq(&self, other: &Basis) -> bool {
245-
self.elements[0].approx_eq(&other.elements[0])
246-
&& self.elements[1].approx_eq(&other.elements[1])
247-
&& self.elements[2].approx_eq(&other.elements[2])
244+
pub fn is_equal_approx(&self, other: &Basis) -> bool {
245+
self.elements[0].is_equal_approx(other.elements[0])
246+
&& self.elements[1].is_equal_approx(other.elements[1])
247+
&& self.elements[2].is_equal_approx(other.elements[2])
248248
}
249249

250250
#[inline]
251251
fn is_orthogonal(&self) -> bool {
252252
let identity = Self::identity();
253253
let m = (*self) * self.transposed();
254-
m.approx_eq(&identity)
254+
m.is_equal_approx(&identity)
255255
}
256256

257257
#[inline]
258258
fn is_rotation(&self) -> bool {
259259
let det = self.determinant();
260-
det.approx_eq(&1.0) && self.is_orthogonal()
260+
det.is_equal_approx(1.0) && self.is_orthogonal()
261261
}
262262

263263
/// Multiplies the matrix from left by the rotation matrix: M -> R.M
@@ -328,9 +328,9 @@ impl Basis {
328328
let k = (i + 2) % 3;
329329

330330
let elements_arr: [[f32; 3]; 3] = [
331-
matrix.elements[0].to_array(),
332-
matrix.elements[1].to_array(),
333-
matrix.elements[2].to_array(),
331+
*matrix.elements[0].as_ref(),
332+
*matrix.elements[1].as_ref(),
333+
*matrix.elements[2].as_ref(),
334334
];
335335

336336
let mut s = (elements_arr[i][i] - elements_arr[j][j] - elements_arr[k][k] + 1.0).sqrt();
@@ -343,7 +343,7 @@ impl Basis {
343343
}
344344

345345
let [a, b, c, r] = temp;
346-
Quat::quaternion(a, b, c, r)
346+
Quat::new(a, b, c, r)
347347
}
348348

349349
/// Returns the scale of the matrix.
@@ -384,17 +384,17 @@ impl Basis {
384384
/// See [`Basis::to_quat`](#method.to_quat) if you need a quaternion instead.
385385
#[inline]
386386
pub fn to_euler(&self) -> Vector3 {
387-
let mut euler = Vector3::zero();
387+
let mut euler = Vector3::ZERO;
388388

389389
let m12 = self.elements[1].z;
390390
if m12 < 1.0 {
391391
if m12 > -1.0 {
392392
// is this a pure X rotation?
393-
if self.elements[1].x.approx_eq(&0.0)
394-
&& self.elements[0].y.approx_eq(&0.0)
395-
&& self.elements[0].z.approx_eq(&0.0)
396-
&& self.elements[2].x.approx_eq(&0.0)
397-
&& self.elements[0].x.approx_eq(&1.0)
393+
if self.elements[1].x.is_equal_approx(0.0)
394+
&& self.elements[0].y.is_equal_approx(0.0)
395+
&& self.elements[0].z.is_equal_approx(0.0)
396+
&& self.elements[2].x.is_equal_approx(0.0)
397+
&& self.elements[0].x.is_equal_approx(1.0)
398398
{
399399
// return the simplest form (human friendlier in editor and scripts)
400400
euler.x = (-m12).atan2(self.elements[1].y);
@@ -471,6 +471,7 @@ impl Basis {
471471
)
472472
}
473473

474+
/*
474475
/// Creates a `Basis` from the rotation and scaling of the provided transform.
475476
#[inline]
476477
pub fn from_transform(transform: &default::Transform3D<f32>) -> Basis {
@@ -494,6 +495,7 @@ impl Basis {
494495
],
495496
}
496497
}
498+
*/
497499

498500
/// Transposed dot product with the **X Axis** of the matrix.
499501
#[inline]
@@ -580,6 +582,15 @@ impl core::ops::Mul<Basis> for Basis {
580582
}
581583
}
582584

585+
impl Mul<Vector3> for Basis {
586+
type Output = Vector3;
587+
588+
#[inline]
589+
fn mul(self, rhs: Self::Output) -> Self::Output {
590+
Self::Output::new(self.tdotx(rhs), self.tdoty(rhs), self.tdotz(rhs))
591+
}
592+
}
593+
583594
#[cfg(test)]
584595
#[allow(clippy::unreadable_literal)]
585596
mod tests {
@@ -620,7 +631,7 @@ mod tests {
620631
#[test]
621632
fn set_is_sane() {
622633
let mut basis = Basis {
623-
elements: [Vector3::zero(), Vector3::zero(), Vector3::zero()],
634+
elements: [Vector3::ZERO, Vector3::ZERO, Vector3::ZERO],
624635
};
625636

626637
basis.set_x(Vector3::new(1.0, 4.0, 7.0));
@@ -634,7 +645,7 @@ mod tests {
634645

635646
fn test_inputs() -> (Basis, Basis) {
636647
let v = Vector3::new(37.51756, 20.39467, 49.96816);
637-
let vn = v.normalize();
648+
let vn = v.normalized();
638649
let b = Basis::from_euler(v);
639650
let bn = Basis::from_euler(vn);
640651
(b, bn)
@@ -644,14 +655,17 @@ mod tests {
644655
fn determinant() {
645656
let (b, _bn) = test_inputs();
646657

647-
assert!(b.determinant().approx_eq(&1.0), "Determinant should be 1.0");
658+
assert!(
659+
b.determinant().is_equal_approx(1.0),
660+
"Determinant should be 1.0"
661+
);
648662
}
649663

650664
#[test]
651665
fn euler() {
652666
let (_b, bn) = test_inputs();
653667

654-
assert!(Vector3::new(0.57079, 0.310283, 0.760213).approx_eq(&bn.to_euler()));
668+
assert!(Vector3::new(0.57079, 0.310283, 0.760213).is_equal_approx(bn.to_euler()));
655669
}
656670

657671
#[test]
@@ -663,7 +677,7 @@ mod tests {
663677
Vector3::new(-0.288147, 0.94041, 0.180557),
664678
Vector3::new(-0.95445, -0.297299, 0.025257),
665679
]);
666-
assert!(expected.approx_eq(&b.orthonormalized()));
680+
assert!(expected.is_equal_approx(&b.orthonormalized()));
667681
}
668682

669683
#[test]
@@ -675,40 +689,40 @@ mod tests {
675689
Vector3::new(0.012407, -0.040492, -0.007774),
676690
Vector3::new(-0.682131, -0.212475, 0.018051),
677691
]);
678-
assert!(expected.approx_eq(&b.scaled(&Vector3::new(0.677813, -0.043058, 0.714685))));
692+
assert!(expected.is_equal_approx(&b.scaled(&Vector3::new(0.677813, -0.043058, 0.714685))));
679693
}
680694

681695
#[test]
682696
fn rotated() {
683697
let (b, _bn) = test_inputs();
684698

685-
let r = Vector3::new(-50.167156, 60.67781, -70.04305).normalize();
699+
let r = Vector3::new(-50.167156, 60.67781, -70.04305).normalized();
686700
let expected = Basis::from_elements([
687701
Vector3::new(-0.676245, 0.113805, 0.727833),
688702
Vector3::new(-0.467094, 0.697765, -0.54309),
689703
Vector3::new(-0.569663, -0.707229, -0.418703),
690704
]);
691-
assert!(expected.approx_eq(&b.rotated(r, 1.0)));
705+
assert!(expected.is_equal_approx(&b.rotated(r, 1.0)));
692706
}
693707

694708
#[test]
695709
fn to_quat() {
696710
let (b, _bn) = test_inputs();
697711

698-
assert!(Quat::quaternion(-0.167156, 0.677813, -0.043058, 0.714685).approx_eq(&b.to_quat()));
712+
assert!(Quat::new(-0.167156, 0.677813, -0.043058, 0.714685).is_equal_approx(&b.to_quat()));
699713
}
700714

701715
#[test]
702716
fn scale() {
703717
let (b, _bn) = test_inputs();
704718

705-
assert!(Vector3::new(1.0, 1.0, 1.0).approx_eq(&b.to_scale()));
719+
assert!(Vector3::new(1.0, 1.0, 1.0).is_equal_approx(b.to_scale()));
706720
}
707721

708722
#[test]
709723
fn approx_eq() {
710724
let (b, _bn) = test_inputs();
711-
assert!(!b.approx_eq(&Basis::from_euler(Vector3::new(37.517, 20.394, 49.968))));
725+
assert!(!b.is_equal_approx(&Basis::from_euler(Vector3::new(37.517, 20.394, 49.968))));
712726
}
713727

714728
#[test]
@@ -719,23 +733,23 @@ mod tests {
719733
Vector3::new(-0.165055, 0.94041, -0.297299),
720734
Vector3::new(0.98324, 0.180557, 0.025257),
721735
]);
722-
assert!(expected.approx_eq(&b.transposed()));
736+
assert!(expected.is_equal_approx(&b.transposed()));
723737
}
724738

725739
#[test]
726740
fn xform() {
727741
let (b, _bn) = test_inputs();
728742

729743
assert!(Vector3::new(-0.273471, 0.478102, -0.690386)
730-
.approx_eq(&b.xform(Vector3::new(0.5, 0.7, -0.2))));
744+
.is_equal_approx(b.xform(Vector3::new(0.5, 0.7, -0.2))));
731745
}
732746

733747
#[test]
734748
fn xform_inv() {
735749
let (b, _bn) = test_inputs();
736750

737751
assert!(Vector3::new(-0.884898, -0.460316, 0.071165)
738-
.approx_eq(&b.xform_inv(Vector3::new(0.077431, -0.165055, 0.98324))));
752+
.is_equal_approx(b.xform_inv(Vector3::new(0.077431, -0.165055, 0.98324))));
739753
}
740754

741755
#[test]
@@ -747,6 +761,6 @@ mod tests {
747761
Vector3::new(-0.165055, 0.94041, -0.297299),
748762
Vector3::new(0.98324, 0.180557, 0.025257),
749763
]);
750-
assert!(expected.approx_eq(&b.inverted()));
764+
assert!(expected.is_equal_approx(&b.inverted()));
751765
}
752766
}

gdnative-core/src/core_types/geom/mod.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,6 @@ mod basis;
55
mod plane;
66
mod transform;
77

8-
pub type Vector3 = euclid::default::Vector3D<f32>;
9-
pub type Vector2 = euclid::default::Vector2D<f32>;
10-
pub type Transform2D = euclid::default::Transform2D<f32>;
11-
pub type Quat = euclid::default::Rotation3D<f32>;
12-
pub type Size2 = euclid::default::Size2D<f32>;
13-
pub type Rect2 = euclid::default::Rect<f32>;
14-
pub type Angle = euclid::Angle<f32>;
15-
pub type Point3 = euclid::default::Point3D<f32>;
16-
pub type Point2 = euclid::default::Point2D<f32>;
17-
pub type Rotation2D = euclid::default::Rotation2D<f32>;
18-
pub type Rotation3D = euclid::default::Rotation3D<f32>;
19-
208
pub use self::aabb::Aabb;
219
pub use self::basis::Basis;
2210
pub use self::plane::Plane;

0 commit comments

Comments
 (0)