Skip to content

Commit 3b88a0a

Browse files
committed
Final tweaks for draft
1 parent daa45c6 commit 3b88a0a

File tree

4 files changed

+78
-21
lines changed

4 files changed

+78
-21
lines changed

godot-core/src/builtin/basis.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ impl Basis {
166166
///
167167
/// _Godot equivalent: `Basis()`_
168168
#[must_use]
169-
pub fn to_quat(&self) -> Quaternion {
169+
pub fn to_quat(self) -> Quaternion {
170170
glam::Quat::from_mat3(&self.orthonormalized().to_glam()).to_front()
171171
}
172172

@@ -409,7 +409,7 @@ impl Basis {
409409
///
410410
/// _Godot equivalent: `Basis.inverse()`_
411411
#[must_use]
412-
pub fn inverse(&self) -> Basis {
412+
pub fn inverse(self) -> Basis {
413413
self.glam(|mat| mat.inverse())
414414
}
415415

@@ -429,7 +429,7 @@ impl Basis {
429429
///
430430
/// _Godot equivalent: `Basis.orthonormalized()`_
431431
#[must_use]
432-
pub fn orthonormalized(&self) -> Self {
432+
pub fn orthonormalized(self) -> Self {
433433
assert!(
434434
!is_equal_approx(self.determinant(), 0.0),
435435
"Determinant should not be zero."
@@ -506,7 +506,7 @@ impl Basis {
506506
/// _Godot equivalent: `Basis.is_finite()`_
507507
#[must_use]
508508
pub fn is_finite(&self) -> bool {
509-
self.to_glam().is_finite()
509+
self[0].is_finite() && self[1].is_finite() && self[2].is_finite()
510510
}
511511

512512
/// Returns `true` if this basis and `other` are approximately equal,
@@ -590,6 +590,12 @@ impl Display for Basis {
590590
}
591591
}
592592

593+
impl From<Quaternion> for Basis {
594+
fn from(quat: Quaternion) -> Self {
595+
Basis::from_quat(quat)
596+
}
597+
}
598+
593599
impl GlamConv for Basis {
594600
type Glam = glam::f32::Mat3;
595601
}

godot-core/src/builtin/projection.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ impl Projection {
238238
/// column flipped.
239239
///
240240
/// _Godot equivalent: Projection.flipped_y()_
241-
pub fn flipped_y(&self) -> Self {
241+
pub fn flipped_y(self) -> Self {
242242
Self {
243243
cols: [self[0], -self[1], self[2], self[3]],
244244
}
@@ -326,7 +326,7 @@ impl Projection {
326326
/// projective transformation.
327327
///
328328
/// _Godot equivalent: Projection.inverse()_
329-
pub fn inverse(&self) -> Self {
329+
pub fn inverse(self) -> Self {
330330
self.glam(|mat| mat.inverse())
331331
}
332332

godot-core/src/builtin/transform2d.rs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* License, v. 2.0. If a copy of the MPL was not distributed with this
44
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
55
*/
6-
use std::{f32::consts::PI, ops::*};
6+
use std::{f32::consts::PI, fmt::Display, ops::*};
77

88
use godot_ffi as sys;
99
use sys::{ffi_methods, GodotFfi};
@@ -106,7 +106,7 @@ impl Transform2D {
106106
/// transformation is composed of rotation, scaling and translation.
107107
///
108108
/// _Godot equivalent: `Transform2D.affine_inverse()`_
109-
pub fn affine_inverse(&self) -> Self {
109+
pub fn affine_inverse(self) -> Self {
110110
self.glam(|aff| aff.inverse())
111111
}
112112

@@ -164,7 +164,7 @@ impl Transform2D {
164164
/// normalized axis vectors (scale of 1 or -1).
165165
///
166166
/// _Godot equivalent: `Transform2D.orthonormalized()`_
167-
pub fn orthonormalized(&self) -> Self {
167+
pub fn orthonormalized(self) -> Self {
168168
Self {
169169
basis: self.basis.orthonormalized(),
170170
origin: self.origin,
@@ -247,6 +247,21 @@ impl Transform2D {
247247
}
248248
}
249249

250+
impl Display for Transform2D {
251+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
252+
// Godot output:
253+
// [X: (1, 2), Y: (3, 4), O: (5, 6)]
254+
// Where X,Y,O are the columns
255+
256+
let [Vector2 { x: x1, y: x2 }, Vector2 { x: y1, y: y2 }] = self.basis.cols;
257+
let Vector2 { x: o1, y: o2 } = self.origin;
258+
259+
f.write_fmt(format_args!(
260+
"[X: ({x1}, {x2}), Y: ({y1}, {y2}), O: ({o1}, {o2})]"
261+
))
262+
}
263+
}
264+
250265
impl Index<usize> for Transform2D {
251266
type Output = Vector2;
252267

@@ -384,7 +399,7 @@ impl Basis2D {
384399
}
385400

386401
/// Introduces an additional scaling.
387-
pub fn scaled(&self, scale: Vector2) -> Self {
402+
pub fn scaled(self, scale: Vector2) -> Self {
388403
Self {
389404
cols: [self[0] * scale.x, self[1] * scale.y],
390405
}
@@ -410,7 +425,7 @@ impl Basis2D {
410425

411426
/// Returns the orthonormalized version of the basis.
412427
#[must_use]
413-
pub fn orthonormalized(&self) -> Self {
428+
pub fn orthonormalized(self) -> Self {
414429
assert!(
415430
!is_equal_approx(self.determinant(), 0.0),
416431
"Determinant should not be zero."
@@ -493,6 +508,14 @@ impl Default for Basis2D {
493508
}
494509
}
495510

511+
impl Display for Basis2D {
512+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
513+
let [Vector2 { x: x1, y: x2 }, Vector2 { x: y1, y: y2 }] = self.cols;
514+
515+
f.write_fmt(format_args!("[X: ({x1}, {x2}), Y: ({y1}, {y2}))]"))
516+
}
517+
}
518+
496519
impl Mul for Basis2D {
497520
type Output = Self;
498521

godot-core/src/builtin/transform3d.rs

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* License, v. 2.0. If a copy of the MPL was not distributed with this
44
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
55
*/
6-
use std::ops::*;
6+
use std::{fmt::Display, ops::*};
77

88
use godot_ffi as sys;
99
use sys::{ffi_methods, GodotFfi};
@@ -81,7 +81,7 @@ impl Transform3D {
8181
/// transformation is composed of rotation, scaling and translation.
8282
///
8383
/// _Godot equivalent: Transform3D.affine_inverse()_
84-
pub fn affine_inverse(&self) -> Self {
84+
pub fn affine_inverse(self) -> Self {
8585
self.glam(|aff| aff.inverse())
8686
}
8787

@@ -129,7 +129,7 @@ impl Transform3D {
129129
/// See [`Basis::new_looking_at()`] for more information.
130130
///
131131
/// _Godot equivalent: Transform3D.looking_at()_
132-
pub fn looking_at(&self, target: Vector3, up: Vector3) -> Self {
132+
pub fn looking_at(self, target: Vector3, up: Vector3) -> Self {
133133
Self {
134134
basis: Basis::new_looking_at(target - self.origin, up),
135135
origin: self.origin,
@@ -140,7 +140,7 @@ impl Transform3D {
140140
/// normalized axis vectors (scale of 1 or -1).
141141
///
142142
/// _Godot equivalent: Transform3D.orthonormalized()_
143-
pub fn orthonormalized(&self) -> Self {
143+
pub fn orthonormalized(self) -> Self {
144144
Self {
145145
basis: self.basis.orthonormalized(),
146146
origin: self.origin,
@@ -153,7 +153,7 @@ impl Transform3D {
153153
/// This can be seen as transforming with respect to the global/parent frame.
154154
///
155155
/// _Godot equivalent: `Transform2D.rotated()`_
156-
pub fn rotated(&self, axis: Vector3, angle: f32) -> Self {
156+
pub fn rotated(self, axis: Vector3, angle: f32) -> Self {
157157
let rotation = Basis::from_axis_angle(axis, angle);
158158
Self {
159159
basis: rotation * self.basis,
@@ -166,7 +166,7 @@ impl Transform3D {
166166
/// This can be seen as transforming with respect to the local frame.
167167
///
168168
/// _Godot equivalent: `Transform2D.rotated_local()`_
169-
pub fn rotated_local(&self, axis: Vector3, angle: f32) -> Self {
169+
pub fn rotated_local(self, axis: Vector3, angle: f32) -> Self {
170170
Self {
171171
basis: self.basis * Basis::from_axis_angle(axis, angle),
172172
origin: self.origin,
@@ -179,7 +179,7 @@ impl Transform3D {
179179
/// This can be seen as transforming with respect to the global/parent frame.
180180
///
181181
/// _Godot equivalent: `Transform2D.scaled()`_
182-
pub fn scaled(&self, scale: Vector3) -> Self {
182+
pub fn scaled(self, scale: Vector3) -> Self {
183183
Self {
184184
basis: Basis::from_scale(scale) * self.basis,
185185
origin: self.origin * scale,
@@ -192,7 +192,7 @@ impl Transform3D {
192192
/// This can be seen as transforming with respect to the local frame.
193193
///
194194
/// _Godot equivalent: `Transform2D.scaled_local()`_
195-
pub fn scaled_local(&self, scale: Vector3) -> Self {
195+
pub fn scaled_local(self, scale: Vector3) -> Self {
196196
Self {
197197
basis: self.basis * Basis::from_scale(scale),
198198
origin: self.origin,
@@ -205,7 +205,7 @@ impl Transform3D {
205205
/// This can be seen as transforming with respect to the global/parent frame.
206206
///
207207
/// _Godot equivalent: `Transform2D.translated()`_
208-
pub fn translated(&self, offset: Vector3) -> Self {
208+
pub fn translated(self, offset: Vector3) -> Self {
209209
Self {
210210
basis: self.basis,
211211
origin: self.origin + offset,
@@ -218,14 +218,42 @@ impl Transform3D {
218218
/// This can be seen as transforming with respect to the local frame.
219219
///
220220
/// _Godot equivalent: `Transform2D.translated()`_
221-
pub fn translated_local(&self, offset: Vector3) -> Self {
221+
pub fn translated_local(self, offset: Vector3) -> Self {
222222
Self {
223223
basis: self.basis,
224224
origin: self.origin + (self.basis * offset),
225225
}
226226
}
227227
}
228228

229+
impl Display for Transform3D {
230+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
231+
// Godot output:
232+
// [X: (1, 2, 3), Y: (4, 5, 6), Z: (7, 8, 9), O: (10, 11, 12)]
233+
// Where X,Y,O are the columns
234+
let [Vector3 {
235+
x: x1,
236+
y: x2,
237+
z: x3,
238+
}, Vector3 {
239+
x: y1,
240+
y: y2,
241+
z: y3,
242+
}, Vector3 {
243+
x: z1,
244+
y: z2,
245+
z: z3,
246+
}] = self.basis.to_cols();
247+
let Vector3 {
248+
x: o1,
249+
y: o2,
250+
z: o3,
251+
} = self.origin;
252+
253+
f.write_fmt(format_args!("[X: ({x1}, {x2}, {x3}), Y: ({y1}, {y2}, {y3}), Z: ({z1}, {z2}, {z3}), O: ({o1}, {o2}, {o3})]"))
254+
}
255+
}
256+
229257
impl From<Projection> for Transform3D {
230258
/// Constructs a let froma Projection by trimming the last row of
231259
/// the projection matrix.

0 commit comments

Comments
 (0)