Skip to content

Commit

Permalink
refactor: changed float[6] to RcVec3f[2] in DtOffMeshConnection class
Browse files Browse the repository at this point in the history
  • Loading branch information
ikpil committed Nov 11, 2023
1 parent 6f0fb20 commit a50ba0b
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 33 deletions.
7 changes: 3 additions & 4 deletions src/DotRecast.Detour.Extras/Unity/Astar/OffMeshLinkCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,14 @@ public void Build(GraphMeshData graphData, NodeLink2[] links, int nodeOffset)
startTile.header.vertCount += 2;
DtOffMeshConnection connection = new DtOffMeshConnection();
connection.poly = poly;
connection.pos = new float[]
connection.pos = new RcVec3f[]
{
l.clamped1.X, l.clamped1.Y, l.clamped1.Z,
l.clamped2.X, l.clamped2.Y, l.clamped2.Z
l.clamped1, l.clamped2
};
connection.rad = 0.1f;
connection.side = startTile == endTile
? 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;
if (startTile.offMeshCons == null)
{
Expand Down
11 changes: 4 additions & 7 deletions src/DotRecast.Detour/DtNavMesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -855,10 +855,7 @@ void ConnectExtOffMeshLinks(DtMeshTile tile, DtMeshTile target, int side)
};

// Find polygon to connect to.
RcVec3f p = new RcVec3f();
p.X = targetCon.pos[3];
p.Y = targetCon.pos[4];
p.Z = targetCon.pos[5];
RcVec3f p = targetCon.pos[1];
var refs = FindNearestPolyInTile(tile, p, ext, out var nearestPt);
if (refs == 0)
{
Expand Down Expand Up @@ -1089,16 +1086,16 @@ void BaseOffMeshLinks(DtMeshTile tile)
};

// 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)
{
continue;
}

float[] p = con.pos; // First vertex
RcVec3f[] p = con.pos; // First vertex
// findNearestPoly may return too optimistic results, further check
// 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;
}
Expand Down
6 changes: 5 additions & 1 deletion src/DotRecast.Detour/DtNavMeshBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,11 @@ public static DtMeshData CreateNavMeshData(DtNavMeshCreateParams option)
con.poly = (offMeshPolyBase + n);
// Copy connection end-points.
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.flags = option.offMeshConDir[i] != 0 ? DtNavMesh.DT_OFFMESH_CON_BIDIR : 0;
con.side = offMeshConClass[i * 2 + 1];
Expand Down
4 changes: 3 additions & 1 deletion src/DotRecast.Detour/DtOffMeshConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/

using DotRecast.Core.Numerics;

namespace DotRecast.Detour
{
/// Defines an navigation mesh off-mesh connection within a dtMeshTile object.
/// An off-mesh connection is a user defined traversable connection made up to two vertices.
public class DtOffMeshConnection
{
/// 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]
public float rad;
Expand Down
6 changes: 4 additions & 2 deletions src/DotRecast.Detour/Io/DtMeshDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,11 @@ private DtOffMeshConnection[] ReadOffMeshCons(RcByteBuffer buf, DtMeshHeader hea
for (int i = 0; i < cons.Length; i++)
{
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();
Expand Down
6 changes: 4 additions & 2 deletions src/DotRecast.Detour/Io/DtMeshDataWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,11 @@ private void WriteOffMeshCons(BinaryWriter stream, DtMeshData data, RcByteOrder
{
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);
Expand Down
28 changes: 16 additions & 12 deletions src/DotRecast.Recast.Demo/Draw/RecastDebugDraw.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,25 +213,27 @@ private void DrawMeshTile(DtNavMesh mesh, DtNavMeshQuery query, DtMeshTile tile,

// End points and their on-mesh locations.
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);
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(con.pos[3], con.pos[4], con.pos[5], col);
Vertex(con.pos[1], col);
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.
Vertex(con.pos[0], con.pos[1], con.pos[2], 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], 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[3], con.pos[4] + 0.2f, con.pos[5], DuRGBA(0, 48, 64, 196));
Vertex(con.pos[1], 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.
AppendArc(con.pos[0], con.pos[1], con.pos[2], con.pos[3], con.pos[4], con.pos[5], 0.25f,
(con.flags & 1) != 0 ? 0.6f : 0, 0.6f, col);
AppendArc(
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();
Expand Down Expand Up @@ -1293,8 +1295,10 @@ public void DebugDrawNavMeshPoly(DtNavMesh mesh, long refs, int col)
Begin(DebugDrawPrimitives.LINES, 2.0f);

// Connection arc.
AppendArc(con.pos[0], con.pos[1], con.pos[2], con.pos[3], con.pos[4], con.pos[5], 0.25f,
(con.flags & 1) != 0 ? 0.6f : 0.0f, 0.6f, c);
AppendArc(
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();
}
Expand Down
2 changes: 1 addition & 1 deletion test/DotRecast.Detour.Test/Io/MeshDataReaderWriterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public void Test(bool cCompatibility, RcByteOrder order)
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].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]));
}
Expand Down
7 changes: 4 additions & 3 deletions test/DotRecast.Detour.Test/NavMeshBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/

using DotRecast.Core.Numerics;
using NUnit.Framework;

namespace DotRecast.Detour.Test;
Expand Down Expand Up @@ -49,9 +50,9 @@ public void TestBVTree()
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));
Expand All @@ -66,4 +67,4 @@ public void TestBVTree()
Assert.That(nmd.polys[118].GetArea(), Is.EqualTo(2));
Assert.That(nmd.polys[118].GetPolyType(), Is.EqualTo(DtPolyTypes.DT_POLYTYPE_OFFMESH_CONNECTION));
}
}
}

0 comments on commit a50ba0b

Please sign in to comment.