@@ -13,13 +13,13 @@ use crate::builtin::Vector3i;
13
13
14
14
/// Vector used for 3D math using floating point coordinates.
15
15
///
16
- /// 2 -element structure that can be used to represent positions in 2D space or any other pair of
16
+ /// 3 -element structure that can be used to represent positions in 2D space or any other triple of
17
17
/// numeric values.
18
18
///
19
19
/// It uses floating-point coordinates of 32-bit precision, unlike the engine's `float` type which
20
20
/// is always 64-bit. The engine can be compiled with the option `precision=double` to use 64-bit
21
21
/// vectors, but this is not yet supported in the `gdextension` crate.
22
- ///
22
+ ///
23
23
/// See [`Vector3i`] for its integer counterpart.
24
24
#[ derive( Debug , Default , Clone , Copy , PartialEq ) ]
25
25
#[ repr( C ) ]
@@ -32,54 +32,50 @@ pub struct Vector3 {
32
32
pub z : f32 ,
33
33
}
34
34
35
- impl_vector_operators ! ( Vector3 , f32 , ( x, y, z) ) ;
36
- impl_vector_index ! ( Vector3 , f32 , ( x, y, z) , Vector3Axis , ( X , Y , Z ) ) ;
37
- impl_common_vector_fns ! ( Vector3 , f32 ) ;
38
- impl_float_vector_fns ! ( Vector3 , f32 ) ;
39
-
40
35
impl Vector3 {
41
- /// Returns a `Vector3` with the given components.
42
- pub const fn new ( x : f32 , y : f32 , z : f32 ) -> Self {
43
- Self { x, y, z }
44
- }
45
-
46
- /// Returns a new `Vector3` with all components set to `v`.
47
- pub const fn splat ( v : f32 ) -> Self {
48
- Self { x : v, y : v, z : v }
49
- }
50
-
51
- /// Constructs a new `Vector3` from a [`Vector3i`].
52
- pub const fn from_vector3i ( v : Vector3i ) -> Self {
53
- Self { x : v. x as f32 , y : v. y as f32 , z : v. z as f32 }
54
- }
55
-
56
- /// Zero vector, a vector with all components set to `0.0`.
36
+ /// Vector with all components set to `0.0`.
57
37
pub const ZERO : Self = Self :: splat ( 0.0 ) ;
58
38
59
- /// One vector, a vector with all components set to `1.0`.
39
+ /// Vector with all components set to `1.0`.
60
40
pub const ONE : Self = Self :: splat ( 1.0 ) ;
61
41
62
- /// Infinity vector, a vector with all components set to `INFIINTY`.
63
- pub const INF : Self = Self :: splat ( f32:: INFINITY ) ;
64
-
65
- /// Left unit vector. Represents the local direction of left, and the global direction of west.
42
+ /// Unit vector in -X direction. Can be interpreted as left in an untransformed 3D world.
66
43
pub const LEFT : Self = Self :: new ( -1.0 , 0.0 , 0.0 ) ;
67
44
68
- /// Right unit vector. Represents the local direction of right, and the global direction of east .
45
+ /// Unit vector in +X direction. Can be interpreted as right in an untransformed 3D world .
69
46
pub const RIGHT : Self = Self :: new ( 1.0 , 0.0 , 0.0 ) ;
70
47
71
- /// Up unit vector.
48
+ /// Unit vector in -Y direction. Typically interpreted as down in a 3D world .
72
49
pub const UP : Self = Self :: new ( 0.0 , -1.0 , 0.0 ) ;
73
50
74
- /// Down unit vector.
51
+ /// Unit vector in +Y direction. Typically interpreted as up in a 3D world .
75
52
pub const DOWN : Self = Self :: new ( 0.0 , 1.0 , 0.0 ) ;
76
53
77
- /// Forward unit vector. Represents the local direction of forward, and the global direction of north .
54
+ /// Unit vector in -Z direction. Can be interpreted as "into the screen" in an untransformed 3D world .
78
55
pub const FORWARD : Self = Self :: new ( 0.0 , 0.0 , -1.0 ) ;
79
56
80
- /// Back unit vector. Represents the local direction of back, and the global direction of south .
57
+ /// Unit vector in +Z direction. Can be interpreted as "out of the screen" in an untransformed 3D world .
81
58
pub const BACK : Self = Self :: new ( 0.0 , 0.0 , 1.0 ) ;
82
59
60
+ /// Returns a `Vector3` with the given components.
61
+ pub const fn new ( x : f32 , y : f32 , z : f32 ) -> Self {
62
+ Self { x, y, z }
63
+ }
64
+
65
+ /// Returns a new `Vector3` with all components set to `v`.
66
+ pub const fn splat ( v : f32 ) -> Self {
67
+ Self :: new ( v, v, v)
68
+ }
69
+
70
+ /// Constructs a new `Vector3` from a [`Vector3i`].
71
+ pub const fn from_vector3i ( v : Vector3i ) -> Self {
72
+ Self {
73
+ x : v. x as f32 ,
74
+ y : v. y as f32 ,
75
+ z : v. z as f32 ,
76
+ }
77
+ }
78
+
83
79
/// Converts the corresponding `glam` type to `Self`.
84
80
fn from_glam ( v : glam:: Vec3 ) -> Self {
85
81
Self :: new ( v. x , v. y , v. z )
@@ -91,13 +87,18 @@ impl Vector3 {
91
87
}
92
88
}
93
89
94
- /// Formats this vector in the same way the Godot engine would .
90
+ /// Formats the vector like Godot: `(x, y, z)` .
95
91
impl fmt:: Display for Vector3 {
96
92
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
97
93
write ! ( f, "({}, {}, {})" , self . x, self . y, self . z)
98
94
}
99
95
}
100
96
97
+ impl_common_vector_fns ! ( Vector3 , f32 ) ;
98
+ impl_float_vector_fns ! ( Vector3 , f32 ) ;
99
+ impl_vector_operators ! ( Vector3 , f32 , ( x, y, z) ) ;
100
+ impl_vector_index ! ( Vector3 , f32 , ( x, y, z) , Vector3Axis , ( X , Y , Z ) ) ;
101
+
101
102
impl GodotFfi for Vector3 {
102
103
ffi_methods ! { type sys:: GDExtensionTypePtr = * mut Self ; .. }
103
104
}
0 commit comments