本文整理汇总了C#中Voxel.GetBox方法的典型用法代码示例。如果您正苦于以下问题:C# Voxel.GetBox方法的具体用法?C# Voxel.GetBox怎么用?C# Voxel.GetBox使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Voxel
的用法示例。
在下文中一共展示了Voxel.GetBox方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BroadphaseSearch
// Search for nearby boxes that meet a filter criteria within a vaguely defined radius
public static Voxel.Box BroadphaseSearch(Voxel v, Voxel.Coord coord, int radius, Func<Voxel.Box, bool> filter)
{
Queue<Voxel.Box> queue = new Queue<Voxel.Box>();
Dictionary<Voxel.Box, int> visited = new Dictionary<Voxel.Box, int>();
Voxel.Box startBox = v.GetBox(coord);
if (startBox == null)
return null;
queue.Enqueue(startBox);
visited[startBox] = 0;
int maxSearch = radius * radius * radius;
int searchIndex = 0;
while (queue.Count > 0 && searchIndex < maxSearch)
{
searchIndex++;
Voxel.Box b = queue.Dequeue();
lock (b.Adjacent)
{
int parentGScore = visited[b];
for (int i = 0; i < b.Adjacent.Count; i++)
{
Voxel.Box adjacent = b.Adjacent[i];
int tentativeGScore = parentGScore + adjacent.Width * adjacent.Height * adjacent.Depth;
int previousGScore;
if (!visited.TryGetValue(adjacent, out previousGScore) || tentativeGScore < previousGScore)
{
visited[adjacent] = tentativeGScore;
if (parentGScore < radius * radius
&& coord.X >= adjacent.X - radius && coord.X < adjacent.X + adjacent.Width + radius
&& coord.Y >= adjacent.Y - radius && coord.Y < adjacent.Y + adjacent.Height + radius
&& coord.Z >= adjacent.Z - radius && coord.Z < adjacent.Z + adjacent.Depth + radius)
{
if (filter(adjacent))
return adjacent;
else
queue.Enqueue(adjacent);
}
}
}
}
}
return null;
}
示例2: Go
public static bool Go(Voxel voxel, Voxel.Coord center, int radius, Action<List<DynamicVoxel>> callback = null)
{
if (!voxel[center].Permanent)
{
// Break off a chunk of this voxel into a new DynamicMap.
List<Voxel.Coord> edges = new List<Voxel.Coord>();
Voxel.Coord ripStart = center.Move(-radius, -radius, -radius);
Voxel.Coord ripEnd = center.Move(radius, radius, radius);
Dictionary<Voxel.Box, bool> permanentBoxes = new Dictionary<Voxel.Box, bool>();
foreach (Voxel.Coord c in ripStart.CoordinatesBetween(ripEnd))
{
Voxel.Box box = voxel.GetBox(c);
if (box != null && box.Type.Permanent)
permanentBoxes[box] = true;
}
foreach (Voxel.Box b in permanentBoxes.Keys)
{
// Top and bottom
for (int x = b.X - 1; x <= b.X + b.Width; x++)
{
for (int z = b.Z - 1; z <= b.Z + b.Depth; z++)
{
Voxel.Coord coord = new Voxel.Coord { X = x, Y = b.Y + b.Height, Z = z };
if (coord.Between(ripStart, ripEnd))
edges.Add(coord);
coord = new Voxel.Coord { X = x, Y = b.Y - 1, Z = z };
if (coord.Between(ripStart, ripEnd))
edges.Add(coord);
}
}
// Outer shell
for (int y = b.Y; y < b.Y + b.Height; y++)
{
// Left and right
for (int z = b.Z - 1; z <= b.Z + b.Depth; z++)
{
Voxel.Coord coord = new Voxel.Coord { X = b.X - 1, Y = y, Z = z };
if (coord.Between(ripStart, ripEnd))
edges.Add(coord);
coord = new Voxel.Coord { X = b.X + b.Width, Y = y, Z = z };
if (coord.Between(ripStart, ripEnd))
edges.Add(coord);
}
// Backward and forward
for (int x = b.X; x < b.X + b.Width; x++)
{
Voxel.Coord coord = new Voxel.Coord { X = x, Y = y, Z = b.Z - 1 };
if (coord.Between(ripStart, ripEnd))
edges.Add(coord);
coord = new Voxel.Coord { X = x, Y = y, Z = b.Z + b.Depth };
if (coord.Between(ripStart, ripEnd))
edges.Add(coord);
}
}
}
if (edges.Contains(center))
return false;
// Top and bottom
for (int x = ripStart.X; x <= ripEnd.X; x++)
{
for (int z = ripStart.Z; z <= ripEnd.Z; z++)
{
Voxel.Coord c = new Voxel.Coord { X = x, Y = ripStart.Y, Z = z };
Voxel.State s = voxel[c];
if (s != Voxel.States.Empty && !s.Permanent)
edges.Add(c);
c = new Voxel.Coord { X = x, Y = ripEnd.Y, Z = z };
s = voxel[c];
if (s != Voxel.States.Empty && !s.Permanent)
edges.Add(c);
}
}
// Sides
for (int y = ripStart.Y + 1; y <= ripEnd.Y - 1; y++)
{
// Left and right
for (int z = ripStart.Z; z <= ripEnd.Z; z++)
{
Voxel.Coord c = new Voxel.Coord { X = ripStart.X, Y = y, Z = z };
Voxel.State s = voxel[c];
if (s != Voxel.States.Empty && !s.Permanent)
edges.Add(c);
c = new Voxel.Coord { X = ripEnd.X, Y = y, Z = z };
s = voxel[c];
if (s != Voxel.States.Empty && !s.Permanent)
edges.Add(c);
}
//.........这里部分代码省略.........
示例3: Narrowphase
public static void Narrowphase(Voxel m, Voxel.Coord start, Voxel.Box target, Stack<Voxel.Coord> result)
{
Voxel.Box currentBox = m.GetBox(start);
if (currentBox == null)
return;
Vector3 targetPos = target.GetCenter();
NarrowphaseEntry startEntry = new NarrowphaseEntry
{
Parent = null,
Coord = start,
G = 0,
F = (targetPos - m.GetRelativePosition(start)).Length(),
};
narrowphaseQueue.Push(startEntry);
narrowphaseQueueLookup[start] = startEntry;
NarrowphaseEntry closestEntry = null;
float closestHeuristic = float.MaxValue;
int iterations = 0;
while (narrowphaseQueue.Count > 0 && iterations < 80)
{
iterations++;
NarrowphaseEntry entry = narrowphaseQueue.Pop();
if (m.GetBox(entry.Coord) == target)
{
closestEntry = entry;
break;
}
narrowphaseQueueLookup.Remove(entry.Coord);
narrowphaseClosed[entry.Coord] = entry.G;
for (int i = 0; i < 6; i++)
{
Voxel.Coord adjacent = entry.Coord.Move(DirectionExtensions.Directions[i]);
if (!currentBox.Contains(adjacent) && !target.Contains(adjacent))
continue;
int tentativeGScore = entry.G + 1;
int previousGScore;
bool hasPreviousGScore = narrowphaseClosed.TryGetValue(adjacent, out previousGScore);
if (hasPreviousGScore && tentativeGScore > previousGScore)
continue;
NarrowphaseEntry alreadyInQueue;
narrowphaseQueueLookup.TryGetValue(adjacent, out alreadyInQueue);
if (alreadyInQueue == null || tentativeGScore < previousGScore)
{
NarrowphaseEntry newEntry = alreadyInQueue != null ? alreadyInQueue : new NarrowphaseEntry();
newEntry.Parent = entry;
newEntry.G = tentativeGScore;
float heuristic = (targetPos - m.GetRelativePosition(adjacent)).Length();
newEntry.F = tentativeGScore + heuristic;
if (heuristic < closestHeuristic)
{
closestEntry = newEntry;
closestHeuristic = heuristic;
}
if (alreadyInQueue == null)
{
newEntry.Coord = adjacent;
narrowphaseQueue.Push(newEntry);
narrowphaseQueueLookup[adjacent] = newEntry;
}
}
}
}
narrowphaseClosed.Clear();
narrowphaseQueue.Clear();
narrowphaseQueueLookup.Clear();
if (closestEntry != null)
VoxelAStar.reconstructNarrowphasePath(closestEntry, result);
}