refactor: changed float[6] to RcVec3f[2] in DtOffMeshConnection class

This commit is contained in:
ikpil 2023-11-11 12:33:32 +09:00 committed by Choi Ikpil
parent 6f0fb2099a
commit a50ba0b1b1
9 changed files with 44 additions and 33 deletions

View File

@ -48,15 +48,14 @@ namespace DotRecast.Detour.Extras.Unity.Astar
startTile.header.vertCount += 2; startTile.header.vertCount += 2;
DtOffMeshConnection connection = new DtOffMeshConnection(); DtOffMeshConnection connection = new DtOffMeshConnection();
connection.poly = poly; connection.poly = poly;
connection.pos = new float[] connection.pos = new RcVec3f[]
{ {
l.clamped1.X, l.clamped1.Y, l.clamped1.Z, l.clamped1, l.clamped2
l.clamped2.X, l.clamped2.Y, l.clamped2.Z
}; };
connection.rad = 0.1f; connection.rad = 0.1f;
connection.side = startTile == endTile connection.side = startTile == endTile
? 0xFF ? 0xFF
: DtNavMeshBuilder.ClassifyOffMeshPoint(RcVecUtils.Create(connection.pos, 3), startTile.header.bmin, startTile.header.bmax); : DtNavMeshBuilder.ClassifyOffMeshPoint(connection.pos[1], startTile.header.bmin, startTile.header.bmax);
connection.userId = (int)l.linkID; connection.userId = (int)l.linkID;
if (startTile.offMeshCons == null) if (startTile.offMeshCons == null)
{ {

View File

@ -855,10 +855,7 @@ namespace DotRecast.Detour
}; };
// Find polygon to connect to. // Find polygon to connect to.
RcVec3f p = new RcVec3f(); RcVec3f p = targetCon.pos[1];
p.X = targetCon.pos[3];
p.Y = targetCon.pos[4];
p.Z = targetCon.pos[5];
var refs = FindNearestPolyInTile(tile, p, ext, out var nearestPt); var refs = FindNearestPolyInTile(tile, p, ext, out var nearestPt);
if (refs == 0) if (refs == 0)
{ {
@ -1089,16 +1086,16 @@ namespace DotRecast.Detour
}; };
// Find polygon to connect to. // Find polygon to connect to.
var refs = FindNearestPolyInTile(tile, new RcVec3f(con.pos[0], con.pos[1], con.pos[2]), ext, out var nearestPt); var refs = FindNearestPolyInTile(tile, con.pos[0], ext, out var nearestPt);
if (refs == 0) if (refs == 0)
{ {
continue; continue;
} }
float[] p = con.pos; // First vertex RcVec3f[] p = con.pos; // First vertex
// findNearestPoly may return too optimistic results, further check // findNearestPoly may return too optimistic results, further check
// to make sure. // to make sure.
if (RcMath.Sqr(nearestPt.X - p[0]) + RcMath.Sqr(nearestPt.Z - p[2]) > RcMath.Sqr(con.rad)) if (RcMath.Sqr(nearestPt.X - p[0].X) + RcMath.Sqr(nearestPt.Z - p[0].Z) > RcMath.Sqr(con.rad))
{ {
continue; continue;
} }

View File

@ -615,7 +615,11 @@ namespace DotRecast.Detour
con.poly = (offMeshPolyBase + n); con.poly = (offMeshPolyBase + n);
// Copy connection end-points. // Copy connection end-points.
int endPts = i * 2 * 3; int endPts = i * 2 * 3;
Array.Copy(option.offMeshConVerts, endPts, con.pos, 0, 6); for (int j = 0; j < 2; ++j)
{
con.pos[j] = RcVecUtils.Create(option.offMeshConVerts, endPts + (j * 3));
}
con.rad = option.offMeshConRad[i]; con.rad = option.offMeshConRad[i];
con.flags = option.offMeshConDir[i] != 0 ? DtNavMesh.DT_OFFMESH_CON_BIDIR : 0; con.flags = option.offMeshConDir[i] != 0 ? DtNavMesh.DT_OFFMESH_CON_BIDIR : 0;
con.side = offMeshConClass[i * 2 + 1]; con.side = offMeshConClass[i * 2 + 1];

View File

