Skip to content

Commit

Permalink
refactor DtTileCacheObstacle class
Browse files Browse the repository at this point in the history
  • Loading branch information
ikpil committed Jun 20, 2024
1 parent 35ef64d commit 267e15f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 39 deletions.
10 changes: 10 additions & 0 deletions src/DotRecast.Detour.TileCache/DtObstacleBox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using DotRecast.Core.Numerics;

namespace DotRecast.Detour.TileCache
{
public class DtObstacleBox
{
public RcVec3f bmin;
public RcVec3f bmax;
}
}
11 changes: 11 additions & 0 deletions src/DotRecast.Detour.TileCache/DtObstacleCylinder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using DotRecast.Core.Numerics;

namespace DotRecast.Detour.TileCache
{
public class DtObstacleCylinder
{
public RcVec3f pos;
public float radius;
public float height;
}
}
11 changes: 11 additions & 0 deletions src/DotRecast.Detour.TileCache/DtObstacleOrientedBox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using DotRecast.Core.Numerics;

namespace DotRecast.Detour.TileCache
{
public class DtObstacleOrientedBox
{
public RcVec3f center;
public RcVec3f extents;
public readonly float[] rotAux = new float[2]; // { Cos(0.5f*angle)*Sin(-0.5f*angle); Cos(0.5f*angle)*Cos(0.5f*angle) - 0.5 }
}
}
54 changes: 27 additions & 27 deletions src/DotRecast.Detour.TileCache/DtTileCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,9 @@ public long AddObstacle(RcVec3f pos, float radius, float height)
DtTileCacheObstacle ob = AllocObstacle();
ob.type = DtTileCacheObstacleType.DT_OBSTACLE_CYLINDER;

ob.pos = pos;
ob.radius = radius;
ob.height = height;
ob.cylinder.pos = pos;
ob.cylinder.radius = radius;
ob.cylinder.height = height;

return AddObstacleRequest(ob).refs;
}
Expand All @@ -363,8 +363,8 @@ public long AddBoxObstacle(RcVec3f bmin, RcVec3f bmax)
DtTileCacheObstacle ob = AllocObstacle();
ob.type = DtTileCacheObstacleType.DT_OBSTACLE_BOX;

ob.bmin = bmin;
ob.bmax = bmax;
ob.box.bmin = bmin;
ob.box.bmax = bmax;

return AddObstacleRequest(ob).refs;
}
Expand All @@ -374,12 +374,12 @@ public long AddBoxObstacle(RcVec3f center, RcVec3f extents, float yRadians)
{
DtTileCacheObstacle ob = AllocObstacle();
ob.type = DtTileCacheObstacleType.DT_OBSTACLE_ORIENTED_BOX;
ob.center = center;
ob.extents = extents;
ob.orientedBox.center = center;
ob.orientedBox.extents = extents;
float coshalf = MathF.Cos(0.5f * yRadians);
float sinhalf = MathF.Sin(-0.5f * yRadians);
ob.rotAux[0] = coshalf * sinhalf;
ob.rotAux[1] = coshalf * coshalf - 0.5f;
ob.orientedBox.rotAux[0] = coshalf * sinhalf;
ob.orientedBox.rotAux[1] = coshalf * coshalf - 0.5f;
return AddObstacleRequest(ob).refs;
}

