This commit is contained in:
maxwes08
2025-09-15 10:48:45 +02:00
parent 50f9d4c0c8
commit 40dd5c3a9e
4 changed files with 90 additions and 31 deletions

View File

@@ -59,7 +59,7 @@ namespace Voxel
chunk.SetBlock(localX, worldY, localZ, block);
}
public (bool success, Blocks block, int x, int y, int z) Raycast(Vector3 origin, Vector3 direction, float maxDistance)
public (bool success, Blocks block, int x, int y, int z, Vector3i normal) Raycast(Vector3 origin, Vector3 direction, float maxDistance)
{
int x = (int)MathF.Floor(origin.X);
int y = (int)MathF.Floor(origin.Y);
@@ -84,49 +84,41 @@ namespace Voxel
: (origin.Z - MathF.Floor(origin.Z)) * tDeltaZ;
float distance = 0f;
Vector3i normal = Vector3i.Zero;
while (distance <= maxDistance)
{
Blocks block = GetBlock(x, y, z);
if (block != Blocks.Air)
{
return (true, block, x, y, z);
return (true, block, x, y, z, normal);
}
// step to next voxel
if (tMaxX < tMaxY)
if (tMaxX < tMaxY && tMaxX < tMaxZ)
{
if (tMaxX < tMaxZ)
{
x += stepX;
distance = tMaxX;
tMaxX += tDeltaX;
}
else
{
z += stepZ;
distance = tMaxZ;
tMaxZ += tDeltaZ;
}
x += stepX;
distance = tMaxX;
tMaxX += tDeltaX;
normal = new Vector3i(stepX > 0 ? -1 : 1, 0, 0);
}
else if (tMaxY < tMaxZ)
{
y += stepY;
distance = tMaxY;
tMaxY += tDeltaY;
normal = new Vector3i(0, stepY > 0 ? -1 : 1, 0);
}
else
{
if (tMaxY < tMaxZ)
{
y += stepY;
distance = tMaxY;
tMaxY += tDeltaY;
}
else
{
z += stepZ;
distance = tMaxZ;
tMaxZ += tDeltaZ;
}
z += stepZ;
distance = tMaxZ;
tMaxZ += tDeltaZ;
normal = new Vector3i(0, 0, stepZ > 0 ? -1 : 1);
}
}
return (false, Blocks.Air, 0, 0, 0);
return (false, Blocks.Air, 0, 0, 0, normal);
}
public void Update()