From 21b3cd6caa35ec64042a8a6dce5055151f114852 Mon Sep 17 00:00:00 2001 From: ikpil Date: Fri, 27 Oct 2023 00:03:03 +0900 Subject: [PATCH] refactor: RcVec3f.Lerp --- src/DotRecast.Core/Numerics/RcVec3f.cs | 13 ++++---- test/DotRecast.Core.Test/Vector3Tests.cs | 38 +++++++++++------------- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/src/DotRecast.Core/Numerics/RcVec3f.cs b/src/DotRecast.Core/Numerics/RcVec3f.cs index 6b6cf1e..e449d5e 100644 --- a/src/DotRecast.Core/Numerics/RcVec3f.cs +++ b/src/DotRecast.Core/Numerics/RcVec3f.cs @@ -228,13 +228,14 @@ namespace DotRecast.Core.Numerics } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static RcVec3f Lerp(RcVec3f v1, RcVec3f v2, float t) + public static RcVec3f Lerp(RcVec3f value1, RcVec3f value2, float amount) { - return new RcVec3f( - v1.X + (v2.X - v1.X) * t, - v1.Y + (v2.Y - v1.Y) * t, - v1.Z + (v2.Z - v1.Z) * t - ); + return (value1 * (1f - amount)) + (value2 * amount); + // return new RcVec3f( + // value1.X + (value2.X - value1.X) * amount, + // value1.Y + (value2.Y - value1.Y) * amount, + // value1.Z + (value2.Z - value1.Z) * amount + // ); } /// Returns the distance between two points. diff --git a/test/DotRecast.Core.Test/Vector3Tests.cs b/test/DotRecast.Core.Test/Vector3Tests.cs index 37d1798..975a733 100644 --- a/test/DotRecast.Core.Test/Vector3Tests.cs +++ b/test/DotRecast.Core.Test/Vector3Tests.cs @@ -99,13 +99,18 @@ public class Vector3Tests { var v1 = new Vector3(Random.Shared.NextSingle(), Random.Shared.NextSingle(), Random.Shared.NextSingle()); var array1 = new float[3]; + var array2 = new float[3]; v1.CopyTo(array1); + v1.CopyTo(array2, 0); var v11 = new RcVec3f(v1.X, v1.Y, v1.Z); var array11 = new float[3]; + var array22 = new float[3]; v11.CopyTo(array11); + v11.CopyTo(array22, 0); Assert.That(array1, Is.EqualTo(array11)); + Assert.That(array2, Is.EqualTo(array22)); } [Test] @@ -167,26 +172,19 @@ public class Vector3Tests [Test] [Repeat(100000)] - public void TestVectorIsFinite() + public void TestVectorLerp() { - // IsFinite + var amt = Random.Shared.NextSingle(); + var v1 = new Vector3(Random.Shared.NextSingle(), Random.Shared.NextSingle(), Random.Shared.NextSingle()); + var v2 = new Vector3(Random.Shared.NextSingle(), Random.Shared.NextSingle(), Random.Shared.NextSingle()); + var v3 = Vector3.Lerp(v1, v2, amt); + + var v11 = new RcVec3f(v1.X, v1.Y, v1.Z); + var v22 = new RcVec3f(v2.X, v2.Y, v2.Z); + var v33 = RcVec3f.Lerp(v11, v22, amt); + + Assert.That(v3.X, Is.EqualTo(v33.X)); + Assert.That(v3.Y, Is.EqualTo(v33.Y)); + Assert.That(v3.Z, Is.EqualTo(v33.Z)); } - - // [Test] - // [Repeat(100000)] - // public void TestVectorLerp() - // { - // var amt = Random.Shared.NextSingle(); - // var v1 = new Vector3(Random.Shared.NextSingle(), Random.Shared.NextSingle(), Random.Shared.NextSingle()); - // var v2 = new Vector3(Random.Shared.NextSingle(), Random.Shared.NextSingle(), Random.Shared.NextSingle()); - // var v3 = Vector3.Lerp(v1, v2, amt); - // - // var v11 = new RcVec3f(v1.X, v1.Y, v1.Z); - // var v22 = new RcVec3f(v2.X, v2.Y, v2.Z); - // var v33 = RcVec3f.Lerp(v11, v22, amt); - // - // Assert.That(v3.X, Is.EqualTo(v33.X)); - // Assert.That(v3.Y, Is.EqualTo(v33.Y)); - // Assert.That(v3.Z, Is.EqualTo(v33.Z)); - // } } \ No newline at end of file