@ -18,6 +18,8 @@ freely, subject to the following restrictions:
3. This notice may not be removed or altered from any source distribution. 3. This notice may not be removed or altered from any source distribution.
*/ */
using DotRecast.Core.Numerics;
namespace DotRecast.Detour namespace DotRecast.Detour
{ {
/// Defines an navigation mesh off-mesh connection within a dtMeshTile object. /// Defines an navigation mesh off-mesh connection within a dtMeshTile object.
@ -25,7 +27,7 @@ namespace DotRecast.Detour
public class DtOffMeshConnection public class DtOffMeshConnection
{ {
/// The endpoints of the connection. [(ax, ay, az, bx, by, bz)] /// The endpoints of the connection. [(ax, ay, az, bx, by, bz)]
public float[] pos = new float[6]; public RcVec3f[] pos = new RcVec3f[2];
/// The radius of the endpoints. [Limit: >= 0] /// The radius of the endpoints. [Limit: >= 0]
public float rad; public float rad;

View File

@ -237,9 +237,11 @@ namespace DotRecast.Detour.Io
for (int i = 0; i < cons.Length; i++) for (int i = 0; i < cons.Length; i++)
{ {
cons[i] = new DtOffMeshConnection(); cons[i] = new DtOffMeshConnection();
for (int j = 0; j < 6; j++) for (int j = 0; j < 2; j++)
{ {
cons[i].pos[j] = buf.GetFloat(); cons[i].pos[j].X = buf.GetFloat();
cons[i].pos[j].Y = buf.GetFloat();
cons[i].pos[j].Z = buf.GetFloat();
} }
cons[i].rad = buf.GetFloat(); cons[i].rad = buf.GetFloat();

View File

@ -159,9 +159,11 @@ namespace DotRecast.Detour.Io
{ {
for (int i = 0; i < data.header.offMeshConCount; i++) for (int i = 0; i < data.header.offMeshConCount; i++)
{ {
for (int j = 0; j < 6; j++) for (int j = 0; j < 2; j++)
{ {
Write(stream, data.offMeshCons[i].pos[j], order); Write(stream, data.offMeshCons[i].pos[j].X, order);
Write(stream, data.offMeshCons[i].pos[j].Y, order);
Write(stream, data.offMeshCons[i].pos[j].Z, order);
} }
Write(stream, data.offMeshCons[i].rad, order); Write(stream, data.offMeshCons[i].rad, order);

View File

@ -213,25 +213,27 @@ public class RecastDebugDraw : DebugDraw
// End points and their on-mesh locations. // End points and their on-mesh locations.
Vertex(va.X, va.Y, va.Z, col); Vertex(va.X, va.Y, va.Z, col);
Vertex(con.pos[0], con.pos[1], con.pos[2], col); Vertex(con.pos[0], col);
col2 = startSet ? col : DuRGBA(220, 32, 16, 196); col2 = startSet ? col : DuRGBA(220, 32, 16, 196);
AppendCircle(con.pos[0], con.pos[1] + 0.1f, con.pos[2], con.rad, col2); AppendCircle(con.pos[0].X, con.pos[0].Y + 0.1f, con.pos[0].Z, con.rad, col2);
Vertex(vb.X, vb.Y, vb.Z, col); Vertex(vb.X, vb.Y, vb.Z, col);
Vertex(con.pos[3], con.pos[4], con.pos[5], col); Vertex(con.pos[1], col);
col2 = endSet ? col : DuRGBA(220, 32, 16, 196); col2 = endSet ? col : DuRGBA(220, 32, 16, 196);
AppendCircle(con.pos[3], con.pos[4] + 0.1f, con.pos[5], con.rad, col2); AppendCircle(con.pos[1].X, con.pos[1].Y + 0.1f, con.pos[1].Z, con.rad, col2);
// End point vertices. // End point vertices.
Vertex(con.pos[0], con.pos[1], con.pos[2], DuRGBA(0, 48, 64, 196)); Vertex(con.pos[0], DuRGBA(0, 48, 64, 196));
Vertex(con.pos[0], con.pos[1] + 0.2f, con.pos[2], DuRGBA(0, 48, 64, 196)); Vertex(con.pos[0].X, con.pos[0].Y + 0.2f, con.pos[0].Z, DuRGBA(0, 48, 64, 196));
Vertex(con.pos[3], con.pos[4], con.pos[5], DuRGBA(0, 48, 64, 196)); Vertex(con.pos[1], DuRGBA(0, 48, 64, 196));
Vertex(con.pos[3], con.pos[4] + 0.2f, con.pos[5], DuRGBA(0, 48, 64, 196)); Vertex(con.pos[1].X, con.pos[1].Y + 0.2f, con.pos[1].Z, DuRGBA(0, 48, 64, 196));
// Connection arc. // Connection arc.
AppendArc(con.pos[0], con.pos[1], con.pos[2], con.pos[3], con.pos[4], con.pos[5], 0.25f, AppendArc(
(con.flags & 1) != 0 ? 0.6f : 0, 0.6f, col); con.pos[0].X, con.pos[0].Y, con.pos[0].Z,
con.pos[1].X, con.pos[1].Y, con.pos[1].Z,
0.25f, (con.flags & 1) != 0 ? 0.6f : 0, 0.6f, col);
} }
End(); End();
@ -1293,8 +1295,10 @@ public class RecastDebugDraw : DebugDraw
Begin(DebugDrawPrimitives.LINES, 2.0f); Begin(DebugDrawPrimitives.LINES, 2.0f);
// Connection arc. // Connection arc.
AppendArc(con.pos[0], con.pos[1], con.pos[2], con.pos[3], con.pos[4], con.pos[5], 0.25f, AppendArc(
(con.flags & 1) != 0 ? 0.6f : 0.0f, 0.6f, c); con.pos[0].X, con.pos[0].Y, con.pos[0].Z,
con.pos[1].X, con.pos[1].Y, con.pos[1].Z,
0.25f, (con.flags & 1) != 0 ? 0.6f : 0.0f, 0.6f, c);
End(); End();
} }

View File

@ -131,7 +131,7 @@ public class MeshDataReaderWriterTest
Assert.That(readData.offMeshCons[i].poly, Is.EqualTo(meshData.offMeshCons[i].poly)); Assert.That(readData.offMeshCons[i].poly, Is.EqualTo(meshData.offMeshCons[i].poly));
Assert.That(readData.offMeshCons[i].side, Is.EqualTo(meshData.offMeshCons[i].side)); Assert.That(readData.offMeshCons[i].side, Is.EqualTo(meshData.offMeshCons[i].side));
Assert.That(readData.offMeshCons[i].userId, Is.EqualTo(meshData.offMeshCons[i].userId)); Assert.That(readData.offMeshCons[i].userId, Is.EqualTo(meshData.offMeshCons[i].userId));
for (int j = 0; j < 6; j++) for (int j = 0; j < 2; j++)
{ {
Assert.That(readData.offMeshCons[i].pos[j], Is.EqualTo(meshData.offMeshCons[i].pos[j])); Assert.That(readData.offMeshCons[i].pos[j], Is.EqualTo(meshData.offMeshCons[i].pos[j]));
} }

View File

@ -16,6 +16,7 @@ freely, subject to the following restrictions:
3. This notice may not be removed or altered from any source distribution. 3. This notice may not be removed or altered from any source distribution.
*/ */
using DotRecast.Core.Numerics;
using NUnit.Framework; using NUnit.Framework;
namespace DotRecast.Detour.Test; namespace DotRecast.Detour.Test;
@ -49,9 +50,9 @@ public class NavMeshBuilderTest
Assert.That(nmd.bvTree[i], Is.Not.Null); Assert.That(nmd.bvTree[i], Is.Not.Null);
} }
for (int i = 0; i < 6; i++) for (int i = 0; i < 2; i++)
{ {
Assert.That(nmd.verts[223 * 3 + i], Is.EqualTo(nmd.offMeshCons[0].pos[i])); Assert.That(RcVecUtils.Create(nmd.verts, 223 * 3 + (i * 3)), Is.EqualTo(nmd.offMeshCons[0].pos[i]));
} }
Assert.That(nmd.offMeshCons[0].rad, Is.EqualTo(0.1f)); Assert.That(nmd.offMeshCons[0].rad, Is.EqualTo(0.1f));
@ -66,4 +67,4 @@ public class NavMeshBuilderTest
Assert.That(nmd.polys[118].GetArea(), Is.EqualTo(2)); Assert.That(nmd.polys[118].GetArea(), Is.EqualTo(2));
Assert.That(nmd.polys[118].GetPolyType(), Is.EqualTo(DtPolyTypes.DT_POLYTYPE_OFFMESH_CONNECTION)); Assert.That(nmd.polys[118].GetPolyType(), Is.EqualTo(DtPolyTypes.DT_POLYTYPE_OFFMESH_CONNECTION));
} }
} }