Expand Down Expand Up @@ -614,15 +614,15 @@ public void BuildNavMeshTile(long refs)
{
if (ob.type == DtTileCacheObstacleType.DT_OBSTACLE_CYLINDER)
{
DtTileCacheBuilder.MarkCylinderArea(layer, tile.header.bmin, m_params.cs, m_params.ch, ob.pos, ob.radius, ob.height, 0);
DtTileCacheBuilder.MarkCylinderArea(layer, tile.header.bmin, m_params.cs, m_params.ch, ob.cylinder.pos, ob.cylinder.radius, ob.cylinder.height, 0);
}
else if (ob.type == DtTileCacheObstacleType.DT_OBSTACLE_BOX)
{
DtTileCacheBuilder.MarkBoxArea(layer, tile.header.bmin, m_params.cs, m_params.ch, ob.bmin, ob.bmax, 0);
DtTileCacheBuilder.MarkBoxArea(layer, tile.header.bmin, m_params.cs, m_params.ch, ob.box.bmin, ob.box.bmax, 0);
}
else if (ob.type == DtTileCacheObstacleType.DT_OBSTACLE_ORIENTED_BOX)
{
DtTileCacheBuilder.MarkBoxArea(layer, tile.header.bmin, m_params.cs, m_params.ch, ob.center, ob.extents, ob.rotAux, 0);
DtTileCacheBuilder.MarkBoxArea(layer, tile.header.bmin, m_params.cs, m_params.ch, ob.orientedBox.center, ob.orientedBox.extents, ob.orientedBox.rotAux, 0);
}
}
}
Expand Down Expand Up @@ -694,27 +694,27 @@ public void GetObstacleBounds(DtTileCacheObstacle ob, ref RcVec3f bmin, ref RcVe
{
if (ob.type == DtTileCacheObstacleType.DT_OBSTACLE_CYLINDER)
{
bmin.X = ob.pos.X - ob.radius;
bmin.Y = ob.pos.Y;
bmin.Z = ob.pos.Z - ob.radius;
bmax.X = ob.pos.X + ob.radius;
bmax.Y = ob.pos.Y + ob.height;
bmax.Z = ob.pos.Z + ob.radius;
bmin.X = ob.cylinder.pos.X - ob.cylinder.radius;
bmin.Y = ob.cylinder.pos.Y;
bmin.Z = ob.cylinder.pos.Z - ob.cylinder.radius;
bmax.X = ob.cylinder.pos.X + ob.cylinder.radius;
bmax.Y = ob.cylinder.pos.Y + ob.cylinder.height;
bmax.Z = ob.cylinder.pos.Z + ob.cylinder.radius;
}
else if (ob.type == DtTileCacheObstacleType.DT_OBSTACLE_BOX)
{
bmin = ob.bmin;
bmax = ob.bmax;
bmin = ob.box.bmin;
bmax = ob.box.bmax;
}
else if (ob.type == DtTileCacheObstacleType.DT_OBSTACLE_ORIENTED_BOX)
{
float maxr = 1.41f * Math.Max(ob.extents.X, ob.extents.Z);
bmin.X = ob.center.X - maxr;
bmax.X = ob.center.X + maxr;
bmin.Y = ob.center.Y - ob.extents.Y;
bmax.Y = ob.center.Y + ob.extents.Y;
bmin.Z = ob.center.Z - maxr;
bmax.Z = ob.center.Z + maxr;
float maxr = 1.41f * Math.Max(ob.orientedBox.extents.X, ob.orientedBox.extents.Z);
bmin.X = ob.orientedBox.center.X - maxr;
bmax.X = ob.orientedBox.center.X + maxr;
bmin.Y = ob.orientedBox.center.Y - ob.orientedBox.extents.Y;
bmax.Y = ob.orientedBox.center.Y + ob.orientedBox.extents.Y;
bmin.Z = ob.orientedBox.center.Z - maxr;
bmax.Z = ob.orientedBox.center.Z + maxr;
}
}

Expand Down
17 changes: 5 additions & 12 deletions src/DotRecast.Detour.TileCache/DtTileCacheObstacle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,17 @@ 3. This notice may not be removed or altered from any source distribution.
*/

using System.Collections.Generic;
using DotRecast.Core.Numerics;

namespace DotRecast.Detour.TileCache
{
public class DtTileCacheObstacle
{
public readonly int index;
public RcVec3f pos = new RcVec3f();
public float radius;
public float height;

public RcVec3f bmin = new RcVec3f();
public RcVec3f bmax = new RcVec3f();

public RcVec3f center = new RcVec3f();
public RcVec3f extents = new RcVec3f();
public readonly float[] rotAux = new float[2]; // { Cos(0.5f*angle)*Sin(-0.5f*angle); Cos(0.5f*angle)*Cos(0.5f*angle) - 0.5 }


public DtObstacleCylinder cylinder = new DtObstacleCylinder();
public DtObstacleBox box = new DtObstacleBox();
public DtObstacleOrientedBox orientedBox = new DtObstacleOrientedBox();

public List<long> touched = new List<long>();
public readonly List<long> pending = new List<long>();
public int salt;
Expand Down

0 comments on commit 267e15f

Please sign in to comment.