This commit is contained in:
maxwes08
2025-09-09 10:57:28 +02:00
parent 3678eaa5f8
commit b6655d71d9
6 changed files with 148 additions and 81 deletions

View File

@@ -12,32 +12,27 @@ namespace Voxel
_chunks = new Dictionary<(int, int), Chunk>();
}
// Get a chunk at world coordinates, returns null if not loaded
public Chunk GetChunk(int chunkX, int chunkZ)
{
_chunks.TryGetValue((chunkX, chunkZ), out Chunk chunk);
return chunk;
}
// Add a chunk
public void AddChunk(Chunk chunk)
{
_chunks[(chunk.X, chunk.Y)] = chunk;
}
// Remove a chunk
public void RemoveChunk(int chunkX, int chunkZ)
{
_chunks.Remove((chunkX, chunkZ));
}
// Iterate over all chunks
public IEnumerable<Chunk> GetAllChunks()
{
return _chunks.Values;
}
// Optional: get a block at world coordinates
public Blocks GetBlock(int worldX, int worldY, int worldZ)
{
int chunkX = worldX / Chunk.Size;
@@ -51,7 +46,6 @@ namespace Voxel
return chunk.GetBlock(localX, worldY, localZ);
}
// Optional: set a block at world coordinates
public void SetBlock(int worldX, int worldY, int worldZ, Blocks block)
{
int chunkX = worldX / Chunk.Size;
@@ -65,32 +59,74 @@ namespace Voxel
chunk.SetBlock(localX, worldY, localZ, block);
}
public (Blocks block, int x, int y, int z) Raycast(float length)
public (bool success, Blocks block, int x, int y, int z) Raycast(Vector3 origin, Vector3 direction, float maxDistance)
{
Vector3 start = Camera.Position;
Vector3 dir = Camera.Front.Normalized();
int x = (int)MathF.Floor(origin.X);
int y = (int)MathF.Floor(origin.Y);
int z = (int)MathF.Floor(origin.Z);
float stepSize = 0.01f;
float distanceTraveled = 0f;
int stepX = direction.X > 0 ? 1 : -1;
int stepY = direction.Y > 0 ? 1 : -1;
int stepZ = direction.Z > 0 ? 1 : -1;
while (distanceTraveled < length)
float tDeltaX = direction.X != 0 ? MathF.Abs(1 / direction.X) : float.MaxValue;
float tDeltaY = direction.Y != 0 ? MathF.Abs(1 / direction.Y) : float.MaxValue;
float tDeltaZ = direction.Z != 0 ? MathF.Abs(1 / direction.Z) : float.MaxValue;
float tMaxX = direction.X > 0
? (MathF.Floor(origin.X) + 1 - origin.X) * tDeltaX
: (origin.X - MathF.Floor(origin.X)) * tDeltaX;
float tMaxY = direction.Y > 0
? (MathF.Floor(origin.Y) + 1 - origin.Y) * tDeltaY
: (origin.Y - MathF.Floor(origin.Y)) * tDeltaY;
float tMaxZ = direction.Z > 0
? (MathF.Floor(origin.Z) + 1 - origin.Z) * tDeltaZ
: (origin.Z - MathF.Floor(origin.Z)) * tDeltaZ;
float distance = 0f;
while (distance <= maxDistance)
{
Vector3 point = start + dir * distanceTraveled;
int bx = (int)MathF.Floor(point.X);
int by = (int)MathF.Floor(point.Y);
int bz = (int)MathF.Floor(point.Z);
Blocks block = GetBlock(bx, by, bz);
Blocks block = GetBlock(x, y, z);
if (block != Blocks.Air)
{
return (block, bx, by, bz);
return (true, block, x, y, z);
}
distanceTraveled += stepSize;
// step to next voxel
if (tMaxX < tMaxY)
{
if (tMaxX < tMaxZ)
{
x += stepX;
distance = tMaxX;
tMaxX += tDeltaX;
}
else
{
z += stepZ;
distance = tMaxZ;
tMaxZ += tDeltaZ;
}
}
else
{
if (tMaxY < tMaxZ)
{
y += stepY;
distance = tMaxY;
tMaxY += tDeltaY;
}
else
{
z += stepZ;
distance = tMaxZ;
tMaxZ += tDeltaZ;
}
}
}
return (Blocks.Air, 0, 0, 0);
return (false, Blocks.Air, 0, 0, 0);
}
public void Update()