From 0cdf8c67b403ba4509a7901de015cc8ae28464f9 Mon Sep 17 00:00:00 2001 From: ikpil Date: Sun, 14 May 2023 17:09:37 +0900 Subject: [PATCH] move RcMath.VDot2D -> Vector3f.Dot2D --- src/DotRecast.Core/RcMath.cs | 29 ++----------------- src/DotRecast.Core/Vector3f.cs | 19 ++++++++++++ .../ObstacleAvoidanceQuery.cs | 10 +++---- .../ConvexConvexIntersection.cs | 2 +- 4 files changed, 28 insertions(+), 32 deletions(-) diff --git a/src/DotRecast.Core/RcMath.cs b/src/DotRecast.Core/RcMath.cs index 8ab2793..9974096 100644 --- a/src/DotRecast.Core/RcMath.cs +++ b/src/DotRecast.Core/RcMath.cs @@ -349,30 +349,7 @@ namespace DotRecast.Core return d < thresholdSqr; } - - /// Derives the dot product of two vectors on the xz-plane. (@p u . @p v) - /// @param[in] u A vector [(x, y, z)] - /// @param[in] v A vector [(x, y, z)] - /// @return The dot product on the xz-plane. - /// - /// The vectors are projected onto the xz-plane, so the y-values are - /// ignored. - public static float VDot2D(float[] u, float[] v) - { - return u[0] * v[0] + u[2] * v[2]; - } - - public static float VDot2D(Vector3f u, Vector3f v) - { - return u.x * v.x + u.z * v.z; - } - - - public static float VDot2D(Vector3f u, float[] v, int vi) - { - return u.x * v[vi] + u.z * v[vi + 2]; - } - + /// Derives the xz-plane 2D perp product of the two vectors. (uz*vx - ux*vz) /// @param[in] u The LHV vector [(x, y, z)] /// @param[in] v The RHV vector [(x, y, z)] @@ -586,10 +563,10 @@ namespace DotRecast.Core public static float[] ProjectPoly(Vector3f axis, float[] poly, int npoly) { float rmin, rmax; - rmin = rmax = VDot2D(axis, poly, 0); + rmin = rmax = axis.Dot2D(poly, 0); for (int i = 1; i < npoly; ++i) { - float d = VDot2D(axis, poly, i * 3); + float d = axis.Dot2D(poly, i * 3); rmin = Math.Min(rmin, d); rmax = Math.Max(rmax, d); } diff --git a/src/DotRecast.Core/Vector3f.cs b/src/DotRecast.Core/Vector3f.cs index 0d0d2d9..90a4bbb 100644 --- a/src/DotRecast.Core/Vector3f.cs +++ b/src/DotRecast.Core/Vector3f.cs @@ -104,6 +104,25 @@ namespace DotRecast.Core ); } + /// Derives the dot product of two vectors on the xz-plane. (@p u . @p v) + /// @param[in] u A vector [(x, y, z)] + /// @param[in] v A vector [(x, y, z)] + /// @return The dot product on the xz-plane. + /// + /// The vectors are projected onto the xz-plane, so the y-values are + /// ignored. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public float Dot2D(Vector3f v) + { + return x * v.x + z * v.z; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public float Dot2D(float[] v, int vi) + { + return x * v[vi] + z * v[vi + 2]; + } + public override bool Equals(object obj) { if (!(obj is Vector3f)) diff --git a/src/DotRecast.Detour.Crowd/ObstacleAvoidanceQuery.cs b/src/DotRecast.Detour.Crowd/ObstacleAvoidanceQuery.cs index 8785d6f..55e2056 100644 --- a/src/DotRecast.Detour.Crowd/ObstacleAvoidanceQuery.cs +++ b/src/DotRecast.Detour.Crowd/ObstacleAvoidanceQuery.cs @@ -160,13 +160,13 @@ namespace DotRecast.Detour.Crowd const float EPS = 0.0001f; Vector3f s = c1.Subtract(c0); float r = r0 + r1; - float c = VDot2D(s, s) - r * r; - float a = VDot2D(v, v); + float c = s.Dot2D(s) - r * r; + float a = v.Dot2D(v); if (a < EPS) return new SweepCircleCircleResult(false, 0f, 0f); // not moving // Overlap, calc time to exit. - float b = VDot2D(v, s); + float b = v.Dot2D(s); float d = b * b - a * c; if (d < 0.0f) return new SweepCircleCircleResult(false, 0f, 0f); // no intersection. @@ -234,7 +234,7 @@ namespace DotRecast.Detour.Crowd vab = vab.Subtract(cir.vel); // Side - side += Clamp(Math.Min(VDot2D(cir.dp, vab) * 0.5f + 0.5f, VDot2D(cir.np, vab) * 2), 0.0f, 1.0f); + side += Clamp(Math.Min(cir.dp.Dot2D(vab) * 0.5f + 0.5f, cir.np.Dot2D(vab) * 2), 0.0f, 1.0f); nside++; SweepCircleCircleResult sres = SweepCircleCircle(pos, rad, vab, cir.p, cir.rad); @@ -274,7 +274,7 @@ namespace DotRecast.Detour.Crowd snorm.x = -sdir.z; snorm.z = sdir.x; // If the velocity is pointing towards the segment, no collision. - if (VDot2D(snorm, vcand) < 0.0f) + if (snorm.Dot2D(vcand) < 0.0f) continue; // Else immediate collision. htmin = 0.0f; diff --git a/src/DotRecast.Detour/ConvexConvexIntersection.cs b/src/DotRecast.Detour/ConvexConvexIntersection.cs index 9f06247..d41925c 100644 --- a/src/DotRecast.Detour/ConvexConvexIntersection.cs +++ b/src/DotRecast.Detour/ConvexConvexIntersection.cs @@ -88,7 +88,7 @@ namespace DotRecast.Detour /*-----Advance rules-----*/ /* Special case: A & B overlap and oppositely oriented. */ - if (code == Intersection.Overlap && VDot2D(A, B) < 0) + if (code == Intersection.Overlap && A.Dot2D(B) < 0) { ii = AddVertex(inters, ii, ip); ii = AddVertex(inters, ii, iq);