Skip to content

Commit 3436ed7

Browse files
HexorgBromeon
authored andcommitted
Deprecated translated() in favor of translated2() which matches Godot's logic
Changed _local naming scheme to match godot. Fixed transform unit-tests
1 parent f3bc339 commit 3436ed7

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ impl Basis {
278278

279279
/// Returns linear interpolation between two basis by weight amount (on the range of 0.0 to 1.0).
280280
#[inline]
281-
pub fn lerp(&self, other: Basis, weight: f32) -> Self {
281+
pub fn lerp(&self, other: &Basis, weight: f32) -> Self {
282282
// this is how godot is doing it at https://github.com/godotengine/godot/blob/master/core/math/basis.cpp#L964
283283
// but Godot engine output for me differs than godot-rust
284284
let a = self.elements[0].linear_interpolate(other.elements[0], weight);

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

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ impl Transform {
8787
}
8888

8989
/// Returns this transform, with its origin moved by a certain `translation`
90+
#[deprecated = "`translated` is not relative to the transform's coordinate system \
91+
and thus inconsistent with GDScript. Please use translated_global() instead. \
92+
This method will be renamed to translated_local in gdnative 0.11."]
9093
#[inline]
9194
pub fn translated(&self, translation: Vector3) -> Self {
9295
Self {
@@ -150,7 +153,7 @@ impl Transform {
150153
/// Returns the rotated transform around the given axis by the given angle (in radians),
151154
/// using matrix multiplication. The axis must be a normalized vector.
152155
#[inline]
153-
pub fn rotate(&mut self, axis: Vector3, phi: f32) {
156+
fn rotate(&mut self, axis: Vector3, phi: f32) {
154157
*self = self.rotated(axis, phi);
155158
}
156159

@@ -196,8 +199,10 @@ impl Transform {
196199

197200
/// Translates the transform by the given offset, relative to
198201
/// the transform's basis vectors.
202+
///
203+
/// This method will be renamed to `translated` in gdnative 0.11
199204
#[inline]
200-
pub fn translated_withbasis(&self, translation: Vector3) -> Self {
205+
pub fn translated_global(&self, translation: Vector3) -> Self {
201206
let mut copy = *self;
202207
copy.translate_withbasis(translation);
203208
copy
@@ -224,15 +229,15 @@ impl Transform {
224229
}
225230

226231
#[inline]
227-
pub fn is_equal_approx(&self, other: Transform) -> bool {
232+
pub fn is_equal_approx(&self, other: &Transform) -> bool {
228233
self.basis.is_equal_approx(&other.basis) && self.origin.is_equal_approx(other.origin)
229234
}
230235

231236
/// Interpolates the transform to other Transform by
232237
/// weight amount (on the range of 0.0 to 1.0).
233238
/// Assuming the two transforms are located on a sphere surface.
234239
#[inline]
235-
pub fn sphere_interpolate_with(&self, other: Transform, weight: f32) -> Self {
240+
pub fn sphere_interpolate_with(&self, other: &Transform, weight: f32) -> Self {
236241
let src_scale = self.basis.scale();
237242
let src_rot = self.basis.to_quat();
238243
let src_loc = self.origin;
@@ -252,9 +257,9 @@ impl Transform {
252257
/// Interpolates the transform to other Transform by
253258
/// weight amount (on the range of 0.0 to 1.0).
254259
#[inline]
255-
pub fn interpolate_with(&self, other: Transform, weight: f32) -> Self {
260+
pub fn interpolate_with(&self, other: &Transform, weight: f32) -> Self {
256261
Transform {
257-
basis: self.basis.lerp(other.basis, weight),
262+
basis: self.basis.lerp(&other.basis, weight),
258263
origin: self.origin.linear_interpolate(other.origin, weight),
259264
}
260265
}
@@ -297,7 +302,7 @@ mod tests {
297302
basis.c(),
298303
Vector3::new(0.0, 0.0, 0.0),
299304
);
300-
t = t.translated_withbasis(Vector3::new(0.5, -1.0, 0.25));
305+
t = t.translated_global(Vector3::new(0.5, -1.0, 0.25));
301306
t = t.scaled(Vector3::new(0.25, 0.5, 2.0));
302307

303308
let basis = Basis::from_euler(Vector3::new(12.23, 50.46, 93.94));
@@ -307,7 +312,7 @@ mod tests {
307312
basis.c(),
308313
Vector3::new(0.0, 0.0, 0.0),
309314
);
310-
t2 = t2.translated_withbasis(Vector3::new(1.5, -2.0, 1.25));
315+
t2 = t2.translated_global(Vector3::new(1.5, -2.0, 1.25));
311316
t2 = t2.scaled(Vector3::new(0.5, 0.58, 1.0));
312317
// Godot reports:
313318
// t = 0.019358, -0.041264, 0.24581, -0.144074, 0.470205, 0.090279, -1.908901, -0.594598, 0.050514 - 0.112395, -0.519672, -0.347224
@@ -348,7 +353,7 @@ mod tests {
348353
Vector3::new(-0.47722515, -0.14864945, 0.012628445),
349354
Vector3::new(-0.5, 1.0, -0.25),
350355
);
351-
assert!(expected.is_equal_approx(t))
356+
assert!(expected.is_equal_approx(&t))
352357
}
353358

354359
#[test]
@@ -363,7 +368,7 @@ mod tests {
363368
Vector3::new(0.99580616, 0.0914323, 0.0031974507),
364369
Vector3::new(0.11239518, -0.519672, -0.34722406),
365370
);
366-
assert!(expected.is_equal_approx(t))
371+
assert!(expected.is_equal_approx(&t))
367372
}
368373

369374
#[test]
@@ -374,14 +379,14 @@ mod tests {
374379
// TODO: Get new godot result. https://github.com/godotengine/godot/commit/61759da5b35e44003ab3ffe3d4024dd611d17eff changed how Transform3D.linear_interpolate works
375380
// For now assuming this is sane - examined the new implementation manually.
376381
let (t, t2) = test_inputs();
377-
let result = t.interpolate_with(t2, 0.5);
382+
let result = t.interpolate_with(&t2, 0.5);
378383
let expected = Transform::from_basis_origin(
379384
Vector3::new(0.24826992, -0.15496635, -0.997503),
380385
Vector3::new(0.038474888, 0.49598676, -0.4808879),
381386
Vector3::new(0.16852, 0.14085774, 0.48833522),
382387
Vector3::new(0.352889, -0.786351, 0.707835),
383388
);
384-
assert!(expected.is_equal_approx(result))
389+
assert!(expected.is_equal_approx(&result))
385390
}
386391

387392
#[test]
@@ -391,13 +396,13 @@ mod tests {
391396
// t2 = 0.477182, 0.118214, 0.09123, -0.165859, 0.521769, 0.191437, -0.086105, -0.367178, 0.926157 - 0.593383, -1.05303, 1.762894
392397
// result = 0.727909, -0.029075, 0.486138, -0.338385, 0.6514, 0.156468, -0.910002, -0.265481, 0.330678 - 0.352889, -0.786351, 0.707835
393398
let (t, t2) = test_inputs();
394-
let result = t.sphere_interpolate_with(t2, 0.5);
399+
let result = t.sphere_interpolate_with(&t2, 0.5);
395400
let expected = Transform::from_basis_origin(
396401
Vector3::new(0.7279087, -0.19632529, -0.45626357),
397402
Vector3::new(-0.05011323, 0.65140045, -0.22942543),
398403
Vector3::new(0.9695858, 0.18105738, 0.33067825),
399404
Vector3::new(0.3528893, -0.78635097, 0.7078349),
400405
);
401-
assert!(expected.is_equal_approx(result))
406+
assert!(expected.is_equal_approx(&result))
402407
}
403408
}

0 commit comments

Comments
 (0)